From aaa12c84510bb3780ccea4cf17749d51bbefe676 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Tue, 12 Sep 2023 17:00:43 +0800 Subject: [PATCH] feat: repo test cases --- controllers/repository_controller_test.go | 174 ++++++++++++++ controllers/suite_test.go | 31 ++- pkg/repository/mock/mock_chart_repo_server.go | 222 ++++++++++++++++++ 3 files changed, 422 insertions(+), 5 deletions(-) create mode 100644 controllers/repository_controller_test.go create mode 100644 pkg/repository/mock/mock_chart_repo_server.go diff --git a/controllers/repository_controller_test.go b/controllers/repository_controller_test.go new file mode 100644 index 00000000..1d939764 --- /dev/null +++ b/controllers/repository_controller_test.go @@ -0,0 +1,174 @@ +/* +Copyright 2023 The Kubebb Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package controllers + +import ( + "context" + "fmt" + "net/http" + "time" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" + "k8s.io/apimachinery/pkg/util/wait" + "k8s.io/client-go/util/retry" + + "github.com/kubebb/core/api/v1alpha1" + "github.com/kubebb/core/pkg/repository/mock" +) + +var _ = Describe("Repository controller", Ordered, func() { + Context("Repository controller", func() { + const ( + repoAname = "repoa" + repoAnamespace = "kube-system" + repoBname = "repob" + repoBnamespace = "default" + ) + var ( + err error + rootCtx = context.Background() + ctx context.Context + cancel context.CancelFunc + + srv = mock.NewMockChartServer(8000) + ) + + BeforeAll(func() { + ctx, cancel = context.WithCancel(rootCtx) + go func() { + if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { + Expect(err).ToNot(HaveOccurred()) + } + }() + }) + + AfterAll(func() { + if err = srv.Shutdown(ctx); err != nil { + Expect(err).ToNot(HaveOccurred()) + } + cancel() + }) + + desc := "Create repository, to test controller polling. It can be created successfully. Only one component is included at the same time. In the first time, only one version is included. In the next cycle, two versions will appear." + It(desc, func() { + repo := v1alpha1.Repository{ + ObjectMeta: metav1.ObjectMeta{ + Name: repoAname, + Namespace: repoAnamespace, + }, + Spec: v1alpha1.RepositorySpec{ + URL: "http://localhost:8000/a", + PullStategy: &v1alpha1.PullStategy{ + IntervalSeconds: 120, + Retry: 5, + }, + }, + } + By("create repository repoa") + if err = k8sClient.Create(ctx, &repo); err != nil { + Expect(err).ToNot(HaveOccurred()) + } + By("there should be only one component and only one version") + repoName := repoAname + ".chartmuseum" + if err = retry.OnError(wait.Backoff{Factor: 1.0, Steps: 3, Duration: time.Second * 10}, func(err error) bool { + return err != nil + }, func() error { + component := v1alpha1.Component{} + if err := k8sClient.Get(ctx, types.NamespacedName{Namespace: repoAnamespace, Name: repoName}, &component); err != nil { + return err + } + if r := len(component.Status.Versions); r != 1 { + return fmt.Errorf("expect only one version, but actuall is %d", r) + } + return nil + }); err != nil { + Expect(err).ToNot(HaveOccurred()) + } + + By("there should be a component with two versions") + time.Sleep(time.Second * 120) + if err = retry.OnError(wait.Backoff{Factor: 1.0, Steps: 3, Duration: time.Second * 10}, func(err error) bool { + return err != nil + }, func() error { + component := v1alpha1.Component{} + if err := k8sClient.Get(ctx, types.NamespacedName{Namespace: repoAnamespace, Name: repoName}, &component); err != nil { + return err + } + if r := len(component.Status.Versions); r != 2 { + return fmt.Errorf("expect two versions, but actuall is %d", r) + } + return nil + }); err != nil { + Expect(err).ToNot(HaveOccurred()) + } + fmt.Println("step1 done") + }) + + It(`Create a repository and accurately filter out chartmuseum and filter out obsolete versions of minio. It is guaranteed that there will be only minio in the end, and only one version.`, func() { + repo := v1alpha1.Repository{ + ObjectMeta: metav1.ObjectMeta{ + Name: repoBname, + Namespace: repoBnamespace, + }, + Spec: v1alpha1.RepositorySpec{ + URL: "http://localhost:8000/b", + PullStategy: &v1alpha1.PullStategy{ + IntervalSeconds: 120, + Retry: 5, + }, + Filter: []v1alpha1.FilterCond{ + { + Name: "chartmuseum", + Operation: v1alpha1.FilterOpIgnore, + }, + { + Name: "minio", + KeepDeprecated: false, + Operation: v1alpha1.FilterOpKeep, + }, + }, + }, + } + By("create repository repob") + if err = k8sClient.Create(ctx, &repo); err != nil { + Expect(err).ToNot(HaveOccurred()) + } + + By("Finally check, there should be only one component and only one version") + repoName := repoBname + ".minio" + if err = retry.OnError(wait.Backoff{Factor: 1.0, Steps: 3, Duration: time.Second * 10}, func(err error) bool { + return err != nil + }, func() error { + component := v1alpha1.Component{} + if err := k8sClient.Get(ctx, types.NamespacedName{Namespace: repoBnamespace, Name: repoName}, &component); err != nil { + return err + } + if r := len(component.Status.Versions); r != 1 { + return fmt.Errorf("expect only one version, but actuall is %d", r) + } + return nil + }); err != nil { + Expect(err).ToNot(HaveOccurred()) + } + fmt.Println("step2 done") + }) + }) +}) diff --git a/controllers/suite_test.go b/controllers/suite_test.go index a8beb1a7..272a416c 100644 --- a/controllers/suite_test.go +++ b/controllers/suite_test.go @@ -19,18 +19,22 @@ package controllers import ( "path/filepath" "testing" + "time" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + "github.com/onsi/gomega/gexec" "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/rest" + ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/envtest" logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/log/zap" corev1alpha1 "github.com/kubebb/core/api/v1alpha1" - - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" + "github.com/kubebb/core/pkg/repository" ) // These tests use Ginkgo (BDD-style Go testing framework). Refer to @@ -39,6 +43,7 @@ import ( var cfg *rest.Config var k8sClient client.Client var testEnv *envtest.Environment +var k8sManager ctrl.Manager func TestAPIs(t *testing.T) { RegisterFailHandler(Fail) @@ -48,7 +53,6 @@ func TestAPIs(t *testing.T) { var _ = BeforeSuite(func() { logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) - By("bootstrapping test environment") testEnv = &envtest.Environment{ CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")}, @@ -64,9 +68,25 @@ var _ = BeforeSuite(func() { err = corev1alpha1.AddToScheme(scheme.Scheme) Expect(err).NotTo(HaveOccurred()) + k8sManager, err = ctrl.NewManager(cfg, ctrl.Options{ + Scheme: scheme.Scheme, + }) + Expect(err).ToNot(HaveOccurred()) + + err = (&RepositoryReconciler{ + Client: k8sManager.GetClient(), + Scheme: scheme.Scheme, + Recorder: k8sManager.GetEventRecorderFor("repository-controller-test"), + C: make(map[string]repository.IWatcher)}).SetupWithManager(k8sManager) + Expect(err).ToNot(HaveOccurred()) + go func() { + err = k8sManager.Start(ctrl.SetupSignalHandler()) + Expect(err).ToNot(HaveOccurred()) + }() + //+kubebuilder:scaffold:scheme - k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) + k8sClient = k8sManager.GetClient() Expect(err).NotTo(HaveOccurred()) Expect(k8sClient).NotTo(BeNil()) @@ -74,6 +94,7 @@ var _ = BeforeSuite(func() { var _ = AfterSuite(func() { By("tearing down the test environment") + gexec.KillAndWait(30 * time.Second) err := testEnv.Stop() Expect(err).NotTo(HaveOccurred()) }) diff --git a/pkg/repository/mock/mock_chart_repo_server.go b/pkg/repository/mock/mock_chart_repo_server.go new file mode 100644 index 00000000..5cb10747 --- /dev/null +++ b/pkg/repository/mock/mock_chart_repo_server.go @@ -0,0 +1,222 @@ +/* +Copyright 2023 The Kubebb Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package mock + +import ( + "fmt" + "net/http" + "time" +) + +var ( + // yaml1 only include chartmuseum with one version + yaml1 = `apiVersion: v1 +entries: + chartmuseum: + - apiVersion: v2 + appVersion: 0.16.0 + created: "2023-07-27T07:49:45.023571147Z" + description: Host your own Helm Chart Repository + digest: 7d9181335a24d8907c2d0f3d72eb0aaf82831287273ccc250b80946d50577e7b + home: https://github.com/helm/chartmuseum + icon: https://raw.githubusercontent.com/chartmuseum/charts/main/logo.jpg + keywords: + - chartmuseum + - helm + - charts repo + maintainers: + - name: chartmuseum + url: https://github.com/chartmuseum + name: chartmuseum + sources: + - https://github.com/chartmuseum/charts/tree/main/src/chartmuseum + - https://github.com/chartmuseum + - https://github.com/helm/chartmuseum + urls: + - https://github.com/kubebb/components/releases/download/chartmuseum-3.10.1/chartmuseum-3.10.1.tgz + version: 3.10.1 +generated: "2023-09-11T08:26:00.296380064Z"` + + // yaml2 include two versions chartmuseum + yaml2 = `apiVersion: v1 +entries: + chartmuseum: + - apiVersion: v2 + appVersion: 0.16.0 + created: "2023-08-14T08:36:07.186243368Z" + description: Host your own Helm Chart Repository + digest: 40d85cd21c5f49ed0b9961a457594fd43fd19e2bc1763dfb318e2a6fb8706ce4 + home: https://github.com/helm/chartmuseum + icon: https://raw.githubusercontent.com/chartmuseum/charts/main/logo.jpg + keywords: + - chartmuseum + - helm + - charts repo + maintainers: + - name: chartmuseum + url: https://github.com/chartmuseum + name: chartmuseum + sources: + - https://github.com/chartmuseum/charts/tree/main/src/chartmuseum + - https://github.com/chartmuseum + - https://github.com/helm/chartmuseum + urls: + - https://github.com/kubebb/components/releases/download/chartmuseum-3.10.2/chartmuseum-3.10.2.tgz + version: 3.10.2 + - apiVersion: v2 + appVersion: 0.16.0 + created: "2023-07-27T07:49:45.023571147Z" + description: Host your own Helm Chart Repository + digest: 7d9181335a24d8907c2d0f3d72eb0aaf82831287273ccc250b80946d50577e7b + home: https://github.com/helm/chartmuseum + icon: https://raw.githubusercontent.com/chartmuseum/charts/main/logo.jpg + keywords: + - chartmuseum + - helm + - charts repo + maintainers: + - name: chartmuseum + url: https://github.com/chartmuseum + name: chartmuseum + sources: + - https://github.com/chartmuseum/charts/tree/main/src/chartmuseum + - https://github.com/chartmuseum + - https://github.com/helm/chartmuseum + urls: + - https://github.com/kubebb/components/releases/download/chartmuseum-3.10.1/chartmuseum-3.10.1.tgz + version: 3.10.1 +generated: "2023-09-11T08:26:00.296380064Z"` + + // for testing filter, we will fitler chartmuseum, only remain minio + yaml3 = `apiVersion: v1 +entries: + chartmuseum: + - apiVersion: v2 + appVersion: 0.16.0 + created: "2023-08-14T08:36:07.186243368Z" + description: Host your own Helm Chart Repository + digest: 40d85cd21c5f49ed0b9961a457594fd43fd19e2bc1763dfb318e2a6fb8706ce4 + home: https://github.com/helm/chartmuseum + icon: https://raw.githubusercontent.com/chartmuseum/charts/main/logo.jpg + keywords: + - chartmuseum + - helm + - charts repo + maintainers: + - name: chartmuseum + url: https://github.com/chartmuseum + name: chartmuseum + sources: + - https://github.com/chartmuseum/charts/tree/main/src/chartmuseum + - https://github.com/chartmuseum + - https://github.com/helm/chartmuseum + urls: + - https://github.com/kubebb/components/releases/download/chartmuseum-3.10.2/chartmuseum-3.10.2.tgz + version: 3.10.2 + - apiVersion: v2 + appVersion: 0.16.0 + created: "2023-07-27T07:49:45.023571147Z" + description: Host your own Helm Chart Repository + digest: 7d9181335a24d8907c2d0f3d72eb0aaf82831287273ccc250b80946d50577e7b + home: https://github.com/helm/chartmuseum + icon: https://raw.githubusercontent.com/chartmuseum/charts/main/logo.jpg + keywords: + - chartmuseum + - helm + - charts repo + maintainers: + - name: chartmuseum + url: https://github.com/chartmuseum + name: chartmuseum + sources: + - https://github.com/chartmuseum/charts/tree/main/src/chartmuseum + - https://github.com/chartmuseum + - https://github.com/helm/chartmuseum + urls: + - https://github.com/kubebb/components/releases/download/chartmuseum-3.10.1/chartmuseum-3.10.1.tgz + version: 3.10.1 + minio: + - apiVersion: v1 + appVersion: RELEASE.2023-02-10T18-48-39Z + created: "2023-08-16T04:27:42.388058016Z" + description: Multi-Cloud Object Storage + digest: 3d67a50567bff4ee9ea71663f169f658bf23223da77d064b0c0668f47c422e0e + home: https://min.io + icon: https://min.io/resources/img/logo/MINIO_wordmark.png + keywords: + - minio + - storage + - object-storage + - s3 + - cluster + maintainers: + - email: dev@minio.io + name: MinIO, Inc + name: minio + sources: + - https://github.com/minio/minio + urls: + - https://github.com/kubebb/components/releases/download/minio-5.0.8/minio-5.0.8.tgz + version: 5.0.8 + - apiVersion: v1 + appVersion: RELEASE.2023-02-10T18-48-39Z + created: "2023-07-03T09:03:55.998503794Z" + description: Multi-Cloud Object Storage + digest: cf3d46b8abeb363e7913d907c243071109d8738dfdb30a7236c9f082c7bd29ba + home: https://min.io + icon: https://min.io/resources/img/logo/MINIO_wordmark.png + deprecated: true + keywords: + - minio + - storage + - object-storage + - s3 + - cluster + maintainers: + - email: dev@minio.io + name: MinIO, Inc + name: minio + sources: + - https://github.com/minio/minio + urls: + - https://github.com/kubebb/components/releases/download/minio-5.0.7/minio-5.0.7.tgz + version: 5.0.7 +generated: "2023-09-11T10:26:00.296380064Z"` +) + +func NewMockChartServer(port int) *http.Server { + srv := &http.Server{ + Addr: fmt.Sprintf(":%d", port), + } + now := time.Now() + http.HandleFunc("/a/index.yaml", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/yaml") + diff := time.Since(now).Seconds() + if diff >= 120 { + // Simulate adding a new chart package + _, _ = w.Write([]byte(yaml2)) + return + } + _, _ = w.Write([]byte(yaml1)) + }) + http.HandleFunc("/b/index.yaml", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/yaml") + _, _ = w.Write([]byte(yaml3)) + }) + srv.Handler = http.DefaultServeMux + return srv +}