github.com/oam-dev/kubevela@v1.9.11/references/cli/uninstall_test.go (about) 1 /* 2 Copyright 2021 The KubeVela Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package cli 18 19 import ( 20 "context" 21 "fmt" 22 "os" 23 "strings" 24 "testing" 25 "time" 26 27 . "github.com/onsi/ginkgo/v2" 28 . "github.com/onsi/gomega" 29 "github.com/stretchr/testify/assert" 30 "sigs.k8s.io/yaml" 31 32 "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" 33 "github.com/oam-dev/kubevela/pkg/oam/util" 34 "github.com/oam-dev/kubevela/pkg/utils/common" 35 pkgutils "github.com/oam-dev/kubevela/pkg/utils/util" 36 ) 37 38 var _ = Describe("Test Install Command", func() { 39 BeforeEach(func() { 40 fluxcd := v1beta1.Application{} 41 err := yaml.Unmarshal([]byte(fluxcdYaml), &fluxcd) 42 Expect(err).Should(BeNil()) 43 Expect(k8sClient.Create(context.Background(), &fluxcd)).Should(SatisfyAny(BeNil(), util.AlreadyExistMatcher{})) 44 rollout := v1beta1.Application{} 45 err = yaml.Unmarshal([]byte(rolloutYaml), &rollout) 46 Expect(err).Should(BeNil()) 47 Expect(k8sClient.Create(context.Background(), &rollout)).Should(SatisfyAny(BeNil(), util.AlreadyExistMatcher{})) 48 }) 49 50 It("Test check addon enabled", func() { 51 addons, err := checkInstallAddon(k8sClient) 52 Expect(err).Should(BeNil()) 53 Expect(len(addons)).Should(BeEquivalentTo(2)) 54 }) 55 56 It("Test disable all addons", func() { 57 err := forceDisableAddon(context.Background(), k8sClient, cfg) 58 Expect(err).Should(BeNil()) 59 Eventually(func() error { 60 addons, err := checkInstallAddon(k8sClient) 61 if err != nil { 62 return err 63 } 64 if len(addons) != 0 { 65 return fmt.Errorf("%v still exist", addons) 66 } 67 return nil 68 }, 1*time.Minute, 5*time.Second).Should(BeNil()) 69 }) 70 }) 71 72 func TestUninstall(t *testing.T) { 73 // Test answering NO when prompted. Should just exit. 74 cmd := NewUnInstallCommand(common.Args{}, "", pkgutils.IOStreams{ 75 Out: os.Stdout, 76 In: strings.NewReader("n\n"), 77 }) 78 cmd.SetArgs([]string{}) 79 err := cmd.Execute() 80 assert.Nil(t, err, "should just exit if answer is no") 81 } 82 83 var fluxcdYaml = ` 84 apiVersion: core.oam.dev/v1beta1 85 kind: Application 86 metadata: 87 name: addon-fluxcd 88 namespace: vela-system 89 labels: 90 addons.oam.dev/name: fluxcd 91 addons.oam.dev/registry: local 92 addons.oam.dev/version: 1.1.0 93 spec: 94 components: 95 - name: ns-flux-system 96 properties: 97 apiVersion: v1 98 kind: Namespace 99 metadata: 100 name: flux-system 101 ` 102 103 var fluxcdRemoteYaml = ` 104 apiVersion: core.oam.dev/v1beta1 105 kind: Application 106 metadata: 107 name: addon-fluxcd 108 namespace: vela-system 109 labels: 110 addons.oam.dev/name: fluxcd 111 addons.oam.dev/registry: KubeVela 112 addons.oam.dev/version: 1.1.0 113 spec: 114 components: 115 - name: ns-flux-system 116 properties: 117 apiVersion: v1 118 kind: Namespace 119 metadata: 120 name: flux-system 121 ` 122 123 var rolloutYaml = ` 124 apiVersion: core.oam.dev/v1beta1 125 kind: Application 126 metadata: 127 name: addon-rollout 128 namespace: vela-system 129 labels: 130 addons.oam.dev/name: rollout 131 spec: 132 components: 133 - name: test-ns 134 properties: 135 apiVersion: v1 136 kind: Namespace 137 metadata: 138 name: test-ns 139 `