From 39f6219c29edfa10af92b65268906104e51c98c4 Mon Sep 17 00:00:00 2001 From: Enhao Cui Date: Mon, 7 Feb 2022 22:54:24 -0800 Subject: [PATCH] [WIP] Multicluster E2E Test Refactor Signed-off-by: Enhao Cui --- multicluster/test/e2e/fixtures.go | 1 - multicluster/test/e2e/framework.go | 1 + multicluster/test/e2e/service_test.go | 58 ++++++++++++++------------- 3 files changed, 32 insertions(+), 28 deletions(-) diff --git a/multicluster/test/e2e/fixtures.go b/multicluster/test/e2e/fixtures.go index 25942e6d220..047944f48b0 100644 --- a/multicluster/test/e2e/fixtures.go +++ b/multicluster/test/e2e/fixtures.go @@ -54,7 +54,6 @@ func setupTest(tb testing.TB) (*TestData, error) { if err := testData.createTestNamespace(); 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 1e7065a3ba1..0157cf0f7d0 100644 --- a/multicluster/test/e2e/framework.go +++ b/multicluster/test/e2e/framework.go @@ -62,6 +62,7 @@ const ( nginxImage = "nginx:latest" agnhostImage = "agnhost:2.26" + nginxPodName = "test-nginx-pod" ) var provider providers.ProviderInterface diff --git a/multicluster/test/e2e/service_test.go b/multicluster/test/e2e/service_test.go index 6deca699da4..af81a63be88 100644 --- a/multicluster/test/e2e/service_test.go +++ b/multicluster/test/e2e/service_test.go @@ -47,32 +47,51 @@ func testServiceExport(t *testing.T, data *TestData) { // 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 // and imported by the west cluster. -// TODO(yang): reorg test function contents -func (data *TestData) testServiceExport(t *testing.T) { - podName := randName("test-nginx-") - clientPodName := "test-service-client" - if err := createPodWrapper(t, data, westCluster, multiClusterTestNamespace, podName, nginxImage, "nginx", nil, nil, nil, nil, false, nil); err != nil { +func (data *TestData) setupTestResources(t *testing.T) { + + if err := createPodWrapper(t, data, westCluster, multiClusterTestNamespace, nginxPodName, 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 { + if err := createPodWrapper(t, data, eastCluster, multiClusterTestNamespace, nginxPodName, 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) +} + +func (data *TestData) tearDownTestResources(t *testing.T) { + deleteServiceWrapper(t, testData, westCluster, multiClusterTestNamespace, westClusterTestService) + deleteServiceWrapper(t, testData, eastCluster, multiClusterTestNamespace, eastClusterTestService) + deletePodWrapper(t, data, westCluster, multiClusterTestNamespace, nginxPodName) + deletePodWrapper(t, data, eastCluster, multiClusterTestNamespace, nginxPodName) +} + +func (data *TestData) getAndProbeFromCluster(t *testing.T, clusterName string){ + svc, err := data.getService(clusterName, multiClusterTestNamespace, fmt.Sprintf("antrea-mc-%s", clusterName)) + if err != nil { + t.Fatalf("Error when getting the imported service %s: %v", fmt.Sprintf("antrea-mc-%s", clusterName), err) + } + + ip := svc.Spec.ClusterIP + if err := data.probeFromCluster(clusterName, ip); err != nil { + t.Fatalf("Error when probe service from %s", clusterName) + } +} + +func (data *TestData) testServiceExport(t *testing.T) { + clientPodName := "test-service-client" + data.setupTestResources(t) + defer data.tearDownTestResources(t) if err := data.deployServiceExport(westCluster); err != nil { t.Fatalf("Error when deploy ServiceExport in west cluster: %v", err) @@ -84,23 +103,8 @@ func (data *TestData) testServiceExport(t *testing.T) { 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) - } - - eastIP := svc.Spec.ClusterIP - if err := data.probeFromCluster(eastCluster, eastIP); err != nil { - t.Fatalf("Error when probe 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 probe service from %s", westCluster) - } + data.getAndProbeFromCluster(t, eastCluster) + data.getAndProbeFromCluster(t, westCluster) if err := data.createPod(eastCluster, clientPodName, multiClusterTestNamespace, "client", agnhostImage, []string{"sleep", strconv.Itoa(3600)}, nil, nil, nil, false, nil); err != nil {