From bcc2063e9e701b2dd87998ac5756b2830a10665b Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Thu, 13 Mar 2025 14:59:38 +0100 Subject: [PATCH] test/e2e: use go net.Dial() ov nc This is simpler as we don't have to rely on an external command. The retry loop is need as we check for a container porcess connection, and while we know podman binds the port before returning there is no way to know whenthe contianer application bound the port so we must retry a bit. Signed-off-by: Paul Holzinger --- test/e2e/common_test.go | 25 ++++++++++++++----------- test/e2e/pod_create_test.go | 9 +++++---- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/test/e2e/common_test.go b/test/e2e/common_test.go index 518a8f028d..f446a2bd35 100644 --- a/test/e2e/common_test.go +++ b/test/e2e/common_test.go @@ -1415,19 +1415,22 @@ func GetPort() int { return 0 // notreached } -func ncz(port int) bool { - timeout := 500 * time.Millisecond - for i := 0; i < 5; i++ { - ncCmd := []string{"-z", "localhost", strconv.Itoa(port)} - GinkgoWriter.Printf("Running: nc %s\n", strings.Join(ncCmd, " ")) - check := SystemExec("nc", ncCmd) - if check.ExitCode() == 0 { - return true +// testPortConnection check if we can connect to the given tcp port +// This is doing some retries in case the container process has not yet bound the port. +func testPortConnection(port int) { + GinkgoHelper() + var conn net.Conn + var err error + + for range 5 { + conn, err = net.Dial("tcp", net.JoinHostPort("localhost", strconv.Itoa(port))) + if err == nil { + conn.Close() + return } - time.Sleep(timeout) - timeout++ + time.Sleep(500 * time.Millisecond) } - return false + Expect(err).ToNot(HaveOccurred()) } func createNetworkName(name string) string { diff --git a/test/e2e/pod_create_test.go b/test/e2e/pod_create_test.go index 4b0f814f93..d18bb1f00e 100644 --- a/test/e2e/pod_create_test.go +++ b/test/e2e/pod_create_test.go @@ -4,6 +4,7 @@ package integration import ( "fmt" + "net" "os" "os/user" "path/filepath" @@ -68,8 +69,8 @@ var _ = Describe("Podman pod create", func() { webserver.WaitWithDefaultTimeout() Expect(webserver).Should(ExitCleanly()) - check := SystemExec("nc", []string{"-z", "localhost", "80"}) - Expect(check).Should(ExitWithError(1, "")) + _, err := net.Dial("tcp", "localhost:80") + Expect(err).To(HaveOccurred()) }) It("podman create pod with network portbindings", func() { @@ -83,7 +84,7 @@ var _ = Describe("Podman pod create", func() { webserver := podmanTest.Podman([]string{"run", "--pod", pod, "-dt", NGINX_IMAGE}) webserver.WaitWithDefaultTimeout() Expect(webserver).Should(ExitCleanly()) - Expect(ncz(port)).To(BeTrue(), "port %d is up", port) + testPortConnection(port) }) It("podman create pod with id file with network portbindings", func() { @@ -97,7 +98,7 @@ var _ = Describe("Podman pod create", func() { webserver := podmanTest.Podman([]string{"run", "--pod-id-file", file, "-dt", NGINX_IMAGE}) webserver.WaitWithDefaultTimeout() Expect(webserver).Should(ExitCleanly()) - Expect(ncz(port)).To(BeTrue(), "port %d is up", port) + testPortConnection(port) }) It("podman create pod with no infra but portbindings should fail", func() {