From 393465300a25a19ce3e8ae366fe63160926559ce Mon Sep 17 00:00:00 2001 From: Jean-Marc Andre Date: Tue, 9 Mar 2021 17:50:41 +0100 Subject: [PATCH] BackupConfiguration initial test suite --- .../backupconfiguration_controller_test.go | 299 ++++++++---------- controllers/suite_test.go | 95 ++++++ 2 files changed, 222 insertions(+), 172 deletions(-) diff --git a/controllers/backupconfiguration_controller_test.go b/controllers/backupconfiguration_controller_test.go index 93200bb..be5f24f 100644 --- a/controllers/backupconfiguration_controller_test.go +++ b/controllers/backupconfiguration_controller_test.go @@ -5,7 +5,7 @@ import ( //"k8s.io/apimachinery/pkg/types" //"reflect" //"fmt" - "time" + //"time" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -19,86 +19,24 @@ import ( "k8s.io/apimachinery/pkg/types" ) -var _ = Describe("Setup the environment", func() { +var _ = Describe("Testing BackupConf controller", func() { const ( - BackupConfName = "test-backupconf" - BackupConfNamespace = "test-backupconf-namespace" - RepoName = "test-repo" - DeploymentName = "test-deployment" - timeout = time.Second * 10 - interval = time.Millisecond * 250 + BackupConfName = "test-backupconf" ) var ( key = types.NamespacedName{ Name: BackupConfName, - Namespace: BackupConfNamespace, - } - ctx = context.Background() - namespace = &corev1.Namespace{ - ObjectMeta: metav1.ObjectMeta{ - Name: BackupConfNamespace, - }, - } - deployment = &appsv1.Deployment{ - ObjectMeta: metav1.ObjectMeta{ - Name: DeploymentName, - Namespace: BackupConfNamespace, - }, - Spec: appsv1.DeploymentSpec{ - Selector: &metav1.LabelSelector{ - MatchLabels: map[string]string{"app": "test-deployment"}, - }, - Template: corev1.PodTemplateSpec{ - ObjectMeta: metav1.ObjectMeta{ - Labels: map[string]string{"app": "test-deployment"}, - }, - Spec: corev1.PodSpec{ - Containers: []corev1.Container{ - corev1.Container{ - Name: "test-container", - Image: "test-image", - }, - }, - }, - }, - }, - } - sa = &corev1.ServiceAccount{ - ObjectMeta: metav1.ObjectMeta{ - Name: "default", - Namespace: BackupConfNamespace, - }, - } - secret = &corev1.Secret{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-secret", - Namespace: BackupConfNamespace, - }, - Data: map[string][]byte{ - "RESTIC_PASSWORD": []byte("toto"), - "AWS_ACCESS_KEY_ID": []byte("titi"), - "AWS_SECRET_ACCESS_KEY": []byte("tata"), - }, - } - repo = &formolv1alpha1.Repo{ - ObjectMeta: metav1.ObjectMeta{ - Name: RepoName, - Namespace: BackupConfNamespace, - }, - Spec: formolv1alpha1.RepoSpec{ - Backend: formolv1alpha1.Backend{ - S3: formolv1alpha1.S3{ - Server: "raid5.desmojim.fr:9000", - Bucket: "testbucket2", - }, - }, - RepositorySecrets: "test-secret", - }, + Namespace: TestNamespace, } + ctx = context.Background() + backupConf = &formolv1alpha1.BackupConfiguration{} + ) + + BeforeEach(func() { backupConf = &formolv1alpha1.BackupConfiguration{ ObjectMeta: metav1.ObjectMeta{ Name: BackupConfName, - Namespace: BackupConfNamespace, + Namespace: TestNamespace, }, Spec: formolv1alpha1.BackupConfigurationSpec{ Repository: RepoName, @@ -108,112 +46,129 @@ var _ = Describe("Setup the environment", func() { Kind: "Deployment", Name: DeploymentName, }, + formolv1alpha1.Target{ + Kind: "Task", + Name: BackupFuncName, + Steps: []formolv1alpha1.Step{ + formolv1alpha1.Step{ + Name: BackupFuncName, + Namespace: TestNamespace, + Env: []corev1.EnvVar{ + corev1.EnvVar{ + Name: "foo", + Value: "bar", + }, + }, + }, + }, + }, }, }, } - ) - - BeforeEach(func() { }) - AfterEach(func() { + Context("There is a backupconf", func() { + JustBeforeEach(func() { + Eventually(func() error { + return k8sClient.Create(ctx, backupConf) + }, timeout, interval).Should(Succeed()) + }) + AfterEach(func() { + Expect(k8sClient.Delete(ctx, backupConf)).Should(Succeed()) + }) + It("Has a schedule", func() { + realBackupConf := &formolv1alpha1.BackupConfiguration{} + Eventually(func() bool { + err := k8sClient.Get(ctx, key, realBackupConf) + if err != nil { + return false + } + return true + }, timeout, interval).Should(BeTrue()) + Expect(realBackupConf.Spec.Schedule).Should(Equal("1 * * * *")) + }) + It("Should also create a CronJob", func() { + cronJob := &batchv1beta1.CronJob{} + Eventually(func() bool { + err := k8sClient.Get(ctx, types.NamespacedName{ + Name: "backup-" + BackupConfName, + Namespace: TestNamespace, + }, cronJob) + if err != nil { + return false + } + return true + }, timeout, interval).Should(BeTrue()) + Expect(cronJob.Spec.Schedule).Should(Equal("1 * * * *")) + }) + It("Should also create a sidecar container", func() { + realDeployment := &appsv1.Deployment{} + Eventually(func() (int, error) { + err := k8sClient.Get(ctx, types.NamespacedName{ + Name: DeploymentName, + Namespace: TestNamespace, + }, realDeployment) + if err != nil { + return 0, err + } + return len(realDeployment.Spec.Template.Spec.Containers), nil + }, timeout, interval).Should(Equal(2)) + }) + It("Should also update the CronJob", func() { + realBackupConf := &formolv1alpha1.BackupConfiguration{} + Eventually(func() bool { + err := k8sClient.Get(ctx, key, realBackupConf) + if err != nil { + return false + } + return true + }, timeout, interval).Should(BeTrue()) + realBackupConf.Spec.Schedule = "1 0 * * *" + suspend := true + realBackupConf.Spec.Suspend = &suspend + Expect(k8sClient.Update(ctx, realBackupConf)).Should(Succeed()) + cronJob := &batchv1beta1.CronJob{} + Eventually(func() (string, error) { + err := k8sClient.Get(ctx, types.NamespacedName{ + Name: "backup-" + BackupConfName, + Namespace: TestNamespace, + }, cronJob) + if err != nil { + return "", err + } + return cronJob.Spec.Schedule, nil + }, timeout, interval).Should(Equal("1 0 * * *")) + Eventually(func() (bool, error) { + err := k8sClient.Get(ctx, types.NamespacedName{ + Name: "backup-" + BackupConfName, + Namespace: TestNamespace, + }, cronJob) + if err != nil { + return false, err + } + return *cronJob.Spec.Suspend == true, nil + }, timeout, interval).Should(BeTrue()) + }) }) + Context("Deleting a backupconf", func() { + JustBeforeEach(func() { + Eventually(func() error { + return k8sClient.Create(ctx, backupConf) + }, timeout, interval).Should(Succeed()) + }) + It("Should also delete the sidecar container", func() { + Expect(k8sClient.Delete(ctx, backupConf)).Should(Succeed()) + realDeployment := &appsv1.Deployment{} + Eventually(func() (int, error) { + err := k8sClient.Get(ctx, types.NamespacedName{ + Name: DeploymentName, + Namespace: TestNamespace, + }, realDeployment) + if err != nil { + return 0, err + } + return len(realDeployment.Spec.Template.Spec.Containers), nil + }, timeout, interval).Should(Equal(1)) - Context("Testing backupconf", func() { - It("Requires initialisation", func() { - Expect(k8sClient.Create(ctx, namespace)).Should(Succeed()) - Expect(k8sClient.Create(ctx, sa)).Should(Succeed()) - Expect(k8sClient.Create(ctx, secret)).Should(Succeed()) - Expect(k8sClient.Create(ctx, repo)).Should(Succeed()) - Expect(k8sClient.Create(ctx, deployment)).Should(Succeed()) - }) - Context("Creating a backupconf", func() { - It("Requires initialisation", func() { - Expect(k8sClient.Create(ctx, backupConf)).Should(Succeed()) - realBackupConf := &formolv1alpha1.BackupConfiguration{} - Eventually(func() bool { - err := k8sClient.Get(ctx, key, realBackupConf) - if err != nil { - return false - } - return true - }, timeout, interval).Should(BeTrue()) - Expect(realBackupConf.Spec.Schedule).Should(Equal("1 * * * *")) - }) - It("Should also create a CronJob", func() { - cronJob := &batchv1beta1.CronJob{} - Eventually(func() bool { - err := k8sClient.Get(ctx, types.NamespacedName{ - Name: "backup-" + BackupConfName, - Namespace: BackupConfNamespace, - }, cronJob) - if err != nil { - return false - } - return true - }, timeout, interval).Should(BeTrue()) - Expect(cronJob.Spec.Schedule).Should(Equal("1 * * * *")) - }) - It("Should also create a sidecar container", func() { - realDeployment := &appsv1.Deployment{} - Eventually(func() (int, error) { - err := k8sClient.Get(ctx, types.NamespacedName{ - Name: DeploymentName, - Namespace: BackupConfNamespace, - }, realDeployment) - if err != nil { - return 0, err - } - return len(realDeployment.Spec.Template.Spec.Containers), nil - }, timeout, interval).Should(Equal(2)) - }) - }) - Context("Updating a backupconf", func() { - It("Should also update the CronJob", func() { - realBackupConf := &formolv1alpha1.BackupConfiguration{} - Expect(k8sClient.Get(ctx, key, realBackupConf)).Should(Succeed()) - realBackupConf.Spec.Schedule = "1 0 * * *" - suspend := true - realBackupConf.Spec.Suspend = &suspend - Expect(k8sClient.Update(ctx, realBackupConf)).Should(Succeed()) - cronJob := &batchv1beta1.CronJob{} - Eventually(func() (string, error) { - err := k8sClient.Get(ctx, types.NamespacedName{ - Name: "backup-" + BackupConfName, - Namespace: BackupConfNamespace, - }, cronJob) - if err != nil { - return "", err - } - return cronJob.Spec.Schedule, nil - }, timeout, interval).Should(Equal("1 0 * * *")) - Eventually(func() (bool, error) { - err := k8sClient.Get(ctx, types.NamespacedName{ - Name: "backup-" + BackupConfName, - Namespace: BackupConfNamespace, - }, cronJob) - if err != nil { - return false, err - } - return *cronJob.Spec.Suspend == true, nil - }, timeout, interval).Should(BeTrue()) - }) - }) - Context("Deleting a backupconf", func() { - It("Should also delete the sidecar container", func() { - Expect(k8sClient.Delete(ctx, backupConf)).Should(Succeed()) - realDeployment := &appsv1.Deployment{} - Eventually(func() (int, error) { - err := k8sClient.Get(ctx, types.NamespacedName{ - Name: DeploymentName, - Namespace: BackupConfNamespace, - }, realDeployment) - if err != nil { - return 0, err - } - return len(realDeployment.Spec.Template.Spec.Containers), nil - }, timeout, interval).Should(Equal(1)) - - }) }) }) diff --git a/controllers/suite_test.go b/controllers/suite_test.go index 4a9bb00..520f310 100644 --- a/controllers/suite_test.go +++ b/controllers/suite_test.go @@ -17,12 +17,17 @@ limitations under the License. package controllers import ( + "context" "fmt" "path/filepath" "testing" + "time" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + appsv1 "k8s.io/api/apps/v1" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/rest" ctrl "sigs.k8s.io/controller-runtime" @@ -38,11 +43,94 @@ import ( // These tests use Ginkgo (BDD-style Go testing framework). Refer to // http://onsi.github.io/ginkgo/ to learn more about Ginkgo. +const ( + BackupFuncName = "test-backup-func" + TestNamespace = "test-namespace" + RepoName = "test-repo" + DeploymentName = "test-deployment" + timeout = time.Second * 10 + interval = time.Millisecond * 250 +) var cfg *rest.Config var k8sClient client.Client var testEnv *envtest.Environment +var ( + namespace = &corev1.Namespace{ + ObjectMeta: metav1.ObjectMeta{ + Name: TestNamespace, + }, + } + deployment = &appsv1.Deployment{ + ObjectMeta: metav1.ObjectMeta{ + Name: DeploymentName, + Namespace: TestNamespace, + }, + Spec: appsv1.DeploymentSpec{ + Selector: &metav1.LabelSelector{ + MatchLabels: map[string]string{"app": "test-deployment"}, + }, + Template: corev1.PodTemplateSpec{ + ObjectMeta: metav1.ObjectMeta{ + Labels: map[string]string{"app": "test-deployment"}, + }, + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + corev1.Container{ + Name: "test-container", + Image: "test-image", + }, + }, + }, + }, + }, + } + sa = &corev1.ServiceAccount{ + ObjectMeta: metav1.ObjectMeta{ + Name: "default", + Namespace: TestNamespace, + }, + } + secret = &corev1.Secret{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-secret", + Namespace: TestNamespace, + }, + Data: map[string][]byte{ + "RESTIC_PASSWORD": []byte("toto"), + "AWS_ACCESS_KEY_ID": []byte("titi"), + "AWS_SECRET_ACCESS_KEY": []byte("tata"), + }, + } + repo = &formolv1alpha1.Repo{ + ObjectMeta: metav1.ObjectMeta{ + Name: RepoName, + Namespace: TestNamespace, + }, + Spec: formolv1alpha1.RepoSpec{ + Backend: formolv1alpha1.Backend{ + S3: formolv1alpha1.S3{ + Server: "raid5.desmojim.fr:9000", + Bucket: "testbucket2", + }, + }, + RepositorySecrets: "test-secret", + }, + } + function = &formolv1alpha1.Function{ + ObjectMeta: metav1.ObjectMeta{ + Name: BackupFuncName, + Namespace: TestNamespace, + }, + Spec: corev1.Container{ + Name: "backup-func", + Image: "myimage", + Args: []string{"a", "set", "of", "args"}, + }, + } +) + func TestAPIs(t *testing.T) { RegisterFailHandler(Fail) @@ -86,7 +174,14 @@ var _ = BeforeSuite(func() { }() k8sClient = k8sManager.GetClient() + ctx := context.Background() Expect(k8sClient).ToNot(BeNil()) + Expect(k8sClient.Create(ctx, namespace)).Should(Succeed()) + Expect(k8sClient.Create(ctx, sa)).Should(Succeed()) + Expect(k8sClient.Create(ctx, secret)).Should(Succeed()) + Expect(k8sClient.Create(ctx, repo)).Should(Succeed()) + Expect(k8sClient.Create(ctx, deployment)).Should(Succeed()) + Expect(k8sClient.Create(ctx, function)).Should(Succeed()) }, 60) var _ = AfterSuite(func() {