From 56018988220dc39af9bab7cfc7cfbfe3931834ce 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 Signed-off-by: Enhao Cui --- multicluster/test/e2e/fixtures.go | 1 - multicluster/test/e2e/framework.go | 1 + multicluster/test/e2e/main_test.go | 3 - multicluster/test/e2e/service_test.go | 98 ++++++++++++++++++--------- 4 files changed, 67 insertions(+), 36 deletions(-) diff --git a/multicluster/test/e2e/fixtures.go b/multicluster/test/e2e/fixtures.go index b3d1ef868af..b48e08a209e 100644 --- a/multicluster/test/e2e/fixtures.go +++ b/multicluster/test/e2e/fixtures.go @@ -54,7 +54,6 @@ func setupTest(tb testing.TB) (*MCTestData, error) { if err := testData.createTestNamespaces(); err != nil { return nil, err } - success = true return testData, nil } diff --git a/multicluster/test/e2e/framework.go b/multicluster/test/e2e/framework.go index 258f34542b2..c11d48f2da0 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 de51f8bf44f..42973480f7b 100644 --- a/multicluster/test/e2e/main_test.go +++ b/multicluster/test/e2e/main_test.go @@ -96,9 +96,6 @@ func TestConnectivity(t *testing.T) { } defer teardownTest(t, data) - t.Run("testServiceExport", func(t *testing.T) { - testServiceExport(t, data) - }) t.Run("testAntreaPolicy", func(t *testing.T) { initializeForPolicyTest(t, data) testMCAntreaPolicy(t, data) diff --git a/multicluster/test/e2e/service_test.go b/multicluster/test/e2e/service_test.go index d58c1e9ad8b..5e23f209560 100644 --- a/multicluster/test/e2e/service_test.go +++ b/multicluster/test/e2e/service_test.go @@ -27,75 +27,108 @@ import ( e2euttils "antrea.io/antrea/test/e2e/utils" ) -func testServiceExport(t *testing.T, data *MCTestData) { - data.testServiceExport(t) +func TestMCService(t *testing.T) { + data, err := setupTest(t) + defer teardownTest(t, data) + if err != nil { + t.Fatalf("Error when setting up test: %v", err) + } + + podName := data.setupTestResources(t) + defer data.tearDownTestResources(t, podName) + + t.Run("TestMCServiceExport", func(t *testing.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) + }) } -// testServiceExport is used to test the service between clusters by following steps -// we create a nginx in on cluster(east), and try to curl it in another cluster(west). -// If we got status code 200, it means that the resources is exported by the east cluster +// 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. -// TODO(yang): reorg test function contents -func (data *MCTestData) testServiceExport(t *testing.T) { - podName := randName("test-nginx-") - clientPodName := "test-service-client" +func testProbeMCService(t *testing.T, data *MCTestData) { + data.testProbeMCService(t) +} +func testANP(t *testing.T, data *MCTestData) { + data.testANP(t) +} + +func (data *MCTestData) setupTestResources(t *testing.T) string { + + podName := randName(nginxPodName) if err := createPodWrapper(t, data, westCluster, multiClusterTestNamespace, podName, nginxImage, "nginx", nil, nil, nil, nil, false, nil); err != nil { t.Fatalf("Error when creating nginx Pod in west cluster: %v", err) } - defer deletePodWrapper(t, data, westCluster, multiClusterTestNamespace, podName) if err := createPodWrapper(t, data, eastCluster, multiClusterTestNamespace, podName, nginxImage, "nginx", nil, nil, nil, nil, false, nil); err != nil { t.Fatalf("Error when creating nginx Pod in east cluster: %v", err) } - defer deletePodWrapper(t, data, eastCluster, multiClusterTestNamespace, podName) if _, err := data.createService(westCluster, westClusterTestService, multiClusterTestNamespace, 80, 80, corev1.ProtocolTCP, map[string]string{"app": "nginx"}, false, false, corev1.ServiceTypeClusterIP, nil, nil); err != nil { t.Fatalf("Error when creating Servie %s in west cluster: %v", westClusterTestService, err) } - defer deleteServiceWrapper(t, testData, westCluster, multiClusterTestNamespace, westClusterTestService) if _, err := data.createService(eastCluster, eastClusterTestService, multiClusterTestNamespace, 80, 80, corev1.ProtocolTCP, map[string]string{"app": "nginx"}, false, false, corev1.ServiceTypeClusterIP, nil, nil); err != nil { t.Fatalf("Error when creating Servie %s in east cluster: %v", eastClusterTestService, err) } - defer deleteServiceWrapper(t, testData, eastCluster, multiClusterTestNamespace, eastClusterTestService) + return podName +} + +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) +} + +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 +} +// 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) } - defer data.deleteServiceExport(westCluster) if err := data.deployServiceExport(eastCluster); err != nil { t.Fatalf("Error when deploy ServiceExport in east cluster: %v", err) } - defer data.deleteServiceExport(eastCluster) time.Sleep(importServiceDelay) +} - 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) - } +func tearDownServiceExport(data *MCTestData) { + data.deleteServiceExport(westCluster) + data.deleteServiceExport(eastCluster) +} - eastIP := svc.Spec.ClusterIP - if err := data.probeFromCluster(eastCluster, eastIP); err != nil { - t.Fatalf("Error when probing service from %s", eastCluster) - } - svc, err = data.getService(westCluster, multiClusterTestNamespace, fmt.Sprintf("antrea-mc-%s", eastClusterTestService)) - if err != nil { - t.Fatalf("Error when getting the imported service %s: %v", fmt.Sprintf("antrea-mc-%s", eastClusterTestService), err) - } - westIP := svc.Spec.ClusterIP - if err := data.probeFromCluster(westCluster, westIP); err != nil { - t.Fatalf("Error when probing service from %s", westCluster) - } +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" if err := data.createPod(eastCluster, clientPodName, multiClusterTestNamespace, "client", agnhostImage, []string{"sleep", strconv.Itoa(3600)}, nil, nil, nil, false, nil); err != nil { t.Fatalf("Error when creating client Pod in east cluster: %v", err) } defer deletePodWrapper(t, data, eastCluster, multiClusterTestNamespace, clientPodName) - _, err = data.podWaitFor(defaultTimeout, eastCluster, clientPodName, multiClusterTestNamespace, func(pod *corev1.Pod) (bool, error) { + _, err := data.podWaitFor(defaultTimeout, eastCluster, clientPodName, multiClusterTestNamespace, func(pod *corev1.Pod) (bool, error) { return pod.Status.Phase == corev1.PodRunning, nil }) if err != nil { @@ -115,6 +148,7 @@ func (data *MCTestData) testServiceExport(t *testing.T) { } defer data.deleteANP(eastCluster, multiClusterTestNamespace, anpBuilder.Name) + westIP := data.probeMCServiceFromCluster(t, westCluster, eastClusterTestService) connectivity := data.probeFromPodInCluster(eastCluster, multiClusterTestNamespace, clientPodName, "client", westIP, "westClusterServiceIP", 80, corev1.ProtocolTCP) if connectivity == antreae2e.Error { t.Errorf("Failure -- could not complete probeFromPodInCluster: %v", err)