github.com/oam-dev/kubevela@v1.9.11/references/cli/kube_test.go (about) 1 /* 2 Copyright 2022 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 "bytes" 21 "context" 22 "os" 23 "strings" 24 25 . "github.com/onsi/ginkgo/v2" 26 . "github.com/onsi/gomega" 27 corev1 "k8s.io/api/core/v1" 28 v1 "k8s.io/apimachinery/pkg/apis/meta/v1" 29 "sigs.k8s.io/controller-runtime/pkg/client" 30 31 "github.com/oam-dev/kubevela/pkg/utils/util" 32 ) 33 34 var _ = Describe("Test kube apply cli", func() { 35 36 When("test vela kube apply", func() { 37 38 It("should not have err and applied all objects", func() { 39 40 buffer := bytes.NewBuffer(nil) 41 ioStreams := util.IOStreams{In: os.Stdin, Out: buffer, ErrOut: buffer} 42 43 o := &KubeApplyOptions{} 44 o.IOStreams = ioStreams 45 o.files = []string{"./test-data/kubeapply"} 46 ctx := context.Background() 47 Expect(o.Complete(ctx)).Should(BeNil()) 48 Expect(o.Validate()).Should(BeNil()) 49 Expect(o.Run(ctx, k8sClient)).Should(BeNil()) 50 51 By("test kube apply in dry-run mod") 52 o.dryRun = true 53 Expect(o.Run(ctx, k8sClient)).Should(BeNil()) 54 buf, ok := ioStreams.Out.(*bytes.Buffer) 55 Expect(ok).Should(BeTrue()) 56 Expect(strings.Contains(buf.String(), "error")).Should(BeFalse()) 57 58 By("test kube apply in different namespace, new namespace") 59 var newns = "test-kube-apply" 60 err := k8sClient.Create(ctx, &corev1.Namespace{ 61 ObjectMeta: v1.ObjectMeta{Name: newns}, 62 }) 63 Expect(err).Should(BeNil()) 64 65 By("apply objects") 66 o = &KubeApplyOptions{} 67 o.IOStreams = ioStreams 68 o.files = []string{"./test-data/kubeapply"} 69 o.namespace = newns 70 Expect(o.Complete(ctx)).Should(BeNil()) 71 Expect(o.Validate()).Should(BeNil()) 72 Expect(o.Run(ctx, k8sClient)).Should(BeNil()) 73 74 By("check objects configmap created") 75 cml := corev1.ConfigMapList{} 76 Expect(k8sClient.List(ctx, &cml, client.InNamespace(newns))).Should(BeNil()) 77 Expect(len(cml.Items)).Should(BeEquivalentTo(3)) 78 79 }) 80 }) 81 })