Skip to content

Commit

Permalink
Tests for provider and sync controllers
Browse files Browse the repository at this point in the history
Signed-off-by: Danil Grigorev <danil.grigorev@suse.com>
  • Loading branch information
Danil-Grigorev committed Dec 12, 2023
1 parent 2f6d1d5 commit eeda535
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 3 deletions.
10 changes: 7 additions & 3 deletions internal/sync/provider_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package sync
import (
"context"

"sigs.k8s.io/cluster-api/util/conditions"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha2"
"sigs.k8s.io/cluster-api/util/conditions"

turtlesv1 "github.com/rancher-sandbox/rancher-turtles/api/v1alpha1"
)
Expand All @@ -27,10 +27,14 @@ func NewProviderSync(cl client.Client, capiProvider *turtlesv1.CAPIProvider) Syn
// Template returning the mirrored CAPI Operator manifest template.
func (ProviderSync) Template(capiProvider *turtlesv1.CAPIProvider) client.Object {
meta := metav1.ObjectMeta{
Name: string(capiProvider.Spec.Name),
Name: capiProvider.Spec.Name,
Namespace: capiProvider.GetNamespace(),
}

if meta.Name == "" {
meta.Name = capiProvider.Name
}

switch capiProvider.Spec.Type {
case turtlesv1.Infrastructure:
return &operatorv1.InfrastructureProvider{ObjectMeta: meta}
Expand Down
90 changes: 90 additions & 0 deletions internal/sync/provider_sync_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package sync_test

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
turtlesv1 "github.com/rancher-sandbox/rancher-turtles/api/v1alpha1"
"github.com/rancher-sandbox/rancher-turtles/internal/sync"
corev1 "k8s.io/api/core/v1"
"k8s.io/utils/pointer"
. "sigs.k8s.io/controller-runtime/pkg/envtest/komega"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha2"
)

var _ = Describe("Provider sync", func() {
var (
err error
ns *corev1.Namespace
capiProvider *turtlesv1.CAPIProvider
infrastructure *operatorv1.InfrastructureProvider
)

BeforeEach(func() {
SetClient(testEnv)
SetContext(ctx)

ns, err = testEnv.CreateNamespace(ctx, "ns")
Expect(err).ToNot(HaveOccurred())

capiProvider = &turtlesv1.CAPIProvider{ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: ns.Name,
}, Spec: turtlesv1.CAPIProviderSpec{
Name: "docker",
Type: turtlesv1.Infrastructure,
}}

infrastructure = &operatorv1.InfrastructureProvider{ObjectMeta: metav1.ObjectMeta{
Name: string(capiProvider.Spec.Name),
Namespace: capiProvider.Namespace,
}}

Expect(testEnv.Client.Create(ctx, capiProvider)).To(Succeed())
})

AfterEach(func() {
testEnv.Cleanup(ctx, ns)
})

It("Should sync spec down", func() {
s := sync.NewProviderSync(testEnv, capiProvider.DeepCopy())
Eventually(s.Get(ctx)).Should(Succeed())

Expect(s.Sync(ctx)).To(Succeed())

var err error
s.Apply(ctx, &err)
Expect(err).To(Succeed())

Eventually(Object(infrastructure)).Should(
HaveField("Spec.ProviderSpec", Equal(capiProvider.Spec.ProviderSpec)))
})

It("Should sync status up", func() {
Expect(testEnv.Client.Create(ctx, infrastructure.DeepCopy())).To(Succeed())
Eventually(UpdateStatus(infrastructure, func() {
infrastructure.Status = operatorv1.InfrastructureProviderStatus{
ProviderStatus: operatorv1.ProviderStatus{
InstalledVersion: pointer.String("v1.2.3"),
},
}
})).Should(Succeed())

s := sync.NewProviderSync(testEnv, capiProvider)

Eventually(func() (err error) {
err = s.Get(ctx)
if err != nil {
return
}
err = s.Sync(ctx)
s.Apply(ctx, &err)
return
}).Should(Succeed())

Expect(capiProvider).To(HaveField("Status.ProviderStatus", Equal(infrastructure.Status.ProviderStatus)))
Expect(capiProvider).To(HaveField("Status.Phase", Equal(turtlesv1.Provisioning)))
})
})
2 changes: 2 additions & 0 deletions internal/sync/secret_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,11 @@ func setFeatures(capiProvider *turtlesv1.CAPIProvider) {
if features.ClusterResourceSet {
variables["EXP_CLUSTER_RESOURCE_SET"] = value
}

if features.ClusterTopology {
variables["CLUSTER_TOPOLOGY"] = value
}

if features.MachinePool {
variables["EXP_MACHINE_POOL"] = value
}
Expand Down
76 changes: 76 additions & 0 deletions internal/sync/secret_sync_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package sync_test

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
turtlesv1 "github.com/rancher-sandbox/rancher-turtles/api/v1alpha1"
"github.com/rancher-sandbox/rancher-turtles/internal/sync"
corev1 "k8s.io/api/core/v1"
. "sigs.k8s.io/controller-runtime/pkg/envtest/komega"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha2"
)

var _ = Describe("Provider sync", func() {
var (
err error
ns *corev1.Namespace
capiProvider *turtlesv1.CAPIProvider
secret *corev1.Secret
)

BeforeEach(func() {
SetClient(testEnv)
SetContext(ctx)

ns, err = testEnv.CreateNamespace(ctx, "ns")
Expect(err).ToNot(HaveOccurred())

capiProvider = &turtlesv1.CAPIProvider{ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: ns.Name,
}, Spec: turtlesv1.CAPIProviderSpec{
Name: "docker",
Type: turtlesv1.Infrastructure,
ProviderSpec: operatorv1.ProviderSpec{
ConfigSecret: &operatorv1.SecretReference{
Name: "test",
},
},
Variables: map[string]string{
"variable": "one",
},
Features: &turtlesv1.Features{
ClusterTopology: true,
},
}}

secret = &corev1.Secret{ObjectMeta: metav1.ObjectMeta{
Name: string(capiProvider.Spec.ProviderSpec.ConfigSecret.Name),
Namespace: capiProvider.Namespace,
}}

Expect(testEnv.Client.Create(ctx, capiProvider)).To(Succeed())
})

AfterEach(func() {
testEnv.Cleanup(ctx, ns)
})

It("Should sync spec down", func() {
s := sync.NewSecretSync(testEnv, capiProvider.DeepCopy())
Eventually(s.Get(ctx)).Should(Succeed())

Expect(s.Sync(ctx)).To(Succeed())

var err error
s.Apply(ctx, &err)
Expect(err).To(Succeed())

Eventually(Object(secret)).Should(
HaveField("Data", HaveKey("variable")))
Eventually(Object(secret)).Should(
HaveField("Data", HaveKey("CLUSTER_TOPOLOGY")))
})
})

0 comments on commit eeda535

Please sign in to comment.