Skip to content

Commit

Permalink
Do ExpectToBeClean in BeforeEach()
Browse files Browse the repository at this point in the history
Do CleanTestEnvironment() in AfterEach()
  • Loading branch information
Zijun Wang committed Apr 26, 2023
1 parent f4d4e60 commit 344ff26
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 40 deletions.
2 changes: 0 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ export KUBEBUILDER_ASSETS ?= ${HOME}/.kubebuilder/bin
export CLUSTER_NAME ?= $(shell kubectl config view --minify -o jsonpath='{.clusters[].name}' | rev | cut -d"/" -f1 | rev | cut -d"." -f1)
export CLUSTER_VPC_ID ?= $(shell aws eks describe-cluster --name $(CLUSTER_NAME) | jq -r ".cluster.resourcesVpcConfig.vpcId")
export AWS_ACCOUNT_ID ?= $(shell aws sts get-caller-identity --query Account --output text)
export GATEWAY_API_CONTROLLER_LOGLEVEL ?= debug
export REGION ?= us-west-2

# Image URL to use all building/pushing image targets
IMG ?= controller:latest
Expand Down
14 changes: 14 additions & 0 deletions docs/developer.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ GATEWAY_API_CONTROLLER_LOGLEVEL=debug make run
LATTICE_ENDPOINT=https://vpc-lattice.us-west-2.amazonaws.com/ make run
```

To easier load environment variables, if you hope to run the controller by GoLand IDE locally, you could run the `scripts/load_env_variables.sh`
And use "EnvFile" GoLand plugin to read the env variables from the generated `.env` file.

### End-to-End Testing

Run the following command to run the end-to-end tests against the Kubernetes cluster pointed to by `kubectl config current-context`:
Expand All @@ -36,6 +39,17 @@ You should set up the correct `REGION` env variable
export REGION=us-west-2
make e2etest
```

Pass `FOCUS` environment variable to run some specific test cases based on filter condition:
```bash
export REGION=us-west-2
export FOCUS="HTTPRoute should support multiple path matches"
make e2etest
```
For more detail on filter condition for ginkgo
https://onsi.github.io/ginkgo/#focused-specs
https://onsi.github.io/ginkgo/#description-based-filtering

Notice: the prerequisites for running the end-to-end tests success are:
- Current eks cluster don't have any k8s resource
- The vpc used by current eks cluster don't have any vpc service network association
Expand Down
54 changes: 16 additions & 38 deletions test/pkg/test/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,8 @@ func NewFramework(ctx context.Context) *Framework {

SetDefaultEventuallyTimeout(180 * time.Second)
SetDefaultEventuallyPollingInterval(10 * time.Second)
BeforeSuite(func() {
framework.CleanServiceNetworkVpcAssociationForCurrentK8sClusterVpc()
})
AfterEach(func() { // Clean up this test case created k8s resources, in case this test case failed-in-the-middle
for _, object := range framework.TestCasesCreatedK8sResource {
Logger(ctx).Infof("In AfterEach Deleting %s %s/%s", reflect.TypeOf(object), object.GetNamespace(), object.GetName())
//Ignore resource-not-found here, as the test case logic itself could already clear the resources
framework.Delete(ctx, object)
}
framework.TestCasesCreatedK8sResource = nil
framework.CleanServiceNetworkVpcAssociationForCurrentK8sClusterVpc()

})
AfterSuite(func() { framework.CleanTestEnvironment(ctx) })
BeforeEach(func() { framework.ExpectToBeClean(ctx) })
AfterEach(func() { framework.CleanTestEnvironment(ctx) })
return framework
}

Expand All @@ -109,6 +97,11 @@ func (env *Framework) ExpectToBeClean(ctx context.Context) {
})

currentClusterVpcId := os.Getenv("CLUSTER_VPC_ID")
retrievedServiceNetworkVpcAssociations, _ := env.LatticeClient.ListServiceNetworkVpcAssociationsAsList(ctx, &vpclattice.ListServiceNetworkVpcAssociationsInput{
VpcIdentifier: aws.String(currentClusterVpcId),
})
Logger(ctx).Infof("Expect VPC used by current cluster don't have any ServiceNetworkVPCAssociation, if it has you should manually delete it")
Expect(len(retrievedServiceNetworkVpcAssociations)).To(Equal(0))
Eventually(func(g Gomega) {
retrievedServiceNetworks, _ := env.LatticeClient.ListServiceNetworksAsList(ctx, &vpclattice.ListServiceNetworksInput{})
for _, sn := range retrievedServiceNetworks {
Expand Down Expand Up @@ -185,35 +178,19 @@ func (env *Framework) CleanTestEnvironment(ctx context.Context) {
// Kubernetes API Objects
namespaces := &v1.NamespaceList{}
Expect(env.List(ctx, namespaces)).WithOffset(1).To(Succeed())
Eventually(func(g Gomega) {
for _, namespace := range namespaces.Items {
if namespace.Name == "kube-system" {
continue
}
parallel.ForEach(TestObjects, func(testObject TestObject, _ int) {
log.Println("ExpectDeleteAllToSucceed for testObject.Type", testObject.Type, "in namespace", namespace.Name)
env.ExpectDeleteAllToSucceed(ctx, testObject.Type, namespace.Name)
})
}
}).Should(Succeed())
for _, object := range env.TestCasesCreatedK8sResource {
Logger(ctx).Infof("Deleting k8s resource %s %s/%s", reflect.TypeOf(object), object.GetNamespace(), object.GetName())
env.Delete(ctx, object)
//Ignore resource-not-found error here, as the test case logic itself could already clear the resources
}

//Theoretically, Deleting all k8s resource by `env.ExpectDeleteAllToSucceed()`, will make controller delete all related VPC Lattice resource,
//but the controller is still developing in the progress and may leaking some vPCLattice resource, need to invoke vpcLattice api to double confirm and delete leaking resource.
env.DeleteAllFrameworkTracedServiceNetworks(ctx)
env.DeleteAllFrameworkTracedVpcLatticeServices(ctx)
env.DeleteAllFrameworkTracedTargetGroups(ctx)
Eventually(func(g Gomega) {
for _, namespace := range namespaces.Items {
if namespace.Name == "kube-system" {
continue
}
parallel.ForEach(TestObjects, func(testObject TestObject, _ int) {
log.Println("EventuallyExpectNoneFound for testObject.Type", testObject.Type, "in namespace", namespace.Name)
defer GinkgoRecover()
env.EventuallyExpectNoneFound(ctx, testObject.ListType)
})
}
}).Should(Succeed())
env.EventuallyExpectNotFound(ctx, env.TestCasesCreatedK8sResource...)
env.TestCasesCreatedK8sResource = nil

}

Expand Down Expand Up @@ -245,9 +222,10 @@ func (env *Framework) ExpectDeleteAllToSucceed(ctx context.Context, object clien
func (env *Framework) EventuallyExpectNotFound(ctx context.Context, objects ...client.Object) {
Eventually(func(g Gomega) {
for _, object := range objects {
Logger(ctx).Infof("Checking whether %s %s %s is not found", reflect.TypeOf(object), object.GetNamespace(), object.GetName())
g.Expect(errors.IsNotFound(env.Get(ctx, client.ObjectKeyFromObject(object), object))).To(BeTrue())
}
}).Should(Succeed())
}).WithOffset(1).Should(Succeed())
}

func (env *Framework) EventuallyExpectNoneFound(ctx context.Context, objectList client.ObjectList) {
Expand Down

0 comments on commit 344ff26

Please sign in to comment.