@@ -547,30 +547,35 @@ func TestGitRepositoryReconciler_reconcileSource_checkoutStrategy(t *testing.T)
547
547
name string
548
548
skipForImplementation string
549
549
reference * sourcev1.GitRepositoryRef
550
+ beforeFunc func (obj * sourcev1.GitRepository , latestRev string )
550
551
want sreconcile.Result
551
552
wantErr bool
552
553
wantRevision string
554
+ wantArtifactOutdated bool
553
555
}{
554
556
{
555
- name : "Nil reference (default branch)" ,
556
- want : sreconcile .ResultSuccess ,
557
- wantRevision : "master/<commit>" ,
557
+ name : "Nil reference (default branch)" ,
558
+ want : sreconcile .ResultSuccess ,
559
+ wantRevision : "master/<commit>" ,
560
+ wantArtifactOutdated : true ,
558
561
},
559
562
{
560
563
name : "Branch" ,
561
564
reference : & sourcev1.GitRepositoryRef {
562
565
Branch : "staging" ,
563
566
},
564
- want : sreconcile .ResultSuccess ,
565
- wantRevision : "staging/<commit>" ,
567
+ want : sreconcile .ResultSuccess ,
568
+ wantRevision : "staging/<commit>" ,
569
+ wantArtifactOutdated : true ,
566
570
},
567
571
{
568
572
name : "Tag" ,
569
573
reference : & sourcev1.GitRepositoryRef {
570
574
Tag : "v0.1.0" ,
571
575
},
572
- want : sreconcile .ResultSuccess ,
573
- wantRevision : "v0.1.0/<commit>" ,
576
+ want : sreconcile .ResultSuccess ,
577
+ wantRevision : "v0.1.0/<commit>" ,
578
+ wantArtifactOutdated : true ,
574
579
},
575
580
{
576
581
name : "Branch commit" ,
@@ -579,8 +584,9 @@ func TestGitRepositoryReconciler_reconcileSource_checkoutStrategy(t *testing.T)
579
584
Branch : "staging" ,
580
585
Commit : "<commit>" ,
581
586
},
582
- want : sreconcile .ResultSuccess ,
583
- wantRevision : "staging/<commit>" ,
587
+ want : sreconcile .ResultSuccess ,
588
+ wantRevision : "staging/<commit>" ,
589
+ wantArtifactOutdated : true ,
584
590
},
585
591
{
586
592
name : "Branch commit" ,
@@ -589,32 +595,56 @@ func TestGitRepositoryReconciler_reconcileSource_checkoutStrategy(t *testing.T)
589
595
Branch : "staging" ,
590
596
Commit : "<commit>" ,
591
597
},
592
- want : sreconcile .ResultSuccess ,
593
- wantRevision : "HEAD/<commit>" ,
598
+ want : sreconcile .ResultSuccess ,
599
+ wantRevision : "HEAD/<commit>" ,
600
+ wantArtifactOutdated : true ,
594
601
},
595
602
{
596
603
name : "SemVer" ,
597
604
reference : & sourcev1.GitRepositoryRef {
598
605
SemVer : "*" ,
599
606
},
600
- want : sreconcile .ResultSuccess ,
601
- wantRevision : "v2.0.0/<commit>" ,
607
+ want : sreconcile .ResultSuccess ,
608
+ wantRevision : "v2.0.0/<commit>" ,
609
+ wantArtifactOutdated : true ,
602
610
},
603
611
{
604
612
name : "SemVer range" ,
605
613
reference : & sourcev1.GitRepositoryRef {
606
614
SemVer : "<v0.2.1" ,
607
615
},
608
- want : sreconcile .ResultSuccess ,
609
- wantRevision : "0.2.0/<commit>" ,
616
+ want : sreconcile .ResultSuccess ,
617
+ wantRevision : "0.2.0/<commit>" ,
618
+ wantArtifactOutdated : true ,
610
619
},
611
620
{
612
621
name : "SemVer prerelease" ,
613
622
reference : & sourcev1.GitRepositoryRef {
614
623
SemVer : ">=1.0.0-0 <1.1.0-0" ,
615
624
},
616
- wantRevision : "v1.0.0-alpha/<commit>" ,
617
- want : sreconcile .ResultSuccess ,
625
+ wantRevision : "v1.0.0-alpha/<commit>" ,
626
+ want : sreconcile .ResultSuccess ,
627
+ wantArtifactOutdated : true ,
628
+ },
629
+ {
630
+ name : "Optimized clone" ,
631
+ reference : & sourcev1.GitRepositoryRef {
632
+ Branch : "staging" ,
633
+ },
634
+ beforeFunc : func (obj * sourcev1.GitRepository , latestRev string ) {
635
+ // Add existing artifact on the object and storage.
636
+ obj .Status = sourcev1.GitRepositoryStatus {
637
+ Artifact : & sourcev1.Artifact {
638
+ Revision : "staging/" + latestRev ,
639
+ Path : randStringRunes (10 ),
640
+ },
641
+ }
642
+ testStorage .Archive (obj .GetArtifact (), "testdata/git/repository" , nil )
643
+ conditions .MarkTrue (obj , sourcev1 .ArtifactInStorageCondition , meta .SucceededReason , "foo" )
644
+ },
645
+ want : sreconcile .ResultSuccess ,
646
+ wantRevision : "staging/<commit>" ,
647
+ wantArtifactOutdated : false ,
618
648
},
619
649
}
620
650
@@ -677,6 +707,10 @@ func TestGitRepositoryReconciler_reconcileSource_checkoutStrategy(t *testing.T)
677
707
obj := obj .DeepCopy ()
678
708
obj .Spec .GitImplementation = i
679
709
710
+ if tt .beforeFunc != nil {
711
+ tt .beforeFunc (obj , headRef .Hash ().String ())
712
+ }
713
+
680
714
var commit git.Commit
681
715
var includes artifactSet
682
716
got , err := r .reconcileSource (ctx , obj , & commit , & includes , tmpDir )
@@ -685,10 +719,10 @@ func TestGitRepositoryReconciler_reconcileSource_checkoutStrategy(t *testing.T)
685
719
}
686
720
g .Expect (err != nil ).To (Equal (tt .wantErr ))
687
721
g .Expect (got ).To (Equal (tt .want ))
688
- if tt .wantRevision != "" {
722
+ if tt .wantRevision != "" && ! tt . wantErr {
689
723
revision := strings .ReplaceAll (tt .wantRevision , "<commit>" , headRef .Hash ().String ())
690
724
g .Expect (commit .String ()).To (Equal (revision ))
691
- g .Expect (conditions .IsTrue (obj , sourcev1 .ArtifactOutdatedCondition )).To (BeTrue ( ))
725
+ g .Expect (conditions .IsTrue (obj , sourcev1 .ArtifactOutdatedCondition )).To (Equal ( tt . wantArtifactOutdated ))
692
726
}
693
727
})
694
728
}
@@ -1782,10 +1816,20 @@ func TestGitRepositoryReconciler_statusConditions(t *testing.T) {
1782
1816
}
1783
1817
1784
1818
func TestGitRepositoryReconciler_notify (t * testing.T ) {
1819
+ concreteCommit := git.Commit {
1820
+ Hash : git .Hash ("some-hash" ),
1821
+ Message : "test commit" ,
1822
+ Encoded : []byte ("commit content" ),
1823
+ }
1824
+ partialCommit := git.Commit {
1825
+ Hash : git .Hash ("some-hash" ),
1826
+ }
1827
+
1785
1828
tests := []struct {
1786
1829
name string
1787
1830
res sreconcile.Result
1788
1831
resErr error
1832
+ commit git.Commit
1789
1833
oldObjBeforeFunc func (obj * sourcev1.GitRepository )
1790
1834
newObjBeforeFunc func (obj * sourcev1.GitRepository )
1791
1835
wantEvent string
@@ -1799,6 +1843,7 @@ func TestGitRepositoryReconciler_notify(t *testing.T) {
1799
1843
name : "new artifact" ,
1800
1844
res : sreconcile .ResultSuccess ,
1801
1845
resErr : nil ,
1846
+ commit : concreteCommit ,
1802
1847
newObjBeforeFunc : func (obj * sourcev1.GitRepository ) {
1803
1848
obj .Status .Artifact = & sourcev1.Artifact {Revision : "xxx" , Checksum : "yyy" }
1804
1849
},
@@ -1808,6 +1853,7 @@ func TestGitRepositoryReconciler_notify(t *testing.T) {
1808
1853
name : "recovery from failure" ,
1809
1854
res : sreconcile .ResultSuccess ,
1810
1855
resErr : nil ,
1856
+ commit : concreteCommit ,
1811
1857
oldObjBeforeFunc : func (obj * sourcev1.GitRepository ) {
1812
1858
obj .Status .Artifact = & sourcev1.Artifact {Revision : "xxx" , Checksum : "yyy" }
1813
1859
conditions .MarkTrue (obj , sourcev1 .FetchFailedCondition , sourcev1 .GitOperationFailedReason , "fail" )
@@ -1823,6 +1869,7 @@ func TestGitRepositoryReconciler_notify(t *testing.T) {
1823
1869
name : "recovery and new artifact" ,
1824
1870
res : sreconcile .ResultSuccess ,
1825
1871
resErr : nil ,
1872
+ commit : concreteCommit ,
1826
1873
oldObjBeforeFunc : func (obj * sourcev1.GitRepository ) {
1827
1874
obj .Status .Artifact = & sourcev1.Artifact {Revision : "xxx" , Checksum : "yyy" }
1828
1875
conditions .MarkTrue (obj , sourcev1 .FetchFailedCondition , sourcev1 .GitOperationFailedReason , "fail" )
@@ -1838,6 +1885,7 @@ func TestGitRepositoryReconciler_notify(t *testing.T) {
1838
1885
name : "no updates" ,
1839
1886
res : sreconcile .ResultSuccess ,
1840
1887
resErr : nil ,
1888
+ commit : concreteCommit ,
1841
1889
oldObjBeforeFunc : func (obj * sourcev1.GitRepository ) {
1842
1890
obj .Status .Artifact = & sourcev1.Artifact {Revision : "xxx" , Checksum : "yyy" }
1843
1891
conditions .MarkTrue (obj , meta .ReadyCondition , meta .SucceededReason , "ready" )
@@ -1847,6 +1895,22 @@ func TestGitRepositoryReconciler_notify(t *testing.T) {
1847
1895
conditions .MarkTrue (obj , meta .ReadyCondition , meta .SucceededReason , "ready" )
1848
1896
},
1849
1897
},
1898
+ {
1899
+ name : "recovery with no-op clone commit" ,
1900
+ res : sreconcile .ResultSuccess ,
1901
+ resErr : nil ,
1902
+ commit : partialCommit ,
1903
+ oldObjBeforeFunc : func (obj * sourcev1.GitRepository ) {
1904
+ obj .Status .Artifact = & sourcev1.Artifact {Revision : "xxx" , Checksum : "yyy" }
1905
+ conditions .MarkTrue (obj , sourcev1 .FetchFailedCondition , sourcev1 .GitOperationFailedReason , "fail" )
1906
+ conditions .MarkFalse (obj , meta .ReadyCondition , meta .FailedReason , "foo" )
1907
+ },
1908
+ newObjBeforeFunc : func (obj * sourcev1.GitRepository ) {
1909
+ obj .Status .Artifact = & sourcev1.Artifact {Revision : "aaa" , Checksum : "bbb" }
1910
+ conditions .MarkTrue (obj , meta .ReadyCondition , meta .SucceededReason , "ready" )
1911
+ },
1912
+ wantEvent : "Normal NewArtifact stored artifact for commit hash" ,
1913
+ },
1850
1914
}
1851
1915
1852
1916
for _ , tt := range tests {
@@ -1868,10 +1932,7 @@ func TestGitRepositoryReconciler_notify(t *testing.T) {
1868
1932
EventRecorder : recorder ,
1869
1933
features : features .FeatureGates (),
1870
1934
}
1871
- commit := & git.Commit {
1872
- Message : "test commit" ,
1873
- }
1874
- reconciler .notify (oldObj , newObj , * commit , tt .res , tt .resErr )
1935
+ reconciler .notify (oldObj , newObj , tt .commit , tt .res , tt .resErr )
1875
1936
1876
1937
select {
1877
1938
case x , ok := <- recorder .Events :
0 commit comments