From fe8539b409f61ce4df5cc7601bcff16b01aa6318 Mon Sep 17 00:00:00 2001 From: Joelle Maslak Date: Sun, 26 Jan 2025 08:49:04 -0700 Subject: [PATCH 1/2] Client timeout test now avoids internet access --- client_test.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/client_test.go b/client_test.go index 1779df7..64fca31 100644 --- a/client_test.go +++ b/client_test.go @@ -33,6 +33,7 @@ package goeapi import ( "fmt" + "net" "os" "os/user" "regexp" @@ -460,8 +461,14 @@ func TestClientConnectInvalidTransport_UnitTest(t *testing.T) { } func TestClientConnectTimeout_UnitTest(t *testing.T) { - // 1.1.1.2 is assumed to be an unreachable bogus address - node, err := Connect("http", "1.1.1.2", "admin", "admin", 80) + // We create a local socket that never gives any responses + // We assume that 127.0.0.1 exists as the loopback on the test host. + sock, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal("Error in test setup (listen).") + } + port := sock.Addr().(*net.TCPAddr).Port + node, err := Connect("http", "127.0.0.1", "admin", "admin", port) if err != nil { t.Fatal("Error in connect.") } @@ -470,6 +477,7 @@ func TestClientConnectTimeout_UnitTest(t *testing.T) { if _, err = node.getConfigText("running-config", "all"); err == nil { t.Fatal("Should timeout and return error") } + sock.Close() } func TestClientConnectDefaultPort_UnitTest(t *testing.T) { From 7d6441eab13cdf0c945d69dc84c833b447cacaca Mon Sep 17 00:00:00 2001 From: Joelle Maslak Date: Sun, 26 Jan 2025 08:55:06 -0700 Subject: [PATCH 2/2] Use defer to close socket in client timeout test --- client_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client_test.go b/client_test.go index 64fca31..c4c4e11 100644 --- a/client_test.go +++ b/client_test.go @@ -467,6 +467,7 @@ func TestClientConnectTimeout_UnitTest(t *testing.T) { if err != nil { t.Fatal("Error in test setup (listen).") } + defer sock.Close() port := sock.Addr().(*net.TCPAddr).Port node, err := Connect("http", "127.0.0.1", "admin", "admin", port) if err != nil { @@ -477,7 +478,6 @@ func TestClientConnectTimeout_UnitTest(t *testing.T) { if _, err = node.getConfigText("running-config", "all"); err == nil { t.Fatal("Should timeout and return error") } - sock.Close() } func TestClientConnectDefaultPort_UnitTest(t *testing.T) {