Skip to content

Commit

Permalink
Fixes test for secret creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Disper committed Sep 18, 2023
1 parent 16a4223 commit 70ba89f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 34 deletions.
35 changes: 12 additions & 23 deletions internal/controller/gardener_cluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ var _ = Describe("Cluster Inventory controller", func() {
It("Create, and remove secret", func() {
By("Create GardenerCluster CR")

clusterCR := fixGardenerClusterCR(kymaName, namespace, kymaName, shootName, secretName)
Expect(k8sClient.Create(context.Background(), &clusterCR)).To(Succeed())
gardenerClusterCR := fixGardenerClusterCR(kymaName, namespace, shootName, secretName)
Expect(k8sClient.Create(context.Background(), &gardenerClusterCR)).To(Succeed())

By("Wait for secret creation")
var kubeconfigSecret corev1.Secret
Expand All @@ -38,17 +38,6 @@ var _ = Describe("Cluster Inventory controller", func() {
Expect(kubeconfigSecret.Labels).To(Equal(expectedSecret.Labels))
Expect(kubeconfigSecret.Data).To(Equal(expectedSecret.Data))
Expect(kubeconfigSecret.Annotations[lastKubeconfigSyncAnnotation]).To(Not(BeEmpty()))

//By("Delete Cluster CR")
//Expect(k8sClient.Delete(context.Background(), &clusterCR)).To(Succeed())

//By("Wait for secret deletion")
//Eventually(func() bool {
// err := k8sClient.Get(context.Background(), key, &kubeconfigSecret)
//
// return err != nil && k8serrors.IsNotFound(err)
//
//}, time.Second*30, time.Second*3).Should(BeTrue())
})
})
})
Expand Down Expand Up @@ -98,19 +87,11 @@ func fixSecretLabels(kymaName, shootName string) map[string]string {
return labels
}

func fixGardenerClusterCR(name, namespace, kymaName, shootName, secretName string) imv1.GardenerCluster {
return newTestInfrastructureManagerCR(name, namespace, shootName, secretName).
func fixGardenerClusterCR(kymaName, namespace, shootName, secretName string) imv1.GardenerCluster {
return newTestInfrastructureManagerCR(kymaName, namespace, shootName, secretName).
WithLabels(fixClusterInventoryLabels(kymaName, shootName)).ToCluster()
}

func (sb *TestGardenerClusterCR) ToCluster() imv1.GardenerCluster {
return sb.gardenerCluster
}

type TestGardenerClusterCR struct {
gardenerCluster imv1.GardenerCluster
}

func newTestInfrastructureManagerCR(name, namespace, shootName, secretName string) *TestGardenerClusterCR {
return &TestGardenerClusterCR{
gardenerCluster: imv1.GardenerCluster{
Expand Down Expand Up @@ -140,6 +121,14 @@ func (sb *TestGardenerClusterCR) WithLabels(labels map[string]string) *TestGarde
return sb
}

func (sb *TestGardenerClusterCR) ToCluster() imv1.GardenerCluster {
return sb.gardenerCluster
}

type TestGardenerClusterCR struct {
gardenerCluster imv1.GardenerCluster
}

func fixClusterInventoryLabels(kymaName, shootName string) map[string]string {
labels := map[string]string{}

Expand Down
35 changes: 24 additions & 11 deletions internal/controller/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,33 @@ limitations under the License.
package controller

import (
"context"
"path/filepath"
"testing"

infrastructuremanagerv1 "github.com/kyma-project/infrastructure-manager/api/v1"
"github.com/kyma-project/infrastructure-manager/internal/controller/mocks"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"path/filepath"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"testing"
)

// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.

var cfg *rest.Config //nolint:gochecknoglobals
var k8sClient client.Client //nolint:gochecknoglobals
var testEnv *envtest.Environment //nolint:gochecknoglobals
var (
cfg *rest.Config //nolint:gochecknoglobals
k8sClient client.Client //nolint:gochecknoglobals
testEnv *envtest.Environment //nolint:gochecknoglobals
suiteCtx context.Context

Check failure on line 44 in internal/controller/suite_test.go

View workflow job for this annotation

GitHub Actions / lint

suiteCtx is a global variable (gochecknoglobals)
cancelSuiteCtx context.CancelFunc

Check failure on line 45 in internal/controller/suite_test.go

View workflow job for this annotation

GitHub Actions / lint

cancelSuiteCtx is a global variable (gochecknoglobals)
)

func TestControllers(t *testing.T) {
RegisterFailHandler(Fail)
Expand All @@ -61,28 +67,34 @@ var _ = BeforeSuite(func() {
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())

kubeconfigProviderMock := &mocks.KubeconfigProvider{}
setupKubeconfigProviderMock(kubeconfigProviderMock)
err = infrastructuremanagerv1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

mgr, err := ctrl.NewManager(cfg, ctrl.Options{Scheme: scheme.Scheme})
Expect(err).ToNot(HaveOccurred())

kubeconfigProviderMock := &mocks.KubeconfigProvider{}
setupKubeconfigProviderMock(kubeconfigProviderMock)

controller := NewGardenerClusterController(mgr, kubeconfigProviderMock, logger)
Expect(controller).NotTo(BeNil())

err = infrastructuremanagerv1.AddToScheme(scheme.Scheme)

err = controller.SetupWithManager(mgr)
Expect(err).To(BeNil())

Expect(err).NotTo(HaveOccurred())

//+kubebuilder:scaffold:scheme

k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())

go func() {
defer GinkgoRecover()
suiteCtx, cancelSuiteCtx = context.WithCancel(context.Background())

err = mgr.Start(suiteCtx)
Expect(err).ToNot(HaveOccurred(), "failed to run manager")
}()
})

func setupKubeconfigProviderMock(kpMock *mocks.KubeconfigProvider) {
Expand All @@ -93,6 +105,7 @@ func setupKubeconfigProviderMock(kpMock *mocks.KubeconfigProvider) {

var _ = AfterSuite(func() {
By("tearing down the test environment")
cancelSuiteCtx()
err := testEnv.Stop()
Expect(err).NotTo(HaveOccurred())
})

0 comments on commit 70ba89f

Please sign in to comment.