From f4c61d00473ca82d30b407a29088f7f2a2b852a5 Mon Sep 17 00:00:00 2001 From: Enhao Cui Date: Mon, 7 Feb 2022 22:54:24 -0800 Subject: [PATCH] Multicluster E2E Test Refactoring Grouped multicluster service export test cases together, and trigger the test cases from multicluster/test/e2e/main_test.go Signed-off-by: Enhao Cui Signed-off-by: wgrayson --- multicluster/test/e2e/framework.go | 1 + multicluster/test/e2e/main_test.go | 11 +++- multicluster/test/e2e/service_test.go | 95 ++++++++++++++++++--------- 3 files changed, 74 insertions(+), 33 deletions(-) diff --git a/multicluster/test/e2e/framework.go b/multicluster/test/e2e/framework.go index bdad7435912..5887db0c054 100644 --- a/multicluster/test/e2e/framework.go +++ b/multicluster/test/e2e/framework.go @@ -49,6 +49,7 @@ const ( nginxImage = "projects.registry.vmware.com/antrea/nginx:1.21.6-alpine" agnhostImage = "agnhost:2.26" + nginxPodName = "test-nginx-pod" ) var provider providers.ProviderInterface diff --git a/multicluster/test/e2e/main_test.go b/multicluster/test/e2e/main_test.go index 735bcff429c..1423a565b17 100644 --- a/multicluster/test/e2e/main_test.go +++ b/multicluster/test/e2e/main_test.go @@ -106,11 +106,16 @@ func TestConnectivity(t *testing.T) { time.Sleep(5 * time.Second) } - t.Run("testServiceExport", func(t *testing.T) { - testServiceExport(t, data) + t.Run("TestMCServiceExport", func(t *testing.T) { + podName := data.setupTestResources(t) + setUpServiceExport(data, t) + t.Run("Case=Connectivity", func(t *testing.T) { testProbeMCService(t, data) }) + t.Run("Case=ANPNoPriority", func(t *testing.T) { testANP(t, data) }) + tearDownServiceExport(data) + data.tearDownTestResources(t, podName) }) - t.Run("testAntreaPolicy", func(t *testing.T) { + t.Run("TestAntreaPolicy", func(t *testing.T) { defer tearDownForPolicyTest() initializeForPolicyTest(t, data) testMCAntreaPolicy(t, data) diff --git a/multicluster/test/e2e/service_test.go b/multicluster/test/e2e/service_test.go index 578075ef894..7f53216d1e2 100644 --- a/multicluster/test/e2e/service_test.go +++ b/multicluster/test/e2e/service_test.go @@ -29,18 +29,8 @@ import ( e2euttils "antrea.io/antrea/test/e2e/utils" ) -func testServiceExport(t *testing.T, data *MCTestData) { - data.testServiceExport(t) -} - -// testServiceExport is used to test the connectivity of Multicluster Service between -// member clusters. We create a nginx Pod and Service in one member cluster, and try to -// curl it from a Pod in another cluster. If we get status code 200, it means that the -// resources are exported and imported successfully in each member cluster. -func (data *MCTestData) testServiceExport(t *testing.T) { - podName := randName("test-nginx-") - clientPodName := "test-service-client" - +func (data *MCTestData) setupTestResources(t *testing.T) string { + podName := randName(nginxPodName) createPodAndService := func(clusterName, clusterServiceName string) { if err := createPodWrapper(t, data, clusterName, multiClusterTestNamespace, podName, "", nginxImage, "nginx", nil, nil, nil, nil, false, nil); err != nil { t.Fatalf("Error when creating nginx Pod in cluster %s: %v", clusterName, err) @@ -50,31 +40,53 @@ func (data *MCTestData) testServiceExport(t *testing.T) { t.Fatalf("Error when creating Service %s in cluster %s: %v", clusterServiceName, clusterName, err) } } - // Create Pod and Service in west cluster + // Create Pod and Service in west and ease cluster createPodAndService(westCluster, westClusterTestService) - defer deletePodWrapper(t, data, westCluster, multiClusterTestNamespace, podName) - defer deleteServiceWrapper(t, testData, westCluster, multiClusterTestNamespace, westClusterTestService) - - // Create Pod and Service in east cluster createPodAndService(eastCluster, eastClusterTestService) - defer deletePodWrapper(t, data, eastCluster, multiClusterTestNamespace, podName) - defer deleteServiceWrapper(t, testData, eastCluster, multiClusterTestNamespace, eastClusterTestService) + return podName +} - deployServiceExport := func(clusterName string) { - if err := data.deployServiceExport(clusterName); err != nil { - t.Fatalf("Error when deploy ServiceExport in cluster %s: %v", clusterName, err) - } +func (data *MCTestData) tearDownTestResources(t *testing.T, podName string) { + deleteServiceWrapper(t, testData, westCluster, multiClusterTestNamespace, westClusterTestService) + deleteServiceWrapper(t, testData, eastCluster, multiClusterTestNamespace, eastClusterTestService) + deletePodWrapper(t, data, westCluster, multiClusterTestNamespace, podName) + deletePodWrapper(t, data, eastCluster, multiClusterTestNamespace, podName) +} + +// Deploy service exports in east and west clusters +func setUpServiceExport(data *MCTestData, t *testing.T) { + if err := data.deployServiceExport(westCluster); err != nil { + t.Fatalf("Error when deploy ServiceExport in west cluster: %v", err) + } + if err := data.deployServiceExport(eastCluster); err != nil { + t.Fatalf("Error when deploy ServiceExport in east cluster: %v", err) } + time.Sleep(importServiceDelay) +} - // Deploy ServiceExport in west cluster - deployServiceExport(westCluster) - defer data.deleteServiceExport(westCluster) +func tearDownServiceExport(data *MCTestData) { + data.deleteServiceExport(westCluster) + data.deleteServiceExport(eastCluster) +} - // Deploy ServiceExport in east cluster - deployServiceExport(eastCluster) - defer data.deleteServiceExport(eastCluster) - time.Sleep(importServiceDelay) +// Try to curl the counter part services in east and west clusters. +// If we get status code 200, it means that the resources is exported by the east cluster +// and imported by the west cluster. +func testProbeMCService(t *testing.T, data *MCTestData) { + data.testProbeMCService(t) +} +func testANP(t *testing.T, data *MCTestData) { + data.testANP(t) +} + +func (data *MCTestData) testProbeMCService(t *testing.T) { + data.probeMCServiceFromCluster(t, eastCluster, westClusterTestService) + data.probeMCServiceFromCluster(t, westCluster, eastClusterTestService) +} + +func (data *MCTestData) testANP(t *testing.T) { + clientPodName := "test-service-client" svc, err := data.getService(eastCluster, multiClusterTestNamespace, fmt.Sprintf("antrea-mc-%s", westClusterTestService)) if err != nil { t.Fatalf("Error when getting the imported service %s: %v", fmt.Sprintf("antrea-mc-%s", westClusterTestService), err) @@ -143,6 +155,19 @@ func (data *MCTestData) testServiceExport(t *testing.T) { data.verifyMCServiceACNP(t, gwClientName, eastIP) } +func (data *MCTestData) probeMCServiceFromCluster(t *testing.T, clusterName string, serviceName string) string { + svc, err := data.getService(clusterName, multiClusterTestNamespace, fmt.Sprintf("antrea-mc-%s", serviceName)) + if err != nil { + t.Fatalf("Error when getting the imported service %s: %v", fmt.Sprintf("antrea-mc-%s", serviceName), err) + } + + ip := svc.Spec.ClusterIP + if err := data.probeFromCluster(clusterName, ip); err != nil { + t.Fatalf("Error when probing service from %s", clusterName) + } + return ip +} + func (data *MCTestData) verifyMCServiceACNP(t *testing.T, clientPodName, eastIP string) { var err error anpBuilder := &e2euttils.AntreaNetworkPolicySpecBuilder{} @@ -252,3 +277,13 @@ func teardownGateway(t *testing.T, data *MCTestData) { } } } + +func (data *MCTestData) probeFromCluster(clusterName string, url string) error { + var rc int + var err error + rc, _, _, err = provider.RunCommandOnNode(clusterName, fmt.Sprintf("curl --connect-timeout 5 -s %s", url)) + if err != nil || rc != 0 { + return fmt.Errorf("error when curl the url %s: %v", url, err) + } + return nil +}