Repository: k3s-io/k3s
File: tests/integration/flannelipv6masq/flannelipv6masq_int_test.go (Go)

Code before:
		var err error
		testLock, err = testutil.K3sTestLock()
		Expect(err).ToNot(HaveOccurred())
		server, err = testutil.K3sStartServer(dualStackServeripv6masqArgs...)
		Expect(err).ToNot(HaveOccurred())
	}
})

var _ = Describe("flannel-ipv6-masq", Ordered, func() {
	BeforeEach(func() {
		if testutil.IsExistingServer() && !testutil.ServerArgsPresent(dualStackServeripv6masqArgs) {
			Skip("Test needs k3s server with: " + strings.Join(dualStackServeripv6masqArgs, " "))
		} else if os.Getenv("CI") == "true" {
			Skip("Github environment does not support IPv6")
		}
	})
	When("a ipv4 and ipv6 cidr is present", func() {
		It("starts up with no problems", func() {
			Eventually(func() error {
				return testutil.K3sDefaultDeployments()
			}, "180s", "10s").Should(Succeed())
		})
		It("creates pods with two IPs", func() {
			podname, err := testutil.K3sCmd("kubectl", "get", "pods", "-n", "kube-system", "-o", "jsonpath={.items[?(@.metadata.labels.app\\.kubernetes\\.io/name==\"traefik\")].metadata.name}")
			Expect(err).NotTo(HaveOccurred())
			Eventually(func() (string, error) {
				return testutil.K3sCmd("kubectl", "exec", podname, "-n", "kube-system", "--", "ip", "a")
			}, "5s", "1s").Should(ContainSubstring("2001:cafe:42:"))
		})
		It("verifies ipv6 masq iptables rule exists", func() {
			Eventually(func() (string, error) {
				return testutil.RunCommand("ip6tables -nt nat -L FLANNEL-POSTRTG")
			}, "5s", "1s").Should(ContainSubstring("MASQUERADE"))
		})
	})
})

var failed bool
var _ = AfterEach(func() {
	failed = failed || CurrentSpecReport().Failed()
})

var _ = AfterSuite(func() {
	if !testutil.IsExistingServer() && os.Getenv("CI") != "true" {
		if failed {
			testutil.K3sSaveLog(server, false)
		}
		Expect(testutil.K3sKillServer(server)).To(Succeed())
		Expect(testutil.K3sCleanup(testLock, "")).To(Succeed())
	}
})

Review comment (suggestion):
1. Do you really need to exec into the pod to do this? The IPs are exposed via `pod.status.podIPs`:
  `kubectl get pod -n kube-system -l app.kubernetes.io/name=traefik -o "jsonpath={.items[].status.podIPs[].ip}"`
  You should also use a label selector instead of filtering by name in the jsonpath expression...
2. If you really want to exec into the pod to get this info, you can do so in one line (without listing the pods first) by running:
  `kubectl exec -n kube-system deployment/traefik -c traefik -- ip a`

Code after:
		var err error
		testLock, err = testutil.K3sTestLock()
		Expect(err).ToNot(HaveOccurred())
		server, err = testutil.K3sStartServer(dualStackServeripv6masqArgs...)
		Expect(err).ToNot(HaveOccurred())
	}
})

var _ = Describe("flannel-ipv6-masq", Ordered, func() {
	BeforeEach(func() {
		if testutil.IsExistingServer() && !testutil.ServerArgsPresent(dualStackServeripv6masqArgs) {
			Skip("Test needs k3s server with: " + strings.Join(dualStackServeripv6masqArgs, " "))
		} else if os.Getenv("CI") == "true" {
			Skip("Github environment does not support IPv6")
		}
	})
	When("a ipv4 and ipv6 cidr is present", func() {
		It("starts up with no problems", func() {
			Eventually(func() error {
				return testutil.K3sDefaultDeployments()
			}, "180s", "10s").Should(Succeed())
		})
		It("creates pods with two IPs", func() {
			Eventually(func() (string, error) {
				return testutil.K3sCmd("kubectl", "exec", "deployment/traefik", "-n", "kube-system", "--", "ip", "a")
			}, "5s", "1s").Should(ContainSubstring("2001:cafe:42:"))
		})
		It("verifies ipv6 masq iptables rule exists", func() {
			Eventually(func() (string, error) {
				return testutil.RunCommand("ip6tables -nt nat -L FLANNEL-POSTRTG")
			}, "5s", "1s").Should(ContainSubstring("MASQUERADE"))
		})
	})
})

var failed bool
var _ = AfterEach(func() {
	failed = failed || CurrentSpecReport().Failed()
})

var _ = AfterSuite(func() {
	if !testutil.IsExistingServer() && os.Getenv("CI") != "true" {
		if failed {
			testutil.K3sSaveLog(server, false)
		}
		Expect(testutil.K3sKillServer(server)).To(Succeed())
		Expect(testutil.K3sCleanup(testLock, "")).To(Succeed())
	}
})

func Test_IntegrationFlannelIpv6Masq(t *testing.T) {