@@ -75,6 +75,7 @@ var _ = Describe("Function Controller", func() {
7575 type reconcileTestCase struct {
7676 spec functionsdevv1alpha1.FunctionSpec
7777 configureMocks func (* funccli.MockManager , * git.MockManager )
78+ statusChecks func (* functionsdevv1alpha1.FunctionStatus )
7879 }
7980
8081 DescribeTable ("should successfully reconcile the resource" ,
@@ -101,6 +102,14 @@ var _ = Describe("Function Controller", func() {
101102 NamespacedName : typeNamespacedName ,
102103 })
103104 Expect (err ).NotTo (HaveOccurred ())
105+
106+ if tc .statusChecks != nil {
107+ f := & functionsdevv1alpha1.Function {}
108+ err := k8sClient .Get (ctx , typeNamespacedName , f )
109+ Expect (err ).NotTo (HaveOccurred ())
110+
111+ tc .statusChecks (& f .Status )
112+ }
104113 },
105114 Entry ("should deploy when middleware update required" , reconcileTestCase {
106115 spec : defaultSpec ,
@@ -149,6 +158,30 @@ var _ = Describe("Function Controller", func() {
149158 gitMock .EXPECT ().CloneRepository (mock .Anything , "https://github.com/foo/bar" , "" , "main" , mock .Anything ).Return (createTmpGitRepo (functions.Function {Name : "func-go" }), nil )
150159 },
151160 }),
161+
162+ Entry ("should contain the git information in the status" , reconcileTestCase {
163+ spec : functionsdevv1alpha1.FunctionSpec {
164+ Repository : functionsdevv1alpha1.FunctionSpecRepository {
165+ URL : "https://github.com/foo/bar" ,
166+ Branch : "my-branch" ,
167+ },
168+ },
169+ configureMocks : func (funcMock * funccli.MockManager , gitMock * git.MockManager ) {
170+ funcMock .EXPECT ().Describe (mock .Anything , functionName , resourceNamespace ).Return (functions.Instance {
171+ Middleware : functions.Middleware {
172+ Version : "v1.0.0" ,
173+ },
174+ }, nil )
175+ funcMock .EXPECT ().GetLatestMiddlewareVersion (mock .Anything , mock .Anything , mock .Anything ).Return ("v1.0.0" , nil )
176+ funcMock .EXPECT ().GetMiddlewareVersion (mock .Anything , functionName , resourceNamespace ).Return ("v1.0.0" , nil )
177+
178+ gitMock .EXPECT ().CloneRepository (mock .Anything , "https://github.com/foo/bar" , "" , "my-branch" , mock .Anything ).Return (createTmpGitRepo (functions.Function {Name : "func-go" }, WithRepoOptionBranch ("my-branch" ), WithRepoOptionCommit ("foobar" )), nil )
179+ },
180+ statusChecks : func (status * functionsdevv1alpha1.FunctionStatus ) {
181+ Expect (status .Git .ResolvedBranch ).Should (Equal ("my-branch" ))
182+ Expect (status .Git .ObservedCommit ).Should (Equal ("foobar" ))
183+ },
184+ }),
152185 )
153186 })
154187})
@@ -165,7 +198,9 @@ func createFunctionResource(name, namespace string, spec functionsdevv1alpha1.Fu
165198 return k8sClient .Create (ctx , & resource )
166199}
167200
168- func createTmpGitRepo (function functions.Function ) * git.Repository {
201+ type RepoOption func (* git.Repository )
202+
203+ func createTmpGitRepo (function functions.Function , repoOptions ... RepoOption ) * git.Repository {
169204 tempDir , err := os .MkdirTemp ("" , function .Name )
170205 Expect (err ).NotTo (HaveOccurred ())
171206
@@ -176,8 +211,31 @@ func createTmpGitRepo(function functions.Function) *git.Repository {
176211 err = os .WriteFile (funcYamlPath , f , 0644 )
177212 Expect (err ).NotTo (HaveOccurred ())
178213
179- return & git.Repository {
214+ opts := & git.Repository {
180215 CloneDir : tempDir ,
181- SubPath : "." ,
216+ }
217+
218+ for _ , repoOption := range repoOptions {
219+ repoOption (opts )
220+ }
221+
222+ return opts
223+ }
224+
225+ func WithRepoOptionSubPath (subPath string ) RepoOption {
226+ return func (repo * git.Repository ) {
227+ repo .SubPath = subPath
228+ }
229+ }
230+
231+ func WithRepoOptionBranch (branch string ) RepoOption {
232+ return func (repo * git.Repository ) {
233+ repo .Branch = branch
234+ }
235+ }
236+
237+ func WithRepoOptionCommit (commit string ) RepoOption {
238+ return func (repo * git.Repository ) {
239+ repo .Commit = commit
182240 }
183241}
0 commit comments