plugins/portmap: fix test flake
The source address selection was random, and sometimes we picked a source address that the container didn't have a route to. Adding a default route fixes that!
This commit is contained in:
parent
e7328869fa
commit
3745ee2d3f
@ -16,6 +16,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -30,9 +31,10 @@ import (
|
|||||||
"github.com/vishvananda/netlink"
|
"github.com/vishvananda/netlink"
|
||||||
)
|
)
|
||||||
|
|
||||||
const TIMEOUT = 20
|
const TIMEOUT = 90
|
||||||
|
|
||||||
var _ = Describe("portmap integration tests", func() {
|
var _ = Describe("portmap integration tests", func() {
|
||||||
|
rand.Seed(time.Now().UTC().UnixNano())
|
||||||
|
|
||||||
var configList *libcni.NetworkConfigList
|
var configList *libcni.NetworkConfigList
|
||||||
var cniConf *libcni.CNIConfig
|
var cniConf *libcni.CNIConfig
|
||||||
@ -51,7 +53,10 @@ var _ = Describe("portmap integration tests", func() {
|
|||||||
"ipMasq": true,
|
"ipMasq": true,
|
||||||
"ipam": {
|
"ipam": {
|
||||||
"type": "host-local",
|
"type": "host-local",
|
||||||
"subnet": "172.16.31.0/24"
|
"subnet": "172.16.31.0/24",
|
||||||
|
"routes": [
|
||||||
|
{"dst": "0.0.0.0/0"}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -89,9 +94,9 @@ var _ = Describe("portmap integration tests", func() {
|
|||||||
// This needs to be done using Ginkgo's asynchronous testing mode.
|
// This needs to be done using Ginkgo's asynchronous testing mode.
|
||||||
It("forwards a TCP port on ipv4", func(done Done) {
|
It("forwards a TCP port on ipv4", func(done Done) {
|
||||||
var err error
|
var err error
|
||||||
hostPort := 9999
|
hostPort := rand.Intn(10000) + 1025
|
||||||
runtimeConfig := libcni.RuntimeConf{
|
runtimeConfig := libcni.RuntimeConf{
|
||||||
ContainerID: "unit-test",
|
ContainerID: fmt.Sprintf("unit-test-%d", hostPort),
|
||||||
NetNS: targetNS.Path(),
|
NetNS: targetNS.Path(),
|
||||||
IfName: "eth0",
|
IfName: "eth0",
|
||||||
CapabilityArgs: map[string]interface{}{
|
CapabilityArgs: map[string]interface{}{
|
||||||
@ -118,7 +123,7 @@ var _ = Describe("portmap integration tests", func() {
|
|||||||
// we'll also manually check the iptables chains
|
// we'll also manually check the iptables chains
|
||||||
ipt, err := iptables.NewWithProtocol(iptables.ProtocolIPv4)
|
ipt, err := iptables.NewWithProtocol(iptables.ProtocolIPv4)
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
dnatChainName := genDnatChain("cni-portmap-unit-test", "unit-test", nil).name
|
dnatChainName := genDnatChain("cni-portmap-unit-test", runtimeConfig.ContainerID, nil).name
|
||||||
|
|
||||||
// Create the network
|
// Create the network
|
||||||
resI, err := cniConf.AddNetworkList(configList, &runtimeConfig)
|
resI, err := cniConf.AddNetworkList(configList, &runtimeConfig)
|
||||||
@ -144,11 +149,14 @@ var _ = Describe("portmap integration tests", func() {
|
|||||||
Fail("could not determine container IP")
|
Fail("could not determine container IP")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
hostIP := getLocalIP()
|
||||||
|
fmt.Fprintf(GinkgoWriter, "hostIP: %s:%d, contIP: %s:%d\n",
|
||||||
|
hostIP, hostPort, contIP, containerPort)
|
||||||
|
|
||||||
// Sanity check: verify that the container is reachable directly
|
// Sanity check: verify that the container is reachable directly
|
||||||
contOK := testEchoServer(fmt.Sprintf("%s:%d", contIP.String(), containerPort))
|
contOK := testEchoServer(fmt.Sprintf("%s:%d", contIP.String(), containerPort))
|
||||||
|
|
||||||
// Verify that a connection to the forwarded port works
|
// Verify that a connection to the forwarded port works
|
||||||
hostIP := getLocalIP()
|
|
||||||
dnatOK := testEchoServer(fmt.Sprintf("%s:%d", hostIP, hostPort))
|
dnatOK := testEchoServer(fmt.Sprintf("%s:%d", hostIP, hostPort))
|
||||||
|
|
||||||
// Verify that a connection to localhost works
|
// Verify that a connection to localhost works
|
||||||
@ -176,7 +184,7 @@ var _ = Describe("portmap integration tests", func() {
|
|||||||
|
|
||||||
close(done)
|
close(done)
|
||||||
|
|
||||||
}, TIMEOUT*3)
|
}, TIMEOUT*9)
|
||||||
})
|
})
|
||||||
|
|
||||||
// testEchoServer returns true if we found an echo server on the port
|
// testEchoServer returns true if we found an echo server on the port
|
||||||
@ -221,6 +229,9 @@ func getLocalIP() string {
|
|||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
for _, addr := range addrs {
|
for _, addr := range addrs {
|
||||||
|
if !addr.IP.IsGlobalUnicast() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
return addr.IP.String()
|
return addr.IP.String()
|
||||||
}
|
}
|
||||||
Fail("no live addresses")
|
Fail("no live addresses")
|
||||||
|
@ -32,7 +32,7 @@ func TestPortmap(t *testing.T) {
|
|||||||
RunSpecs(t, "portmap Suite")
|
RunSpecs(t, "portmap Suite")
|
||||||
}
|
}
|
||||||
|
|
||||||
// OpenEchoServer opens a server that handles one connection before closing.
|
// OpenEchoServer opens a server that listens until closeChan is closed.
|
||||||
// It opens on a random port and sends the port number on portChan when
|
// It opens on a random port and sends the port number on portChan when
|
||||||
// the server is up and running. If an error is encountered, closes portChan.
|
// the server is up and running. If an error is encountered, closes portChan.
|
||||||
// If closeChan is closed, closes the socket.
|
// If closeChan is closed, closes the socket.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user