mirror of
https://github.com/containers/podman.git
synced 2025-07-02 00:30:00 +08:00
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:
@ -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 {
|
||||
|
@ -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() {
|
||||
|
Reference in New Issue
Block a user