From 89bf4e4e0e249af6b9232aa2c84c9b539b38f9bb Mon Sep 17 00:00:00 2001 From: Howard Lau Date: Sun, 1 Nov 2020 09:24:13 +0000 Subject: [PATCH 1/4] Add tests Signed-off-by: Howard Lau --- controllers/seaweed_controller_test.go | 95 ++++++++++++++++++++++++++ controllers/suite_test.go | 18 +++++ 2 files changed, 113 insertions(+) create mode 100644 controllers/seaweed_controller_test.go diff --git a/controllers/seaweed_controller_test.go b/controllers/seaweed_controller_test.go new file mode 100644 index 0000000..da6492d --- /dev/null +++ b/controllers/seaweed_controller_test.go @@ -0,0 +1,95 @@ +package controllers + +import ( + "context" + "time" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + appsv1 "k8s.io/api/apps/v1" + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/resource" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" + + seaweedv1 "github.com/seaweedfs/seaweedfs-operator/api/v1" +) + +var _ = Describe("Seaweed Controller", func() { + Context("Basic Functionality", func() { + It("Should create StatefulSets", func() { + By("By creating a new Seaweed", func() { + const ( + namespace = "default" + name = "test-seaweed" + + timeout = time.Second * 10 + duration = time.Second * 10 + interval = time.Millisecond * 250 + ) + + ctx := context.Background() + seaweed := &seaweedv1.Seaweed{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: namespace, + Name: name, + }, + Spec: seaweedv1.SeaweedSpec{ + Image: "chrislusf/seaweedfs:2.07", + VolumeServerDiskCount: 1, + Master: &seaweedv1.MasterSpec{ + Replicas: 3, + }, + Volume: &seaweedv1.VolumeSpec{ + Replicas: 1, + ResourceRequirements: corev1.ResourceRequirements{ + Requests: corev1.ResourceList{ + corev1.ResourceStorage: resource.MustParse("1Gi"), + }, + }, + }, + Filer: &seaweedv1.FilerSpec{ + Replicas: 2, + }, + }, + } + Expect(k8sClient.Create(ctx, seaweed)).Should(Succeed()) + + masterKey := types.NamespacedName{Name: name + "-master", Namespace: namespace} + volumeKey := types.NamespacedName{Name: name + "-volume", Namespace: namespace} + filerKey := types.NamespacedName{Name: name + "-filer", Namespace: namespace} + + masterSts := &appsv1.StatefulSet{} + volumeSts := &appsv1.StatefulSet{} + filerSts := &appsv1.StatefulSet{} + + Eventually(func() bool { + err := k8sClient.Get(ctx, masterKey, masterSts) + if err != nil { + return false + } + return true + }, timeout, interval).Should(BeTrue()) + Expect(*masterSts.Spec.Replicas).Should(Equal(seaweed.Spec.Master.Replicas)) + + Eventually(func() bool { + err := k8sClient.Get(ctx, volumeKey, volumeSts) + if err != nil { + return false + } + return true + }, timeout, interval).Should(BeTrue()) + Expect(*volumeSts.Spec.Replicas).Should(Equal(seaweed.Spec.Volume.Replicas)) + + Eventually(func() bool { + err := k8sClient.Get(ctx, filerKey, filerSts) + if err != nil { + return false + } + return true + }, timeout, interval).Should(BeTrue()) + Expect(*filerSts.Spec.Replicas).Should(Equal(seaweed.Spec.Filer.Replicas)) + }) + }) + }) +}) diff --git a/controllers/suite_test.go b/controllers/suite_test.go index bd86b5a..cf09def 100644 --- a/controllers/suite_test.go +++ b/controllers/suite_test.go @@ -24,6 +24,7 @@ import ( . "github.com/onsi/gomega" "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" "sigs.k8s.io/controller-runtime/pkg/envtest/printer" @@ -68,6 +69,23 @@ var _ = BeforeSuite(func(done Done) { // +kubebuilder:scaffold:scheme k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) + k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{ + Scheme: scheme.Scheme, + }) + Expect(err).ToNot(HaveOccurred()) + + err = (&SeaweedReconciler{ + Client: k8sManager.GetClient(), + Log: ctrl.Log.WithName("controllers").WithName("Seaweed"), + Scheme: k8sManager.GetScheme(), + }).SetupWithManager(k8sManager) + Expect(err).ToNot(HaveOccurred()) + + go func() { + err = k8sManager.Start(ctrl.SetupSignalHandler()) + Expect(err).ToNot(HaveOccurred()) + }() + Expect(err).ToNot(HaveOccurred()) Expect(k8sClient).ToNot(BeNil()) From 08430619b9b660534004d3cd0b0c99f15183cdf8 Mon Sep 17 00:00:00 2001 From: Howard Lau Date: Sun, 1 Nov 2020 09:28:08 +0000 Subject: [PATCH 2/4] fix lint Signed-off-by: Howard Lau --- controllers/seaweed_controller_test.go | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/controllers/seaweed_controller_test.go b/controllers/seaweed_controller_test.go index da6492d..1e25379 100644 --- a/controllers/seaweed_controller_test.go +++ b/controllers/seaweed_controller_test.go @@ -65,28 +65,19 @@ var _ = Describe("Seaweed Controller", func() { Eventually(func() bool { err := k8sClient.Get(ctx, masterKey, masterSts) - if err != nil { - return false - } - return true + return err != nil }, timeout, interval).Should(BeTrue()) Expect(*masterSts.Spec.Replicas).Should(Equal(seaweed.Spec.Master.Replicas)) Eventually(func() bool { err := k8sClient.Get(ctx, volumeKey, volumeSts) - if err != nil { - return false - } - return true + return err != nil }, timeout, interval).Should(BeTrue()) Expect(*volumeSts.Spec.Replicas).Should(Equal(seaweed.Spec.Volume.Replicas)) Eventually(func() bool { err := k8sClient.Get(ctx, filerKey, filerSts) - if err != nil { - return false - } - return true + return err != nil }, timeout, interval).Should(BeTrue()) Expect(*filerSts.Spec.Replicas).Should(Equal(seaweed.Spec.Filer.Replicas)) }) From 152049b5cf7dc03cd040efcba83d7a013ea52681 Mon Sep 17 00:00:00 2001 From: Howard Lau Date: Sun, 1 Nov 2020 09:32:43 +0000 Subject: [PATCH 3/4] fix lint Signed-off-by: Howard Lau --- controllers/seaweed_controller_test.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/controllers/seaweed_controller_test.go b/controllers/seaweed_controller_test.go index 1e25379..4c1f631 100644 --- a/controllers/seaweed_controller_test.go +++ b/controllers/seaweed_controller_test.go @@ -23,8 +23,7 @@ var _ = Describe("Seaweed Controller", func() { namespace = "default" name = "test-seaweed" - timeout = time.Second * 10 - duration = time.Second * 10 + timeout = time.Second * 30 interval = time.Millisecond * 250 ) @@ -67,18 +66,21 @@ var _ = Describe("Seaweed Controller", func() { err := k8sClient.Get(ctx, masterKey, masterSts) return err != nil }, timeout, interval).Should(BeTrue()) + Expect(masterSts.Spec.Replicas).ShouldNot(BeNil()) Expect(*masterSts.Spec.Replicas).Should(Equal(seaweed.Spec.Master.Replicas)) Eventually(func() bool { err := k8sClient.Get(ctx, volumeKey, volumeSts) return err != nil }, timeout, interval).Should(BeTrue()) + Expect(volumeSts.Spec.Replicas).ShouldNot(BeNil()) Expect(*volumeSts.Spec.Replicas).Should(Equal(seaweed.Spec.Volume.Replicas)) Eventually(func() bool { err := k8sClient.Get(ctx, filerKey, filerSts) return err != nil }, timeout, interval).Should(BeTrue()) + Expect(filerSts.Spec.Replicas).ShouldNot(BeNil()) Expect(*filerSts.Spec.Replicas).Should(Equal(seaweed.Spec.Filer.Replicas)) }) }) From 647b74f909591329e428fcdf0b867bf23db67d95 Mon Sep 17 00:00:00 2001 From: Howard Lau Date: Sun, 1 Nov 2020 09:34:52 +0000 Subject: [PATCH 4/4] fix wrong if Signed-off-by: Howard Lau --- controllers/seaweed_controller_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/controllers/seaweed_controller_test.go b/controllers/seaweed_controller_test.go index 4c1f631..b13914b 100644 --- a/controllers/seaweed_controller_test.go +++ b/controllers/seaweed_controller_test.go @@ -64,21 +64,21 @@ var _ = Describe("Seaweed Controller", func() { Eventually(func() bool { err := k8sClient.Get(ctx, masterKey, masterSts) - return err != nil + return err == nil }, timeout, interval).Should(BeTrue()) Expect(masterSts.Spec.Replicas).ShouldNot(BeNil()) Expect(*masterSts.Spec.Replicas).Should(Equal(seaweed.Spec.Master.Replicas)) Eventually(func() bool { err := k8sClient.Get(ctx, volumeKey, volumeSts) - return err != nil + return err == nil }, timeout, interval).Should(BeTrue()) Expect(volumeSts.Spec.Replicas).ShouldNot(BeNil()) Expect(*volumeSts.Spec.Replicas).Should(Equal(seaweed.Spec.Volume.Replicas)) Eventually(func() bool { err := k8sClient.Get(ctx, filerKey, filerSts) - return err != nil + return err == nil }, timeout, interval).Should(BeTrue()) Expect(filerSts.Spec.Replicas).ShouldNot(BeNil()) Expect(*filerSts.Spec.Replicas).Should(Equal(seaweed.Spec.Filer.Replicas))