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