@@ -29,6 +29,7 @@ import (
2929 "k8s.io/client-go/kubernetes/scheme"
3030 "sigs.k8s.io/controller-runtime/pkg/client"
3131 fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
32+ "sigs.k8s.io/controller-runtime/pkg/client/interceptor"
3233
3334 "github.com/fluxcd/pkg/apis/meta"
3435 "github.com/fluxcd/pkg/auth/aws"
@@ -405,7 +406,9 @@ func Test_buildGitConfig(t *testing.T) {
405406 gitRepoURL string
406407 gitRepoProxyData map [string ][]byte
407408 srcOpts SourceOptions
409+ injectGetError error
408410 wantErr bool
411+ wantErrContains string
409412 wantCheckoutRef * sourcev1.GitRepositoryRef
410413 wantPushBranch string
411414 wantSwitchBranch bool
@@ -527,6 +530,20 @@ func Test_buildGitConfig(t *testing.T) {
527530 gitSpec : & imagev1.GitSpec {},
528531 wantErr : true ,
529532 },
533+ {
534+ name : "transient error fetching gitRepo" ,
535+ gitSpec : & imagev1.GitSpec {
536+ Checkout : & imagev1.GitCheckoutSpec {
537+ Reference : sourcev1.GitRepositoryRef {Branch : "main" },
538+ },
539+ Push : & imagev1.PushSpec {Branch : "main" },
540+ },
541+ gitRepoName : testGitRepoName ,
542+ gitRepoURL : testGitURL ,
543+ injectGetError : fmt .Errorf ("transient-test-error" ),
544+ wantErr : true ,
545+ wantErrContains : "transient-test-error" ,
546+ },
530547 {
531548 name : "use gitrepo timeout" ,
532549 gitSpec : & imagev1.GitSpec {},
@@ -616,6 +633,17 @@ func Test_buildGitConfig(t *testing.T) {
616633 clientBuilder := fakeclient .NewClientBuilder ().
617634 WithScheme (scheme .Scheme ).
618635 WithObjects (testObjects ... )
636+ if tt .injectGetError != nil {
637+ injectErr := tt .injectGetError
638+ clientBuilder = clientBuilder .WithInterceptorFuncs (interceptor.Funcs {
639+ Get : func (ctx context.Context , cl client.WithWatch , key client.ObjectKey , obj client.Object , opts ... client.GetOption ) error {
640+ if _ , ok := obj .(* sourcev1.GitRepository ); ok {
641+ return injectErr
642+ }
643+ return cl .Get (ctx , key , obj , opts ... )
644+ },
645+ })
646+ }
619647 c := clientBuilder .Build ()
620648
621649 gitRepoKey := types.NamespacedName {
@@ -633,6 +661,9 @@ func Test_buildGitConfig(t *testing.T) {
633661 g .Fail (fmt .Sprintf ("unexpected error: %v" , err ))
634662 return
635663 }
664+ if tt .wantErrContains != "" {
665+ g .Expect (err .Error ()).To (ContainSubstring (tt .wantErrContains ))
666+ }
636667 if err == nil {
637668 g .Expect (gitSrcCfg .checkoutRef ).To (Equal (tt .wantCheckoutRef ), "unexpected checkoutRef" )
638669 g .Expect (gitSrcCfg .pushBranch ).To (Equal (tt .wantPushBranch ), "unexpected push branch" )
0 commit comments