From 274add53449b8c11e914c431338e1203c18729a3 Mon Sep 17 00:00:00 2001 From: James Sturtevant Date: Thu, 29 Apr 2021 14:47:11 -0700 Subject: [PATCH] enable tests against existing workload cluster --- docs/development.md | 1 + test/e2e/azure_lb.go | 8 +++++--- test/e2e/azure_test.go | 14 ++++++++++--- test/e2e/common.go | 38 ++++++++++++++++++++++++++++++------ test/e2e/conformance_test.go | 11 +++++++++-- 5 files changed, 58 insertions(+), 14 deletions(-) diff --git a/docs/development.md b/docs/development.md index a1ebf902aab..795ca73a9ae 100644 --- a/docs/development.md +++ b/docs/development.md @@ -403,6 +403,7 @@ You can optionally set the following variables: | `SKIP_CREATE_MGMT_CLUSTER` | Skip management cluster creation. If skipping managment cluster creation you must specify `KUBECONFIG` and `SKIP_CLEANUP` | `false` | | `LOCAL_ONLY` | Use Kind local registry and run the subset of tests which don't require a remotely pushed controller image. | `true` | | `REGISTRY` | Registry to push the controller image. | `capzci.azurecr.io/ci-e2e` | +| `CLUSTER_NAME` | Name of an exisiting workload cluster. Will run specs against existing workload cluster. Use in conjunction with `SKIP_CREATE_MGMT_CLUSTER` and `KUBECONFIG` | `capz-e2e-uf9yll` | | `KUBECONFIG` | Used with `SKIP_CREATE_MGMT_CLUSTER` set to true. Location of kubeconfig for the management cluster you would like to use. Use `kind get kubeconfig --name capz-e2e > kubeconfig.capz-e2e` to get the capz e2e kind cluster config | '~/.kube/config' | You can also customize the configuration of the CAPZ cluster created by the E2E tests (except for `CLUSTER_NAME`, `AZURE_RESOURCE_GROUP`, `AZURE_VNET_NAME`, `CONTROL_PLANE_MACHINE_COUNT`, and `WORKER_MACHINE_COUNT`, since they are generated by individual test cases). See [Customizing the cluster deployment](#customizing-the-cluster-deployment) for more details. diff --git a/test/e2e/azure_lb.go b/test/e2e/azure_lb.go index 18b780dfb62..633c42913c7 100644 --- a/test/e2e/azure_lb.go +++ b/test/e2e/azure_lb.go @@ -23,6 +23,8 @@ import ( "fmt" "net" + "sigs.k8s.io/cluster-api/util" + . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" k8snet "k8s.io/utils/net" @@ -69,10 +71,10 @@ func AzureLBSpec(ctx context.Context, inputGetter func() AzureLBSpecInput) { Expect(clientset).NotTo(BeNil()) By("creating an HTTP deployment") - deploymentName := "web" + deploymentName := "web" + util.RandomString(6) // if case of input.SkipCleanup we need a unique name for windows if input.Windows { - deploymentName = "web-windows" + deploymentName = "web-windows" + util.RandomString(6) } webDeployment := deploymentBuilder.CreateDeployment("httpd", deploymentName, corev1.NamespaceDefault) @@ -173,7 +175,7 @@ func AzureLBSpec(ctx context.Context, inputGetter func() AzureLBSpecInput) { elbIP := extractServiceIp(svc) Log("starting to create curl-to-elb job") - elbJob := job.CreateCurlJob("curl-to-elb-job", elbIP) + elbJob := job.CreateCurlJob("curl-to-elb-job"+util.RandomString(6), elbIP) _, err = jobsClient.Create(ctx, elbJob, metav1.CreateOptions{}) Expect(err).NotTo(HaveOccurred()) elbJobInput := WaitForJobCompleteInput{ diff --git a/test/e2e/azure_test.go b/test/e2e/azure_test.go index 57b21c7e305..a82e0592bd0 100644 --- a/test/e2e/azure_test.go +++ b/test/e2e/azure_test.go @@ -25,6 +25,8 @@ import ( "path/filepath" "time" + "sigs.k8s.io/cluster-api/util" + . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" corev1 "k8s.io/api/core/v1" @@ -32,7 +34,6 @@ import ( "k8s.io/utils/pointer" capi_e2e "sigs.k8s.io/cluster-api/test/e2e" "sigs.k8s.io/cluster-api/test/framework/clusterctl" - "sigs.k8s.io/cluster-api/util" ) var _ = Describe("Workload cluster creation", func() { @@ -58,10 +59,17 @@ var _ = Describe("Workload cluster creation", func() { Expect(e2eConfig.Variables).To(HaveKey(capi_e2e.KubernetesVersion)) Expect(e2eConfig.Variables).To(HaveKey(capi_e2e.CNIPath)) + clusterName = os.Getenv("CLUSTER_NAME") + if clusterName == "" { + clusterName = fmt.Sprintf("capz-e2e-%s", util.RandomString(6)) + } + fmt.Fprintf(GinkgoWriter, "INFO: using cluster %s", clusterName) + // Setup a Namespace where to host objects for this spec and create a watcher for the namespace events. - namespace, cancelWatches = setupSpecNamespace(ctx, specName, bootstrapClusterProxy, artifactFolder) + var err error + namespace, cancelWatches, err = setupSpecNamespace(ctx, clusterName, bootstrapClusterProxy, artifactFolder) + Expect(err).NotTo(HaveOccurred()) - clusterName = fmt.Sprintf("capz-e2e-%s", util.RandomString(6)) Expect(os.Setenv(AzureResourceGroup, clusterName)).NotTo(HaveOccurred()) Expect(os.Setenv(AzureVNetName, fmt.Sprintf("%s-vnet", clusterName))).NotTo(HaveOccurred()) result = new(clusterctl.ApplyClusterTemplateAndWaitResult) diff --git a/test/e2e/common.go b/test/e2e/common.go index 1b430ce0157..bec993ca9e6 100644 --- a/test/e2e/common.go +++ b/test/e2e/common.go @@ -26,6 +26,10 @@ import ( "path" "path/filepath" + apierrors "k8s.io/apimachinery/pkg/api/errors" + + e2e_namespace "sigs.k8s.io/cluster-api-provider-azure/test/e2e/kubernetes/namespace" + . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" corev1 "k8s.io/api/core/v1" @@ -33,7 +37,6 @@ import ( "k8s.io/client-go/tools/clientcmd" clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4" "sigs.k8s.io/cluster-api/test/framework" - "sigs.k8s.io/cluster-api/util" "sigs.k8s.io/cluster-api/util/kubeconfig" crclient "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -61,16 +64,39 @@ func Byf(format string, a ...interface{}) { By(fmt.Sprintf(format, a...)) } -func setupSpecNamespace(ctx context.Context, specName string, clusterProxy framework.ClusterProxy, artifactFolder string) (*corev1.Namespace, context.CancelFunc) { - Byf("Creating a namespace for hosting the %q test spec", specName) +func setupSpecNamespace(ctx context.Context, namespaceName string, clusterProxy framework.ClusterProxy, artifactFolder string) (*corev1.Namespace, context.CancelFunc, error) { + Byf("Creating namespace %q for hosting the cluster", namespaceName) + logPath := filepath.Join(artifactFolder, "clusters", clusterProxy.GetName()) + namespace, err := e2e_namespace.Get(ctx, clusterProxy.GetClientSet(), namespaceName) + if err != nil && !apierrors.IsNotFound(err) { + return nil, nil, err + } + + // namespace exists wire it up + if err == nil { + Byf("Creating event watcher for existing namespace %q", namespace.Name) + watchesCtx, cancelWatches := context.WithCancel(ctx) + go func() { + defer GinkgoRecover() + framework.WatchNamespaceEvents(watchesCtx, framework.WatchNamespaceEventsInput{ + ClientSet: clusterProxy.GetClientSet(), + Name: namespace.Name, + LogFolder: logPath, + }) + }() + + return namespace, cancelWatches, nil + } + + // create and wire up namespace namespace, cancelWatches := framework.CreateNamespaceAndWatchEvents(ctx, framework.CreateNamespaceAndWatchEventsInput{ Creator: clusterProxy.GetClient(), ClientSet: clusterProxy.GetClientSet(), - Name: fmt.Sprintf("%s-%s", specName, util.RandomString(6)), - LogFolder: filepath.Join(artifactFolder, "clusters", clusterProxy.GetName()), + Name: namespaceName, + LogFolder: logPath, }) - return namespace, cancelWatches + return namespace, cancelWatches, nil } func dumpSpecResourcesAndCleanup(ctx context.Context, specName string, clusterProxy framework.ClusterProxy, artifactFolder string, namespace *corev1.Namespace, cancelWatches context.CancelFunc, cluster *clusterv1.Cluster, intervalsGetter func(spec, key string) []interface{}, skipCleanup bool) { diff --git a/test/e2e/conformance_test.go b/test/e2e/conformance_test.go index 4846220e518..3d450024a57 100644 --- a/test/e2e/conformance_test.go +++ b/test/e2e/conformance_test.go @@ -56,10 +56,17 @@ var _ = Describe("Conformance Tests", func() { Expect(e2eConfig.Variables).To(HaveKey(capi_e2e.KubernetesVersion)) Expect(e2eConfig.Variables).To(HaveKey(capi_e2e.CNIPath)) + clusterName = os.Getenv("CLUSTER_NAME") + if clusterName == "" { + clusterName = fmt.Sprintf("capz-conf-%s", util.RandomString(6)) + } + fmt.Fprintf(GinkgoWriter, "INFO: using cluster %s", clusterName) + // Setup a Namespace where to host objects for this spec and create a watcher for the namespace events. - namespace, cancelWatches = setupSpecNamespace(ctx, specName, bootstrapClusterProxy, artifactFolder) + var err error + namespace, cancelWatches, err = setupSpecNamespace(ctx, clusterName, bootstrapClusterProxy, artifactFolder) + Expect(err).NotTo(HaveOccurred()) - clusterName = fmt.Sprintf("capz-conf-%s", util.RandomString(6)) Expect(os.Setenv(AzureResourceGroup, clusterName)).NotTo(HaveOccurred()) Expect(os.Setenv(AzureVNetName, fmt.Sprintf("%s-vnet", clusterName))).NotTo(HaveOccurred()) })