1
1
package cache
2
2
3
3
import (
4
+ "context"
4
5
"fmt"
5
6
"io/ioutil"
6
7
"os"
7
8
"path"
8
9
"sort"
10
+ "strings"
9
11
"testing"
10
12
"time"
11
13
14
+ "github.com/cenkalti/backoff/v3"
12
15
"github.com/google/go-cmp/cmp"
13
16
"gopkg.in/src-d/go-git.v4"
14
17
"gopkg.in/src-d/go-git.v4/config"
@@ -487,49 +490,49 @@ func sortPipelines(pipelines []Pipeline) {
487
490
})
488
491
}
489
492
490
- func TestGitOriginURL (t * testing.T ) {
491
- setup := func (t * testing.T , remotes []config.RemoteConfig ) (string , string ) {
492
- tmpDir , err := ioutil .TempDir ("" , "" )
493
- if err != nil {
494
- t .Fatal (err )
495
- }
496
- repo , err := git .PlainInit (tmpDir , false )
497
- if err != nil {
498
- t .Fatal (err )
499
- }
500
-
501
- for _ , remoteConfig := range remotes {
502
- if _ , err := repo .CreateRemote (& remoteConfig ); err != nil {
503
- t .Fatal (err )
504
- }
505
- }
493
+ func createRepository (t * testing.T , remotes []config.RemoteConfig ) (string , string ) {
494
+ tmpDir , err := ioutil .TempDir ("" , "" )
495
+ if err != nil {
496
+ t .Fatal (err )
497
+ }
498
+ repo , err := git .PlainInit (tmpDir , false )
499
+ if err != nil {
500
+ t .Fatal (err )
501
+ }
506
502
507
- // Populate repository with single commit
508
- w , err := repo .Worktree ()
509
- if err != nil {
510
- t .Fatal (err )
511
- }
512
- if err := ioutil .WriteFile (path .Join (tmpDir , "file.txt" ), []byte ("abcd" ), os .ModeAppend ); err != nil {
513
- t .Fatal (err )
514
- }
515
- sha , err := w .Commit ("message" , & git.CommitOptions {
516
- Author : & object.Signature {
517
- Name : "name" ,
518
- Email : "email" ,
519
- When : time .Date (2019 , 19 , 12 , 21 , 49 , 0 , 0 , time .UTC ),
520
- },
521
- })
522
- if err != nil {
503
+ for _ , remoteConfig := range remotes {
504
+ if _ , err := repo .CreateRemote (& remoteConfig ); err != nil {
523
505
t .Fatal (err )
524
506
}
507
+ }
525
508
526
- if _ , err := repo .CreateTag ("0.1.0" , sha , nil ); err != nil {
527
- t .Fatal (err )
528
- }
509
+ // Populate repository with single commit
510
+ w , err := repo .Worktree ()
511
+ if err != nil {
512
+ t .Fatal (err )
513
+ }
514
+ if err := ioutil .WriteFile (path .Join (tmpDir , "file.txt" ), []byte ("abcd" ), os .ModeAppend ); err != nil {
515
+ t .Fatal (err )
516
+ }
517
+ sha , err := w .Commit ("message" , & git.CommitOptions {
518
+ Author : & object.Signature {
519
+ Name : "name" ,
520
+ Email : "email" ,
521
+ When : time .Date (2019 , 19 , 12 , 21 , 49 , 0 , 0 , time .UTC ),
522
+ },
523
+ })
524
+ if err != nil {
525
+ t .Fatal (err )
526
+ }
529
527
530
- return tmpDir , sha .String ()
528
+ if _ , err := repo .CreateTag ("0.1.0" , sha , nil ); err != nil {
529
+ t .Fatal (err )
531
530
}
532
531
532
+ return tmpDir , sha .String ()
533
+ }
534
+
535
+ func TestGitOriginURL (t * testing.T ) {
533
536
t .Run ("invalid path" , func (t * testing.T ) {
534
537
_ , _ , err := GitOriginURL ("invalid path" , "HEAD" )
535
538
if err != ErrUnknownRepositoryURL {
@@ -538,7 +541,7 @@ func TestGitOriginURL(t *testing.T) {
538
541
})
539
542
540
543
t .Run ("invalid path in git repository" , func (t * testing.T ) {
541
- repositoryPath , _ := setup (t , nil )
544
+ repositoryPath , _ := createRepository (t , nil )
542
545
defer os .RemoveAll (repositoryPath )
543
546
544
547
_ , _ , err := GitOriginURL (path .Join (repositoryPath , "invalidpath" ), "HEAD" )
@@ -560,7 +563,7 @@ func TestGitOriginURL(t *testing.T) {
560
563
Fetch : nil ,
561
564
},
562
565
}
563
- repositoryPath , _ := setup (t , remotes )
566
+ repositoryPath , _ := createRepository (t , remotes )
564
567
defer os .RemoveAll (repositoryPath )
565
568
566
569
urls , _ , err := GitOriginURL (repositoryPath , "HEAD" )
@@ -575,7 +578,7 @@ func TestGitOriginURL(t *testing.T) {
575
578
})
576
579
577
580
t .Run ("commit references" , func (t * testing.T ) {
578
- repositoryPath , sha := setup (t , nil )
581
+ repositoryPath , sha := createRepository (t , nil )
579
582
defer os .RemoveAll (repositoryPath )
580
583
581
584
expectedCommit := Commit {
@@ -609,5 +612,132 @@ func TestGitOriginURL(t *testing.T) {
609
612
})
610
613
}
611
614
})
615
+ }
612
616
617
+ type testProvider struct {
618
+ id string
619
+ url string
620
+ callNumber int
621
+ }
622
+
623
+ func (p testProvider ) ID () string { return p .id }
624
+
625
+ func (p * testProvider ) RefStatuses (ctx context.Context , url , ref , sha string ) ([]string , error ) {
626
+ if ! strings .Contains (url , p .url ) {
627
+ return nil , ErrUnknownRepositoryURL
628
+ }
629
+ switch p .callNumber ++ ; p .callNumber {
630
+ case 1 :
631
+ return []string {url + "_status0" }, nil
632
+ case 2 :
633
+ return []string {url + "_status0" , url + "_status1" }, nil
634
+ default :
635
+ return []string {url + "_status0" , url + "_status1" , url + "_status2" }, nil
636
+ }
637
+ }
638
+
639
+ func (p testProvider ) Commit (ctx context.Context , repo , sha string ) (Commit , error ) {
640
+ if ! strings .Contains (repo , p .url ) {
641
+ return Commit {}, ErrUnknownRepositoryURL
642
+ }
643
+ return Commit {}, nil
644
+ }
645
+
646
+ func TestCache_monitorRefStatus (t * testing.T ) {
647
+ ctx := context .Background ()
648
+ p := testProvider {"provider" , "url" , 0 }
649
+ commitc := make (chan Commit )
650
+ errc := make (chan error )
651
+
652
+ b := backoff.ExponentialBackOff {
653
+ InitialInterval : time .Millisecond ,
654
+ RandomizationFactor : backoff .DefaultRandomizationFactor ,
655
+ Multiplier : backoff .DefaultMultiplier ,
656
+ MaxInterval : 10 * time .Millisecond ,
657
+ MaxElapsedTime : 10 * time .Millisecond ,
658
+ Clock : backoff .SystemClock ,
659
+ }
660
+
661
+ go func () {
662
+ err := monitorRefStatuses (ctx , & p , b , "url" , "ref" , commitc )
663
+ close (commitc )
664
+ errc <- err
665
+ close (errc )
666
+ }()
667
+
668
+ var c Commit
669
+ for c = range commitc {
670
+ }
671
+
672
+ if err := <- errc ; err != nil {
673
+ t .Fatal (err )
674
+ }
675
+
676
+ statuses := []string {"url_status0" , "url_status1" , "url_status2" }
677
+ if diff := cmp .Diff (c .Statuses , statuses ); len (diff ) > 0 {
678
+ t .Fatal (diff )
679
+ }
680
+ }
681
+
682
+ func TestCache_broadcastMonitorRefStatus (t * testing.T ) {
683
+ ctx := context .Background ()
684
+ c := NewCache (nil , []SourceProvider {
685
+ & testProvider {"origin" , "origin" , 0 },
686
+ & testProvider {"other" , "other" , 0 },
687
+ })
688
+
689
+ repositoryPath , sha := createRepository (t , []config.RemoteConfig {
690
+ {
691
+ Name : "origin" ,
692
+ URLs : []string {"origin1" , "origin2" },
693
+ },
694
+ {
695
+ Name : "other" ,
696
+ URLs : []string {"other1" },
697
+ },
698
+ })
699
+
700
+ commitc := make (chan Commit )
701
+ errc := make (chan error )
702
+ b := backoff.ExponentialBackOff {
703
+ InitialInterval : time .Millisecond ,
704
+ RandomizationFactor : backoff .DefaultRandomizationFactor ,
705
+ Multiplier : backoff .DefaultMultiplier ,
706
+ MaxInterval : 10 * time .Millisecond ,
707
+ MaxElapsedTime : 10 * time .Millisecond ,
708
+ Clock : backoff .SystemClock ,
709
+ }
710
+
711
+ go func () {
712
+ err := c .broadcastMonitorRefStatus (ctx , repositoryPath , sha , commitc , b )
713
+ close (commitc )
714
+ errc <- err
715
+ close (errc )
716
+ }()
717
+
718
+ statuses := make (map [string ]struct {}, 0 )
719
+ for commit := range commitc {
720
+ for _ , status := range commit .Statuses {
721
+ statuses [status ] = struct {}{}
722
+ }
723
+ }
724
+
725
+ if err := <- errc ; err != nil {
726
+ t .Fatal (err )
727
+ }
728
+
729
+ expectedStatuses := map [string ]struct {}{
730
+ "origin1_status0" : {},
731
+ "origin1_status1" : {},
732
+ "origin1_status2" : {},
733
+ "origin2_status0" : {},
734
+ "origin2_status1" : {},
735
+ "origin2_status2" : {},
736
+ "other1_status0" : {},
737
+ "other1_status1" : {},
738
+ "other1_status2" : {},
739
+ }
740
+ if diff := cmp .Diff (statuses , expectedStatuses ); len (diff ) > 0 {
741
+ t .Fatal (diff )
742
+ }
613
743
}
0 commit comments