Skip to content

Commit

Permalink
Merge pull request #221 from darkowlzz/testenv-refactor
Browse files Browse the repository at this point in the history
Testenv test refactor
  • Loading branch information
darkowlzz committed Nov 11, 2021
2 parents 938b500 + 8b96c15 commit ad21e5f
Show file tree
Hide file tree
Showing 7 changed files with 1,036 additions and 1,147 deletions.
104 changes: 36 additions & 68 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,95 +17,63 @@ limitations under the License.
package controllers

import (
"fmt"
"os"
"path/filepath"
"testing"
"time"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/fluxcd/pkg/runtime/testenv"
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"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"

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.

var cfg *rest.Config
var k8sClient client.Client
var k8sManager ctrl.Manager
var imageAutoReconciler *ImageUpdateAutomationReconciler
var testEnv *envtest.Environment

func TestAPIs(t *testing.T) {
RegisterFailHandler(Fail)

RunSpecsWithDefaultAndCustomReporters(t,
"Controller Suite",
[]Reporter{printer.NewlineReporter{}})
}

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

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())
const (
contextTimeout = time.Second * 10
)

Expect(sourcev1.AddToScheme(scheme.Scheme)).To(Succeed())
Expect(imagev1_reflect.AddToScheme(scheme.Scheme)).To(Succeed())
var (
testEnv *testenv.Environment
ctx = ctrl.SetupSignalHandler()
)

Expect(imagev1.AddToScheme(scheme.Scheme)).To(Succeed())
// +kubebuilder:scaffold:scheme
func TestMain(m *testing.M) {
utilruntime.Must(imagev1_reflect.AddToScheme(scheme.Scheme))
utilruntime.Must(sourcev1.AddToScheme(scheme.Scheme))
utilruntime.Must(imagev1.AddToScheme(scheme.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"),
))

imageAutoReconciler = &ImageUpdateAutomationReconciler{
Client: k8sManager.GetClient(),
if err := (&ImageUpdateAutomationReconciler{
Client: testEnv,
Scheme: scheme.Scheme,
}).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(ctrl.SetupSignalHandler())
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()

// 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())
code := m.Run()

close(done)
}, 60)
fmt.Println("Stopping the test environment")
if err := testEnv.Stop(); err != nil {
panic(fmt.Sprintf("Failed to stop the test environment: %v", err))
}

var _ = AfterSuite(func() {
By("tearing down the test environment")
err := testEnv.Stop()
Expect(err).ToNot(HaveOccurred())
})
os.Exit(code)
}

0 comments on commit ad21e5f

Please sign in to comment.