Skip to content

Commit

Permalink
managedcluster controller test
Browse files Browse the repository at this point in the history
Signed-off-by: Artem Bortnikov <[email protected]>
  • Loading branch information
BROngineer committed Dec 23, 2024
1 parent 2e92fb6 commit 930bd3d
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
101 changes: 101 additions & 0 deletions internal/controller/managedcluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"time"

hcv2 "github.com/fluxcd/helm-controller/api/v2"
sourcev1 "github.com/fluxcd/source-controller/api/v1"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
Expand All @@ -31,6 +32,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"

hmc "github.com/Mirantis/hmc/api/v1alpha1"
. "sigs.k8s.io/controller-runtime/pkg/envtest/komega"
)

var _ = Describe("ManagedCluster Controller", func() {
Expand All @@ -42,6 +44,8 @@ var _ = Describe("ManagedCluster Controller", func() {
templateName = "test-template"
svcTemplateName = "test-svc-template"
credentialName = "test-credential"

helmChartURL = "http://source-controller.hmc-system.svc.cluster.local./helmchart/hmc-system/test-chart/0.1.0.tar.gz"
)

ctx := context.Background()
Expand Down Expand Up @@ -123,6 +127,11 @@ var _ = Describe("ManagedCluster Controller", func() {
Expect(k8sClient.Create(ctx, svcTemplate)).To(Succeed())
svcTemplate.Status = hmc.ServiceTemplateStatus{
TemplateStatusCommon: hmc.TemplateStatusCommon{
ChartRef: &hcv2.CrossNamespaceSourceReference{
Kind: "HelmChart",
Name: "ref-test",
Namespace: "default",
},
TemplateValidationStatus: hmc.TemplateValidationStatus{
Valid: true,
},
Expand Down Expand Up @@ -212,17 +221,109 @@ var _ = Describe("ManagedCluster Controller", func() {
Expect(k8sClient.Delete(ctx, management)).To(Succeed())
Expect(k8sClient.Delete(ctx, namespace)).To(Succeed())
})

It("should successfully reconcile the resource", func() {
By("Reconciling the created resource")
controllerReconciler := &ManagedClusterReconciler{
Client: k8sClient,
Config: &rest.Config{},
}

By("Ensure finalizer is added")
_, err := controllerReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: typeNamespacedName,
})
Expect(err).NotTo(HaveOccurred())
Eventually(Object(managedCluster)).Should(SatisfyAll(
HaveField("Finalizers", ContainElement(hmc.ManagedClusterFinalizer)),
))

By("Reconciling resource with finalizer")
_, err = controllerReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: typeNamespacedName,
})
Expect(err).To(HaveOccurred())
Eventually(Get(managedCluster)).Should(Succeed())
// todo: conditions check fails due error related to indexer:
// "field label not supported: .spec.supportedTemplates[].Name"
// when reconciliation fails reconciler executes deferred function to update status
// which fails due to indexer error.
// The same conditions checks supposed to be added after each reconciliation afterwards
// to ensure object being reconciled is in expected state.
// Eventually(Object(managedCluster)).Should(SatisfyAll(
// HaveField("Status.Conditions", ContainElement(SatisfyAll(
// HaveField("Type", hmc.TemplateReadyCondition),
// HaveField("Status", metav1.ConditionTrue),
// HaveField("Reason", hmc.SucceededReason),
// HaveField("Message", "Template is valid"),
// ))),
// ))

By("Creating absent required resources: HelmChart, HelmRepository")
helmRepo := &sourcev1.HelmRepository{
ObjectMeta: metav1.ObjectMeta{
Name: "test-repository",
Namespace: "default",
},
Spec: sourcev1.HelmRepositorySpec{
Insecure: true,
Interval: metav1.Duration{
Duration: 10 * time.Minute,
},
Provider: "generic",
Type: "oci",
URL: "oci://hmc-local-registry:5000/charts",
},
}
Expect(k8sClient.Create(ctx, helmRepo)).To(Succeed())

helmChart := &sourcev1.HelmChart{
ObjectMeta: metav1.ObjectMeta{
Name: "ref-test",
Namespace: "default",
},
Spec: sourcev1.HelmChartSpec{
Chart: "test",
Interval: metav1.Duration{
Duration: 10 * time.Minute,
},
ReconcileStrategy: sourcev1.ReconcileStrategyChartVersion,
SourceRef: sourcev1.LocalHelmChartSourceReference{
Kind: "HelmRepository",
Name: helmRepo.Name,
},
Version: "0.1.0",
},
}
Expect(k8sClient.Create(ctx, helmChart)).To(Succeed())

_, err = controllerReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: typeNamespacedName,
})
Expect(err).To(HaveOccurred())

By("Patching ClusterTemplate status")
Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(template), template)).Should(Succeed())
template.Status.ChartRef = &hcv2.CrossNamespaceSourceReference{
Kind: "HelmChart",
Name: "ref-test",
Namespace: "default",
}
Expect(k8sClient.Status().Update(ctx, template)).To(Succeed())

helmChart.Status.URL = helmChartURL
helmChart.Status.Artifact = &sourcev1.Artifact{
URL: helmChartURL,
LastUpdateTime: metav1.Now(),
}
Expect(k8sClient.Status().Update(ctx, helmChart)).To(Succeed())

// todo: next error occurs due to dependency on helm library. The best way to mitigate this is to
// inject an interface into the reconciler struct that can be mocked out for testing.
_, err = controllerReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: typeNamespacedName,
})
Expect(err).To(HaveOccurred())
})
})
})
2 changes: 2 additions & 0 deletions internal/controller/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
. "sigs.k8s.io/controller-runtime/pkg/envtest/komega"

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

View workflow job for this annotation

GitHub Actions / Build and Unit Test

dot-imports: should not use dot imports (revive)
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
Expand Down Expand Up @@ -147,6 +148,7 @@ var _ = BeforeSuite(func() {
Expect(err).NotTo(HaveOccurred())
mgrClient = mgr.GetClient()
Expect(mgrClient).NotTo(BeNil())
SetClient(mgrClient)

err = hmcmirantiscomv1alpha1.SetupIndexers(ctx, mgr)
Expect(err).NotTo(HaveOccurred())
Expand Down

0 comments on commit 930bd3d

Please sign in to comment.