Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 controllers: fix ProviderServiceAccount tests to create objects on-demand #2983

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 19 additions & 21 deletions controllers/serviceaccount_controller_intg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package controllers

import (
"fmt"
"os"
"reflect"

Expand All @@ -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"
Expand All @@ -39,7 +37,7 @@ import (
helpers "sigs.k8s.io/cluster-api-provider-vsphere/internal/test/helpers/vmware"
)

var _ = Describe("ProviderServiceAccount controller integration tests", func() {
var _ = Describe("ProviderServiceAccount controller integration tests", Label("FOO"), func() {
sbueringer marked this conversation as resolved.
Show resolved Hide resolved
var intCtx *helpers.IntegrationTestContext

BeforeEach(func() {
Expand All @@ -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)
Expand Down Expand Up @@ -127,15 +131,11 @@ var _ = Describe("ProviderServiceAccount controller integration tests", func() {
})
})

Context("With non-existent Cluster object", func() {
Context("With non-existent Cluster object", Label("FOO"), 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() {
Expand All @@ -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() {
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions controllers/servicediscovery_controller_intg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
59 changes: 19 additions & 40 deletions internal/test/helpers/vmware/intg_test_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type IntegrationTestContext struct {
Namespace string
VSphereCluster *vmwarev1.VSphereCluster
Cluster *clusterv1.Cluster
KubeconfigSecret *corev1.Secret
VSphereClusterKey client.ObjectKey
envTest *envtest.Environment
}
Expand Down Expand Up @@ -100,7 +101,7 @@ func NewIntegrationTestContextWithClusters(ctx context.Context, integrationTestC
})

vsphereClusterName := capiutil.RandomString(6)
testCtx.Cluster = createCluster(ctx, integrationTestClient, testCtx.Namespace, vsphereClusterName)
testCtx.Cluster = createCluster(testCtx.Namespace, vsphereClusterName)

var config *rest.Config

Expand Down Expand Up @@ -138,34 +139,41 @@ 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",
},
},
},
Data: map[string][]byte{
"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() {
chrischdi marked this conversation as resolved.
Show resolved Hide resolved
testCtx.VSphereCluster = createVSphereCluster(ctx, integrationTestClient, testCtx.Namespace, vsphereClusterName, testCtx.Cluster.GetName())
testCtx.VSphereCluster = createVSphereCluster(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 {
// 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 createCluster(namespace, name string) *clusterv1.Cluster {
By("Create the CAPI Cluster and wait for it to exist")
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{
Expand All @@ -182,46 +190,17 @@ 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 createVSphereCluster(namespace, name, capiClusterName string) *vmwarev1.VSphereCluster {
vsphereCluster := &vmwarev1.VSphereCluster{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: name,
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() {
sbueringer marked this conversation as resolved.
Show resolved Hide resolved
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
}

Expand Down