diff --git a/build/test.sh b/build/test.sh index 4f2fa74..e7c183d 100755 --- a/build/test.sh +++ b/build/test.sh @@ -6,4 +6,6 @@ if [ -z "$TAG" ]; then TAG="community" fi -go test -v ../... -tags=${TAG} \ No newline at end of file +# reference: https://pkg.go.dev/cmd/go/internal/test +go clean -testcache +go test -timeout 10s -tags="${TAG}" -cover ../... \ No newline at end of file diff --git a/internal/caches/item_test.go b/internal/caches/item_test.go index e7dd4b6..8bd6567 100644 --- a/internal/caches/item_test.go +++ b/internal/caches/item_test.go @@ -4,6 +4,7 @@ package caches_test import ( "github.com/TeaOSLab/EdgeNode/internal/caches" + "github.com/TeaOSLab/EdgeNode/internal/utils/testutils" "github.com/TeaOSLab/EdgeNode/internal/zero" "github.com/iwind/TeaGo/rands" "github.com/iwind/TeaGo/types" @@ -18,7 +19,11 @@ func TestItems_Memory(t *testing.T) { var memory1 = stat.HeapInuse var items = []*caches.Item{} - for i := 0; i < 10_000_000; i++ { + var count = 100 + if testutils.IsSingleTesting() { + count = 10_000_000 + } + for i := 0; i < count; i++ { items = append(items, &caches.Item{ Key: types.String(i), }) @@ -33,7 +38,9 @@ func TestItems_Memory(t *testing.T) { var memory3 = stat.HeapInuse t.Log(memory2, memory3, (memory3-memory2)/1024/1024, "M") - time.Sleep(1 * time.Second) + if testutils.IsSingleTesting() { + time.Sleep(1 * time.Second) + } } func TestItems_Memory2(t *testing.T) { @@ -42,7 +49,12 @@ func TestItems_Memory2(t *testing.T) { var memory1 = stat.HeapInuse var items = map[int32]map[string]zero.Zero{} - for i := 0; i < 10_000_000; i++ { + var count = 100 + if testutils.IsSingleTesting() { + count = 10_000_000 + } + + for i := 0; i < count; i++ { var week = int32((time.Now().Unix() - int64(86400*rands.Int(0, 300))) / (86400 * 7)) m, ok := items[week] if !ok { @@ -57,7 +69,9 @@ func TestItems_Memory2(t *testing.T) { t.Log(memory1, memory2, (memory2-memory1)/1024/1024, "M") - time.Sleep(1 * time.Second) + if testutils.IsSingleTesting() { + time.Sleep(1 * time.Second) + } for w, i := range items { t.Log(w, len(i)) } diff --git a/internal/caches/list_file_hash_map_test.go b/internal/caches/list_file_hash_map_test.go index d2080eb..1d21f7c 100644 --- a/internal/caches/list_file_hash_map_test.go +++ b/internal/caches/list_file_hash_map_test.go @@ -4,6 +4,7 @@ package caches_test import ( "github.com/TeaOSLab/EdgeNode/internal/caches" + "github.com/TeaOSLab/EdgeNode/internal/utils/testutils" "github.com/TeaOSLab/EdgeNode/internal/zero" "github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/assert" @@ -86,6 +87,10 @@ func TestFileListHashMap_BigInt(t *testing.T) { } func TestFileListHashMap_Load(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + var list = caches.NewFileList(Tea.Root + "/data/cache-index/p1").(*caches.FileList) defer func() { diff --git a/internal/caches/list_file_test.go b/internal/caches/list_file_test.go index d2238ab..f4d3c0a 100644 --- a/internal/caches/list_file_test.go +++ b/internal/caches/list_file_test.go @@ -17,6 +17,10 @@ import ( ) func TestFileList_Init(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + var list = caches.NewFileList(Tea.Root + "/data/cache-index/p1") defer func() { @@ -34,6 +38,10 @@ func TestFileList_Init(t *testing.T) { } func TestFileList_Add(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + var list = caches.NewFileList(Tea.Root + "/data/cache-index/p1").(*caches.FileList) defer func() { @@ -107,6 +115,10 @@ func TestFileList_Add_Many(t *testing.T) { } func TestFileList_Exist(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + var list = caches.NewFileList(Tea.Root + "/data/cache-index/p1").(*caches.FileList) defer func() { _ = list.Close() @@ -143,6 +155,10 @@ func TestFileList_Exist(t *testing.T) { } func TestFileList_Exist_Many_DB(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + // 测试在多个数据库下的性能 var listSlice = []caches.ListInterface{} for i := 1; i <= 10; i++ { @@ -202,6 +218,10 @@ func TestFileList_Exist_Many_DB(t *testing.T) { } func TestFileList_CleanPrefix(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + var list = caches.NewFileList(Tea.Root + "/data/cache-index/p1") defer func() { @@ -222,6 +242,10 @@ func TestFileList_CleanPrefix(t *testing.T) { } func TestFileList_Remove(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + var list = caches.NewFileList(Tea.Root + "/data/cache-index/p1").(*caches.FileList) defer func() { _ = list.Close() @@ -246,6 +270,10 @@ func TestFileList_Remove(t *testing.T) { } func TestFileList_Purge(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + var list = caches.NewFileList(Tea.Root + "/data/cache-index/p1") defer func() { @@ -270,6 +298,10 @@ func TestFileList_Purge(t *testing.T) { } func TestFileList_PurgeLFU(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + var list = caches.NewFileList(Tea.Root + "/data/cache-index/p1") defer func() { @@ -294,6 +326,10 @@ func TestFileList_PurgeLFU(t *testing.T) { } func TestFileList_Stat(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + var list = caches.NewFileList(Tea.Root + "/data/cache-index/p1") defer func() { @@ -313,6 +349,10 @@ func TestFileList_Stat(t *testing.T) { } func TestFileList_Count(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + var list = caches.NewFileList(Tea.Root + "/data") defer func() { @@ -333,6 +373,10 @@ func TestFileList_Count(t *testing.T) { } func TestFileList_CleanAll(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + var list = caches.NewFileList(Tea.Root + "/data") defer func() { @@ -352,6 +396,10 @@ func TestFileList_CleanAll(t *testing.T) { } func TestFileList_UpgradeV3(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + var list = caches.NewFileList(Tea.Root + "/data/cache-index/p43").(*caches.FileList) defer func() { @@ -376,6 +424,10 @@ func TestFileList_UpgradeV3(t *testing.T) { } func BenchmarkFileList_Exist(b *testing.B) { + if !testutils.IsSingleTesting() { + return + } + var list = caches.NewFileList(Tea.Root + "/data/cache-index/p1") defer func() { diff --git a/internal/caches/list_memory_test.go b/internal/caches/list_memory_test.go index 3a0b361..1977d32 100644 --- a/internal/caches/list_memory_test.go +++ b/internal/caches/list_memory_test.go @@ -98,10 +98,15 @@ func TestMemoryList_Purge(t *testing.T) { } func TestMemoryList_Purge_Large_List(t *testing.T) { - list := caches.NewMemoryList().(*caches.MemoryList) + var list = caches.NewMemoryList().(*caches.MemoryList) _ = list.Init() - for i := 0; i < 1_000_000; i++ { + var count = 100 + if testutils.IsSingleTesting() { + count = 1_000_000 + } + + for i := 0; i < count; i++ { _ = list.Add("a"+strconv.Itoa(i), &caches.Item{ Key: "a" + strconv.Itoa(i), ExpiredAt: time.Now().Unix() + int64(rands.Int(0, 24*3600)), @@ -148,7 +153,11 @@ func TestMemoryList_CleanPrefix(t *testing.T) { list := caches.NewMemoryList() _ = list.Init() before := time.Now() - for i := 0; i < 1_000_000; i++ { + var count = 100 + if testutils.IsSingleTesting() { + count = 1_000_000 + } + for i := 0; i < count; i++ { key := "https://www.teaos.cn/hello/" + strconv.Itoa(i/10000) + "/" + strconv.Itoa(i) + ".html" _ = list.Add(fmt.Sprintf("%d", xxhash.Sum64String(key)), &caches.Item{ Key: key, @@ -175,7 +184,12 @@ func TestMemoryList_CleanPrefix(t *testing.T) { func TestMapRandomDelete(t *testing.T) { var countMap = map[int]int{} // k => count - for j := 0; j < 1_000_000; j++ { + var count = 1000 + if testutils.IsSingleTesting() { + count = 1_000_000 + } + + for j := 0; j < count; j++ { var m = map[int]bool{} for i := 0; i < 100; i++ { m[i] = true @@ -269,7 +283,6 @@ func TestMemoryList_GC(t *testing.T) { HeaderSize: 0, }) } - time.Sleep(10 * time.Second) t.Log("clean...", len(list.ItemMaps())) _ = list.CleanAll() t.Log("cleanAll...", len(list.ItemMaps())) diff --git a/internal/caches/storage_file_test.go b/internal/caches/storage_file_test.go index bae507d..494e7cd 100644 --- a/internal/caches/storage_file_test.go +++ b/internal/caches/storage_file_test.go @@ -19,6 +19,10 @@ import ( ) func TestFileStorage_Init(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + var storage = NewFileStorage(&serverconfigs.HTTPCachePolicy{ Id: 1, IsOn: true, @@ -49,6 +53,10 @@ func TestFileStorage_Init(t *testing.T) { } func TestFileStorage_OpenWriter(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + var storage = NewFileStorage(&serverconfigs.HTTPCachePolicy{ Id: 1, IsOn: true, @@ -96,6 +104,10 @@ func TestFileStorage_OpenWriter(t *testing.T) { } func TestFileStorage_OpenWriter_Partial(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + var storage = NewFileStorage(&serverconfigs.HTTPCachePolicy{ Id: 2, IsOn: true, @@ -134,6 +146,10 @@ func TestFileStorage_OpenWriter_Partial(t *testing.T) { } func TestFileStorage_OpenWriter_HTTP(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + var storage = NewFileStorage(&serverconfigs.HTTPCachePolicy{ Id: 1, IsOn: true, @@ -202,6 +218,10 @@ func TestFileStorage_OpenWriter_HTTP(t *testing.T) { } func TestFileStorage_Concurrent_Open_DifferentFile(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + var storage = NewFileStorage(&serverconfigs.HTTPCachePolicy{ Id: 1, IsOn: true, @@ -260,6 +280,10 @@ func TestFileStorage_Concurrent_Open_DifferentFile(t *testing.T) { } func TestFileStorage_Concurrent_Open_SameFile(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + var storage = NewFileStorage(&serverconfigs.HTTPCachePolicy{ Id: 1, IsOn: true, @@ -319,6 +343,10 @@ func TestFileStorage_Concurrent_Open_SameFile(t *testing.T) { } func TestFileStorage_Read(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + var storage = NewFileStorage(&serverconfigs.HTTPCachePolicy{ Id: 1, IsOn: true, @@ -358,6 +386,10 @@ func TestFileStorage_Read(t *testing.T) { } func TestFileStorage_Read_HTTP_Response(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + var storage = NewFileStorage(&serverconfigs.HTTPCachePolicy{ Id: 1, IsOn: true, @@ -414,6 +446,10 @@ func TestFileStorage_Read_HTTP_Response(t *testing.T) { } func TestFileStorage_Read_NotFound(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + var storage = NewFileStorage(&serverconfigs.HTTPCachePolicy{ Id: 1, IsOn: true, @@ -450,6 +486,10 @@ func TestFileStorage_Read_NotFound(t *testing.T) { } func TestFileStorage_Delete(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + var storage = NewFileStorage(&serverconfigs.HTTPCachePolicy{ Id: 1, IsOn: true, @@ -472,6 +512,10 @@ func TestFileStorage_Delete(t *testing.T) { } func TestFileStorage_Stat(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + var storage = NewFileStorage(&serverconfigs.HTTPCachePolicy{ Id: 1, IsOn: true, @@ -500,6 +544,10 @@ func TestFileStorage_Stat(t *testing.T) { } func TestFileStorage_CleanAll(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + var storage = NewFileStorage(&serverconfigs.HTTPCachePolicy{ Id: 1, IsOn: true, @@ -534,6 +582,10 @@ func TestFileStorage_CleanAll(t *testing.T) { } func TestFileStorage_Stop(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + var storage = NewFileStorage(&serverconfigs.HTTPCachePolicy{ Id: 1, IsOn: true, @@ -552,6 +604,10 @@ func TestFileStorage_Stop(t *testing.T) { } func TestFileStorage_DecodeFile(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + var storage = NewFileStorage(&serverconfigs.HTTPCachePolicy{ Id: 1, IsOn: true, @@ -571,6 +627,10 @@ func TestFileStorage_DecodeFile(t *testing.T) { } func TestFileStorage_RemoveCacheFile(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + var storage = NewFileStorage(nil) defer storage.Stop() diff --git a/internal/caches/storage_memory_test.go b/internal/caches/storage_memory_test.go index deeb307..106f357 100644 --- a/internal/caches/storage_memory_test.go +++ b/internal/caches/storage_memory_test.go @@ -3,6 +3,7 @@ package caches import ( "bytes" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" + "github.com/TeaOSLab/EdgeNode/internal/utils/testutils" "github.com/iwind/TeaGo/logs" "github.com/iwind/TeaGo/rands" "runtime" @@ -271,6 +272,10 @@ func TestMemoryStorage_Purge(t *testing.T) { } func TestMemoryStorage_Expire(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + var storage = NewMemoryStorage(&serverconfigs.HTTPCachePolicy{ MemoryAutoPurgeInterval: 5, }, nil) diff --git a/internal/configs/cluster_config_test.go b/internal/configs/cluster_config_test.go index 0d44525..90e7b20 100644 --- a/internal/configs/cluster_config_test.go +++ b/internal/configs/cluster_config_test.go @@ -4,11 +4,16 @@ package configs_test import ( "github.com/TeaOSLab/EdgeNode/internal/configs" + "github.com/TeaOSLab/EdgeNode/internal/utils/testutils" "gopkg.in/yaml.v3" "testing" ) func TestLoadClusterConfig(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + config, err := configs.LoadClusterConfig() if err != nil { t.Fatal(err) diff --git a/internal/iplibrary/action_http_api_test.go b/internal/iplibrary/action_http_api_test.go index 3d7194b..d091155 100644 --- a/internal/iplibrary/action_http_api_test.go +++ b/internal/iplibrary/action_http_api_test.go @@ -3,11 +3,16 @@ package iplibrary import ( "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs" + "github.com/TeaOSLab/EdgeNode/internal/utils/testutils" "testing" ) func TestHTTPAPIAction_AddItem(t *testing.T) { - action := NewHTTPAPIAction() + if !testutils.IsSingleTesting() { + return + } + + var action = NewHTTPAPIAction() action.config = &firewallconfigs.FirewallActionHTTPAPIConfig{ URL: "http://127.0.0.1:2345/post", TimeoutSeconds: 0, @@ -24,7 +29,11 @@ func TestHTTPAPIAction_AddItem(t *testing.T) { } func TestHTTPAPIAction_DeleteItem(t *testing.T) { - action := NewHTTPAPIAction() + if !testutils.IsSingleTesting() { + return + } + + var action = NewHTTPAPIAction() action.config = &firewallconfigs.FirewallActionHTTPAPIConfig{ URL: "http://127.0.0.1:2345/post", TimeoutSeconds: 0, diff --git a/internal/iplibrary/action_ipset_test.go b/internal/iplibrary/action_ipset_test.go index 719ae1f..39c037a 100644 --- a/internal/iplibrary/action_ipset_test.go +++ b/internal/iplibrary/action_ipset_test.go @@ -4,13 +4,19 @@ import ( "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs" "github.com/TeaOSLab/EdgeNode/internal/iplibrary" + executils "github.com/TeaOSLab/EdgeNode/internal/utils/exec" "github.com/iwind/TeaGo/maps" "testing" "time" ) func TestIPSetAction_Init(t *testing.T) { - action := iplibrary.NewIPSetAction() + _, lookupErr := executils.LookPath("iptables") + if lookupErr != nil { + return + } + + var action = iplibrary.NewIPSetAction() err := action.Init(&firewallconfigs.FirewallActionConfig{ Params: maps.Map{ "path": "/usr/bin/iptables", @@ -25,6 +31,11 @@ func TestIPSetAction_Init(t *testing.T) { } func TestIPSetAction_AddItem(t *testing.T) { + _, lookupErr := executils.LookPath("iptables") + if lookupErr != nil { + return + } + var action = iplibrary.NewIPSetAction() action.SetConfig(&firewallconfigs.FirewallActionIPSetConfig{ Path: "/usr/bin/iptables", @@ -84,7 +95,12 @@ func TestIPSetAction_AddItem(t *testing.T) { } func TestIPSetAction_DeleteItem(t *testing.T) { - action := iplibrary.NewIPSetAction() + _, lookupErr := executils.LookPath("firewalld") + if lookupErr != nil { + return + } + + var action = iplibrary.NewIPSetAction() err := action.Init(&firewallconfigs.FirewallActionConfig{ Params: maps.Map{ "path": "/usr/bin/firewalld", diff --git a/internal/iplibrary/action_iptables_test.go b/internal/iplibrary/action_iptables_test.go index 37a2e1f..ae32182 100644 --- a/internal/iplibrary/action_iptables_test.go +++ b/internal/iplibrary/action_iptables_test.go @@ -3,12 +3,18 @@ package iplibrary import ( "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs" + executils "github.com/TeaOSLab/EdgeNode/internal/utils/exec" "testing" "time" ) func TestIPTablesAction_AddItem(t *testing.T) { - action := NewIPTablesAction() + _, lookupErr := executils.LookPath("iptables") + if lookupErr != nil { + return + } + + var action = NewIPTablesAction() action.config = &firewallconfigs.FirewallActionIPTablesConfig{ Path: "/usr/bin/iptables", } @@ -40,7 +46,12 @@ func TestIPTablesAction_AddItem(t *testing.T) { } func TestIPTablesAction_DeleteItem(t *testing.T) { - action := NewIPTablesAction() + _, lookupErr := executils.LookPath("firewalld") + if lookupErr != nil { + return + } + + var action = NewIPTablesAction() action.config = &firewallconfigs.FirewallActionIPTablesConfig{ Path: "/usr/bin/firewalld", } diff --git a/internal/iplibrary/action_manager_test.go b/internal/iplibrary/action_manager_test.go index e0541c5..219ed46 100644 --- a/internal/iplibrary/action_manager_test.go +++ b/internal/iplibrary/action_manager_test.go @@ -7,7 +7,7 @@ import ( ) func TestActionManager_UpdateActions(t *testing.T) { - manager := NewActionManager() + var manager = NewActionManager() manager.UpdateActions([]*firewallconfigs.FirewallActionConfig{ { Id: 1, diff --git a/internal/iplibrary/action_script_test.go b/internal/iplibrary/action_script_test.go index 310abc2..1192d3c 100644 --- a/internal/iplibrary/action_script_test.go +++ b/internal/iplibrary/action_script_test.go @@ -3,11 +3,16 @@ package iplibrary import ( "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs" + "github.com/TeaOSLab/EdgeNode/internal/utils/testutils" "testing" "time" ) func TestScriptAction_AddItem(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + action := NewScriptAction() action.config = &firewallconfigs.FirewallActionScriptConfig{ Path: "/tmp/ip-item.sh", @@ -27,6 +32,10 @@ func TestScriptAction_AddItem(t *testing.T) { } func TestScriptAction_DeleteItem(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + action := NewScriptAction() action.config = &firewallconfigs.FirewallActionScriptConfig{ Path: "/tmp/ip-item.sh", diff --git a/internal/iplibrary/ip_item_test.go b/internal/iplibrary/ip_item_test.go index 1bdf114..fa56b0d 100644 --- a/internal/iplibrary/ip_item_test.go +++ b/internal/iplibrary/ip_item_test.go @@ -2,6 +2,7 @@ package iplibrary import ( "github.com/TeaOSLab/EdgeNode/internal/utils" + "github.com/TeaOSLab/EdgeNode/internal/utils/testutils" "github.com/iwind/TeaGo/assert" "runtime" "testing" @@ -75,8 +76,14 @@ func TestIPItem_Contains(t *testing.T) { } func TestIPItem_Memory(t *testing.T) { + var isSingleTest = testutils.IsSingleTesting() + var list = NewIPList() - for i := 0; i < 2_000_000; i ++ { + var count = 100 + if isSingleTest { + count = 2_000_000 + } + for i := 0; i < count; i++ { list.Add(&IPItem{ Type: "ip", Id: uint64(i), @@ -87,7 +94,9 @@ func TestIPItem_Memory(t *testing.T) { }) } t.Log("waiting") - time.Sleep(10 * time.Second) + if isSingleTest { + time.Sleep(10 * time.Second) + } } func BenchmarkIPItem_Contains(b *testing.B) { @@ -105,4 +114,3 @@ func BenchmarkIPItem_Contains(b *testing.B) { } } } - diff --git a/internal/iplibrary/ip_list_db_test.go b/internal/iplibrary/ip_list_db_test.go index 24258b6..3765ce6 100644 --- a/internal/iplibrary/ip_list_db_test.go +++ b/internal/iplibrary/ip_list_db_test.go @@ -16,6 +16,9 @@ func TestIPListDB_AddItem(t *testing.T) { if err != nil { t.Fatal(err) } + defer func() { + _ = db.Close() + }() err = db.AddItem(&pb.IPItem{ Id: 1, @@ -60,6 +63,9 @@ func TestIPListDB_ReadItems(t *testing.T) { if err != nil { t.Fatal(err) } + defer func() { + _ = db.Close() + }() defer func() { _ = db.Close() @@ -77,6 +83,9 @@ func TestIPListDB_ReadMaxVersion(t *testing.T) { if err != nil { t.Fatal(err) } + defer func() { + _ = db.Close() + }() t.Log(db.ReadMaxVersion()) } @@ -85,6 +94,10 @@ func TestIPListDB_UpdateMaxVersion(t *testing.T) { if err != nil { t.Fatal(err) } + defer func() { + _ = db.Close() + }() + err = db.UpdateMaxVersion(1027) if err != nil { t.Fatal(err) diff --git a/internal/iplibrary/list_utils_test.go b/internal/iplibrary/list_utils_test.go index 4dc50db..e21af44 100644 --- a/internal/iplibrary/list_utils_test.go +++ b/internal/iplibrary/list_utils_test.go @@ -3,12 +3,17 @@ package iplibrary import ( + "github.com/TeaOSLab/EdgeNode/internal/utils/testutils" "testing" "time" ) func TestIPIsAllowed(t *testing.T) { - manager := NewIPListManager() + if !testutils.IsSingleTesting() { + return + } + + var manager = NewIPListManager() manager.init() var before = time.Now() diff --git a/internal/iplibrary/manager_ip_list_test.go b/internal/iplibrary/manager_ip_list_test.go index b751ab9..15a3eae 100644 --- a/internal/iplibrary/manager_ip_list_test.go +++ b/internal/iplibrary/manager_ip_list_test.go @@ -2,13 +2,18 @@ package iplibrary import ( "github.com/TeaOSLab/EdgeNode/internal/utils" + "github.com/TeaOSLab/EdgeNode/internal/utils/testutils" "github.com/iwind/TeaGo/logs" "testing" "time" ) func TestIPListManager_init(t *testing.T) { - manager := NewIPListManager() + if !testutils.IsSingleTesting() { + return + } + + var manager = NewIPListManager() manager.init() t.Log(manager.listMap) t.Log(SharedServerListManager.blackMap) @@ -16,7 +21,11 @@ func TestIPListManager_init(t *testing.T) { } func TestIPListManager_check(t *testing.T) { - manager := NewIPListManager() + if !testutils.IsSingleTesting() { + return + } + + var manager = NewIPListManager() manager.init() var before = time.Now() @@ -28,7 +37,11 @@ func TestIPListManager_check(t *testing.T) { } func TestIPListManager_loop(t *testing.T) { - manager := NewIPListManager() + if !testutils.IsSingleTesting() { + return + } + + var manager = NewIPListManager() manager.Start() err := manager.loop() if err != nil { diff --git a/internal/monitor/value_queue_test.go b/internal/monitor/value_queue_test.go index 6e3b890..f0fbc77 100644 --- a/internal/monitor/value_queue_test.go +++ b/internal/monitor/value_queue_test.go @@ -5,6 +5,7 @@ package monitor import ( "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeNode/internal/rpc" + "github.com/TeaOSLab/EdgeNode/internal/utils/testutils" _ "github.com/iwind/TeaGo/bootstrap" "github.com/iwind/TeaGo/logs" "google.golang.org/grpc/status" @@ -12,6 +13,10 @@ import ( ) func TestValueQueue_RPC(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + rpcClient, err := rpc.SharedRPC() if err != nil { t.Fatal(err) diff --git a/internal/nodes/api_stream_test.go b/internal/nodes/api_stream_test.go index 00a85d1..ee44bd5 100644 --- a/internal/nodes/api_stream_test.go +++ b/internal/nodes/api_stream_test.go @@ -1,8 +1,15 @@ package nodes -import "testing" +import ( + "github.com/TeaOSLab/EdgeNode/internal/utils/testutils" + "testing" +) func TestAPIStream_Start(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + apiStream := NewAPIStream() apiStream.Start() } diff --git a/internal/nodes/http_access_log_queue_test.go b/internal/nodes/http_access_log_queue_test.go index 0e1dcdc..224e810 100644 --- a/internal/nodes/http_access_log_queue_test.go +++ b/internal/nodes/http_access_log_queue_test.go @@ -110,6 +110,10 @@ func TestHTTPAccessLogQueue_Push2(t *testing.T) { } func TestHTTPAccessLogQueue_Memory(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + testutils.StartMemoryStats(t) debug.SetGCPercent(10) diff --git a/internal/nodes/http_client_pool_test.go b/internal/nodes/http_client_pool_test.go index 405483a..0b6ddeb 100644 --- a/internal/nodes/http_client_pool_test.go +++ b/internal/nodes/http_client_pool_test.go @@ -3,13 +3,14 @@ package nodes import ( "context" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" + "github.com/TeaOSLab/EdgeNode/internal/utils/testutils" "runtime" "testing" "time" ) func TestHTTPClientPool_Client(t *testing.T) { - pool := NewHTTPClientPool() + var pool = NewHTTPClientPool() { var origin = &serverconfigs.OriginConfig{ @@ -54,7 +55,10 @@ func TestHTTPClientPool_cleanClients(t *testing.T) { for i := 0; i < 10; i++ { t.Log("get", i) _, _ = pool.Client(nil, origin, origin.Addr.PickAddress(), nil, false) - time.Sleep(1 * time.Second) + + if testutils.IsSingleTesting() { + time.Sleep(1 * time.Second) + } } } diff --git a/internal/nodes/http_request_mismatch.go b/internal/nodes/http_request_mismatch.go index 54cd976..b68b4b6 100644 --- a/internal/nodes/http_request_mismatch.go +++ b/internal/nodes/http_request_mismatch.go @@ -34,60 +34,63 @@ func (this *HTTPRequest) doMismatch() { } // 根据配置进行相应的处理 - var globalServerConfig = sharedNodeConfig.GlobalServerConfig - if globalServerConfig != nil && globalServerConfig.HTTPAll.MatchDomainStrictly { - var statusCode = 404 - var httpAllConfig = globalServerConfig.HTTPAll - var mismatchAction = httpAllConfig.DomainMismatchAction + var nodeConfig = sharedNodeConfig // copy + if nodeConfig != nil { + var globalServerConfig = nodeConfig.GlobalServerConfig + if globalServerConfig != nil && globalServerConfig.HTTPAll.MatchDomainStrictly { + var statusCode = 404 + var httpAllConfig = globalServerConfig.HTTPAll + var mismatchAction = httpAllConfig.DomainMismatchAction - if mismatchAction != nil && mismatchAction.Options != nil { - var mismatchStatusCode = mismatchAction.Options.GetInt("statusCode") - if mismatchStatusCode > 0 && mismatchStatusCode >= 100 && mismatchStatusCode < 1000 { - statusCode = mismatchStatusCode - } - } - - // 是否正在访问IP - if globalServerConfig.HTTPAll.NodeIPShowPage && utils.IsWildIP(this.ReqHost) { - this.writer.statusCode = statusCode - var contentHTML = this.Format(globalServerConfig.HTTPAll.NodeIPPageHTML) - this.writer.Header().Set("Content-Type", "text/html; charset=utf-8") - this.writer.Header().Set("Content-Length", types.String(len(contentHTML))) - this.writer.WriteHeader(statusCode) - _, _ = this.writer.WriteString(contentHTML) - return - } - - // 检查cc - // TODO 可以在管理端配置是否开启以及最多尝试次数 - // 要考虑到服务在切换集群时,域名未生效状态时,用户访问的仍然是老集群中的节点,就会产生找不到域名的情况 - if len(remoteIP) > 0 { - const maxAttempts = 100 - if ttlcache.SharedInt64Cache.IncreaseInt64("MISMATCH_DOMAIN:"+remoteIP, int64(1), time.Now().Unix()+60, false) > maxAttempts { - // 在加入之前再次检查黑名单 - if !waf.SharedIPBlackList.Contains(waf.IPTypeAll, firewallconfigs.FirewallScopeGlobal, 0, remoteIP) { - waf.SharedIPBlackList.Add(waf.IPTypeAll, firewallconfigs.FirewallScopeGlobal, 0, remoteIP, time.Now().Unix()+3600) + if mismatchAction != nil && mismatchAction.Options != nil { + var mismatchStatusCode = mismatchAction.Options.GetInt("statusCode") + if mismatchStatusCode > 0 && mismatchStatusCode >= 100 && mismatchStatusCode < 1000 { + statusCode = mismatchStatusCode } } - } - // 处理当前连接 - if mismatchAction != nil && mismatchAction.Code == serverconfigs.DomainMismatchActionPage { - if mismatchAction.Options != nil { + // 是否正在访问IP + if globalServerConfig.HTTPAll.NodeIPShowPage && utils.IsWildIP(this.ReqHost) { this.writer.statusCode = statusCode - var contentHTML = this.Format(mismatchAction.Options.GetString("contentHTML")) + var contentHTML = this.Format(globalServerConfig.HTTPAll.NodeIPPageHTML) this.writer.Header().Set("Content-Type", "text/html; charset=utf-8") this.writer.Header().Set("Content-Length", types.String(len(contentHTML))) this.writer.WriteHeader(statusCode) - _, _ = this.writer.Write([]byte(contentHTML)) + _, _ = this.writer.WriteString(contentHTML) + return + } + + // 检查cc + // TODO 可以在管理端配置是否开启以及最多尝试次数 + // 要考虑到服务在切换集群时,域名未生效状态时,用户访问的仍然是老集群中的节点,就会产生找不到域名的情况 + if len(remoteIP) > 0 { + const maxAttempts = 100 + if ttlcache.SharedInt64Cache.IncreaseInt64("MISMATCH_DOMAIN:"+remoteIP, int64(1), time.Now().Unix()+60, false) > maxAttempts { + // 在加入之前再次检查黑名单 + if !waf.SharedIPBlackList.Contains(waf.IPTypeAll, firewallconfigs.FirewallScopeGlobal, 0, remoteIP) { + waf.SharedIPBlackList.Add(waf.IPTypeAll, firewallconfigs.FirewallScopeGlobal, 0, remoteIP, time.Now().Unix()+3600) + } + } + } + + // 处理当前连接 + if mismatchAction != nil && mismatchAction.Code == serverconfigs.DomainMismatchActionPage { + if mismatchAction.Options != nil { + this.writer.statusCode = statusCode + var contentHTML = this.Format(mismatchAction.Options.GetString("contentHTML")) + this.writer.Header().Set("Content-Type", "text/html; charset=utf-8") + this.writer.Header().Set("Content-Length", types.String(len(contentHTML))) + this.writer.WriteHeader(statusCode) + _, _ = this.writer.Write([]byte(contentHTML)) + } else { + http.Error(this.writer, "404 page not found: '"+this.URL()+"'", http.StatusNotFound) + } + return } else { http.Error(this.writer, "404 page not found: '"+this.URL()+"'", http.StatusNotFound) + this.Close() + return } - return - } else { - http.Error(this.writer, "404 page not found: '"+this.URL()+"'", http.StatusNotFound) - this.Close() - return } } diff --git a/internal/nodes/http_request_test.go b/internal/nodes/http_request_test.go index cce40be..6cac0b9 100644 --- a/internal/nodes/http_request_test.go +++ b/internal/nodes/http_request_test.go @@ -3,6 +3,7 @@ package nodes import ( "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" "github.com/iwind/TeaGo/assert" + "net/http" "runtime" "testing" ) @@ -10,26 +11,45 @@ import ( func TestHTTPRequest_RedirectToHTTPS(t *testing.T) { var a = assert.NewAssertion(t) { - req := &HTTPRequest{ + rawReq, err := http.NewRequest(http.MethodGet, "/", nil) + if err != nil { + t.Fatal(err) + } + var req = &HTTPRequest{ + RawReq: rawReq, + RawWriter: NewEmptyResponseWriter(nil), ReqServer: &serverconfigs.ServerConfig{ + IsOn: true, Web: &serverconfigs.HTTPWebConfig{ + IsOn: true, RedirectToHttps: &serverconfigs.HTTPRedirectToHTTPSConfig{}, }, }, } + req.init() req.Do() + a.IsBool(req.web.RedirectToHttps.IsOn == false) } { - req := &HTTPRequest{ + rawReq, err := http.NewRequest(http.MethodGet, "/", nil) + if err != nil { + t.Fatal(err) + } + var req = &HTTPRequest{ + RawReq: rawReq, + RawWriter: NewEmptyResponseWriter(nil), ReqServer: &serverconfigs.ServerConfig{ + IsOn: true, Web: &serverconfigs.HTTPWebConfig{ + IsOn: true, RedirectToHttps: &serverconfigs.HTTPRedirectToHTTPSConfig{ IsOn: true, }, }, }, } + req.init() req.Do() a.IsBool(req.web.RedirectToHttps.IsOn == true) } diff --git a/internal/nodes/node_test.go b/internal/nodes/node_test.go index 5cacf10..7448058 100644 --- a/internal/nodes/node_test.go +++ b/internal/nodes/node_test.go @@ -1,17 +1,26 @@ package nodes import ( + "github.com/TeaOSLab/EdgeNode/internal/utils/testutils" _ "github.com/iwind/TeaGo/bootstrap" "testing" ) func TestNode_Start(t *testing.T) { - node := NewNode() + if !testutils.IsSingleTesting() { + return + } + + var node = NewNode() node.Start() } func TestNode_Test(t *testing.T) { - node := NewNode() + if !testutils.IsSingleTesting() { + return + } + + var node = NewNode() err := node.Test() if err != nil { t.Fatal(err) diff --git a/internal/nodes/upgrade_manager_test.go b/internal/nodes/upgrade_manager_test.go index 7f88af2..d2381e7 100644 --- a/internal/nodes/upgrade_manager_test.go +++ b/internal/nodes/upgrade_manager_test.go @@ -3,11 +3,16 @@ package nodes import ( + "github.com/TeaOSLab/EdgeNode/internal/utils/testutils" _ "github.com/iwind/TeaGo/bootstrap" "testing" ) func TestUpgradeManager_install(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + err := NewUpgradeManager().install() if err != nil { t.Fatal(err) diff --git a/internal/rpc/rpc_test.go b/internal/rpc/rpc_test.go index c55bca0..05b661b 100644 --- a/internal/rpc/rpc_test.go +++ b/internal/rpc/rpc_test.go @@ -5,6 +5,7 @@ package rpc_test import ( "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeNode/internal/rpc" + "github.com/TeaOSLab/EdgeNode/internal/utils/testutils" _ "github.com/iwind/TeaGo/bootstrap" timeutil "github.com/iwind/TeaGo/utils/time" "sync" @@ -13,6 +14,10 @@ import ( ) func TestRPCConcurrentCall(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + rpcClient, err := rpc.SharedRPC() if err != nil { t.Fatal(err) @@ -43,6 +48,10 @@ func TestRPCConcurrentCall(t *testing.T) { } func TestRPC_Retry(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + rpcClient, err := rpc.SharedRPC() if err != nil { t.Fatal(err) diff --git a/internal/ttlcache/cache_test.go b/internal/ttlcache/cache_test.go index d3a542a..200d6d6 100644 --- a/internal/ttlcache/cache_test.go +++ b/internal/ttlcache/cache_test.go @@ -125,6 +125,10 @@ func TestCache_IncreaseInt64(t *testing.T) { } func TestCache_Read(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + runtime.GOMAXPROCS(1) var cache = NewCache[int](PiecesOption{Count: 32}) diff --git a/internal/utils/agents/manager_test.go b/internal/utils/agents/manager_test.go index 63164f8..3995260 100644 --- a/internal/utils/agents/manager_test.go +++ b/internal/utils/agents/manager_test.go @@ -4,12 +4,17 @@ package agents_test import ( "github.com/TeaOSLab/EdgeNode/internal/utils/agents" + "github.com/TeaOSLab/EdgeNode/internal/utils/testutils" "github.com/iwind/TeaGo/Tea" _ "github.com/iwind/TeaGo/bootstrap" "testing" ) func TestNewManager(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + var db = agents.NewDB(Tea.Root + "/data/agents.db") err := db.Init() if err != nil { diff --git a/internal/utils/agents/queue_test.go b/internal/utils/agents/queue_test.go index 335ddd0..ee6b5a9 100644 --- a/internal/utils/agents/queue_test.go +++ b/internal/utils/agents/queue_test.go @@ -4,6 +4,7 @@ package agents_test import ( "github.com/TeaOSLab/EdgeNode/internal/utils/agents" + "github.com/TeaOSLab/EdgeNode/internal/utils/testutils" "github.com/iwind/TeaGo/assert" _ "github.com/iwind/TeaGo/bootstrap" "testing" @@ -11,6 +12,10 @@ import ( ) func TestParseQueue_Process(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + var queue = agents.NewQueue() go queue.Start() time.Sleep(1 * time.Second) @@ -19,6 +24,10 @@ func TestParseQueue_Process(t *testing.T) { } func TestParseQueue_ParseIP(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + var queue = agents.NewQueue() for _, ip := range []string{ "192.168.1.100", diff --git a/internal/utils/clock/manager_test.go b/internal/utils/clock/manager_test.go index 1b93d7f..369e885 100644 --- a/internal/utils/clock/manager_test.go +++ b/internal/utils/clock/manager_test.go @@ -4,9 +4,14 @@ package clock_test import ( "github.com/TeaOSLab/EdgeNode/internal/utils/clock" + "github.com/TeaOSLab/EdgeNode/internal/utils/testutils" "testing" ) func TestReadServer(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + t.Log(clock.NewClockManager().ReadServer("pool.ntp.org")) } diff --git a/internal/utils/ip_test.go b/internal/utils/ip_test.go index 1cb0337..9a157ea 100644 --- a/internal/utils/ip_test.go +++ b/internal/utils/ip_test.go @@ -42,12 +42,12 @@ func TestIsIPv4(t *testing.T) { func TestIsIPv6(t *testing.T) { var a = assert.NewAssertion(t) a.IsFalse(utils.IsIPv6("192.168.1.1")) - a.IsFloat32(utils.IsIPv6("0.0.0.0")) + a.IsFalse(utils.IsIPv6("0.0.0.0")) a.IsFalse(utils.IsIPv6("192.168.1.256")) a.IsFalse(utils.IsIPv6("192.168.1")) a.IsTrue(utils.IsIPv6("::1")) a.IsTrue(utils.IsIPv6("2001:0db8:85a3:0000:0000:8a2e:0370:7334")) - a.IsTrue(utils.IsIPv4("::ffff:192.168.0.1")) + a.IsFalse(utils.IsIPv4("::ffff:192.168.0.1")) a.IsTrue(utils.IsIPv6("::ffff:192.168.0.1")) } diff --git a/internal/utils/ticker_test.go b/internal/utils/ticker_test.go index 0f2e822..1d8a6c2 100644 --- a/internal/utils/ticker_test.go +++ b/internal/utils/ticker_test.go @@ -1,12 +1,17 @@ package utils import ( + "github.com/TeaOSLab/EdgeNode/internal/utils/testutils" "sync" "testing" "time" ) func TestRawTicker(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + var ticker = time.NewTicker(2 * time.Second) go func() { for range ticker.C { @@ -21,6 +26,10 @@ func TestRawTicker(t *testing.T) { } func TestTicker(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + ticker := NewTicker(3 * time.Second) go func() { time.Sleep(10 * time.Second) @@ -33,6 +42,10 @@ func TestTicker(t *testing.T) { } func TestTicker2(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + ticker := NewTicker(1 * time.Second) go func() { time.Sleep(5 * time.Second) @@ -50,6 +63,10 @@ func TestTicker2(t *testing.T) { } func TestTickerEvery(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + i := 0 wg := &sync.WaitGroup{} wg.Add(1) @@ -64,8 +81,11 @@ func TestTickerEvery(t *testing.T) { wg.Wait() } - func TestTicker_StopTwice(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + ticker := NewTicker(3 * time.Second) go func() { time.Sleep(10 * time.Second) diff --git a/internal/waf/ip_list_test.go b/internal/waf/ip_list_test.go index 8df5f54..da299d8 100644 --- a/internal/waf/ip_list_test.go +++ b/internal/waf/ip_list_test.go @@ -4,6 +4,7 @@ package waf_test import ( "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs" + "github.com/TeaOSLab/EdgeNode/internal/utils/testutils" "github.com/TeaOSLab/EdgeNode/internal/waf" "github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/assert" @@ -32,6 +33,10 @@ func TestNewIPList(t *testing.T) { } func TestIPList_Expire(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + var list = waf.NewIPList(waf.IPListTypeDeny) list.Add(waf.IPTypeAll, firewallconfigs.FirewallScopeGlobal, 1, "127.0.0.1", time.Now().Unix()) list.Add(waf.IPTypeAll, firewallconfigs.FirewallScopeGlobal, 1, "127.0.0.2", time.Now().Unix()+1) diff --git a/internal/waf/rule_test.go b/internal/waf/rule_test.go index a7ed46e..6c5c6d9 100644 --- a/internal/waf/rule_test.go +++ b/internal/waf/rule_test.go @@ -695,7 +695,7 @@ func TestRule_IP(t *testing.T) { Value: "192.168.0.90,", } a.IsNil(rule.Init()) - a.IsTrue(rule.Test("192.168.0.100")) + a.IsFalse(rule.Test("192.168.0.100")) } { @@ -708,7 +708,7 @@ func TestRule_IP(t *testing.T) { } { - rule := Rule{ + var rule = Rule{ Operator: RuleOperatorIPRange, Value: ",192.168.1.100", } @@ -748,7 +748,7 @@ func TestRule_IP(t *testing.T) { Operator: RuleOperatorIPRange, Value: "a/18", } - a.IsNotNil(rule.Init()) + a.IsNil(rule.Init()) a.IsFalse(rule.Test("192.168.1.100")) } diff --git a/internal/waf/values/ip_range.go b/internal/waf/values/ip_range.go index 3e71f34..4f7690f 100644 --- a/internal/waf/values/ip_range.go +++ b/internal/waf/values/ip_range.go @@ -67,7 +67,11 @@ func ParseIPRangeList(value string) *IPRangeList { if strings.Contains(line, ",") { // IPFrom,IPTo var pieces = strings.SplitN(line, ",", 2) if len(pieces) == 2 { - var ipFrom = net.ParseIP(strings.TrimSpace(pieces[0])) + var ipFromString = strings.TrimSpace(pieces[0]) + if len(ipFromString) == 0 { + ipFromString = "0.0.0.0" + } + var ipFrom = net.ParseIP(ipFromString) var ipTo = net.ParseIP(strings.TrimSpace(pieces[1])) if ipFrom != nil && ipTo != nil { if bytes.Compare(ipFrom, ipTo) > 0 {