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 <pholzing@redhat.com>
This commit is contained in:
Paul Holzinger
2025-03-13 14:59:38 +01:00
parent f8787bb219
commit bcc2063e9e
2 changed files with 19 additions and 15 deletions

View File

@ -1415,19 +1415,22 @@ func GetPort() int {
return 0 // notreached return 0 // notreached
} }
func ncz(port int) bool { // testPortConnection check if we can connect to the given tcp port
timeout := 500 * time.Millisecond // This is doing some retries in case the container process has not yet bound the port.
for i := 0; i < 5; i++ { func testPortConnection(port int) {
ncCmd := []string{"-z", "localhost", strconv.Itoa(port)} GinkgoHelper()
GinkgoWriter.Printf("Running: nc %s\n", strings.Join(ncCmd, " ")) var conn net.Conn
check := SystemExec("nc", ncCmd) var err error
if check.ExitCode() == 0 {
return true for range 5 {
conn, err = net.Dial("tcp", net.JoinHostPort("localhost", strconv.Itoa(port)))
if err == nil {
conn.Close()
return
} }
time.Sleep(timeout) time.Sleep(500 * time.Millisecond)
timeout++
} }
return false Expect(err).ToNot(HaveOccurred())
} }
func createNetworkName(name string) string { func createNetworkName(name string) string {

View File

@ -4,6 +4,7 @@ package integration
import ( import (
"fmt" "fmt"
"net"
"os" "os"
"os/user" "os/user"
"path/filepath" "path/filepath"
@ -68,8 +69,8 @@ var _ = Describe("Podman pod create", func() {
webserver.WaitWithDefaultTimeout() webserver.WaitWithDefaultTimeout()
Expect(webserver).Should(ExitCleanly()) Expect(webserver).Should(ExitCleanly())
check := SystemExec("nc", []string{"-z", "localhost", "80"}) _, err := net.Dial("tcp", "localhost:80")
Expect(check).Should(ExitWithError(1, "")) Expect(err).To(HaveOccurred())
}) })
It("podman create pod with network portbindings", func() { 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 := podmanTest.Podman([]string{"run", "--pod", pod, "-dt", NGINX_IMAGE})
webserver.WaitWithDefaultTimeout() webserver.WaitWithDefaultTimeout()
Expect(webserver).Should(ExitCleanly()) 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() { 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 := podmanTest.Podman([]string{"run", "--pod-id-file", file, "-dt", NGINX_IMAGE})
webserver.WaitWithDefaultTimeout() webserver.WaitWithDefaultTimeout()
Expect(webserver).Should(ExitCleanly()) 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() { It("podman create pod with no infra but portbindings should fail", func() {