diff --git a/controllers/serviceaccount_controller_intg_test.go b/controllers/serviceaccount_controller_intg_test.go index 8b72afe3db..cb9fb3b377 100644 --- a/controllers/serviceaccount_controller_intg_test.go +++ b/controllers/serviceaccount_controller_intg_test.go @@ -17,7 +17,6 @@ limitations under the License. package controllers import ( - "fmt" "os" "reflect" @@ -30,7 +29,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" "k8s.io/utils/ptr" - clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" "sigs.k8s.io/cluster-api/util/conditions" "sigs.k8s.io/cluster-api/util/patch" "sigs.k8s.io/controller-runtime/pkg/client" @@ -61,6 +59,12 @@ var _ = Describe("ProviderServiceAccount controller integration tests", func() { targetNSObj *corev1.Namespace ) BeforeEach(func() { + By("Creating the Cluster, vSphereCluster and KubeconfigSecret", func() { + helpers.CreateAndWait(ctx, intCtx.Client, intCtx.Cluster) + helpers.CreateAndWait(ctx, intCtx.Client, intCtx.VSphereCluster) + helpers.CreateAndWait(ctx, intCtx.Client, intCtx.KubeconfigSecret) + }) + pSvcAccount = getTestProviderServiceAccount(intCtx.Namespace, intCtx.VSphereCluster) createTestResource(ctx, intCtx.Client, pSvcAccount) assertEventuallyExistsInNamespace(ctx, intCtx.Client, intCtx.Namespace, pSvcAccount.GetName(), pSvcAccount) @@ -129,13 +133,9 @@ var _ = Describe("ProviderServiceAccount controller integration tests", func() { Context("With non-existent Cluster object", func() { It("cannot reconcile the ProviderServiceAccount object", func() { - By("Deleting the CAPI cluster object", func() { - clusterName, ok := intCtx.VSphereCluster.GetLabels()[clusterv1.ClusterNameLabel] - Expect(ok).To(BeTrue()) - cluster := &clusterv1.Cluster{} - key := client.ObjectKey{Namespace: intCtx.Namespace, Name: clusterName} - Expect(intCtx.Client.Get(ctx, key, cluster)).To(Succeed()) - Expect(intCtx.Client.Delete(ctx, cluster)).To(Succeed()) + By("Creating the vSphereCluster and KubeconfigSecret only", func() { + helpers.CreateAndWait(ctx, intCtx.Client, intCtx.VSphereCluster) + helpers.CreateAndWait(ctx, intCtx.Client, intCtx.KubeconfigSecret) }) By("Creating the ProviderServiceAccount", func() { @@ -155,16 +155,9 @@ var _ = Describe("ProviderServiceAccount controller integration tests", func() { Context("With non-existent Cluster credentials secret", func() { It("cannot reconcile the ProviderServiceAccount object", func() { - By("Deleting the CAPI kubeconfig secret object", func() { - clusterName, ok := intCtx.VSphereCluster.GetLabels()[clusterv1.ClusterNameLabel] - Expect(ok).To(BeTrue()) - secret := &corev1.Secret{ - ObjectMeta: metav1.ObjectMeta{ - Namespace: intCtx.Namespace, - Name: fmt.Sprintf("%s-kubeconfig", clusterName), - }, - } - Expect(intCtx.Client.Delete(ctx, secret)).To(Succeed()) + By("Creating the Cluster and vSphereCluster only", func() { + helpers.CreateAndWait(ctx, intCtx.Client, intCtx.Cluster) + helpers.CreateAndWait(ctx, intCtx.Client, intCtx.VSphereCluster) }) By("Creating the ProviderServiceAccount", func() { @@ -187,6 +180,11 @@ var _ = Describe("ProviderServiceAccount controller integration tests", func() { var role *rbacv1.Role var roleBinding *rbacv1.RoleBinding BeforeEach(func() { + By("Creating the Cluster, vSphereCluster and KubeconfigSecret", func() { + helpers.CreateAndWait(ctx, intCtx.Client, intCtx.Cluster) + helpers.CreateAndWait(ctx, intCtx.Client, intCtx.VSphereCluster) + helpers.CreateAndWait(ctx, intCtx.Client, intCtx.KubeconfigSecret) + }) pSvcAccount = getTestProviderServiceAccount(intCtx.Namespace, intCtx.VSphereCluster) pSvcAccount.Spec.TargetNamespace = "default" // Pause the ProviderServiceAccount so we can create dependent but legacy resources diff --git a/controllers/servicediscovery_controller_intg_test.go b/controllers/servicediscovery_controller_intg_test.go index b54b121256..51dd09c3c7 100644 --- a/controllers/servicediscovery_controller_intg_test.go +++ b/controllers/servicediscovery_controller_intg_test.go @@ -32,6 +32,11 @@ var _ = Describe("Service Discovery controller integration tests", func() { ) BeforeEach(func() { intCtx = helpers.NewIntegrationTestContextWithClusters(ctx, testEnv.Manager.GetClient()) + By("Creating the Cluster, vSphereCluster and KubeconfigSecret", func() { + helpers.CreateAndWait(ctx, intCtx.Client, intCtx.Cluster) + helpers.CreateAndWait(ctx, intCtx.Client, intCtx.VSphereCluster) + helpers.CreateAndWait(ctx, intCtx.Client, intCtx.KubeconfigSecret) + }) }) AfterEach(func() { intCtx.AfterEach() diff --git a/internal/test/helpers/vmware/intg_test_context.go b/internal/test/helpers/vmware/intg_test_context.go index e17f4e7a4b..9f5b0acee0 100644 --- a/internal/test/helpers/vmware/intg_test_context.go +++ b/internal/test/helpers/vmware/intg_test_context.go @@ -46,6 +46,7 @@ type IntegrationTestContext struct { Namespace string VSphereCluster *vmwarev1.VSphereCluster Cluster *clusterv1.Cluster + KubeconfigSecret *corev1.Secret VSphereClusterKey client.ObjectKey envTest *envtest.Environment } @@ -100,7 +101,7 @@ func NewIntegrationTestContextWithClusters(ctx context.Context, integrationTestC }) vsphereClusterName := capiutil.RandomString(6) - testCtx.Cluster = createCluster(ctx, integrationTestClient, testCtx.Namespace, vsphereClusterName) + testCtx.Cluster = generateCluster(testCtx.Namespace, vsphereClusterName) var config *rest.Config @@ -126,8 +127,8 @@ func NewIntegrationTestContextWithClusters(ctx context.Context, integrationTestC testCtx.envTest = envTest }) - By("Create the kubeconfig secret for the cluster", func() { - buf, err := writeKubeConfig(config) + By("Generating the kubeconfig secret for the cluster", func() { + buf, err := generateKubeConfig(config) Expect(err).ToNot(HaveOccurred()) secret := &corev1.Secret{ ObjectMeta: metav1.ObjectMeta{ @@ -138,7 +139,8 @@ func NewIntegrationTestContextWithClusters(ctx context.Context, integrationTestC APIVersion: clusterv1.GroupVersion.String(), Kind: "Cluster", Name: testCtx.Cluster.Name, - UID: testCtx.Cluster.UID, + // Using a random id in this case is okay because we don't rely on it to be the correct uid. + UID: "should-be-uid-of-cluster", }, }, }, @@ -146,26 +148,32 @@ func NewIntegrationTestContextWithClusters(ctx context.Context, integrationTestC "value": buf, }, } - Expect(integrationTestClient.Create(ctx, secret)).To(Succeed()) - Eventually(func() error { - return integrationTestClient.Get(ctx, client.ObjectKeyFromObject(secret), secret) - }).Should(Succeed()) + testCtx.KubeconfigSecret = secret }) - By("Create a vsphere cluster and wait for it to exist", func() { - testCtx.VSphereCluster = createVSphereCluster(ctx, integrationTestClient, testCtx.Namespace, vsphereClusterName, testCtx.Cluster.GetName()) + By("Generating a vsphere cluster", func() { + testCtx.VSphereCluster = generateVSphereCluster(testCtx.Namespace, vsphereClusterName, testCtx.Cluster.GetName()) testCtx.VSphereClusterKey = client.ObjectKeyFromObject(testCtx.VSphereCluster) }) return testCtx } -func createCluster(ctx context.Context, integrationTestClient client.Client, namespace, name string) *clusterv1.Cluster { - By("Create the CAPI Cluster and wait for it to exist") +// CreateAndWait creates and waits for an object to exist. +func CreateAndWait(ctx context.Context, integrationTestClient client.Client, obj client.Object) { + GinkgoHelper() + Expect(integrationTestClient.Create(ctx, obj)).To(Succeed()) + Eventually(func() error { + return integrationTestClient.Get(ctx, client.ObjectKeyFromObject(obj), obj) + }).Should(Succeed()) +} + +func generateCluster(namespace, name string) *clusterv1.Cluster { + By("Generate the CAPI Cluster") cluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ - Namespace: namespace, - GenerateName: name, + Namespace: namespace, + Name: fmt.Sprintf("%s-%s", name, capiutil.RandomString(6)), }, Spec: clusterv1.ClusterSpec{ ClusterNetwork: &clusterv1.ClusterNetwork{ @@ -182,14 +190,10 @@ func createCluster(ctx context.Context, integrationTestClient client.Client, nam }, }, } - Expect(integrationTestClient.Create(ctx, cluster)).To(Succeed()) - Eventually(func() error { - return integrationTestClient.Get(ctx, client.ObjectKeyFromObject(cluster), cluster) - }).Should(Succeed()) return cluster } -func createVSphereCluster(ctx context.Context, integrationTestClient client.Client, namespace, name, capiClusterName string) *vmwarev1.VSphereCluster { +func generateVSphereCluster(namespace, name, capiClusterName string) *vmwarev1.VSphereCluster { vsphereCluster := &vmwarev1.VSphereCluster{ ObjectMeta: metav1.ObjectMeta{ Namespace: namespace, @@ -197,36 +201,11 @@ func createVSphereCluster(ctx context.Context, integrationTestClient client.Clie Labels: map[string]string{clusterv1.ClusterNameLabel: capiClusterName}, }, } - Expect(integrationTestClient.Create(ctx, vsphereCluster)).To(Succeed()) - Eventually(func() error { - return integrationTestClient.Get(ctx, client.ObjectKeyFromObject(vsphereCluster), vsphereCluster) - }).Should(Succeed()) - - // TODO: remove if not needed - By("Creating a extensions ca", func() { - secret := &corev1.Secret{ - ObjectMeta: metav1.ObjectMeta{ - Name: vsphereCluster.Name + "-extensions-ca", - Namespace: namespace, - }, - Data: map[string][]byte{ - "ca.crt": []byte("test-ca"), - "tls.crt": []byte("test-tls.crt"), - "tls.key": []byte("test-tls.key"), - }, - Type: corev1.SecretTypeTLS, - } - Expect(integrationTestClient.Create(ctx, secret)).To(Succeed()) - secretKey := client.ObjectKey{Namespace: secret.Namespace, Name: secret.Name} - Eventually(func() error { - return integrationTestClient.Get(ctx, secretKey, secret) - }).Should(Succeed()) - }) return vsphereCluster } -// writeKubeConfig writes an existing *rest.Config out as the typical kubeconfig YAML data. -func writeKubeConfig(config *rest.Config) ([]byte, error) { +// generateKubeConfig writes an existing *rest.Config out as the typical kubeconfig YAML data. +func generateKubeConfig(config *rest.Config) ([]byte, error) { return clientcmd.Write(api.Config{ Clusters: map[string]*api.Cluster{ config.ServerName: {