From 40a4f174d7c43a87b1aee2c36e59a24c9d779bfa Mon Sep 17 00:00:00 2001 From: xuheng Date: Sat, 1 Aug 2026 00:33:36 +0800 Subject: [PATCH 1/2] =?UTF-8?q?test:=20=E4=BF=AE=E5=A4=8D=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E4=BE=9D=E8=B5=96=E6=9C=AC=E5=9C=B0=E7=BD=91=E7=BB=9C?= =?UTF-8?q?=E7=8E=AF=E5=A2=83=E5=92=8C=E6=9D=83=E9=99=90=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - TestPing 改用公网稳定地址 1.1.1.1,不再硬编码局域网 IP - TestPingWithInvalidIP 改用 IANA 保留地址 192.0.2.1 - CI test 步骤加 sudo(ICMP 需要 root 权限) - Makefile 去掉无效的 -tags integration --- .github/workflows/build-test.yml | 2 +- Makefile | 2 +- pkg/lmap/ping_test.go | 10 ++++++---- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/.github/workflows/build-test.yml b/.github/workflows/build-test.yml index 5f7186c..f330b9a 100644 --- a/.github/workflows/build-test.yml +++ b/.github/workflows/build-test.yml @@ -24,7 +24,7 @@ jobs: - name: test run: | - make test + sudo make test - name: build run: | diff --git a/Makefile b/Makefile index 44d5df5..58d36e2 100644 --- a/Makefile +++ b/Makefile @@ -42,7 +42,7 @@ install: @go mod tidy test: - @go test -cpu=1,2,4 -v -tags integration ./... + @go test -cpu=1,2,4 -v ./... vet: @$(foreach n, $(packages),\ diff --git a/pkg/lmap/ping_test.go b/pkg/lmap/ping_test.go index 2938332..ad0c73c 100644 --- a/pkg/lmap/ping_test.go +++ b/pkg/lmap/ping_test.go @@ -25,16 +25,18 @@ import ( ) func TestPing(t *testing.T) { - ip := net.ParseIP("192.168.0.106") + // 使用公网稳定地址,避免依赖本地网络环境 + ip := net.ParseIP("1.1.1.1") res := Ping(ip) got.T(t).True(res) } func TestPingWithInvalidIP(t *testing.T) { - // 测试无效IP的情况 - invalidIP := net.ParseIP("0.0.0.0") + // 测试不可达IP的情况 + // 192.0.2.1 是 IANA 保留的 TEST-NET-1 地址,不会被真实路由响应 + invalidIP := net.ParseIP("192.0.2.1") res := Ping(invalidIP) - // 对于无效IP,期望返回false + // 对于不可达IP,期望返回false got.T(t).False(res) } From 4cc8d9c2c88323f260e364dbd592abf7e7f92091 Mon Sep 17 00:00:00 2001 From: xuheng Date: Sat, 1 Aug 2026 00:35:57 +0800 Subject: [PATCH 2/2] =?UTF-8?q?test:=20TestPing=20=E6=94=B9=E7=94=A8?= =?UTF-8?q?=E5=9B=9E=E7=8E=AF=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GitHub Actions 禁止出站 ICMP,公网地址在 CI 中永远不可达。 回环地址不依赖出站网络,与 TestPingWithLocalhost 合并。 --- pkg/lmap/ping_test.go | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/pkg/lmap/ping_test.go b/pkg/lmap/ping_test.go index ad0c73c..51c5b72 100644 --- a/pkg/lmap/ping_test.go +++ b/pkg/lmap/ping_test.go @@ -25,8 +25,9 @@ import ( ) func TestPing(t *testing.T) { - // 使用公网稳定地址,避免依赖本地网络环境 - ip := net.ParseIP("1.1.1.1") + // 使用回环地址:不依赖出站网络(GitHub Actions 禁止出站 ICMP), + // 也不依赖本地局域网环境 + ip := net.ParseIP("127.0.0.1") res := Ping(ip) got.T(t).True(res) } @@ -39,11 +40,3 @@ func TestPingWithInvalidIP(t *testing.T) { // 对于不可达IP,期望返回false got.T(t).False(res) } - -func TestPingWithLocalhost(t *testing.T) { - // 测试本地回环地址 - localhost := net.ParseIP("127.0.0.1") - res := Ping(localhost) - // 本地回环地址通常应该返回true - got.T(t).True(res) -}