github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/dataprotection/backup/scheduler_test.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package backup
    21  
    22  import (
    23  	. "github.com/onsi/ginkgo/v2"
    24  	. "github.com/onsi/gomega"
    25  
    26  	"sigs.k8s.io/controller-runtime/pkg/client"
    27  
    28  	dpv1alpha1 "github.com/1aal/kubeblocks/apis/dataprotection/v1alpha1"
    29  	"github.com/1aal/kubeblocks/pkg/constant"
    30  	ctrlutil "github.com/1aal/kubeblocks/pkg/controllerutil"
    31  	"github.com/1aal/kubeblocks/pkg/generics"
    32  	testapps "github.com/1aal/kubeblocks/pkg/testutil/apps"
    33  	testdp "github.com/1aal/kubeblocks/pkg/testutil/dataprotection"
    34  )
    35  
    36  var _ = Describe("Scheduler Test", func() {
    37  	buildScheduler := func() *Scheduler {
    38  		return &Scheduler{
    39  			RequestCtx: ctrlutil.RequestCtx{
    40  				Log:      logger,
    41  				Ctx:      testCtx.Ctx,
    42  				Recorder: recorder,
    43  			},
    44  			Client: testCtx.Cli,
    45  		}
    46  	}
    47  
    48  	cleanEnv := func() {
    49  		// must wait till resources deleted and no longer existed before the testcases start,
    50  		// otherwise if later it needs to create some new resource objects with the same name,
    51  		// in race conditions, it will find the existence of old objects, resulting failure to
    52  		// create the new objects.
    53  		By("clean resources")
    54  
    55  		// delete rest mocked objects
    56  		inNS := client.InNamespace(testCtx.DefaultNamespace)
    57  
    58  		testapps.ClearResourcesWithRemoveFinalizerOption(&testCtx, generics.BackupPolicySignature, true, inNS)
    59  		testapps.ClearResourcesWithRemoveFinalizerOption(&testCtx, generics.BackupScheduleSignature, true, inNS)
    60  	}
    61  
    62  	BeforeEach(func() {
    63  		cleanEnv()
    64  	})
    65  
    66  	AfterEach(func() {
    67  		cleanEnv()
    68  	})
    69  
    70  	When("with default settings", func() {
    71  		var (
    72  			backupPolicy   *dpv1alpha1.BackupPolicy
    73  			backupSchedule *dpv1alpha1.BackupSchedule
    74  
    75  			scheduler *Scheduler
    76  		)
    77  
    78  		BeforeEach(func() {
    79  			backupPolicy = testdp.NewBackupPolicyFactory(testCtx.DefaultNamespace, testdp.BackupPolicyName).
    80  				SetBackupRepoName(testdp.BackupRepoName).
    81  				SetPathPrefix(testdp.BackupPathPrefix).
    82  				SetTarget(constant.AppInstanceLabelKey, testdp.ClusterName,
    83  					constant.KBAppComponentLabelKey, testdp.ComponentName,
    84  					constant.RoleLabelKey, constant.Leader).
    85  				AddBackupMethod(testdp.BackupMethodName, false, testdp.ActionSetName).
    86  				AddBackupMethod(testdp.VSBackupMethodName, true, "").
    87  				Create(&testCtx).GetObject()
    88  			backupSchedule = testdp.NewFakeBackupSchedule(&testCtx, nil)
    89  
    90  			scheduler = buildScheduler()
    91  		})
    92  
    93  		Context("test Schedule", func() {
    94  			It("should schedule", func() {
    95  				scheduler.BackupSchedule = backupSchedule
    96  				scheduler.BackupPolicy = backupPolicy
    97  				Expect(scheduler.Schedule()).Should(Succeed())
    98  			})
    99  
   100  			It("schedule should fail if invalid backup policy", func() {
   101  				scheduler.BackupSchedule = backupSchedule
   102  				scheduler.BackupPolicy = backupPolicy
   103  				for i := range scheduler.BackupPolicy.Spec.BackupMethods {
   104  					scheduler.BackupPolicy.Spec.BackupMethods[i].Name = "not-exist"
   105  				}
   106  				Expect(scheduler.Schedule()).ShouldNot(Succeed())
   107  			})
   108  		})
   109  	})
   110  })