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 <artem.bortnikov@telekom.com>
  • Loading branch information
aobort committed Jun 5, 2024
1 parent ff275f6 commit a8c04d2
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 67 deletions.
36 changes: 24 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ module github.com/aenix-io/etcd-operator
go 1.22.3

require (
github.com/go-logr/logr v1.4.1
github.com/google/uuid v1.6.0
github.com/onsi/ginkgo/v2 v2.19.0
github.com/onsi/gomega v1.33.1
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.19.0
go.etcd.io/etcd/client/v3 v3.5.14
go.uber.org/zap v1.26.0
go.uber.org/zap/exp v0.2.0
k8s.io/api v0.30.1
k8s.io/apimachinery v0.30.1
k8s.io/client-go v0.30.1
Expand All @@ -19,11 +24,10 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/coreos/go-semver v0.3.1 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/zapr v1.3.0 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
Expand All @@ -36,40 +40,48 @@ require (
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20240424215950-a892ee059fd6 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/imdario/mergo v0.3.6 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.18.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.etcd.io/etcd/api/v3 v3.5.14 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.14 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/oauth2 v0.12.0 // indirect
golang.org/x/oauth2 v0.18.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/term v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.21.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230822172742-b8732ec3820d // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240311132316-a219d84964c2 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240314234333-6e1732d8331c // indirect
google.golang.org/grpc v1.62.1 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiextensions-apiserver v0.30.1 // indirect
Expand Down
91 changes: 69 additions & 22 deletions go.sum

Large diffs are not rendered by default.

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
Loading

0 comments on commit a8c04d2

Please sign in to comment.