aboutsummaryrefslogtreecommitdiff
path: root/test/e2e/run_staticip_test.go
blob: 12783cd296ca19e61fa3e079308ccba2812be725 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package integration

import (
	"fmt"
	"net/http"
	"os"
	"time"

	. "github.com/containers/podman/v4/test/utils"
	"github.com/containers/storage/pkg/stringid"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	. "github.com/onsi/gomega/gexec"
)

var _ = Describe("Podman run with --ip flag", func() {
	var (
		tempdir    string
		err        error
		podmanTest *PodmanTestIntegration
	)

	BeforeEach(func() {
		SkipIfRootless("rootless does not support --ip without network")
		tempdir, err = CreateTempDirInTempDir()
		if err != nil {
			os.Exit(1)
		}
		podmanTest = PodmanTestCreate(tempdir)
		podmanTest.Setup()
		// Clean up the CNI networks used by the tests
		os.RemoveAll("/var/lib/cni/networks/podman")
	})

	AfterEach(func() {
		podmanTest.Cleanup()
		f := CurrentGinkgoTestDescription()
		processTestResult(f)

	})

	It("Podman run --ip with garbage address", func() {
		result := podmanTest.Podman([]string{"run", "-ti", "--ip", "114232346", ALPINE, "ls"})
		result.WaitWithDefaultTimeout()
		Expect(result).To(ExitWithError())
	})

	It("Podman run --ip with v6 address", func() {
		result := podmanTest.Podman([]string{"run", "-ti", "--ip", "2001:db8:bad:beef::1", ALPINE, "ls"})
		result.WaitWithDefaultTimeout()
		Expect(result).To(ExitWithError())
	})

	It("Podman run --ip with non-allocatable IP", func() {
		result := podmanTest.Podman([]string{"run", "-ti", "--ip", "203.0.113.124", ALPINE, "ls"})
		result.WaitWithDefaultTimeout()
		Expect(result).To(ExitWithError())
	})

	It("Podman run with specified static IP has correct IP", func() {
		ip := GetRandomIPAddress()
		result := podmanTest.Podman([]string{"run", "-ti", "--ip", ip, ALPINE, "ip", "addr"})
		result.WaitWithDefaultTimeout()
		Expect(result).Should(Exit(0))
		Expect(result.OutputToString()).To(ContainSubstring(ip + "/16"))
	})

	It("Podman run with specified static IPv6 has correct IP", func() {
		netName := "ipv6-" + stringid.GenerateNonCryptoID()
		ipv6 := "fd46:db93:aa76:ac37::10"
		net := podmanTest.Podman([]string{"network", "create", "--subnet", "fd46:db93:aa76:ac37::/64", netName})
		net.WaitWithDefaultTimeout()
		defer podmanTest.removeNetwork(netName)
		Expect(net).To(Exit(0))

		result := podmanTest.Podman([]string{"run", "-ti", "--network", netName, "--ip6", ipv6, ALPINE, "ip", "addr"})
		result.WaitWithDefaultTimeout()
		Expect(result).Should(Exit(0))
		Expect(result.OutputToString()).To(ContainSubstring(ipv6 + "/64"))
	})

	It("Podman run with --network bridge:ip=", func() {
		ip := GetRandomIPAddress()
		result := podmanTest.Podman([]string{"run", "-ti", "--network", "bridge:ip=" + ip, ALPINE, "ip", "addr"})
		result.WaitWithDefaultTimeout()
		Expect(result).Should(Exit(0))
		Expect(result.OutputToString()).To(ContainSubstring(ip + "/16"))
	})

	It("Podman run with --network net:ip=,mac=,interface_name=", func() {
		ip := GetRandomIPAddress()
		mac := "44:33:22:11:00:99"
		intName := "myeth"
		result := podmanTest.Podman([]string{"run", "-ti", "--network", "bridge:ip=" + ip + ",mac=" + mac + ",interface_name=" + intName, ALPINE, "ip", "addr"})
		result.WaitWithDefaultTimeout()
		Expect(result).Should(Exit(0))
		Expect(result.OutputToString()).To(ContainSubstring(ip + "/16"))
		Expect(result.OutputToString()).To(ContainSubstring(mac))
		Expect(result.OutputToString()).To(ContainSubstring(intName))
	})

	It("Podman run two containers with the same IP", func() {
		ip := GetRandomIPAddress()
		result := podmanTest.Podman([]string{"run", "-d", "--name", "nginx", "--ip", ip, NGINX_IMAGE})
		result.WaitWithDefaultTimeout()
		Expect(result).Should(Exit(0))

		// We need to set "no_proxy" in proxy environment
		if env, found := os.LookupEnv("no_proxy"); found {
			defer os.Setenv("no_proxy", env)
		} else {
			defer os.Unsetenv("no_proxy")
		}
		os.Setenv("no_proxy", ip)
		for retries := 20; retries > 0; retries-- {
			response, err := http.Get(fmt.Sprintf("http://%s", ip))
			if err == nil && response.StatusCode == http.StatusOK {
				break
			}
			if retries == 1 {
				logps := podmanTest.Podman([]string{"ps", "-a"})
				logps.WaitWithDefaultTimeout()
				logps = podmanTest.Podman([]string{"logs", "nginx"})
				logps.WaitWithDefaultTimeout()
				Fail("Timed out waiting for nginx container, see ps & log above.")
			}

			if err != nil {
				fmt.Printf("nginx not ready yet; error=%v; %d retries left...\n", err, retries)
			} else {
				fmt.Printf("nginx not ready yet; response=%v; %d retries left...\n", response.StatusCode, retries)
			}
			time.Sleep(1 * time.Second)
		}
		result = podmanTest.Podman([]string{"run", "--ip", ip, ALPINE, "ip", "addr"})
		result.WaitWithDefaultTimeout()
		Expect(result).To(ExitWithError())
		Expect(result.ErrorToString()).To(ContainSubstring(" address %s ", ip))
	})
})