@@ -27,9 +27,11 @@ import (
2727 "github.com/go-git/go-billy/v5/memfs"
2828 "github.com/go-git/go-git/v5"
2929 "github.com/go-git/go-git/v5/config"
30+ "github.com/go-git/go-git/v5/plumbing"
3031 "github.com/go-git/go-git/v5/plumbing/object"
3132 "github.com/go-git/go-git/v5/storage/memory"
3233 . "github.com/onsi/ginkgo"
34+ . "github.com/onsi/ginkgo/extensions/table"
3335 . "github.com/onsi/gomega"
3436 corev1 "k8s.io/api/core/v1"
3537 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -201,5 +203,165 @@ var _ = Describe("GitRepositoryReconciler", func() {
201203 }
202204 Eventually (exists (got .Status .Artifact .Path ), timeout , interval ).ShouldNot (BeTrue ())
203205 })
206+
207+ type refTestCase struct {
208+ reference * sourcev1.GitRepositoryRef
209+ createBranches []string
210+ createTags []string
211+
212+ waitForReason string
213+ expectStatus corev1.ConditionStatus
214+ expectMessage string
215+ expectRevision string
216+ }
217+
218+ DescribeTable ("Reference test configuration" , func (t refTestCase ) {
219+ err = gitServer .StartHTTP ()
220+ defer os .RemoveAll (gitServer .Root ())
221+ defer gitServer .StopHTTP ()
222+ Expect (err ).NotTo (HaveOccurred ())
223+
224+ u , err := url .Parse (gitServer .HTTPAddress ())
225+ Expect (err ).NotTo (HaveOccurred ())
226+ u .Path = path .Join (u .Path , "repository.git" )
227+
228+ fs := memfs .New ()
229+ gitrepo , err := git .Init (memory .NewStorage (), fs )
230+ Expect (err ).NotTo (HaveOccurred ())
231+
232+ remote , err := gitrepo .CreateRemote (& config.RemoteConfig {
233+ Name : "origin" ,
234+ URLs : []string {u .String ()},
235+ })
236+ Expect (err ).NotTo (HaveOccurred ())
237+
238+ ff , err := fs .Create ("fixture" )
239+ Expect (err ).NotTo (HaveOccurred ())
240+ _ = ff .Close ()
241+
242+ wt , err := gitrepo .Worktree ()
243+ Expect (err ).NotTo (HaveOccurred ())
244+
245+ _ , err = wt .Add (fs .Join ("fixture" ))
246+ Expect (err ).NotTo (HaveOccurred ())
247+
248+ cHash , err := wt .Commit ("Sample" , & git.CommitOptions {Author : & object.Signature {
249+ Name : "John Doe" ,
250+ 251+ When : time .Now (),
252+ }})
253+ Expect (err ).NotTo (HaveOccurred ())
254+ err = remote .Push (& git.PushOptions {})
255+ Expect (err ).NotTo (HaveOccurred ())
256+
257+ for _ , branch := range t .createBranches {
258+ ref := plumbing .NewHashReference (plumbing .ReferenceName ("refs/heads/" + branch ), cHash )
259+ err = gitrepo .Storer .SetReference (ref )
260+ Expect (err ).NotTo (HaveOccurred ())
261+ err = remote .Push (& git.PushOptions {})
262+ Expect (err ).NotTo (HaveOccurred ())
263+ }
264+
265+ for _ , tag := range t .createTags {
266+ ref := plumbing .NewHashReference (plumbing .ReferenceName ("refs/tags/" + tag ), cHash )
267+ err = gitrepo .Storer .SetReference (ref )
268+ Expect (err ).NotTo (HaveOccurred ())
269+ err = remote .Push (& git.PushOptions {
270+ RefSpecs : []config.RefSpec {"refs/tags/*:refs/tags/*" },
271+ })
272+ Expect (err ).NotTo (HaveOccurred ())
273+ }
274+
275+ key := types.NamespacedName {
276+ Name : "gitrepository-sample-" + randStringRunes (5 ),
277+ Namespace : namespace .Name ,
278+ }
279+ created := & sourcev1.GitRepository {
280+ ObjectMeta : metav1.ObjectMeta {
281+ Name : key .Name ,
282+ Namespace : key .Namespace ,
283+ },
284+ Spec : sourcev1.GitRepositorySpec {
285+ URL : u .String (),
286+ Interval : metav1.Duration {Duration : indexInterval },
287+ Reference : t .reference ,
288+ },
289+ }
290+ Expect (k8sClient .Create (context .Background (), created )).Should (Succeed ())
291+ defer k8sClient .Delete (context .Background (), created )
292+
293+ got := & sourcev1.GitRepository {}
294+ var cond sourcev1.SourceCondition
295+ Eventually (func () bool {
296+ _ = k8sClient .Get (context .Background (), key , got )
297+ for _ , c := range got .Status .Conditions {
298+ if c .Reason == t .waitForReason {
299+ cond = c
300+ return true
301+ }
302+ }
303+ return false
304+ }, timeout , interval ).Should (BeTrue ())
305+
306+ Expect (cond .Status ).To (Equal (t .expectStatus ))
307+ Expect (cond .Message ).To (ContainSubstring (t .expectMessage ))
308+ Expect (got .Status .Artifact == nil ).To (Equal (t .expectRevision == "" ))
309+ if t .expectRevision != "" {
310+ Expect (got .Status .Artifact .Revision ).To (Equal (t .expectRevision + "/" + cHash .String ()))
311+ }
312+ },
313+ Entry ("branch" , refTestCase {
314+ reference : & sourcev1.GitRepositoryRef {Branch : "some-branch" },
315+ createBranches : []string {"some-branch" },
316+ waitForReason : sourcev1 .GitOperationSucceedReason ,
317+ expectStatus : corev1 .ConditionTrue ,
318+ expectRevision : "some-branch" ,
319+ }),
320+ Entry ("branch non existing" , refTestCase {
321+ reference : & sourcev1.GitRepositoryRef {Branch : "invalid-branch" },
322+ waitForReason : sourcev1 .GitOperationFailedReason ,
323+ expectStatus : corev1 .ConditionFalse ,
324+ expectMessage : "couldn't find remote ref" ,
325+ }),
326+ Entry ("tag" , refTestCase {
327+ reference : & sourcev1.GitRepositoryRef {Tag : "some-tag" },
328+ createTags : []string {"some-tag" },
329+ waitForReason : sourcev1 .GitOperationSucceedReason ,
330+ expectStatus : corev1 .ConditionTrue ,
331+ expectRevision : "some-tag" ,
332+ }),
333+ Entry ("tag non existing" , refTestCase {
334+ reference : & sourcev1.GitRepositoryRef {Tag : "invalid-tag" },
335+ waitForReason : sourcev1 .GitOperationFailedReason ,
336+ expectStatus : corev1 .ConditionFalse ,
337+ expectMessage : "couldn't find remote ref" ,
338+ }),
339+ Entry ("semver" , refTestCase {
340+ reference : & sourcev1.GitRepositoryRef {SemVer : "1.0.0" },
341+ createTags : []string {"v1.0.0" },
342+ waitForReason : sourcev1 .GitOperationSucceedReason ,
343+ expectStatus : corev1 .ConditionTrue ,
344+ expectRevision : "v1.0.0" ,
345+ }),
346+ Entry ("semver range" , refTestCase {
347+ reference : & sourcev1.GitRepositoryRef {SemVer : ">=0.1.0 <1.0.0" },
348+ createTags : []string {"0.1.0" , "0.1.1" , "0.2.0" , "1.0.0" },
349+ waitForReason : sourcev1 .GitOperationSucceedReason ,
350+ expectStatus : corev1 .ConditionTrue ,
351+ expectRevision : "0.2.0" ,
352+ }),
353+ Entry ("semver invalid" , refTestCase {
354+ reference : & sourcev1.GitRepositoryRef {SemVer : "v1.0.0" },
355+ waitForReason : sourcev1 .GitOperationFailedReason ,
356+ expectStatus : corev1 .ConditionFalse ,
357+ expectMessage : "semver parse range error" ,
358+ }),
359+ Entry ("semver no match" , refTestCase {
360+ reference : & sourcev1.GitRepositoryRef {SemVer : "1.0.0" },
361+ waitForReason : sourcev1 .GitOperationFailedReason ,
362+ expectStatus : corev1 .ConditionFalse ,
363+ expectMessage : "no match found for semver: 1.0.0" ,
364+ }),
365+ )
204366 })
205367})
0 commit comments