Skip to content

Commit

Permalink
feat: repo test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
0xff-dev committed Sep 14, 2023
1 parent 2ad4cde commit 253fe3a
Show file tree
Hide file tree
Showing 3 changed files with 430 additions and 3 deletions.
174 changes: 174 additions & 0 deletions controllers/repository_controller_test.go
Original file line number Diff line number Diff line change
@@ -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"

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"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("Repository controller", Ordered, 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()
})

Context("Repository controller", func() {
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://127.0.0.1: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://127.0.0.1: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")
})
})
})
37 changes: 34 additions & 3 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ package controllers
import (
"path/filepath"
"testing"
"time"

"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/kubebb/core/pkg/repository"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand All @@ -39,6 +42,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)
Expand All @@ -48,7 +52,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")},
Expand All @@ -64,16 +67,44 @@ 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())

})

var _ = AfterSuite(func() {
By("tearing down the test environment")
err := testEnv.Stop()
err := func() error {
sleep := 1 * time.Millisecond
var e error
for i := 0; i < 100; i++ {
if e = testEnv.Stop(); e == nil {
return nil
}
sleep *= 2
time.Sleep(sleep)
}
return e
}()

Expect(err).NotTo(HaveOccurred())
})
Loading

0 comments on commit 253fe3a

Please sign in to comment.