Skip to content

Commit

Permalink
add custom health controller unit test (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
zreigz authored Feb 2, 2024
1 parent 3497f35 commit 6d29a5b
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 79 deletions.
84 changes: 84 additions & 0 deletions internal/controller/customhealth_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package controller

import (
"context"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/pluralsh/deployment-operator/api/v1alpha1"
"github.com/pluralsh/deployment-operator/pkg/controller/service"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

var _ = Describe("Customhealt Controller", Ordered, func() {
Context("When reconciling a resource", func() {
const (
resourceName = "test"
namespace = "default"
script = "test script"
)

ctx := context.Background()

typeNamespacedName := types.NamespacedName{
Name: resourceName,
Namespace: "default",
}

customHealth := &v1alpha1.CustomHealth{}

BeforeAll(func() {
By("creating the custom resource for the Kind CustomHealth")
err := kClient.Get(ctx, typeNamespacedName, customHealth)
if err != nil && errors.IsNotFound(err) {
resource := &v1alpha1.CustomHealth{
ObjectMeta: metav1.ObjectMeta{
Name: resourceName,
Namespace: namespace,
},
Spec: v1alpha1.CustomHealthSpec{
Script: script,
},
}
Expect(kClient.Create(ctx, resource)).To(Succeed())
}
})

AfterAll(func() {
resource := &v1alpha1.CustomHealth{}
err := kClient.Get(ctx, typeNamespacedName, resource)
Expect(err).NotTo(HaveOccurred())

By("Cleanup the specific resource instance CustomHealth")
Expect(kClient.Delete(ctx, resource)).To(Succeed())
})

It("should successfully reconcile resource", func() {
By("Reconciling the import resource")
_ = struct {
expectedStatus v1alpha1.CustomHealthStatus
}{
expectedStatus: v1alpha1.CustomHealthStatus{
Conditions: []metav1.Condition{},
},
}
sr := &service.ServiceReconciler{}
reconciler := &CustomHealthReconciler{
Client: kClient,
Scheme: kClient.Scheme(),
ServiceReconciler: sr,
}
_, err := reconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: typeNamespacedName,
})
Expect(err).NotTo(HaveOccurred())

Expect(sr.LuaScript).Should(Equal(script))

})
})

})
24 changes: 17 additions & 7 deletions internal/controller/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

deploymentsv1alpha1 "github.com/pluralsh/deployment-operator/api/v1alpha1"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"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"

deploymentsv1alpha1 "github.com/pluralsh/deployment-operator/api/v1alpha1"
//+kubebuilder:scaffold:imports
)

// 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
var testEnv *envtest.Environment
var kClient client.Client

func TestControllers(t *testing.T) {
RegisterFailHandler(Fail)
Expand All @@ -52,7 +52,6 @@ var _ = BeforeSuite(func() {
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
ErrorIfCRDPathMissing: true,

// The BinaryAssetsDirectory is only required if you want to run the tests directly
// without call the makefile target test. If not informed it will look for the
// default path defined in controller-runtime which is /usr/local/kubebuilder/.
Expand All @@ -66,12 +65,23 @@ var _ = BeforeSuite(func() {
err = deploymentsv1alpha1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

//+kubebuilder:scaffold:scheme
// cfg is defined in this file globally.
cfg, err = testEnv.Start()
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())

err = deploymentsv1alpha1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

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

})

var _ = AfterSuite(func() {
By("tearing down the test environment")

err := testEnv.Stop()
Expect(err).NotTo(HaveOccurred())
})
72 changes: 0 additions & 72 deletions pkg/lua/suite_test.go

This file was deleted.

0 comments on commit 6d29a5b

Please sign in to comment.