mirror of https://github.com/containers/podman.git
test/e2e: fix ncat tests
These tests are flaking for unknown reasons. One problem could be ncat did not bound the port before we connect to it. Simple fix do not use ncat and just use the golang API to listen on the port without the extra ncat process. This should be race free in theory. Also do not run the nc container in the background, we want to see the errors from the ncat process in the container. And because both tests do the same thing deduplicate them into one that just uses a loop to create both tests. Fixes #23263 Signed-off-by: Paul Holzinger <pholzing@redhat.com>
This commit is contained in:
parent
d62ef3fb21
commit
5209495865
|
@ -3,10 +3,12 @@ package integration
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/containernetworking/plugins/pkg/ns"
|
"github.com/containernetworking/plugins/pkg/ns"
|
||||||
|
@ -543,56 +545,58 @@ EXPOSE 2004-2005/tcp`, ALPINE)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
It("podman run network bind to 127.0.0.1", func() {
|
for _, local := range []bool{true, false} {
|
||||||
slirp4netnsHelp := SystemExec("slirp4netns", []string{"--help"})
|
local := local
|
||||||
Expect(slirp4netnsHelp).Should(ExitCleanly())
|
testName := "HostIP"
|
||||||
networkConfiguration := "slirp4netns:outbound_addr=127.0.0.1,allow_host_loopback=true"
|
if local {
|
||||||
port := strconv.Itoa(GetPort())
|
testName = "127.0.0.1"
|
||||||
|
|
||||||
if strings.Contains(slirp4netnsHelp.OutputToString(), "outbound-addr") {
|
|
||||||
ncListener := StartSystemExec("nc", []string{"-v", "-n", "-l", "-p", port})
|
|
||||||
session := podmanTest.Podman([]string{"run", "--network", networkConfiguration, "-dt", ALPINE, "nc", "-w", "2", "10.0.2.2", port})
|
|
||||||
session.WaitWithDefaultTimeout()
|
|
||||||
ncListener.WaitWithDefaultTimeout()
|
|
||||||
|
|
||||||
Expect(session).Should(ExitCleanly())
|
|
||||||
Expect(ncListener).Should(Exit(0))
|
|
||||||
Expect(ncListener.ErrorToString()).To(ContainSubstring("Connection from 127.0.0.1"))
|
|
||||||
} else {
|
|
||||||
session := podmanTest.Podman([]string{"run", "--network", networkConfiguration, "-dt", ALPINE, "nc", "-w", "2", "10.0.2.2", port})
|
|
||||||
session.WaitWithDefaultTimeout()
|
|
||||||
Expect(session).To(ExitWithError(125, "outbound_addr not supported"))
|
|
||||||
}
|
}
|
||||||
})
|
It(fmt.Sprintf("podman run network slirp4netns bind to %s", testName), func() {
|
||||||
|
ip := "127.0.0.1"
|
||||||
It("podman run network bind to HostIP", func() {
|
if !local {
|
||||||
// Determine our likeliest outgoing IP address
|
// Determine our likeliest outgoing IP address
|
||||||
conn, err := net.Dial("udp", "8.8.8.8:80")
|
conn, err := net.Dial("udp", "8.8.8.8:80")
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
ip := conn.LocalAddr().(*net.UDPAddr).IP
|
ip = conn.LocalAddr().(*net.UDPAddr).IP.String()
|
||||||
|
}
|
||||||
port := strconv.Itoa(GetPort())
|
port := strconv.Itoa(GetPort())
|
||||||
|
|
||||||
slirp4netnsHelp := SystemExec("slirp4netns", []string{"--help"})
|
networkConfiguration := fmt.Sprintf("slirp4netns:outbound_addr=%s,allow_host_loopback=true", ip)
|
||||||
Expect(slirp4netnsHelp).Should(ExitCleanly())
|
|
||||||
networkConfiguration := fmt.Sprintf("slirp4netns:outbound_addr=%s,allow_host_loopback=true", ip.String())
|
|
||||||
|
|
||||||
if strings.Contains(slirp4netnsHelp.OutputToString(), "outbound-addr") {
|
listener, err := net.Listen("tcp", ":"+port)
|
||||||
ncListener := StartSystemExec("nc", []string{"-v", "-n", "-l", "-p", port})
|
Expect(err).ToNot(HaveOccurred())
|
||||||
session := podmanTest.Podman([]string{"run", "--network", networkConfiguration, ALPINE, "nc", "-w", "2", "10.0.2.2", port})
|
defer listener.Close()
|
||||||
session.Wait(30)
|
|
||||||
ncListener.Wait(30)
|
|
||||||
|
|
||||||
|
msg := RandomString(10)
|
||||||
|
wg := &sync.WaitGroup{}
|
||||||
|
wg.Add(1)
|
||||||
|
// now use a new goroutine to start accepting connection in the background and make the checks there
|
||||||
|
go func() {
|
||||||
|
defer GinkgoRecover()
|
||||||
|
defer wg.Done()
|
||||||
|
conn, err := listener.Accept()
|
||||||
|
Expect(err).ToNot(HaveOccurred(), "accept new connection")
|
||||||
|
defer conn.Close()
|
||||||
|
addr := conn.RemoteAddr()
|
||||||
|
// addr will be in the form ip:port, we don't care about the port as it is random
|
||||||
|
Expect(addr.String()).To(HavePrefix(ip+":"), "remote address")
|
||||||
|
gotBytes, err := io.ReadAll(conn)
|
||||||
|
Expect(err).ToNot(HaveOccurred(), "read from connection")
|
||||||
|
Expect(string(gotBytes)).To(Equal(msg), "received correct message from container")
|
||||||
|
}()
|
||||||
|
|
||||||
|
session := podmanTest.Podman([]string{"run", "--network", networkConfiguration, ALPINE, "sh", "-c", "echo -n " + msg + " | nc -w 30 10.0.2.2 " + port})
|
||||||
|
session.WaitWithDefaultTimeout()
|
||||||
Expect(session).Should(ExitCleanly())
|
Expect(session).Should(ExitCleanly())
|
||||||
Expect(ncListener).Should(Exit(0))
|
|
||||||
Expect(ncListener.ErrorToString()).To(ContainSubstring("Connection from " + ip.String()))
|
// explicitly close the socket here before we wait to unlock Accept() calls in case of hangs
|
||||||
} else {
|
listener.Close()
|
||||||
session := podmanTest.Podman([]string{"run", "--network", networkConfiguration, ALPINE, "nc", "-w", "2", "10.0.2.2", port})
|
// wait for the checks in the goroutine to be done
|
||||||
session.Wait(30)
|
wg.Wait()
|
||||||
Expect(session).To(ExitWithError(125, "outbound_addr not supported"))
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
It("podman run network expose ports in image metadata", func() {
|
It("podman run network expose ports in image metadata", func() {
|
||||||
session := podmanTest.Podman([]string{"create", "--name", "test", "-t", "-P", NGINX_IMAGE})
|
session := podmanTest.Podman([]string{"create", "--name", "test", "-t", "-P", NGINX_IMAGE})
|
||||||
|
|
Loading…
Reference in New Issue