github.com/oam-dev/kubevela@v1.9.11/references/cli/config_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 "os" 22 "strings" 23 24 . "github.com/onsi/ginkgo/v2" 25 . "github.com/onsi/gomega" 26 v1 "k8s.io/api/core/v1" 27 "sigs.k8s.io/yaml" 28 29 "github.com/oam-dev/kubevela/pkg/cmd" 30 "github.com/oam-dev/kubevela/pkg/utils/util" 31 ) 32 33 var _ = Describe("Test the commands of the config", func() { 34 var arg cmd.Factory 35 BeforeEach(func() { 36 arg = cmd.NewTestFactory(cfg, k8sClient) 37 }) 38 39 It("Test apply a template", func() { 40 buffer := bytes.NewBuffer(nil) 41 cmd := TemplateCommandGroup(arg, "", util.IOStreams{In: os.Stdin, Out: buffer, ErrOut: buffer}) 42 cmd.SetArgs([]string{"apply", "-f", "./test-data/config-templates/image-registry.cue", "--name", "test"}) 43 err := cmd.Execute() 44 Expect(err).Should(BeNil()) 45 Expect(buffer.String()).Should(Equal("the config template test applied successfully\n")) 46 }) 47 48 It("Test apply a new template", func() { 49 buffer := bytes.NewBuffer(nil) 50 cmd := TemplateCommandGroup(arg, "", util.IOStreams{In: os.Stdin, Out: buffer, ErrOut: buffer}) 51 cmd.SetArgs([]string{"apply", "-f", "./test-data/config-templates/image-registry.cue", "--name", "test2"}) 52 err := cmd.Execute() 53 Expect(err).Should(BeNil()) 54 Expect(buffer.String()).Should(Equal("the config template test2 applied successfully\n")) 55 }) 56 57 It("Test list the templates", func() { 58 buffer := bytes.NewBuffer(nil) 59 cmd := TemplateCommandGroup(arg, "", util.IOStreams{In: os.Stdin, Out: buffer, ErrOut: buffer}) 60 cmd.SetArgs([]string{"list", "-A"}) 61 err := cmd.Execute() 62 Expect(err).Should(BeNil()) 63 Expect(strings.Contains(buffer.String(), "vela-system")).Should(Equal(true)) 64 Expect(strings.Contains(buffer.String(), "test")).Should(Equal(true)) 65 Expect(strings.Contains(buffer.String(), "\n")).Should(Equal(true)) 66 }) 67 68 It("Test show the templates", func() { 69 buffer := bytes.NewBuffer(nil) 70 cmd := TemplateCommandGroup(arg, "", util.IOStreams{In: os.Stdin, Out: buffer, ErrOut: buffer}) 71 cmd.SetArgs([]string{"show", "test2"}) 72 err := cmd.Execute() 73 Expect(err).Should(BeNil()) 74 Expect(line(buffer.String())).Should(Equal(24)) 75 }) 76 77 It("Test create the config with the args", func() { 78 buffer := bytes.NewBuffer(nil) 79 cmd := ConfigCommandGroup(arg, "", util.IOStreams{In: os.Stdin, Out: buffer, ErrOut: buffer}) 80 cmd.SetArgs([]string{"create", "test", "--template=test", "registry=test.kubevela.net", "auth.username=yueda", "auth.password=yueda123", "useHTTP=true"}) 81 err := cmd.Execute() 82 Expect(err).Should(BeNil()) 83 Expect(buffer.String()).Should(Equal("the config test applied successfully\n")) 84 }) 85 86 It("Test create the config with the file", func() { 87 buffer := bytes.NewBuffer(nil) 88 cmd := ConfigCommandGroup(arg, "", util.IOStreams{In: os.Stdin, Out: buffer, ErrOut: buffer}) 89 cmd.SetArgs([]string{"create", "testfile", "--template=test2", "--namespace=default", "-f", "./test-data/config/registry.yaml"}) 90 err := cmd.Execute() 91 Expect(err).Should(BeNil()) 92 Expect(buffer.String()).Should(Equal("the config testfile applied successfully\n")) 93 }) 94 95 It("Test create the config without the template", func() { 96 buffer := bytes.NewBuffer(nil) 97 cmd := ConfigCommandGroup(arg, "", util.IOStreams{In: os.Stdin, Out: buffer, ErrOut: buffer}) 98 cmd.SetArgs([]string{"create", "without-template", "--namespace=default", "-f", "./test-data/config/registry.yaml"}) 99 err := cmd.Execute() 100 Expect(err).Should(BeNil()) 101 Expect(buffer.String()).Should(Equal("the config without-template applied successfully\n")) 102 }) 103 104 It("Test creating and distributing the config", func() { 105 buffer := bytes.NewBuffer(nil) 106 cmd := ConfigCommandGroup(arg, "", util.IOStreams{In: os.Stdin, Out: buffer, ErrOut: buffer}) 107 cmd.SetArgs([]string{"create", "distribution", "--namespace=default", "-f", "./test-data/config/registry.yaml", "--target", "test"}) 108 err := cmd.Execute() 109 Expect(err).Should(BeNil()) 110 Expect(buffer.String()).Should(Equal("the config distribution applied successfully\n")) 111 }) 112 113 It("Test list the configs", func() { 114 buffer := bytes.NewBuffer(nil) 115 cmd := ConfigCommandGroup(arg, "", util.IOStreams{In: os.Stdin, Out: buffer, ErrOut: buffer}) 116 cmd.SetArgs([]string{"list", "-A"}) 117 err := cmd.Execute() 118 Expect(err).Should(BeNil()) 119 Expect(line(buffer.String())).Should(Equal(5)) 120 }) 121 122 It("Test list the configs with the namespace filter", func() { 123 buffer := bytes.NewBuffer(nil) 124 cmd := ConfigCommandGroup(arg, "", util.IOStreams{In: os.Stdin, Out: buffer, ErrOut: buffer}) 125 cmd.SetArgs([]string{"list", "-n", "default"}) 126 err := cmd.Execute() 127 Expect(err).Should(BeNil()) 128 Expect(line(buffer.String())).Should(Equal(4)) 129 }) 130 131 It("Test list the configs with the template filter", func() { 132 buffer := bytes.NewBuffer(nil) 133 cmd := ConfigCommandGroup(arg, "", util.IOStreams{In: os.Stdin, Out: buffer, ErrOut: buffer}) 134 cmd.SetArgs([]string{"list", "-A", "-t", "test2"}) 135 err := cmd.Execute() 136 Expect(err).Should(BeNil()) 137 Expect(line(buffer.String())).Should(Equal(2)) 138 }) 139 140 It("Test dry run the config", func() { 141 buffer := bytes.NewBuffer(nil) 142 cmd := ConfigCommandGroup(arg, "", util.IOStreams{In: os.Stdin, Out: buffer, ErrOut: buffer}) 143 cmd.SetArgs([]string{"create", "testfile", "--template=test", "-f", "./test-data/config/registry.yaml", "--dry-run"}) 144 err := cmd.Execute() 145 Expect(err).Should(BeNil()) 146 var secret v1.Secret 147 Expect(yaml.Unmarshal(buffer.Bytes(), &secret)).Should(BeNil()) 148 Expect(secret.Name).Should(Equal("testfile")) 149 Expect(secret.Labels["config.oam.dev/type"]).Should(Equal("test")) 150 Expect(secret.Labels["config.oam.dev/catalog"]).Should(Equal("velacore-config")) 151 Expect(string(secret.Type)).Should(Equal("kubernetes.io/dockerconfigjson")) 152 }) 153 154 It("Distribute a config", func() { 155 buffer := bytes.NewBuffer(nil) 156 cmd := ConfigCommandGroup(arg, "", util.IOStreams{In: os.Stdin, Out: buffer, ErrOut: buffer}) 157 cmd.SetArgs([]string{"distribute", "testfile", "-t", "test"}) 158 err := cmd.Execute() 159 Expect(err).Should(BeNil()) 160 Expect(buffer.String()).Should(Equal("the distribution distribute-testfile applied successfully\n")) 161 }) 162 163 It("Recall a config", func() { 164 buffer := bytes.NewBuffer(nil) 165 cmd := ConfigCommandGroup(arg, "", util.IOStreams{In: strings.NewReader("y\n"), Out: buffer, ErrOut: buffer}) 166 cmd.SetArgs([]string{"distribute", "testfile", "--recall"}) 167 err := cmd.Execute() 168 Expect(err).Should(BeNil()) 169 Expect(buffer.String()).Should(Equal("Do you want to recall this config (y/n)the distribution distribute-testfile deleted successfully\n")) 170 }) 171 172 It("Test delete a config", func() { 173 buffer := bytes.NewBuffer(nil) 174 cmd := ConfigCommandGroup(arg, "", util.IOStreams{In: strings.NewReader("y\n"), Out: buffer, ErrOut: buffer}) 175 cmd.SetArgs([]string{"delete", "distribution", "-n", "default"}) 176 assumeYes = false 177 err := cmd.Execute() 178 Expect(err).Should(BeNil()) 179 Expect(buffer.String()).Should(Equal("Do you want to delete this config (y/n)the config distribution deleted successfully\n")) 180 }) 181 182 It("Test delete a template", func() { 183 buffer := bytes.NewBuffer(nil) 184 cmd := TemplateCommandGroup(arg, "", util.IOStreams{In: strings.NewReader("y\n"), Out: buffer, ErrOut: buffer}) 185 cmd.SetArgs([]string{"delete", "test"}) 186 assumeYes = false 187 err := cmd.Execute() 188 Expect(err).Should(BeNil()) 189 Expect(buffer.String()).Should(Equal("Do you want to delete this template (y/n)the config template test deleted successfully\n")) 190 }) 191 }) 192 193 func line(data string) int { 194 return len(strings.Split(strings.TrimRight(data, "\n"), "\n")) 195 }