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

🌱 Improve checking if guest cluster client works in unit tests #3136

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 10 additions & 4 deletions controllers/serviceaccount_controller_intg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ limitations under the License.
package controllers

import (
"fmt"
"reflect"
"time"

"github.com/google/uuid"
. "github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -53,15 +55,19 @@ var _ = Describe("ProviderServiceAccount controller integration tests", func() {
targetNSObj *corev1.Namespace
)
BeforeEach(func() {
By("Creating the Cluster, vSphereCluster and KubeconfigSecret", func() {
By(fmt.Sprintf("Creating the Cluster (%s), vSphereCluster (%s) and KubeconfigSecret", intCtx.Cluster.Name, intCtx.VSphereCluster.Name), func() {
helpers.CreateAndWait(ctx, intCtx.Client, intCtx.Cluster)
helpers.CreateAndWait(ctx, intCtx.Client, intCtx.VSphereCluster)
helpers.CreateAndWait(ctx, intCtx.Client, intCtx.KubeconfigSecret)
})

By("Verifying that the guest cluster client works")
guestClient, err := tracker.GetClient(ctx, client.ObjectKeyFromObject(intCtx.Cluster))
Expect(err).ToNot(HaveOccurred())
var guestClient client.Client
var err error
Eventually(func() error {
guestClient, err = tracker.GetClient(ctx, client.ObjectKeyFromObject(intCtx.Cluster))
return err
}, time.Minute, 5*time.Second).Should(Succeed())
// Note: Create a Service informer, so the test later doesn't fail if this doesn't work.
Expect(guestClient.List(ctx, &corev1.ServiceList{}, client.InNamespace(metav1.NamespaceDefault))).To(Succeed())

Expand Down Expand Up @@ -181,7 +187,7 @@ 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() {
By(fmt.Sprintf("Creating the Cluster (%s), vSphereCluster (%s) and KubeconfigSecret", intCtx.Cluster.Name, intCtx.VSphereCluster.Name), func() {
helpers.CreateAndWait(ctx, intCtx.Client, intCtx.Cluster)
helpers.CreateAndWait(ctx, intCtx.Client, intCtx.VSphereCluster)
helpers.CreateAndWait(ctx, intCtx.Client, intCtx.KubeconfigSecret)
Expand Down
13 changes: 10 additions & 3 deletions controllers/servicediscovery_controller_intg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ limitations under the License.
package controllers

import (
"fmt"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
Expand All @@ -33,15 +36,19 @@ 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() {
By(fmt.Sprintf("Creating the Cluster (%s), vSphereCluster (%s) and KubeconfigSecret", intCtx.Cluster.Name, intCtx.VSphereCluster.Name), func() {
helpers.CreateAndWait(ctx, intCtx.Client, intCtx.Cluster)
helpers.CreateAndWait(ctx, intCtx.Client, intCtx.VSphereCluster)
helpers.CreateAndWait(ctx, intCtx.Client, intCtx.KubeconfigSecret)
})

By("Verifying that the guest cluster client works")
guestClient, err := tracker.GetClient(ctx, client.ObjectKeyFromObject(intCtx.Cluster))
Expect(err).ToNot(HaveOccurred())
var guestClient client.Client
var err error
Eventually(func() error {
guestClient, err = tracker.GetClient(ctx, client.ObjectKeyFromObject(intCtx.Cluster))
return err
}, time.Minute, 5*time.Second).Should(Succeed())
// Note: Create a Service informer, so the test later doesn't fail if this doesn't work.
Expect(guestClient.List(ctx, &corev1.ServiceList{}, client.InNamespace(metav1.NamespaceDefault))).To(Succeed())
})
Expand Down