From 54fed4cf1e1baa1b12c905752204a5bda52ecdde Mon Sep 17 00:00:00 2001 From: Tim Myers Date: Wed, 11 Jan 2023 12:13:52 -0700 Subject: [PATCH] fix: add tests (#11) --- .github/workflows/go-tests.yml | 2 +- .tool-versions | 2 +- Makefile | 2 +- controllers/controller.go | 12 +++ ...uite_test.go => controllers_suite_test.go} | 26 +++++- .../controller.go} | 16 ++-- controllers/proxy/controller_test.go | 23 +++++ controllers/proxy/proxy_suite_test.go | 89 +++++++++++++++++++ main.go | 5 +- 9 files changed, 162 insertions(+), 15 deletions(-) create mode 100644 controllers/controller.go rename controllers/{suite_test.go => controllers_suite_test.go} (68%) rename controllers/{proxy_controller.go => proxy/controller.go} (76%) create mode 100644 controllers/proxy/controller_test.go create mode 100644 controllers/proxy/proxy_suite_test.go diff --git a/.github/workflows/go-tests.yml b/.github/workflows/go-tests.yml index ff857f9..21394a2 100644 --- a/.github/workflows/go-tests.yml +++ b/.github/workflows/go-tests.yml @@ -18,4 +18,4 @@ jobs: go-tools: true - name: Run tests run: | - ginkgo test ./... + make test diff --git a/.tool-versions b/.tool-versions index 42ccdc2..abca2a3 100644 --- a/.tool-versions +++ b/.tool-versions @@ -1,4 +1,4 @@ adr-tools 3.0.0 -golang 1.19.4 +golang 1.19.5 kubebuilder 3.8.0 golangci-lint 1.50.1 \ No newline at end of file diff --git a/Makefile b/Makefile index c4623ba..5ffa0e2 100644 --- a/Makefile +++ b/Makefile @@ -67,7 +67,7 @@ vet: ## Run go vet against code. go vet ./... .PHONY: test -test: manifests generate fmt vet envtest ## Run tests. +test: manifests generate envtest ## Run tests. KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out ##@ Build diff --git a/controllers/controller.go b/controllers/controller.go new file mode 100644 index 0000000..f9a3c88 --- /dev/null +++ b/controllers/controller.go @@ -0,0 +1,12 @@ +package controllers + +import ( + "k8s.io/apimachinery/pkg/runtime" + "sigs.k8s.io/controller-runtime/pkg/client" +) + +// Reconciler holds generic reconciler information. +type Reconciler struct { + Client client.Client + Scheme *runtime.Scheme +} diff --git a/controllers/suite_test.go b/controllers/controllers_suite_test.go similarity index 68% rename from controllers/suite_test.go rename to controllers/controllers_suite_test.go index 33646dc..b8954e2 100644 --- a/controllers/suite_test.go +++ b/controllers/controllers_suite_test.go @@ -1,6 +1,7 @@ -package controllers +package controllers_test import ( + "context" "path/filepath" "testing" @@ -9,12 +10,15 @@ import ( "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" infrakitv1alpha1 "github.com/INFURA/infrakit/api/v1alpha1" + "github.com/INFURA/infrakit/controllers" + "github.com/INFURA/infrakit/controllers/proxy" //+kubebuilder:scaffold:imports ) @@ -24,6 +28,8 @@ import ( var cfg *rest.Config var k8sClient client.Client var testEnv *envtest.Environment +var ctx context.Context +var cancel context.CancelFunc func TestAPIs(t *testing.T) { RegisterFailHandler(Fail) @@ -34,6 +40,8 @@ func TestAPIs(t *testing.T) { var _ = BeforeSuite(func() { logf.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")}, @@ -55,6 +63,22 @@ var _ = BeforeSuite(func() { Expect(err).NotTo(HaveOccurred()) Expect(k8sClient).NotTo(BeNil()) + k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{ + Scheme: scheme.Scheme, + }) + Expect(err).ToNot(HaveOccurred()) + + err = (&proxy.MainReconciler{Reconciler: controllers.Reconciler{ + Client: k8sManager.GetClient(), + Scheme: k8sManager.GetScheme(), + }}).SetupWithManager(k8sManager) + Expect(err).ToNot(HaveOccurred()) + + go func() { + defer GinkgoRecover() + err = k8sManager.Start(ctx) + Expect(err).ToNot(HaveOccurred(), "failed to run manager") + }() }) var _ = AfterSuite(func() { diff --git a/controllers/proxy_controller.go b/controllers/proxy/controller.go similarity index 76% rename from controllers/proxy_controller.go rename to controllers/proxy/controller.go index 5d03d16..df32cbc 100644 --- a/controllers/proxy_controller.go +++ b/controllers/proxy/controller.go @@ -1,20 +1,18 @@ -package controllers +package proxy import ( "context" - "k8s.io/apimachinery/pkg/runtime" + "github.com/INFURA/infrakit/controllers" ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/log" infrakitv1alpha1 "github.com/INFURA/infrakit/api/v1alpha1" ) -// ProxyReconciler reconciles a Proxy object -type ProxyReconciler struct { - client.Client - Scheme *runtime.Scheme +// MainReconciler reconciles a Proxy object +type MainReconciler struct { + controllers.Reconciler } //+kubebuilder:rbac:groups=infrakit.infura.io,resources=proxies,verbs=get;list;watch;create;update;patch;delete @@ -30,7 +28,7 @@ type ProxyReconciler struct { // // For more details, check Reconcile and its Result here: // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.13.1/pkg/reconcile -func (r *ProxyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { +func (r *MainReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { _ = log.FromContext(ctx) // TODO(user): your logic here @@ -39,7 +37,7 @@ func (r *ProxyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl } // SetupWithManager sets up the controller with the Manager. -func (r *ProxyReconciler) SetupWithManager(mgr ctrl.Manager) error { +func (r *MainReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). For(&infrakitv1alpha1.Proxy{}). Complete(r) diff --git a/controllers/proxy/controller_test.go b/controllers/proxy/controller_test.go new file mode 100644 index 0000000..38b7f51 --- /dev/null +++ b/controllers/proxy/controller_test.go @@ -0,0 +1,23 @@ +package proxy_test + +import ( + "context" + + infrakitv1alpha1 "github.com/INFURA/infrakit/api/v1alpha1" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +var _ = Describe("Proxy controller", func() { + It("Works", func() { + ctx := context.Background() + proxy := &infrakitv1alpha1.Proxy{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-proxy", + Namespace: "default", + }, + } + Expect(k8sClient.Create(ctx, proxy)).Should(Succeed()) + }) +}) diff --git a/controllers/proxy/proxy_suite_test.go b/controllers/proxy/proxy_suite_test.go new file mode 100644 index 0000000..768fe48 --- /dev/null +++ b/controllers/proxy/proxy_suite_test.go @@ -0,0 +1,89 @@ +package proxy_test + +import ( + "context" + "path/filepath" + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + "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" + + infrakitv1alpha1 "github.com/INFURA/infrakit/api/v1alpha1" + "github.com/INFURA/infrakit/controllers" + "github.com/INFURA/infrakit/controllers/proxy" + //+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 testEnv *envtest.Environment +var ctx context.Context +var cancel context.CancelFunc + +func TestAPIs(t *testing.T) { + RegisterFailHandler(Fail) + + RunSpecs(t, "Controller Suite") +} + +var _ = BeforeSuite(func() { + logf.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")}, + ErrorIfCRDPathMissing: true, + } + + var err error + // cfg is defined in this file globally. + cfg, err = testEnv.Start() + Expect(err).NotTo(HaveOccurred()) + Expect(cfg).NotTo(BeNil()) + + err = infrakitv1alpha1.AddToScheme(scheme.Scheme) + Expect(err).NotTo(HaveOccurred()) + + //+kubebuilder:scaffold:scheme + + k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) + Expect(err).NotTo(HaveOccurred()) + Expect(k8sClient).NotTo(BeNil()) + + k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{ + Scheme: scheme.Scheme, + }) + Expect(err).ToNot(HaveOccurred()) + + err = (&proxy.MainReconciler{controllers.Reconciler{ + Client: k8sManager.GetClient(), + Scheme: k8sManager.GetScheme(), + }}).SetupWithManager(k8sManager) + Expect(err).ToNot(HaveOccurred()) + + go func() { + defer GinkgoRecover() + err = k8sManager.Start(ctx) + Expect(err).ToNot(HaveOccurred(), "failed to run manager") + }() +}) + +var _ = AfterSuite(func() { + cancel() + By("tearing down the test environment") + err := testEnv.Stop() + Expect(err).NotTo(HaveOccurred()) +}) diff --git a/main.go b/main.go index 6aeff3b..476cecd 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,7 @@ import ( infrakitv1alpha1 "github.com/INFURA/infrakit/api/v1alpha1" "github.com/INFURA/infrakit/controllers" + "github.com/INFURA/infrakit/controllers/proxy" //+kubebuilder:scaffold:imports ) @@ -73,10 +74,10 @@ func main() { os.Exit(1) } - if err = (&controllers.ProxyReconciler{ + if err = (&proxy.MainReconciler{Reconciler: controllers.Reconciler{ Client: mgr.GetClient(), Scheme: mgr.GetScheme(), - }).SetupWithManager(mgr); err != nil { + }}).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "Proxy") os.Exit(1) }