Skip to content

Commit

Permalink
Add integration test for controller
Browse files Browse the repository at this point in the history
The test works fine but for some reason the AfterSuite tear down
function isn't working as expected. It times out after trying to shut
down the API server. The test comes back as passed but because the API
server won't shut down in time, `make test` fails.

kubernetes-sigs/controller-runtime#1571
  • Loading branch information
RyanMillerC committed Feb 28, 2023
1 parent d1b6640 commit 1f41401
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
61 changes: 61 additions & 0 deletions controllers/catfact_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package controllers

import (
"context"
"time"

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

tacomoev1alpha1 "github.com/ryanmillerc/cat-facts-operator/api/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

var _ = Describe("CatFact controller", func() {

// Define utility constants for object names and testing timeouts/durations and intervals.
const (
CatFactName = "my-cat-fact"
CatFactNamespace = "default"

timeout = time.Second * 10
duration = time.Second * 10
interval = time.Millisecond * 250
)

Context("When creating a CatFact", func() {
It("Should generate a fact if a fact isn't provided", func() {
By("Magic")
ctx := context.Background()
catFact := &tacomoev1alpha1.CatFact{
TypeMeta: metav1.TypeMeta{
APIVersion: "taco.moe/v1alpha1",
Kind: "CatFact",
},
ObjectMeta: metav1.ObjectMeta{
Name: CatFactName,
Namespace: CatFactNamespace,
},
Spec: tacomoev1alpha1.CatFactSpec{
Fact: "",
},
}
Expect(k8sClient.Create(ctx, catFact)).Should(Succeed())

catFactLookupKey := types.NamespacedName{Name: CatFactName, Namespace: CatFactNamespace}
createdCatFact := &tacomoev1alpha1.CatFact{}

// We'll need to retry getting this newly created CronJob, given that creation may not immediately happen.
Eventually(func() bool {
err := k8sClient.Get(ctx, catFactLookupKey, createdCatFact)
if err != nil {
return false
}
return true
}, timeout, interval).Should(BeTrue())
// Let's make sure a fact was set on the object
Expect(createdCatFact.Spec.Fact).Should(Equal(""))
})
})
})
18 changes: 18 additions & 0 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package controllers

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

Expand All @@ -25,6 +26,7 @@ import (

"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
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"
Expand Down Expand Up @@ -71,6 +73,22 @@ var _ = BeforeSuite(func() {
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())

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

err = (&CatFactReconciler{
Client: k8sManager.GetClient(),
Scheme: k8sManager.GetScheme(),
}).SetupWithManager(k8sManager)
Expect(err).ToNot(HaveOccurred())

go func() {
defer GinkgoRecover()
err = k8sManager.Start(context.TODO())
Expect(err).ToNot(HaveOccurred(), "failed to run manager")
}()
})

var _ = AfterSuite(func() {
Expand Down

0 comments on commit 1f41401

Please sign in to comment.