Skip to content

Commit

Permalink
Move common controller setup method to its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
bastjan committed Feb 14, 2023
1 parent f99eb8f commit 72c593a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
30 changes: 30 additions & 0 deletions controllers/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package controllers_test

import (
"testing"

"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

billingv1 "github.com/appuio/control-api/apis/billing/v1"
orgv1 "github.com/appuio/control-api/apis/organization/v1"
userv1 "github.com/appuio/control-api/apis/user/v1"
controlv1 "github.com/appuio/control-api/apis/v1"
)

func prepareTest(t *testing.T, initObjs ...client.Object) client.WithWatch {
scheme := runtime.NewScheme()
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(orgv1.AddToScheme(scheme))
utilruntime.Must(controlv1.AddToScheme(scheme))
utilruntime.Must(billingv1.AddToScheme(scheme))
utilruntime.Must(userv1.AddToScheme(scheme))

return fake.NewClientBuilder().
WithScheme(scheme).
WithObjects(initObjs...).
Build()
}
22 changes: 0 additions & 22 deletions controllers/user_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,10 @@ import (
"github.com/stretchr/testify/require"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

billingv1 "github.com/appuio/control-api/apis/billing/v1"
orgv1 "github.com/appuio/control-api/apis/organization/v1"
userv1 "github.com/appuio/control-api/apis/user/v1"
controlv1 "github.com/appuio/control-api/apis/v1"
. "github.com/appuio/control-api/controllers"
)
Expand Down Expand Up @@ -67,17 +59,3 @@ func Test_UserController_Reconcile_Success(t *testing.T) {
assert.Len(t, crb.Subjects, 1, "ClusterRoleBinding should have subject referencing the user")
assert.Equal(t, userPrefix+subject.Name, crb.Subjects[0].Name, "user name must match and include prefix")
}

func prepareTest(t *testing.T, initObjs ...client.Object) client.WithWatch {
scheme := runtime.NewScheme()
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(orgv1.AddToScheme(scheme))
utilruntime.Must(controlv1.AddToScheme(scheme))
utilruntime.Must(billingv1.AddToScheme(scheme))
utilruntime.Must(userv1.AddToScheme(scheme))

return fake.NewClientBuilder().
WithScheme(scheme).
WithObjects(initObjs...).
Build()
}

0 comments on commit 72c593a

Please sign in to comment.