2022-11-04 23:07:12 +00:00
|
|
|
package dualstack
|
2022-06-14 15:40:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/k3s-io/k3s/tests/e2e"
|
|
|
|
. "github.com/onsi/ginkgo/v2"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Valid nodeOS: generic/ubuntu2004, opensuse/Leap-15.3.x86_64
|
|
|
|
var nodeOS = flag.String("nodeOS", "generic/ubuntu2004", "VM operating system")
|
2022-11-04 23:07:12 +00:00
|
|
|
var serverCount = flag.Int("serverCount", 3, "number of server nodes")
|
2022-10-10 13:09:46 +00:00
|
|
|
var agentCount = flag.Int("agentCount", 1, "number of agent nodes")
|
2022-08-05 16:16:10 +00:00
|
|
|
var hardened = flag.Bool("hardened", false, "true or false")
|
2023-01-12 00:09:45 +00:00
|
|
|
var ci = flag.Bool("ci", false, "running on CI")
|
2023-08-01 16:32:34 +00:00
|
|
|
var local = flag.Bool("local", false, "deploy a locally built K3s binary")
|
2022-06-14 15:40:29 +00:00
|
|
|
|
|
|
|
func Test_E2EDualStack(t *testing.T) {
|
|
|
|
flag.Parse()
|
|
|
|
RegisterFailHandler(Fail)
|
2022-11-04 23:07:12 +00:00
|
|
|
suiteConfig, reporterConfig := GinkgoConfiguration()
|
|
|
|
RunSpecs(t, "DualStack Test Suite", suiteConfig, reporterConfig)
|
2022-06-14 15:40:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
kubeConfigFile string
|
|
|
|
serverNodeNames []string
|
|
|
|
agentNodeNames []string
|
|
|
|
)
|
|
|
|
|
2022-11-04 23:07:12 +00:00
|
|
|
var _ = ReportAfterEach(e2e.GenReport)
|
|
|
|
|
2022-09-06 19:56:37 +00:00
|
|
|
var _ = Describe("Verify DualStack Configuration", Ordered, func() {
|
2022-06-14 15:40:29 +00:00
|
|
|
|
|
|
|
It("Starts up with no issues", func() {
|
|
|
|
var err error
|
2023-08-01 16:32:34 +00:00
|
|
|
if *local {
|
|
|
|
serverNodeNames, agentNodeNames, err = e2e.CreateLocalCluster(*nodeOS, *serverCount, *agentCount)
|
|
|
|
} else {
|
|
|
|
serverNodeNames, agentNodeNames, err = e2e.CreateCluster(*nodeOS, *serverCount, *agentCount)
|
|
|
|
}
|
2022-10-06 19:27:44 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred(), e2e.GetVagrantLog(err))
|
2022-06-14 15:40:29 +00:00
|
|
|
fmt.Println("CLUSTER CONFIG")
|
|
|
|
fmt.Println("OS:", *nodeOS)
|
|
|
|
fmt.Println("Server Nodes:", serverNodeNames)
|
|
|
|
fmt.Println("Agent Nodes:", agentNodeNames)
|
|
|
|
kubeConfigFile, err = e2e.GenKubeConfigFile(serverNodeNames[0])
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Checks Node Status", func() {
|
|
|
|
Eventually(func(g Gomega) {
|
|
|
|
nodes, err := e2e.ParseNodes(kubeConfigFile, false)
|
|
|
|
g.Expect(err).NotTo(HaveOccurred())
|
|
|
|
for _, node := range nodes {
|
|
|
|
g.Expect(node.Status).Should(Equal("Ready"))
|
|
|
|
}
|
2023-01-12 00:09:45 +00:00
|
|
|
}, "620s", "5s").Should(Succeed())
|
2022-06-14 15:40:29 +00:00
|
|
|
_, err := e2e.ParseNodes(kubeConfigFile, true)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Checks Pod Status", func() {
|
|
|
|
Eventually(func(g Gomega) {
|
|
|
|
pods, err := e2e.ParsePods(kubeConfigFile, false)
|
|
|
|
g.Expect(err).NotTo(HaveOccurred())
|
|
|
|
for _, pod := range pods {
|
|
|
|
if strings.Contains(pod.Name, "helm-install") {
|
|
|
|
g.Expect(pod.Status).Should(Equal("Completed"), pod.Name)
|
|
|
|
} else {
|
|
|
|
g.Expect(pod.Status).Should(Equal("Running"), pod.Name)
|
|
|
|
}
|
|
|
|
}
|
2023-01-12 00:09:45 +00:00
|
|
|
}, "620s", "5s").Should(Succeed())
|
2022-06-14 15:40:29 +00:00
|
|
|
_, err := e2e.ParsePods(kubeConfigFile, true)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Verifies that each node has IPv4 and IPv6", func() {
|
2022-10-19 13:06:32 +00:00
|
|
|
nodeIPs, err := e2e.GetNodeIPs(kubeConfigFile)
|
2022-06-14 15:40:29 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
for _, node := range nodeIPs {
|
2022-10-19 13:06:32 +00:00
|
|
|
Expect(node.IPv4).Should(ContainSubstring("10.10.10"))
|
|
|
|
Expect(node.IPv6).Should(ContainSubstring("fd11:decf:c0ff"))
|
2022-06-14 15:40:29 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
It("Verifies that each pod has IPv4 and IPv6", func() {
|
2022-10-19 13:06:32 +00:00
|
|
|
podIPs, err := e2e.GetPodIPs(kubeConfigFile)
|
2022-06-14 15:40:29 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
for _, pod := range podIPs {
|
2022-10-19 13:06:32 +00:00
|
|
|
Expect(pod.IPv4).Should(Or(ContainSubstring("10.10.10"), ContainSubstring("10.42.")), pod.Name)
|
|
|
|
Expect(pod.IPv6).Should(Or(ContainSubstring("fd11:decf:c0ff"), ContainSubstring("2001:cafe:42")), pod.Name)
|
2022-06-14 15:40:29 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Verifies ClusterIP Service", func() {
|
2022-08-05 16:16:10 +00:00
|
|
|
_, err := e2e.DeployWorkload("dualstack_clusterip.yaml", kubeConfigFile, *hardened)
|
2022-06-14 15:40:29 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Eventually(func() (string, error) {
|
|
|
|
cmd := "kubectl get pods -o=name -l k8s-app=nginx-app-clusterip --field-selector=status.phase=Running --kubeconfig=" + kubeConfigFile
|
|
|
|
return e2e.RunCommand(cmd)
|
|
|
|
}, "120s", "5s").Should(ContainSubstring("ds-clusterip-pod"))
|
|
|
|
|
|
|
|
// Checks both IPv4 and IPv6
|
|
|
|
clusterips, err := e2e.FetchClusterIP(kubeConfigFile, "ds-clusterip-svc", true)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
for _, ip := range strings.Split(clusterips, ",") {
|
|
|
|
if strings.Contains(ip, "::") {
|
|
|
|
ip = "[" + ip + "]"
|
|
|
|
}
|
|
|
|
pods, err := e2e.ParsePods(kubeConfigFile, false)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
for _, pod := range pods {
|
|
|
|
if !strings.HasPrefix(pod.Name, "ds-clusterip-pod") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
cmd := fmt.Sprintf("curl -L --insecure http://%s", ip)
|
|
|
|
Eventually(func() (string, error) {
|
|
|
|
return e2e.RunCmdOnNode(cmd, serverNodeNames[0])
|
|
|
|
}, "60s", "5s").Should(ContainSubstring("Welcome to nginx!"), "failed cmd: "+cmd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
It("Verifies Ingress", func() {
|
2022-08-05 16:16:10 +00:00
|
|
|
_, err := e2e.DeployWorkload("dualstack_ingress.yaml", kubeConfigFile, *hardened)
|
2022-06-14 15:40:29 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred(), "Ingress manifest not deployed")
|
|
|
|
cmd := "kubectl get ingress ds-ingress --kubeconfig=" + kubeConfigFile + " -o jsonpath=\"{.spec.rules[*].host}\""
|
|
|
|
hostName, err := e2e.RunCommand(cmd)
|
|
|
|
Expect(err).NotTo(HaveOccurred(), "failed cmd: "+cmd)
|
2022-10-19 13:06:32 +00:00
|
|
|
nodeIPs, err := e2e.GetNodeIPs(kubeConfigFile)
|
2022-06-14 15:40:29 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred(), "failed cmd: "+cmd)
|
|
|
|
for _, node := range nodeIPs {
|
2022-10-19 13:06:32 +00:00
|
|
|
cmd := fmt.Sprintf("curl --header host:%s http://%s/name.html", hostName, node.IPv4)
|
2022-06-14 15:40:29 +00:00
|
|
|
Eventually(func() (string, error) {
|
|
|
|
return e2e.RunCommand(cmd)
|
|
|
|
}, "10s", "2s").Should(ContainSubstring("ds-clusterip-pod"), "failed cmd: "+cmd)
|
2022-10-19 13:06:32 +00:00
|
|
|
cmd = fmt.Sprintf("curl --header host:%s http://[%s]/name.html", hostName, node.IPv6)
|
2022-06-14 15:40:29 +00:00
|
|
|
Eventually(func() (string, error) {
|
|
|
|
return e2e.RunCommand(cmd)
|
|
|
|
}, "5s", "1s").Should(ContainSubstring("ds-clusterip-pod"), "failed cmd: "+cmd)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Verifies NodePort Service", func() {
|
2022-08-05 16:16:10 +00:00
|
|
|
_, err := e2e.DeployWorkload("dualstack_nodeport.yaml", kubeConfigFile, *hardened)
|
2022-06-14 15:40:29 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
cmd := "kubectl get service ds-nodeport-svc --kubeconfig=" + kubeConfigFile + " --output jsonpath=\"{.spec.ports[0].nodePort}\""
|
|
|
|
nodeport, err := e2e.RunCommand(cmd)
|
|
|
|
Expect(err).NotTo(HaveOccurred(), "failed cmd: "+cmd)
|
2022-10-19 13:06:32 +00:00
|
|
|
nodeIPs, err := e2e.GetNodeIPs(kubeConfigFile)
|
2022-06-14 15:40:29 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
for _, node := range nodeIPs {
|
2022-10-19 13:06:32 +00:00
|
|
|
cmd = "curl -L --insecure http://" + node.IPv4 + ":" + nodeport + "/name.html"
|
2022-06-14 15:40:29 +00:00
|
|
|
Eventually(func() (string, error) {
|
|
|
|
return e2e.RunCommand(cmd)
|
|
|
|
}, "10s", "1s").Should(ContainSubstring("ds-nodeport-pod"), "failed cmd: "+cmd)
|
2022-10-19 13:06:32 +00:00
|
|
|
cmd = "curl -L --insecure http://[" + node.IPv6 + "]:" + nodeport + "/name.html"
|
2022-06-14 15:40:29 +00:00
|
|
|
Eventually(func() (string, error) {
|
|
|
|
return e2e.RunCommand(cmd)
|
|
|
|
}, "10s", "1s").Should(ContainSubstring("ds-nodeport-pod"), "failed cmd: "+cmd)
|
|
|
|
}
|
|
|
|
})
|
2022-10-10 13:09:46 +00:00
|
|
|
It("Verifies podSelector Network Policy", func() {
|
|
|
|
_, err := e2e.DeployWorkload("pod_client.yaml", kubeConfigFile, *hardened)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
cmd := "kubectl exec svc/client-curl --kubeconfig=" + kubeConfigFile + " -- curl -m7 ds-clusterip-svc/name.html"
|
|
|
|
Eventually(func() (string, error) {
|
|
|
|
return e2e.RunCommand(cmd)
|
|
|
|
}, "20s", "3s").Should(ContainSubstring("ds-clusterip-pod"), "failed cmd: "+cmd)
|
|
|
|
_, err = e2e.DeployWorkload("netpol-fail.yaml", kubeConfigFile, *hardened)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
cmd = "kubectl exec svc/client-curl --kubeconfig=" + kubeConfigFile + " -- curl -m7 ds-clusterip-svc/name.html"
|
|
|
|
Eventually(func() error {
|
|
|
|
_, err = e2e.RunCommand(cmd)
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
return err
|
|
|
|
}, "20s", "3s")
|
|
|
|
_, err = e2e.DeployWorkload("netpol-work.yaml", kubeConfigFile, *hardened)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
cmd = "kubectl exec svc/client-curl --kubeconfig=" + kubeConfigFile + " -- curl -m7 ds-clusterip-svc/name.html"
|
|
|
|
Eventually(func() (string, error) {
|
|
|
|
return e2e.RunCommand(cmd)
|
|
|
|
}, "20s", "3s").Should(ContainSubstring("ds-clusterip-pod"), "failed cmd: "+cmd)
|
|
|
|
})
|
2022-06-14 15:40:29 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
var failed bool
|
|
|
|
var _ = AfterEach(func() {
|
2022-09-13 21:09:28 +00:00
|
|
|
failed = failed || CurrentSpecReport().Failed()
|
2022-06-14 15:40:29 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
var _ = AfterSuite(func() {
|
2023-01-12 00:09:45 +00:00
|
|
|
if failed && !*ci {
|
2022-06-14 15:40:29 +00:00
|
|
|
fmt.Println("FAILED!")
|
|
|
|
} else {
|
2023-08-01 16:32:34 +00:00
|
|
|
Expect(e2e.GetCoverageReport(append(serverNodeNames, agentNodeNames...))).To(Succeed())
|
2022-06-14 15:40:29 +00:00
|
|
|
Expect(e2e.DestroyCluster()).To(Succeed())
|
|
|
|
Expect(os.Remove(kubeConfigFile)).To(Succeed())
|
|
|
|
}
|
|
|
|
})
|