From 210a8f3f33049cd2f5bce6a28b1cd022ff994e86 Mon Sep 17 00:00:00 2001 From: Zhecheng Li Date: Fri, 28 Aug 2020 14:18:23 +0800 Subject: [PATCH] [IPv6] Support IPv6 in e2e (#1129) --- test/e2e/basic_test.go | 8 ++++++- test/e2e/connectivity_test.go | 10 ++++++-- test/e2e/framework.go | 45 +++++++++++++++++++++++++++++------ test/e2e/main_test.go | 7 +++++- 4 files changed, 59 insertions(+), 11 deletions(-) diff --git a/test/e2e/basic_test.go b/test/e2e/basic_test.go index 997a83ca581..fed5dc69306 100644 --- a/test/e2e/basic_test.go +++ b/test/e2e/basic_test.go @@ -63,7 +63,13 @@ func TestPodAssignIP(t *testing.T) { t.Errorf("Error when waiting for Pod IP: %v", err) } else { t.Logf("Pod IP is '%s'", podIP) - isValid, err := validatePodIP(clusterInfo.podNetworkCIDR, podIP) + var isValid bool + var err error + if net.ParseIP(podIP).To4() != nil { + isValid, err = validatePodIP(clusterInfo.podV4NetworkCIDR, podIP) + } else { + isValid, err = validatePodIP(clusterInfo.podV6NetworkCIDR, podIP) + } if err != nil { t.Errorf("Error when trying to validate Pod IP: %v", err) } else if !isValid { diff --git a/test/e2e/connectivity_test.go b/test/e2e/connectivity_test.go index db6fdea3405..4361aef10c9 100644 --- a/test/e2e/connectivity_test.go +++ b/test/e2e/connectivity_test.go @@ -16,6 +16,7 @@ package e2e import ( "fmt" + "net" "strings" "testing" "time" @@ -41,7 +42,7 @@ func waitForPodIPs(t *testing.T, data *TestData, podNames []string) map[string]s return podIPs } -// runPingMesh runs a ping mesh between all the provided Pods after first retrieveing their IP +// runPingMesh runs a ping mesh between all the provided Pods after first retrieving their IP // addresses. func (data *TestData) runPingMesh(t *testing.T, podNames []string) { podIPs := waitForPodIPs(t, data, podNames) @@ -396,7 +397,12 @@ func TestPingLargeMTU(t *testing.T) { podIPs := waitForPodIPs(t, data, podNames) pingSize := 2000 - cmd := fmt.Sprintf("ping -c %d -s %d %s", pingCount, pingSize, podIPs[podName1]) + var cmd string + if net.ParseIP(podIPs[podName1]).To4() != nil { + cmd = fmt.Sprintf("ping -c %d -s %d %s", pingCount, pingSize, podIPs[podName1]) + } else { + cmd = fmt.Sprintf("ping -6 -c %d -s %d %s", pingCount, pingSize, podIPs[podName1]) + } t.Logf("Running ping with size %d between Pods %s and %s", pingSize, podName0, podName1) stdout, stderr, err := data.runCommandFromPod(testNamespace, podName0, busyboxContainerName, strings.Fields(cmd)) if err != nil { diff --git a/test/e2e/framework.go b/test/e2e/framework.go index fc9a28e2e3d..c6ba488b6b2 100644 --- a/test/e2e/framework.go +++ b/test/e2e/framework.go @@ -88,11 +88,12 @@ type ClusterNode struct { } type ClusterInfo struct { - numWorkerNodes int - numNodes int - podNetworkCIDR string - masterNodeName string - nodes map[int]ClusterNode + numWorkerNodes int + numNodes int + podV4NetworkCIDR string + podV6NetworkCIDR string + masterNodeName string + nodes map[int]ClusterNode } var clusterInfo ClusterInfo @@ -219,7 +220,32 @@ func collectClusterInfo() error { if matches := re.FindStringSubmatch(stdout); len(matches) == 0 { return fmt.Errorf("cannot retrieve cluster CIDR, unexpected kubectl output: %s", stdout) } else { - clusterInfo.podNetworkCIDR = matches[1] + cidrs := strings.Split(matches[1], ",") + if len(cidrs) == 1 { + _, cidr, err := net.ParseCIDR(cidrs[0]) + if err != nil { + return fmt.Errorf("CIDR cannot be parsed: %s", cidrs[0]) + } + if cidr.IP.To4() != nil { + clusterInfo.podV4NetworkCIDR = cidrs[0] + } else { + clusterInfo.podV6NetworkCIDR = cidrs[0] + } + } else if len(cidrs) == 2 { + _, cidr, err := net.ParseCIDR(cidrs[0]) + if err != nil { + return fmt.Errorf("CIDR cannot be parsed: %s", cidrs[0]) + } + if cidr.IP.To4() != nil { + clusterInfo.podV4NetworkCIDR = cidrs[0] + clusterInfo.podV6NetworkCIDR = cidrs[1] + } else { + clusterInfo.podV4NetworkCIDR = cidrs[1] + clusterInfo.podV6NetworkCIDR = cidrs[0] + } + } else { + return fmt.Errorf("unexpected cluster CIDR: %s", matches[1]) + } } return nil }(); err != nil { @@ -1097,7 +1123,12 @@ func parseArpingStdout(out string) (sent uint32, received uint32, loss float32, } func (data *TestData) runPingCommandFromTestPod(podName string, targetIP string, count int) error { - cmd := []string{"ping", "-c", strconv.Itoa(count), targetIP} + var cmd []string + if net.ParseIP(targetIP).To4() != nil { + cmd = []string{"ping", "-c", strconv.Itoa(count), targetIP} + } else { + cmd = []string{"ping", "-6", "-c", strconv.Itoa(count), targetIP} + } _, _, err := data.runCommandFromPod(testNamespace, podName, busyboxContainerName, cmd) return err } diff --git a/test/e2e/main_test.go b/test/e2e/main_test.go index b3626a84599..aeab18d6251 100644 --- a/test/e2e/main_test.go +++ b/test/e2e/main_test.go @@ -101,7 +101,12 @@ func testMain(m *testing.M) int { if err := collectClusterInfo(); err != nil { log.Fatalf("Error when collecting information about K8s cluster: %v", err) } else { - log.Printf("Pod network: '%s'", clusterInfo.podNetworkCIDR) + if clusterInfo.podV4NetworkCIDR != "" { + log.Printf("Pod IPv4 network: '%s'", clusterInfo.podV4NetworkCIDR) + } + if clusterInfo.podV6NetworkCIDR != "" { + log.Printf("Pod IPv6 network: '%s'", clusterInfo.podV6NetworkCIDR) + } log.Printf("Num nodes: %d", clusterInfo.numNodes) }