Skip to content

Commit

Permalink
fix panic caused by nil logger in tests
Browse files Browse the repository at this point in the history
Signed-off-by: Artem Bortnikov <[email protected]>
  • Loading branch information
aobort committed May 28, 2024
1 parent 3d1c4dc commit 62bac86
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 33 deletions.
8 changes: 4 additions & 4 deletions internal/controller/etcdcluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var _ = Describe("EtcdCluster Controller", func() {
ns *corev1.Namespace
)

BeforeEach(func(ctx SpecContext) {
BeforeEach(func() {
reconciler = &EtcdClusterReconciler{
Client: k8sClient,
Scheme: k8sClient.Scheme(),
Expand All @@ -63,7 +63,7 @@ var _ = Describe("EtcdCluster Controller", func() {
err error
)

BeforeEach(func(ctx SpecContext) {
BeforeEach(func() {
etcdcluster = etcdaenixiov1alpha1.EtcdCluster{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-etcdcluster-",
Expand Down Expand Up @@ -110,7 +110,7 @@ var _ = Describe("EtcdCluster Controller", func() {
DeferCleanup(k8sClient.Delete, &statefulSet)
})

It("should reconcile a new EtcdCluster", func(ctx SpecContext) {
It("should reconcile a new EtcdCluster", func() {
By("reconciling the EtcdCluster", func() {
_, err = reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(&etcdcluster)})
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -142,7 +142,7 @@ var _ = Describe("EtcdCluster Controller", func() {
})
})

It("should successfully reconcile the resource twice and mark as ready", func(ctx SpecContext) {
It("should successfully reconcile the resource twice and mark as ready", func() {
By("reconciling the EtcdCluster", func() {
_, err = reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(&etcdcluster)})
Expect(err).ToNot(HaveOccurred())
Expand Down
3 changes: 3 additions & 0 deletions internal/controller/factory/builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ import (
)

func contextWithGVK(ctx context.Context, resource client.Object, scheme *runtime.Scheme) (context.Context, error) {
if resource == nil {
return nil, fmt.Errorf("resource cannot be nil")
}
gvk, err := apiutil.GVKForObject(resource, scheme)
if err != nil {
return nil, fmt.Errorf("failed to get GVK: %w", err)
Expand Down
10 changes: 5 additions & 5 deletions internal/controller/factory/configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
var _ = Describe("CreateOrUpdateClusterStateConfigMap handlers", func() {
var ns *corev1.Namespace

BeforeEach(func(ctx SpecContext) {
BeforeEach(func() {
ns = &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-",
Expand All @@ -52,7 +52,7 @@ var _ = Describe("CreateOrUpdateClusterStateConfigMap handlers", func() {
err error
)

BeforeEach(func(ctx SpecContext) {
BeforeEach(func() {
etcdcluster = etcdaenixiov1alpha1.EtcdCluster{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-etcdcluster-",
Expand All @@ -75,7 +75,7 @@ var _ = Describe("CreateOrUpdateClusterStateConfigMap handlers", func() {
}
})

AfterEach(func(ctx SpecContext) {
AfterEach(func() {
err = Get(&configMap)()
if err == nil {
Expect(k8sClient.Delete(ctx, &configMap)).Should(Succeed())
Expand All @@ -84,7 +84,7 @@ var _ = Describe("CreateOrUpdateClusterStateConfigMap handlers", func() {
}
})

It("should successfully ensure the configmap", func(ctx SpecContext) {
It("should successfully ensure the configmap", func() {
var configMapUID types.UID
By("processing new etcd cluster", func() {
Expect(CreateOrUpdateClusterStateConfigMap(ctx, &etcdcluster, k8sClient)).To(Succeed())
Expand Down Expand Up @@ -114,7 +114,7 @@ var _ = Describe("CreateOrUpdateClusterStateConfigMap handlers", func() {
})
})

It("should fail to create the configmap with invalid owner reference", func(ctx SpecContext) {
It("should fail to create the configmap with invalid owner reference", func() {
Expect(CreateOrUpdateClusterStateConfigMap(ctx, &etcdcluster, clientWithEmptyScheme)).NotTo(Succeed())
})
})
Expand Down
14 changes: 7 additions & 7 deletions internal/controller/factory/pdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
var _ = Describe("CreateOrUpdatePdb handlers", func() {
var ns *corev1.Namespace

BeforeEach(func(ctx SpecContext) {
BeforeEach(func() {
ns = &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-",
Expand All @@ -52,7 +52,7 @@ var _ = Describe("CreateOrUpdatePdb handlers", func() {
err error
)

BeforeEach(func(ctx SpecContext) {
BeforeEach(func() {
etcdcluster = etcdaenixiov1alpha1.EtcdCluster{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-etcdcluster-",
Expand All @@ -76,7 +76,7 @@ var _ = Describe("CreateOrUpdatePdb handlers", func() {
}
})

AfterEach(func(ctx SpecContext) {
AfterEach(func() {
err = Get(&podDisruptionBudget)()
if err == nil {
Expect(k8sClient.Delete(ctx, &podDisruptionBudget)).Should(Succeed())
Expand All @@ -85,7 +85,7 @@ var _ = Describe("CreateOrUpdatePdb handlers", func() {
}
})

It("should create PDB with pre-filled data", func(ctx SpecContext) {
It("should create PDB with pre-filled data", func() {
etcdcluster.Spec.PodDisruptionBudgetTemplate.Spec.MinAvailable = ptr.To(intstr.FromInt32(int32(3)))
Expect(CreateOrUpdatePdb(ctx, &etcdcluster, k8sClient)).To(Succeed())
Eventually(Get(&podDisruptionBudget)).Should(Succeed())
Expand All @@ -94,20 +94,20 @@ var _ = Describe("CreateOrUpdatePdb handlers", func() {
Expect(podDisruptionBudget.Spec.MaxUnavailable).To(BeNil())
})

It("should create PDB with empty data", func(ctx SpecContext) {
It("should create PDB with empty data", func() {
Expect(CreateOrUpdatePdb(ctx, &etcdcluster, k8sClient)).To(Succeed())
Eventually(Get(&podDisruptionBudget)).Should(Succeed())
Expect(etcdcluster.Spec.PodDisruptionBudgetTemplate.Spec.MinAvailable).To(BeNil())
Expect(podDisruptionBudget.Spec.MinAvailable.IntValue()).To(Equal(2))
Expect(podDisruptionBudget.Spec.MaxUnavailable).To(BeNil())
})

It("should skip deletion of PDB if not filled and not exist", func(ctx SpecContext) {
It("should skip deletion of PDB if not filled and not exist", func() {
etcdcluster.Spec.PodDisruptionBudgetTemplate = nil
Expect(CreateOrUpdatePdb(ctx, &etcdcluster, k8sClient)).NotTo(HaveOccurred())
})

It("should delete created PDB after updating CR", func(ctx SpecContext) {
It("should delete created PDB after updating CR", func() {
Expect(CreateOrUpdatePdb(ctx, &etcdcluster, k8sClient)).To(Succeed())
Eventually(Get(&podDisruptionBudget)).Should(Succeed())
etcdcluster.Spec.PodDisruptionBudgetTemplate = nil
Expand Down
16 changes: 8 additions & 8 deletions internal/controller/factory/statefulset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (
var _ = Describe("CreateOrUpdateStatefulSet handler", func() {
var ns *corev1.Namespace

BeforeEach(func(ctx SpecContext) {
BeforeEach(func() {
ns = &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-",
Expand All @@ -55,7 +55,7 @@ var _ = Describe("CreateOrUpdateStatefulSet handler", func() {
err error
)

BeforeEach(func(ctx SpecContext) {
BeforeEach(func() {
etcdcluster = etcdaenixiov1alpha1.EtcdCluster{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-etcdcluster-",
Expand All @@ -78,7 +78,7 @@ var _ = Describe("CreateOrUpdateStatefulSet handler", func() {
}
})

AfterEach(func(ctx SpecContext) {
AfterEach(func() {
err = Get(&statefulSet)()
if err == nil {
Expect(k8sClient.Delete(ctx, &statefulSet)).Should(Succeed())
Expand All @@ -87,14 +87,14 @@ var _ = Describe("CreateOrUpdateStatefulSet handler", func() {
}
})

It("should successfully ensure the statefulSet with empty spec", func(ctx SpecContext) {
It("should successfully ensure the statefulSet with empty spec", func() {
Expect(CreateOrUpdateStatefulSet(ctx, &etcdcluster, k8sClient)).To(Succeed())
Eventually(Object(&statefulSet)).Should(
HaveField("Spec.Replicas", Equal(etcdcluster.Spec.Replicas)),
)
})

It("should successfully ensure the statefulSet with filled spec", func(ctx SpecContext) {
It("should successfully ensure the statefulSet with filled spec", func() {
etcdcluster.Spec.Storage = etcdaenixiov1alpha1.StorageSpec{
VolumeClaimTemplate: etcdaenixiov1alpha1.EmbeddedPersistentVolumeClaim{
EmbeddedObjectMetadata: etcdaenixiov1alpha1.EmbeddedObjectMetadata{
Expand Down Expand Up @@ -276,7 +276,7 @@ var _ = Describe("CreateOrUpdateStatefulSet handler", func() {
})
})

It("should successfully override probes", func(ctx SpecContext) {
It("should successfully override probes", func() {
etcdcluster.Spec.PodTemplate.Spec = corev1.PodSpec{
Containers: []corev1.Container{
{
Expand Down Expand Up @@ -353,7 +353,7 @@ var _ = Describe("CreateOrUpdateStatefulSet handler", func() {
})
})

It("should successfully create statefulSet with emptyDir", func(ctx SpecContext) {
It("should successfully create statefulSet with emptyDir", func() {
size := resource.MustParse("1Gi")
etcdcluster.Spec.Storage = etcdaenixiov1alpha1.StorageSpec{
EmptyDir: &corev1.EmptyDirVolumeSource{
Expand All @@ -368,7 +368,7 @@ var _ = Describe("CreateOrUpdateStatefulSet handler", func() {
})
})

It("should fail on creating the statefulset with invalid owner reference", func(ctx SpecContext) {
It("should fail on creating the statefulset with invalid owner reference", func() {
Expect(CreateOrUpdateStatefulSet(ctx, &etcdcluster, clientWithEmptyScheme)).NotTo(Succeed())
})
})
Expand Down
10 changes: 10 additions & 0 deletions internal/controller/factory/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ limitations under the License.
package factory

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

"github.com/aenix-io/etcd-operator/internal/log"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
kruntime "k8s.io/apimachinery/pkg/runtime"
Expand All @@ -45,6 +47,13 @@ var cfg *rest.Config
var k8sClient, clientWithEmptyScheme client.Client
var testEnv *envtest.Environment

// global context for test suites
var ctx = log.Setup(context.TODO(), log.Parameters{
LogLevel: "debug",
StacktraceLevel: "error",
Development: true,
})

func TestFactories(t *testing.T) {
RegisterFailHandler(Fail)

Expand Down Expand Up @@ -87,6 +96,7 @@ var _ = BeforeSuite(func() {
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())
SetClient(k8sClient)
SetContext(ctx)
})

var _ = AfterSuite(func() {
Expand Down
18 changes: 9 additions & 9 deletions internal/controller/factory/svc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
var _ = Describe("CreateOrUpdateService handlers", func() {
var ns *corev1.Namespace

BeforeEach(func(ctx SpecContext) {
BeforeEach(func() {
ns = &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-",
Expand All @@ -53,7 +53,7 @@ var _ = Describe("CreateOrUpdateService handlers", func() {
err error
)

BeforeEach(func(ctx SpecContext) {
BeforeEach(func() {
etcdcluster = etcdaenixiov1alpha1.EtcdCluster{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-etcdcluster-",
Expand Down Expand Up @@ -82,7 +82,7 @@ var _ = Describe("CreateOrUpdateService handlers", func() {
}
})

AfterEach(func(ctx SpecContext) {
AfterEach(func() {
err = Get(&headlessService)()
if err == nil {
Expect(k8sClient.Delete(ctx, &headlessService)).Should(Succeed())
Expand All @@ -97,15 +97,15 @@ var _ = Describe("CreateOrUpdateService handlers", func() {
}
})

It("should successfully ensure headless service", func(ctx SpecContext) {
It("should successfully ensure headless service", func() {
Expect(CreateOrUpdateHeadlessService(ctx, &etcdcluster, k8sClient)).To(Succeed())
Eventually(Object(&headlessService)).Should(SatisfyAll(
HaveField("Spec.Type", Equal(corev1.ServiceTypeClusterIP)),
HaveField("Spec.ClusterIP", Equal(corev1.ClusterIPNone)),
))
})

It("should successfully ensure headless service with custom metadata", func(ctx SpecContext) {
It("should successfully ensure headless service with custom metadata", func() {
etcdcluster.Spec.HeadlessServiceTemplate = &etcdaenixiov1alpha1.EmbeddedMetadataResource{
EmbeddedObjectMetadata: etcdaenixiov1alpha1.EmbeddedObjectMetadata{
Name: "headless-name",
Expand All @@ -129,15 +129,15 @@ var _ = Describe("CreateOrUpdateService handlers", func() {
Expect(k8sClient.Delete(ctx, svc)).Should(Succeed())
})

It("should successfully ensure client service", func(ctx SpecContext) {
It("should successfully ensure client service", func() {
Expect(CreateOrUpdateClientService(ctx, &etcdcluster, k8sClient)).To(Succeed())
Eventually(Object(&clientService)).Should(SatisfyAll(
HaveField("Spec.Type", Equal(corev1.ServiceTypeClusterIP)),
HaveField("Spec.ClusterIP", Not(Equal(corev1.ClusterIPNone))),
))
})

It("should successfully ensure client service with custom metadata", func(ctx SpecContext) {
It("should successfully ensure client service with custom metadata", func() {
etcdcluster.Spec.ServiceTemplate = &etcdaenixiov1alpha1.EmbeddedService{
EmbeddedObjectMetadata: etcdaenixiov1alpha1.EmbeddedObjectMetadata{
Name: "client-name",
Expand All @@ -161,7 +161,7 @@ var _ = Describe("CreateOrUpdateService handlers", func() {
Expect(k8sClient.Delete(ctx, svc)).Should(Succeed())
})

It("should successfully ensure client service with custom spec", func(ctx SpecContext) {
It("should successfully ensure client service with custom spec", func() {
etcdcluster.Spec.ServiceTemplate = &etcdaenixiov1alpha1.EmbeddedService{
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeLoadBalancer,
Expand Down Expand Up @@ -191,7 +191,7 @@ var _ = Describe("CreateOrUpdateService handlers", func() {
))
})

It("should fail on creating the client service with invalid owner reference", func(ctx SpecContext) {
It("should fail on creating the client service with invalid owner reference", func() {
Expect(CreateOrUpdateHeadlessService(ctx, &etcdcluster, clientWithEmptyScheme)).NotTo(Succeed())
Expect(CreateOrUpdateClientService(ctx, &etcdcluster, clientWithEmptyScheme)).NotTo(Succeed())
})
Expand Down
10 changes: 10 additions & 0 deletions internal/controller/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ limitations under the License.
package controller

import (
"context"
"fmt"
"path/filepath"
"runtime"
"testing"
"time"

"github.com/aenix-io/etcd-operator/internal/log"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "sigs.k8s.io/controller-runtime/pkg/envtest/komega"
Expand All @@ -45,6 +47,13 @@ var cfg *rest.Config
var k8sClient client.Client
var testEnv *envtest.Environment

// global context for test suites
var ctx = log.Setup(context.TODO(), log.Parameters{
LogLevel: "debug",
StacktraceLevel: "error",
Development: true,
})

func TestControllers(t *testing.T) {
SetDefaultEventuallyTimeout(time.Second * 5)
RegisterFailHandler(Fail)
Expand Down Expand Up @@ -85,6 +94,7 @@ var _ = BeforeSuite(func() {
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())
SetClient(k8sClient)
SetContext(ctx)
})

var _ = AfterSuite(func() {
Expand Down

0 comments on commit 62bac86

Please sign in to comment.