From d7b627ba3a7302339c52f517cae02393e3300803 Mon Sep 17 00:00:00 2001 From: Arvinderpal Wander Date: Sun, 9 Feb 2020 15:25:27 -0800 Subject: [PATCH] Added a delete test. Refactored move test. --- cmd/clusterctl/test/e2e/delete_test.go | 108 +++++++++++++++++++++++++ cmd/clusterctl/test/e2e/move_test.go | 35 ++++---- 2 files changed, 123 insertions(+), 20 deletions(-) create mode 100644 cmd/clusterctl/test/e2e/delete_test.go diff --git a/cmd/clusterctl/test/e2e/delete_test.go b/cmd/clusterctl/test/e2e/delete_test.go new file mode 100644 index 000000000000..67b89cccd2d0 --- /dev/null +++ b/cmd/clusterctl/test/e2e/delete_test.go @@ -0,0 +1,108 @@ +// +build e2e + +/* +Copyright 2020 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package e2e + +import ( + "fmt" + "time" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + + appsv1 "k8s.io/api/apps/v1" + apierrors "k8s.io/apimachinery/pkg/api/errors" + "sigs.k8s.io/cluster-api/test/framework" + "sigs.k8s.io/cluster-api/test/framework/management/kind" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/kind/pkg/cluster" + + clusterctlclient "sigs.k8s.io/cluster-api/cmd/clusterctl/pkg/client" +) + +var _ = Describe("clusterctl delete", func() { + var ( + cfgFile string + mgmtCluster *kind.Cluster + mgmtClient client.Client + deleteOptions clusterctlclient.DeleteOptions + cctlClient clusterctlclient.Client + ) + + BeforeEach(func() { + var err error + // Create the mgmt cluster and client + mgmtCluster, err = CreateKindCluster(kindConfigFile) + Expect(err).ToNot(HaveOccurred()) + mgmtClient, err = mgmtCluster.GetClient() + Expect(err).NotTo(HaveOccurred()) + cctlClient, err = clusterctlclient.New(cfgFile) + Expect(err).ToNot(HaveOccurred()) + initOptmgmtCluster := clusterctlclient.InitOptions{ + Kubeconfig: mgmtCluster.KubeconfigPath, + CoreProvider: "cluster-api:v0.3.0", + BootstrapProviders: []string{"kubeadm-bootstrap:v0.3.0"}, + ControlPlaneProviders: []string{"kubeadm-control-plane:v0.3.0"}, + InfrastructureProviders: []string{"docker:v0.3.0"}, + } + _, err = cctlClient.Init(initOptmgmtCluster) + Expect(err).ToNot(HaveOccurred()) + framework.WaitForAPIServiceAvailable(ctx, mgmtCluster, "v1beta1.webhook.cert-manager.io") + CheckAndWaitCAPITestDeploymentsExist(mgmtClient) + // Create a capi Cluster and capd objects + err = mgmtCluster.Apply(ctx, []byte(testClusterYAML)) + Expect(err).ToNot(HaveOccurred()) + + }, setupTimeout) + + JustBeforeEach(func() { + err := cctlClient.Delete(deleteOptions) + Expect(err).ToNot(HaveOccurred()) + }) + + AfterEach(func() { + fmt.Fprintf(GinkgoWriter, "Tearing down kind clusters\n") + mgmtCluster.Teardown(ctx) + fmt.Fprintf(GinkgoWriter, "Tearing down kind workload cluster\n") + if err := cluster.NewProvider().Delete("foo-cluster", ""); err != nil { + // Treat this as a non critical error + fmt.Fprintf(GinkgoWriter, "Deleting the kind cluster \"foo-cluster\" failed. You may need to remove this by hand.\n") + } + }) + + Context("deletes the infra provider", func() { + BeforeEach(func() { + deleteOptions = clusterctlclient.DeleteOptions{ + Kubeconfig: mgmtCluster.KubeconfigPath, + Providers: []string{"docker"}, + } + }) + It("should delete of all infra provider components except the hosting namespace and the CRDs.", func() { + Eventually( + func() error { + err := mgmtClient.Get(ctx, client.ObjectKey{Namespace: "capd-system", Name: "capd-controller-manager"}, &appsv1.Deployment{}) + if err == nil || !apierrors.IsNotFound(err) { + // deployment still exists or some other error other than not found occured. + return fmt.Errorf("%v", err) + } + return nil + }, 3*time.Minute, 5*time.Second, + ).ShouldNot(HaveOccurred()) + }) + }) +}) diff --git a/cmd/clusterctl/test/e2e/move_test.go b/cmd/clusterctl/test/e2e/move_test.go index 763589d4f95f..8f652f4b8e12 100644 --- a/cmd/clusterctl/test/e2e/move_test.go +++ b/cmd/clusterctl/test/e2e/move_test.go @@ -145,30 +145,25 @@ var _ = Describe("clusterctl move", func() { // Should delete all Cluster API objects from the previous management cluster. Eventually( func() error { - if err := fromMgmtClient.Get(ctx, client.ObjectKey{Namespace: "default", Name: "foo-cluster"}, &clusterv1.Cluster{}); err != nil { - if !apierrors.IsNotFound(err) { - return err - } + err := fromMgmtClient.Get(ctx, client.ObjectKey{Namespace: "default", Name: "foo-cluster"}, &clusterv1.Cluster{}) + if err == nil || !apierrors.IsNotFound(err) { + return fmt.Errorf("%v", err) } - if err := fromMgmtClient.Get(ctx, client.ObjectKey{Namespace: "default", Name: "controlplane-0"}, &clusterv1.Machine{}); err != nil { - if !apierrors.IsNotFound(err) { - return err - } + err = fromMgmtClient.Get(ctx, client.ObjectKey{Namespace: "default", Name: "controlplane-0"}, &clusterv1.Machine{}) + if err == nil || !apierrors.IsNotFound(err) { + return fmt.Errorf("%v", err) } - if err := fromMgmtClient.Get(ctx, client.ObjectKey{Namespace: "default", Name: "controlplane-0-config"}, &bootstrapv1.KubeadmConfig{}); err != nil { - if !apierrors.IsNotFound(err) { - return err - } + err = fromMgmtClient.Get(ctx, client.ObjectKey{Namespace: "default", Name: "controlplane-0-config"}, &bootstrapv1.KubeadmConfig{}) + if err == nil || !apierrors.IsNotFound(err) { + return fmt.Errorf("%v", err) } - if err := fromMgmtClient.Get(ctx, client.ObjectKey{Namespace: "default", Name: "foo-cluster"}, &infrav1.DockerCluster{}); err != nil { - if !apierrors.IsNotFound(err) { - return err - } + err = fromMgmtClient.Get(ctx, client.ObjectKey{Namespace: "default", Name: "foo-cluster"}, &infrav1.DockerCluster{}) + if err == nil || !apierrors.IsNotFound(err) { + return fmt.Errorf("%v", err) } - if err := fromMgmtClient.Get(ctx, client.ObjectKey{Namespace: "default", Name: "controlplane-0"}, &infrav1.DockerMachine{}); err != nil { - if !apierrors.IsNotFound(err) { - return err - } + err = fromMgmtClient.Get(ctx, client.ObjectKey{Namespace: "default", Name: "controlplane-0"}, &infrav1.DockerMachine{}) + if err == nil || !apierrors.IsNotFound(err) { + return fmt.Errorf("%v", err) } return nil }, 3*time.Minute, 5*time.Second,