Skip to content

Commit

Permalink
Merge pull request #356 from fluxcd/testenv
Browse files Browse the repository at this point in the history
Rewrite all the tests to testenv with gomega
  • Loading branch information
darkowlzz authored Apr 26, 2022
2 parents 674b246 + 0e87398 commit cd9eaff
Show file tree
Hide file tree
Showing 9 changed files with 1,217 additions and 1,440 deletions.
117 changes: 44 additions & 73 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,102 +17,73 @@ limitations under the License.
package controllers

import (
"context"
"fmt"
"math/rand"
"os"
"path/filepath"
"testing"
"time"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"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"
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

imagev1_reflect "github.com/fluxcd/image-reflector-controller/api/v1beta1"
"github.com/fluxcd/pkg/runtime/testenv"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta2"

imagev1 "github.com/fluxcd/image-automation-controller/api/v1beta1"
// +kubebuilder:scaffold:imports
)

// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
// These tests make use of plain Go using Gomega for assertions.
// At the beginning of every (sub)test Gomega can be initialized
// using gomega.NewWithT.
// Refer to http://onsi.github.io/gomega/ to learn more about
// Gomega.

var cfg *rest.Config
var k8sClient client.Client
var k8sManager ctrl.Manager
var imageAutoReconciler *ImageUpdateAutomationReconciler
var testEnv *envtest.Environment
var ctx context.Context
var cancel context.CancelFunc

func TestAPIs(t *testing.T) {
RegisterFailHandler(Fail)
var (
testEnv *testenv.Environment
ctx = ctrl.SetupSignalHandler()
)

RunSpecsWithDefaultAndCustomReporters(t,
"Controller Suite",
[]Reporter{printer.NewlineReporter{}})
func init() {
rand.Seed(time.Now().UnixNano())
}

var _ = BeforeSuite(func(done Done) {
ctrl.SetLogger(
zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)),
)
ctx, cancel = context.WithCancel(context.TODO())

By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{
filepath.Join("..", "config", "crd", "bases"),
filepath.Join("testdata", "crds"),
},
}

var err error
cfg, err = testEnv.Start()
Expect(err).ToNot(HaveOccurred())
Expect(cfg).ToNot(BeNil())

Expect(sourcev1.AddToScheme(scheme.Scheme)).To(Succeed())
Expect(imagev1_reflect.AddToScheme(scheme.Scheme)).To(Succeed())
func TestMain(m *testing.M) {
utilruntime.Must(imagev1_reflect.AddToScheme(scheme.Scheme))
utilruntime.Must(sourcev1.AddToScheme(scheme.Scheme))
utilruntime.Must(imagev1.AddToScheme(scheme.Scheme))

Expect(imagev1.AddToScheme(scheme.Scheme)).To(Succeed())
// +kubebuilder:scaffold:scheme

k8sManager, err = ctrl.NewManager(cfg, ctrl.Options{
Scheme: scheme.Scheme,
})
Expect(err).ToNot(HaveOccurred())
testEnv = testenv.New(testenv.WithCRDPath(
filepath.Join("..", "config", "crd", "bases"),
filepath.Join("testdata", "crds"),
))

controllerName := "image-automation-controller"
imageAutoReconciler = &ImageUpdateAutomationReconciler{
Client: k8sManager.GetClient(),
if err := (&ImageUpdateAutomationReconciler{
Client: testEnv,
Scheme: scheme.Scheme,
EventRecorder: k8sManager.GetEventRecorderFor(controllerName),
EventRecorder: testEnv.GetEventRecorderFor(controllerName),
}).SetupWithManager(testEnv, ImageUpdateAutomationReconcilerOptions{}); err != nil {
panic(fmt.Sprintf("Failed to start ImageUpdateAutomationReconciler: %v", err))
}
Expect(imageAutoReconciler.SetupWithManager(k8sManager, ImageUpdateAutomationReconcilerOptions{})).To(Succeed())

go func() {
defer GinkgoRecover()
err = k8sManager.Start(ctx)
Expect(err).ToNot(HaveOccurred())
fmt.Println("Starting the test environment")
if err := testEnv.Start(ctx); err != nil {
panic(fmt.Sprintf("Failed to start the test environment manager: %v", err))
}
}()
<-testEnv.Manager.Elected()

code := m.Run()

// Specifically an uncached client. Use <reconciler>.Get if you
// want to see what the reconcilers see.
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).ToNot(HaveOccurred())
Expect(k8sClient).ToNot(BeNil())

close(done)
}, 60)

var _ = AfterSuite(func() {
cancel()
By("tearing down the test environment")
err := testEnv.Stop()
Expect(err).ToNot(HaveOccurred())
})
fmt.Println("Stopping the test environment")
if err := testEnv.Stop(); err != nil {
panic(fmt.Sprintf("Failed to stop the test environment: %v", err))
}

os.Exit(code)
}
Loading

0 comments on commit cd9eaff

Please sign in to comment.