github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/dataprotection/action/action_job_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 action_test
    21  
    22  import (
    23  	. "github.com/onsi/ginkgo/v2"
    24  	. "github.com/onsi/gomega"
    25  
    26  	batchv1 "k8s.io/api/batch/v1"
    27  	corev1 "k8s.io/api/core/v1"
    28  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    29  	"sigs.k8s.io/controller-runtime/pkg/client"
    30  
    31  	dpv1alpha1 "github.com/1aal/kubeblocks/apis/dataprotection/v1alpha1"
    32  	"github.com/1aal/kubeblocks/pkg/constant"
    33  	"github.com/1aal/kubeblocks/pkg/dataprotection/action"
    34  	"github.com/1aal/kubeblocks/pkg/generics"
    35  	testapps "github.com/1aal/kubeblocks/pkg/testutil/apps"
    36  	testdp "github.com/1aal/kubeblocks/pkg/testutil/dataprotection"
    37  	viper "github.com/1aal/kubeblocks/pkg/viperx"
    38  )
    39  
    40  var _ = Describe("JobAction Test", func() {
    41  	const (
    42  		actionName = "test-job-action"
    43  		container  = "container"
    44  	)
    45  
    46  	var (
    47  		command = []string{"ls"}
    48  	)
    49  
    50  	cleanEnv := func() {
    51  		By("clean resources")
    52  		inNS := client.InNamespace(testCtx.DefaultNamespace)
    53  		testapps.ClearResourcesWithRemoveFinalizerOption(&testCtx, generics.JobSignature, true, inNS)
    54  		testapps.ClearResourcesWithRemoveFinalizerOption(&testCtx, generics.BackupSignature, true, inNS)
    55  	}
    56  
    57  	BeforeEach(func() {
    58  		cleanEnv()
    59  		viper.Set(constant.KBToolsImage, testdp.KBToolImage)
    60  	})
    61  
    62  	AfterEach(func() {
    63  		cleanEnv()
    64  		viper.Set(constant.KBToolsImage, "")
    65  	})
    66  
    67  	Context("create job action", func() {
    68  		It("should return error when pod spec is empty", func() {
    69  			act := &action.JobAction{}
    70  			status, err := act.Execute(buildActionCtx())
    71  			Expect(err).To(HaveOccurred())
    72  			Expect(status.Phase).Should(Equal(dpv1alpha1.ActionPhaseFailed))
    73  		})
    74  
    75  		It("should success to execute job action", func() {
    76  			labels := map[string]string{
    77  				"dp-test-action": actionName,
    78  			}
    79  
    80  			act := &action.JobAction{
    81  				Name: actionName,
    82  				ObjectMeta: metav1.ObjectMeta{
    83  					Name:      actionName,
    84  					Namespace: testCtx.DefaultNamespace,
    85  					Labels:    labels,
    86  				},
    87  				PodSpec: &corev1.PodSpec{
    88  					Containers: []corev1.Container{
    89  						{
    90  							Name:    container,
    91  							Image:   testdp.KBToolImage,
    92  							Command: command,
    93  						},
    94  					},
    95  					RestartPolicy: corev1.RestartPolicyNever,
    96  				},
    97  				Owner: testdp.NewFakeBackup(&testCtx, nil),
    98  			}
    99  
   100  			By("should success to execute")
   101  			status, err := act.Execute(buildActionCtx())
   102  			Expect(err).Should(Succeed())
   103  			Expect(status).ShouldNot(BeNil())
   104  			Expect(status.Phase).Should(Equal(dpv1alpha1.ActionPhaseRunning))
   105  
   106  			By("check the job was created")
   107  			job := &batchv1.Job{}
   108  			key := client.ObjectKey{Name: actionName, Namespace: testCtx.DefaultNamespace}
   109  			Eventually(testapps.CheckObjExists(&testCtx, key, job, true)).Should(Succeed())
   110  
   111  			By("set job status to complete")
   112  			testdp.PatchK8sJobStatus(&testCtx, client.ObjectKeyFromObject(job), batchv1.JobComplete)
   113  
   114  			By("action status should be completed")
   115  			status, err = act.Execute(buildActionCtx())
   116  			Expect(err).ShouldNot(HaveOccurred())
   117  			Expect(status.Phase).Should(Equal(dpv1alpha1.ActionPhaseCompleted))
   118  		})
   119  	})
   120  })