Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
fix: add tests (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
timmyers authored Jan 11, 2023
1 parent 1d1ae6a commit 54fed4c
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ jobs:
go-tools: true
- name: Run tests
run: |
ginkgo test ./...
make test
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions controllers/controller.go
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controllers
package controllers_test

import (
"context"
"path/filepath"
"testing"

Expand All @@ -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
)

Expand All @@ -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)
Expand All @@ -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")},
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -30,7 +28,7 @@ type ProxyReconciler struct {
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/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
Expand All @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions controllers/proxy/controller_test.go
Original file line number Diff line number Diff line change
@@ -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())
})
})
89 changes: 89 additions & 0 deletions controllers/proxy/proxy_suite_test.go
Original file line number Diff line number Diff line change
@@ -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())
})
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 54fed4c

Please sign in to comment.