Skip to content

Commit 2e6e60e

Browse files
committed
Refactor controller test structure
1 parent 34eb2d7 commit 2e6e60e

File tree

1 file changed

+90
-75
lines changed

1 file changed

+90
-75
lines changed

internal/controller/function_controller_test.go

Lines changed: 90 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
. "github.com/onsi/ginkgo/v2"
2525
. "github.com/onsi/gomega"
2626
"github.com/stretchr/testify/mock"
27-
"k8s.io/apimachinery/pkg/api/errors"
2827
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2928
"k8s.io/apimachinery/pkg/types"
3029
"k8s.io/client-go/tools/record"
@@ -46,29 +45,16 @@ var _ = Describe("Function Controller", func() {
4645
Name: resourceName,
4746
Namespace: resourceNamespace,
4847
}
49-
function := &functionsdevv1alpha1.Function{}
50-
51-
BeforeEach(func() {
52-
By("creating the custom resource for the Kind Function")
53-
err := k8sClient.Get(ctx, typeNamespacedName, function)
54-
if err != nil && errors.IsNotFound(err) {
55-
resource := &functionsdevv1alpha1.Function{
56-
ObjectMeta: metav1.ObjectMeta{
57-
Name: resourceName,
58-
Namespace: resourceNamespace,
59-
},
60-
Spec: functionsdevv1alpha1.FunctionSpec{
61-
Source: functionsdevv1alpha1.FunctionSpecSource{
62-
RepositoryURL: "https://github.com/foo/bar",
63-
},
64-
Registry: functionsdevv1alpha1.FunctionSpecRegistry{
65-
Path: "quay.io/foo/bar",
66-
},
67-
},
68-
}
69-
Expect(k8sClient.Create(ctx, resource)).To(Succeed())
70-
}
71-
})
48+
49+
defaultSpec := functionsdevv1alpha1.FunctionSpec{
50+
Source: functionsdevv1alpha1.FunctionSpecSource{
51+
RepositoryURL: "https://github.com/foo/bar",
52+
Reference: "my-branch",
53+
},
54+
Registry: functionsdevv1alpha1.FunctionSpecRegistry{
55+
Path: "quay.io/foo/bar",
56+
},
57+
}
7258

7359
AfterEach(func() {
7460
resource := &functionsdevv1alpha1.Function{}
@@ -79,60 +65,23 @@ var _ = Describe("Function Controller", func() {
7965
Expect(k8sClient.Delete(ctx, resource)).To(Succeed())
8066
})
8167

82-
Context("should successfully reconcile the resource", func() {
83-
It("should deploy when middleware update required", func() {
84-
By("Reconciling the created resource")
85-
funcCliManagerMock := funccli.NewMockManager(GinkgoT())
86-
funcCliManagerMock.EXPECT().Describe(mock.Anything, functionName, resourceNamespace).Return(functions.Instance{
87-
Name: "",
88-
Image: "quay.io/foo/bar@sha256:foobar",
89-
Namespace: resourceNamespace,
90-
Middleware: functions.Middleware{
91-
Version: "v1.0.0",
92-
},
93-
}, nil)
94-
funcCliManagerMock.EXPECT().GetLatestMiddlewareVersion(mock.Anything, mock.Anything, mock.Anything).Return("v2.0.0", nil)
95-
funcCliManagerMock.EXPECT().GetMiddlewareVersion(mock.Anything, functionName, resourceNamespace).Return("v1.0.0", nil)
96-
funcCliManagerMock.EXPECT().Deploy(mock.Anything, mock.Anything, resourceNamespace, funccli.DeployOptions{
97-
Registry: "quay.io/foo/bar",
98-
GitUrl: "https://github.com/foo/bar",
99-
Builder: "s2i",
100-
}).Return(nil)
101-
102-
gitManagerMock := git.NewMockManager(GinkgoT())
103-
gitManagerMock.EXPECT().CloneRepository(mock.Anything, "https://github.com/foo/bar", "main").Return(&git.Repository{CloneDir: "testdata/foo-bar"}, nil)
104-
105-
controllerReconciler := &FunctionReconciler{
106-
Client: k8sClient,
107-
Scheme: k8sClient.Scheme(),
108-
Recorder: &record.FakeRecorder{},
109-
FuncCliManager: funcCliManagerMock,
110-
GitManager: gitManagerMock,
111-
}
68+
type reconcileTestCase struct {
69+
spec functionsdevv1alpha1.FunctionSpec
70+
configureMocks func(*funccli.MockManager, *git.MockManager)
71+
}
11272

113-
_, err := controllerReconciler.Reconcile(ctx, reconcile.Request{
114-
NamespacedName: typeNamespacedName,
115-
})
73+
DescribeTable("should successfully reconcile the resource",
74+
func(tc reconcileTestCase) {
75+
By("creating the Function")
76+
err := createFunctionResource(resourceName, resourceNamespace, tc.spec)
11677
Expect(err).NotTo(HaveOccurred())
117-
})
11878

119-
It("should skip deploy when middleware already up to date", func() {
120-
By("Reconciling the created resource")
79+
By("Setting up mocks")
12180
funcCliManagerMock := funccli.NewMockManager(GinkgoT())
122-
funcCliManagerMock.EXPECT().Describe(mock.Anything, functionName, resourceNamespace).Return(functions.Instance{
123-
Name: "",
124-
Image: "quay.io/foo/bar@sha256:foobar",
125-
Namespace: resourceNamespace,
126-
Middleware: functions.Middleware{
127-
Version: "v1.0.0",
128-
},
129-
}, nil)
130-
funcCliManagerMock.EXPECT().GetLatestMiddlewareVersion(mock.Anything, mock.Anything, mock.Anything).Return("v1.0.0", nil)
131-
funcCliManagerMock.EXPECT().GetMiddlewareVersion(mock.Anything, functionName, resourceNamespace).Return("v1.0.0", nil)
132-
13381
gitManagerMock := git.NewMockManager(GinkgoT())
134-
gitManagerMock.EXPECT().CloneRepository(mock.Anything, "https://github.com/foo/bar", "main").Return(&git.Repository{CloneDir: "testdata/foo-bar"}, nil)
82+
tc.configureMocks(funcCliManagerMock, gitManagerMock)
13583

84+
By("Reconciling the created resource")
13685
controllerReconciler := &FunctionReconciler{
13786
Client: k8sClient,
13887
Scheme: k8sClient.Scheme(),
@@ -141,11 +90,77 @@ var _ = Describe("Function Controller", func() {
14190
GitManager: gitManagerMock,
14291
}
14392

144-
_, err := controllerReconciler.Reconcile(ctx, reconcile.Request{
93+
_, err = controllerReconciler.Reconcile(ctx, reconcile.Request{
14594
NamespacedName: typeNamespacedName,
14695
})
14796
Expect(err).NotTo(HaveOccurred())
148-
})
149-
})
97+
},
98+
Entry("should deploy when middleware update required", reconcileTestCase{
99+
spec: defaultSpec,
100+
configureMocks: func(funcMock *funccli.MockManager, gitMock *git.MockManager) {
101+
funcMock.EXPECT().Describe(mock.Anything, functionName, resourceNamespace).Return(functions.Instance{
102+
Middleware: functions.Middleware{
103+
Version: "v1.0.0",
104+
},
105+
}, nil)
106+
funcMock.EXPECT().GetLatestMiddlewareVersion(mock.Anything, mock.Anything, mock.Anything).Return("v2.0.0", nil)
107+
funcMock.EXPECT().GetMiddlewareVersion(mock.Anything, functionName, resourceNamespace).Return("v1.0.0", nil)
108+
funcMock.EXPECT().Deploy(mock.Anything, mock.Anything, resourceNamespace, funccli.DeployOptions{
109+
Registry: "quay.io/foo/bar",
110+
GitUrl: "https://github.com/foo/bar",
111+
Builder: "s2i",
112+
}).Return(nil)
113+
114+
gitMock.EXPECT().CloneRepository(mock.Anything, "https://github.com/foo/bar", "my-branch").Return(&git.Repository{CloneDir: "testdata/foo-bar"}, nil)
115+
},
116+
}),
117+
Entry("should skip deploy when middleware already up to date", reconcileTestCase{
118+
spec: defaultSpec,
119+
configureMocks: func(funcMock *funccli.MockManager, gitMock *git.MockManager) {
120+
funcMock.EXPECT().Describe(mock.Anything, functionName, resourceNamespace).Return(functions.Instance{
121+
Middleware: functions.Middleware{
122+
Version: "v1.0.0",
123+
},
124+
}, nil)
125+
funcMock.EXPECT().GetLatestMiddlewareVersion(mock.Anything, mock.Anything, mock.Anything).Return("v1.0.0", nil)
126+
funcMock.EXPECT().GetMiddlewareVersion(mock.Anything, functionName, resourceNamespace).Return("v1.0.0", nil)
127+
128+
gitMock.EXPECT().CloneRepository(mock.Anything, "https://github.com/foo/bar", "my-branch").Return(&git.Repository{CloneDir: "testdata/foo-bar"}, nil)
129+
},
130+
}),
131+
Entry("should use main as default branch", reconcileTestCase{
132+
spec: functionsdevv1alpha1.FunctionSpec{
133+
Source: functionsdevv1alpha1.FunctionSpecSource{
134+
RepositoryURL: "https://github.com/foo/bar",
135+
},
136+
Registry: functionsdevv1alpha1.FunctionSpecRegistry{
137+
Path: "quay.io/foo/bar",
138+
},
139+
},
140+
configureMocks: func(funcMock *funccli.MockManager, gitMock *git.MockManager) {
141+
funcMock.EXPECT().Describe(mock.Anything, functionName, resourceNamespace).Return(functions.Instance{
142+
Middleware: functions.Middleware{
143+
Version: "v1.0.0",
144+
},
145+
}, nil)
146+
funcMock.EXPECT().GetLatestMiddlewareVersion(mock.Anything, mock.Anything, mock.Anything).Return("v1.0.0", nil)
147+
funcMock.EXPECT().GetMiddlewareVersion(mock.Anything, functionName, resourceNamespace).Return("v1.0.0", nil)
148+
149+
gitMock.EXPECT().CloneRepository(mock.Anything, "https://github.com/foo/bar", "main").Return(&git.Repository{CloneDir: "testdata/foo-bar"}, nil)
150+
},
151+
}),
152+
)
150153
})
151154
})
155+
156+
func createFunctionResource(name, namespace string, spec functionsdevv1alpha1.FunctionSpec) error {
157+
resource := functionsdevv1alpha1.Function{
158+
ObjectMeta: metav1.ObjectMeta{
159+
Name: name,
160+
Namespace: namespace,
161+
},
162+
Spec: spec,
163+
}
164+
165+
return k8sClient.Create(ctx, &resource)
166+
}

0 commit comments

Comments
 (0)