github.com/oam-dev/kubevela@v1.9.11/pkg/addon/helper_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 addon 18 19 import ( 20 "bytes" 21 "context" 22 "errors" 23 "net/http" 24 "net/http/httptest" 25 "os" 26 "strings" 27 28 v1 "k8s.io/api/core/v1" 29 "sigs.k8s.io/yaml" 30 31 . "github.com/onsi/ginkgo/v2" 32 . "github.com/onsi/gomega" 33 ) 34 35 func setupMockServer() *httptest.Server { 36 var listenURL string 37 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { 38 fileList := []string{ 39 "index.yaml", 40 "fluxcd-test-version-1.0.0.tgz", 41 "fluxcd-test-version-2.0.0.tgz", 42 "vela-workflow-v0.3.5.tgz", 43 "foo-v1.0.0.tgz", 44 "bar-v1.0.0.tgz", 45 "bar-v2.0.0.tgz", 46 "mock-be-dep-addon-v1.0.0.tgz", 47 "has-clusters-arg-v1.0.0.tgz", 48 } 49 for _, f := range fileList { 50 if strings.Contains(req.URL.Path, f) { 51 file, err := os.ReadFile("../../e2e/addon/mock/testrepo/helm-repo/" + f) 52 if err != nil { 53 _, _ = w.Write([]byte(err.Error())) 54 } 55 if f == "index.yaml" { 56 // in index.yaml, url is hardcoded to 127.0.0.1:9098, 57 // so we need to replace it with the real random listen url 58 file = bytes.ReplaceAll(file, []byte("http://127.0.0.1:9098"), []byte(listenURL)) 59 } 60 _, _ = w.Write(file) 61 } 62 } 63 })) 64 listenURL = s.URL 65 return s 66 } 67 68 var _ = Describe("test FindAddonPackagesDetailFromRegistry", func() { 69 Describe("when no registry is added, no matter what you do, it will just return error", func() { 70 Context("when empty addonNames and registryNames is supplied", func() { 71 It("should return error", func() { 72 _, err := FindAddonPackagesDetailFromRegistry(context.Background(), k8sClient, []string{}, []string{}) 73 Expect(err).To(HaveOccurred()) 74 }) 75 It("should return error", func() { 76 _, err := FindAddonPackagesDetailFromRegistry(context.Background(), k8sClient, nil, nil) 77 Expect(err).To(HaveOccurred()) 78 }) 79 }) 80 Context("when non-empty addonNames and registryNames is supplied", func() { 81 It("should return error saying ErrRegistryNotExist", func() { 82 _, err := FindAddonPackagesDetailFromRegistry(context.Background(), k8sClient, []string{"fluxcd"}, []string{"some-registry"}) 83 Expect(errors.Is(err, ErrRegistryNotExist)).To(BeTrue()) 84 }) 85 }) 86 }) 87 88 Describe("one versioned registry is added", func() { 89 var s *httptest.Server 90 91 BeforeEach(func() { 92 s = setupMockServer() 93 // Prepare registry 94 reg := &Registry{ 95 Name: "addon_helper_test", 96 Helm: &HelmSource{ 97 URL: s.URL, 98 }, 99 } 100 ds := NewRegistryDataStore(k8sClient) 101 Expect(ds.AddRegistry(context.Background(), *reg)).To(Succeed()) 102 }) 103 104 AfterEach(func() { 105 // Clean up registry 106 ds := NewRegistryDataStore(k8sClient) 107 Expect(ds.DeleteRegistry(context.Background(), "addon_helper_test")).To(Succeed()) 108 s.Close() 109 }) 110 111 Context("when empty addonNames and registryNames is supplied", func() { 112 It("should return error, empty addonNames are not allowed", func() { 113 _, err := FindAddonPackagesDetailFromRegistry(context.Background(), k8sClient, []string{}, []string{"addon_helper_test"}) 114 Expect(err).To(HaveOccurred()) 115 }) 116 It("should return error, empty addonNames are not allowed", func() { 117 _, err := FindAddonPackagesDetailFromRegistry(context.Background(), k8sClient, nil, []string{"addon_helper_test"}) 118 Expect(err).To(HaveOccurred()) 119 }) 120 }) 121 122 Context("one existing addon name provided", func() { 123 It("should return one valid result, matching all registries", func() { 124 res, err := FindAddonPackagesDetailFromRegistry(context.Background(), k8sClient, []string{"foo"}, nil) 125 126 Expect(err).To(Succeed()) 127 Expect(res).To(HaveLen(1)) 128 Expect(res[0].Name).To(Equal("foo")) 129 }) 130 It("should return one valid result, matching one registry", func() { 131 res, err := FindAddonPackagesDetailFromRegistry(context.Background(), k8sClient, []string{"foo"}, []string{"addon_helper_test"}) 132 Expect(err).To(Succeed()) 133 Expect(res).To(HaveLen(1)) 134 Expect(res[0].Name).To(Equal("foo")) 135 }) 136 }) 137 138 Context("one non-existent addon name provided", func() { 139 It("should return error as ErrNotExist", func() { 140 res, err := FindAddonPackagesDetailFromRegistry(context.Background(), k8sClient, []string{"non-existent-addon"}, nil) 141 Expect(errors.Is(err, ErrNotExist)).To(BeTrue()) 142 Expect(res).To(BeNil()) 143 }) 144 }) 145 146 Context("two existing addon names provided", func() { 147 It("should return two valid result", func() { 148 res, err := FindAddonPackagesDetailFromRegistry(context.Background(), k8sClient, []string{"foo", "bar"}, nil) 149 Expect(err).To(Succeed()) 150 Expect(res).To(HaveLen(2)) 151 Expect(res[0].Name).To(Equal("foo")) 152 Expect(res[1].Name).To(Equal("bar")) 153 }) 154 }) 155 156 Context("one existing addon name and one non-existent addon name provided", func() { 157 It("should return only one valid result", func() { 158 res, err := FindAddonPackagesDetailFromRegistry(context.Background(), k8sClient, []string{"foo", "non-existent-addon"}, nil) 159 Expect(err).To(Succeed()) 160 Expect(res).To(HaveLen(1)) 161 Expect(res[0].Name).To(Equal("foo")) 162 }) 163 }) 164 }) 165 166 Describe("one non-versioned registry is added", func() { 167 var server *httptest.Server 168 BeforeEach(func() { 169 // Prepare local non-versioned registry 170 server = httptest.NewServer(ossHandler) 171 cm := v1.ConfigMap{} 172 cmYaml := strings.ReplaceAll(registryCmYaml, "TEST_SERVER_URL", server.URL) 173 cmYaml = strings.ReplaceAll(cmYaml, "KubeVela", "testreg") 174 Expect(yaml.Unmarshal([]byte(cmYaml), &cm)).Should(BeNil()) 175 _ = k8sClient.Create(ctx, &cm) 176 Expect(k8sClient.Update(ctx, &cm)).Should(BeNil()) 177 }) 178 179 AfterEach(func() { 180 server.Close() 181 }) 182 183 Context("when empty addonNames and registryNames is supplied", func() { 184 It("should return error, empty addonNames are not allowed", func() { 185 _, err := FindAddonPackagesDetailFromRegistry(context.Background(), k8sClient, []string{}, []string{}) 186 Expect(err).To(HaveOccurred()) 187 }) 188 It("should return error, empty addonNames are not allowed", func() { 189 _, err := FindAddonPackagesDetailFromRegistry(context.Background(), k8sClient, nil, []string{"testreg"}) 190 Expect(err).To(HaveOccurred()) 191 }) 192 }) 193 194 Context("one existing addon name provided", func() { 195 It("should return one valid result, matching all registries", func() { 196 res, err := FindAddonPackagesDetailFromRegistry(context.Background(), k8sClient, []string{"example"}, nil) 197 Expect(err).To(Succeed()) 198 Expect(res).To(HaveLen(1)) 199 Expect(res[0].Name).To(Equal("example")) 200 Expect(res[0].InstallPackage).ToNot(BeNil()) 201 }) 202 It("should return one valid result, matching one registry", func() { 203 res, err := FindAddonPackagesDetailFromRegistry(context.Background(), k8sClient, []string{"example"}, []string{"testreg"}) 204 Expect(err).To(Succeed()) 205 Expect(res).To(HaveLen(1)) 206 Expect(res[0].Name).To(Equal("example")) 207 Expect(res[0].InstallPackage).ToNot(BeNil()) 208 }) 209 }) 210 211 Context("one non-existent addon name provided", func() { 212 It("should return error as ErrNotExist", func() { 213 res, err := FindAddonPackagesDetailFromRegistry(context.Background(), k8sClient, []string{"non-existent-addon"}, nil) 214 Expect(errors.Is(err, ErrNotExist)).To(BeTrue()) 215 Expect(res).To(BeNil()) 216 }) 217 }) 218 219 Context("one existing addon name and one non-existent addon name provided", func() { 220 It("should return only one valid result", func() { 221 res, err := FindAddonPackagesDetailFromRegistry(context.Background(), k8sClient, []string{"example", "non-existent-addon"}, nil) 222 Expect(err).To(Succeed()) 223 Expect(res).To(HaveLen(1)) 224 Expect(res[0].Name).To(Equal("example")) 225 Expect(res[0].InstallPackage).ToNot(BeNil()) 226 }) 227 }) 228 }) 229 })