@@ -48,6 +48,7 @@ import (
4848
4949 kstatus "github.com/fluxcd/cli-utils/pkg/kstatus/status"
5050 "github.com/fluxcd/pkg/apis/meta"
51+ "github.com/fluxcd/pkg/auth/github"
5152 "github.com/fluxcd/pkg/git"
5253 "github.com/fluxcd/pkg/gittestserver"
5354 "github.com/fluxcd/pkg/runtime/conditions"
@@ -686,46 +687,132 @@ func TestGitRepositoryReconciler_reconcileSource_authStrategy(t *testing.T) {
686687func TestGitRepositoryReconciler_getAuthOpts_provider (t * testing.T ) {
687688 tests := []struct {
688689 name string
690+ url string
691+ secret * corev1.Secret
689692 beforeFunc func (obj * sourcev1.GitRepository )
690693 wantProviderOptsName string
694+ wantErr error
691695 }{
692696 {
693697 name : "azure provider" ,
698+ url : "https://dev.azure.com/foo/bar/_git/baz" ,
694699 beforeFunc : func (obj * sourcev1.GitRepository ) {
695700 obj .Spec .Provider = sourcev1 .GitProviderAzure
696701 },
697702 wantProviderOptsName : sourcev1 .GitProviderAzure ,
698703 },
704+ {
705+ name : "github provider with no secret ref" ,
706+ url : "https://github.com/org/repo.git" ,
707+ beforeFunc : func (obj * sourcev1.GitRepository ) {
708+ obj .Spec .Provider = sourcev1 .GitProviderGitHub
709+ },
710+ wantProviderOptsName : sourcev1 .GitProviderGitHub ,
711+ wantErr : errors .New ("secretRef with github app data must be specified when provider is set to github" ),
712+ },
713+ {
714+ name : "github provider with secret ref that does not exist" ,
715+ url : "https://github.com/org/repo.git" ,
716+ beforeFunc : func (obj * sourcev1.GitRepository ) {
717+ obj .Spec .Provider = sourcev1 .GitProviderGitHub
718+ obj .Spec .SecretRef = & meta.LocalObjectReference {
719+ Name : "githubAppSecret" ,
720+ }
721+ },
722+ wantErr : errors .New ("failed to get secret '/githubAppSecret': secrets \" githubAppSecret\" not found" ),
723+ },
724+ {
725+ name : "github provider with github app data in secret" ,
726+ url : "https://example.com/org/repo" ,
727+ secret : & corev1.Secret {
728+ ObjectMeta : metav1.ObjectMeta {
729+ Name : "githubAppSecret" ,
730+ },
731+ Data : map [string ][]byte {
732+ github .AppIDKey : []byte ("123" ),
733+ github .AppInstallationIDKey : []byte ("456" ),
734+ github .AppPrivateKey : []byte ("abc" ),
735+ },
736+ },
737+ beforeFunc : func (obj * sourcev1.GitRepository ) {
738+ obj .Spec .Provider = sourcev1 .GitProviderGitHub
739+ obj .Spec .SecretRef = & meta.LocalObjectReference {
740+ Name : "githubAppSecret" ,
741+ }
742+ },
743+ wantProviderOptsName : sourcev1 .GitProviderGitHub ,
744+ },
745+ {
746+ name : "generic provider with github app data in secret" ,
747+ url : "https://example.com/org/repo" ,
748+ secret : & corev1.Secret {
749+ ObjectMeta : metav1.ObjectMeta {
750+ Name : "githubAppSecret" ,
751+ },
752+ Data : map [string ][]byte {
753+ github .AppIDKey : []byte ("123" ),
754+ },
755+ },
756+ beforeFunc : func (obj * sourcev1.GitRepository ) {
757+ obj .Spec .Provider = sourcev1 .GitProviderGeneric
758+ obj .Spec .SecretRef = & meta.LocalObjectReference {
759+ Name : "githubAppSecret" ,
760+ }
761+ },
762+ wantErr : errors .New ("secretRef '/githubAppSecret' has github app data but provider is not set to github" ),
763+ },
699764 {
700765 name : "generic provider" ,
766+ url : "https://example.com/org/repo" ,
701767 beforeFunc : func (obj * sourcev1.GitRepository ) {
702768 obj .Spec .Provider = sourcev1 .GitProviderGeneric
703769 },
704770 },
705771 {
772+ url : "https://example.com/org/repo" ,
706773 name : "no provider" ,
707774 },
708775 }
709776
710777 for _ , tt := range tests {
711778 t .Run (tt .name , func (t * testing.T ) {
712779 g := NewWithT (t )
780+ clientBuilder := fakeclient .NewClientBuilder ().
781+ WithScheme (testEnv .GetScheme ()).
782+ WithStatusSubresource (& sourcev1.GitRepository {})
783+
784+ if tt .secret != nil {
785+ clientBuilder .WithObjects (tt .secret )
786+ }
787+
713788 obj := & sourcev1.GitRepository {}
714- r := & GitRepositoryReconciler {}
715- url , _ := url .Parse ("https://dev.azure.com/foo/bar/_git/baz" )
789+ r := & GitRepositoryReconciler {
790+ EventRecorder : record .NewFakeRecorder (32 ),
791+ Client : clientBuilder .Build (),
792+ features : features .FeatureGates (),
793+ patchOptions : getPatchOptions (gitRepositoryReadyCondition .Owned , "sc" ),
794+ }
795+
796+ url , err := url .Parse (tt .url )
797+ g .Expect (err ).ToNot (HaveOccurred ())
716798
717799 if tt .beforeFunc != nil {
718800 tt .beforeFunc (obj )
719801 }
720802 opts , err := r .getAuthOpts (context .TODO (), obj , * url )
721803
722- g .Expect (err ).ToNot (HaveOccurred ())
723- g .Expect (opts ).ToNot (BeNil ())
724- if tt .wantProviderOptsName != "" {
725- g .Expect (opts .ProviderOpts ).ToNot (BeNil ())
726- g .Expect (opts .ProviderOpts .Name ).To (Equal (tt .wantProviderOptsName ))
804+ if tt .wantErr != nil {
805+ g .Expect (err ).To (HaveOccurred ())
806+ g .Expect (err .Error ()).To (ContainSubstring (tt .wantErr .Error ()))
727807 } else {
728- g .Expect (opts .ProviderOpts ).To (BeNil ())
808+ g .Expect (err ).ToNot (HaveOccurred ())
809+ g .Expect (opts ).ToNot (BeNil ())
810+ if tt .wantProviderOptsName != "" {
811+ g .Expect (opts .ProviderOpts ).ToNot (BeNil ())
812+ g .Expect (opts .ProviderOpts .Name ).To (Equal (tt .wantProviderOptsName ))
813+ } else {
814+ g .Expect (opts .ProviderOpts ).To (BeNil ())
815+ }
729816 }
730817 })
731818 }
0 commit comments