Skip to content

Commit

Permalink
Multicluster E2E Test Refactoring
Browse files Browse the repository at this point in the history
Grouped multicluster service export test cases together, and
trigger the test cases from multicluster/test/e2e/main_test.go

Signed-off-by: Enhao Cui <[email protected]>
Signed-off-by: wgrayson <[email protected]>
  • Loading branch information
Enhao Cui authored and GraysonWu committed Jun 16, 2022
1 parent 5b9cde5 commit f4c61d0
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 33 deletions.
1 change: 1 addition & 0 deletions multicluster/test/e2e/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions multicluster/test/e2e/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
95 changes: 65 additions & 30 deletions multicluster/test/e2e/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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
}

0 comments on commit f4c61d0

Please sign in to comment.