@@ -81,48 +81,30 @@ func TestGetRepositoryCredentialsIfExists_ShouldFail(t *testing.T) {
8181 }
8282}
8383
84- func TestCheckoutBranch_ShouldFailOnGetSecret (t * testing.T ) {
84+ func TestCheckoutBranch_ShouldFailOnGetCurrentBranchName (t * testing.T ) {
8585 c := & codebaseApi.Codebase {
8686 ObjectMeta : metaV1.ObjectMeta {
8787 Name : "fake-name" ,
8888 Namespace : fakeNamespace ,
8989 },
9090 Spec : codebaseApi.CodebaseSpec {
91+ GitServer : "git" ,
9192 Repository : & codebaseApi.Repository {
9293 Url : "repo" ,
9394 },
9495 Strategy : codebaseApi .Clone ,
9596 },
9697 }
97-
98- scheme := runtime .NewScheme ()
99- scheme .AddKnownTypes (codebaseApi .GroupVersion , c )
100- fakeCl := fake .NewClientBuilder ().WithScheme (scheme ).WithRuntimeObjects (c ).Build ()
101-
102- mGit := gitServerMocks .NewMockGit (t )
103- mGit .On ("GetCurrentBranchName" , testify .Anything , "project-path" ).Return ("some-other-branch" , nil )
104-
105- err := CheckoutBranch (context .Background (), "repo" , "project-path" , "branch" , mGit , c , fakeCl , func (config gitproviderv2.Config ) gitproviderv2.Git {
106- return mGit
107- })
108- assert .Error (t , err )
109-
110- if ! strings .Contains (err .Error (), "failed to get secret repository-codebase-fake-name-temp" ) {
111- t .Fatalf ("wrong error returned: %s" , err .Error ())
112- }
113- }
114-
115- func TestCheckoutBranch_ShouldFailOnGetCurrentBranchName (t * testing.T ) {
116- c := & codebaseApi.Codebase {
98+ gs := & codebaseApi.GitServer {
11799 ObjectMeta : metaV1.ObjectMeta {
118- Name : "fake-name " ,
100+ Name : "git " ,
119101 Namespace : fakeNamespace ,
120102 },
121- Spec : codebaseApi.CodebaseSpec {
122- Repository : & codebaseApi. Repository {
123- Url : "repo" ,
124- } ,
125- Strategy : codebaseApi . Clone ,
103+ Spec : codebaseApi.GitServerSpec {
104+ NameSshKeySecret : fakeName ,
105+ GitHost : fakeName ,
106+ SshPort : 22 ,
107+ GitUser : fakeName ,
126108 },
127109 }
128110 s := & coreV1.Secret {
@@ -135,15 +117,32 @@ func TestCheckoutBranch_ShouldFailOnGetCurrentBranchName(t *testing.T) {
135117 "password" : []byte ("pass" ),
136118 },
137119 }
120+ ssh := & coreV1.Secret {
121+ ObjectMeta : metaV1.ObjectMeta {
122+ Name : fakeName ,
123+ Namespace : fakeNamespace ,
124+ },
125+ Data : map [string ][]byte {
126+ util .PrivateSShKeyName : []byte ("fake-ssh-key" ),
127+ },
128+ }
138129 scheme := runtime .NewScheme ()
139- scheme .AddKnownTypes (coreV1 .SchemeGroupVersion , s )
140- scheme .AddKnownTypes (codebaseApi .GroupVersion , c )
141- fakeCl := fake .NewClientBuilder ().WithScheme (scheme ).WithRuntimeObjects (s , c ).Build ()
130+ scheme .AddKnownTypes (coreV1 .SchemeGroupVersion , s , ssh )
131+ scheme .AddKnownTypes (codebaseApi .GroupVersion , c , gs )
132+ fakeCl := fake .NewClientBuilder ().WithScheme (scheme ).WithRuntimeObjects (s , c , gs , ssh ).Build ()
142133
143134 mGit := gitServerMocks .NewMockGit (t )
144135 mGit .On ("GetCurrentBranchName" , testify .Anything , "project-path" ).Return ("" , errors .New ("FATAL:FAILED" ))
145136
146- err := CheckoutBranch (context .Background (), "repo" , "project-path" , "branch" , mGit , c , fakeCl , func (config gitproviderv2.Config ) gitproviderv2.Git {
137+ err := CheckoutBranch (context .Background (), "branch" , & GitRepositoryContext {
138+ GitServer : gs ,
139+ GitServerSecret : ssh ,
140+ PrivateSSHKey : "fake-ssh-key" ,
141+ UserName : "user" ,
142+ Token : "pass" ,
143+ RepoGitUrl : "repo" ,
144+ WorkDir : "project-path" ,
145+ }, c , fakeCl , func (config gitproviderv2.Config ) gitproviderv2.Git {
147146 return mGit
148147 })
149148 assert .Error (t , err )
@@ -154,19 +153,31 @@ func TestCheckoutBranch_ShouldFailOnGetCurrentBranchName(t *testing.T) {
154153}
155154
156155func TestCheckoutBranch_ShouldFailOnCheckout (t * testing.T ) {
157- repo := "repo"
158156 c := & codebaseApi.Codebase {
159157 ObjectMeta : metaV1.ObjectMeta {
160158 Name : "fake-name" ,
161159 Namespace : fakeNamespace ,
162160 },
163161 Spec : codebaseApi.CodebaseSpec {
162+ GitServer : "git" ,
164163 Repository : & codebaseApi.Repository {
165164 Url : "repo" ,
166165 },
167166 Strategy : codebaseApi .Clone ,
168167 },
169168 }
169+ gs := & codebaseApi.GitServer {
170+ ObjectMeta : metaV1.ObjectMeta {
171+ Name : "git" ,
172+ Namespace : fakeNamespace ,
173+ },
174+ Spec : codebaseApi.GitServerSpec {
175+ NameSshKeySecret : fakeName ,
176+ GitHost : fakeName ,
177+ SshPort : 22 ,
178+ GitUser : fakeName ,
179+ },
180+ }
170181 s := & coreV1.Secret {
171182 ObjectMeta : metaV1.ObjectMeta {
172183 Name : "repository-codebase-fake-name-temp" ,
@@ -177,16 +188,33 @@ func TestCheckoutBranch_ShouldFailOnCheckout(t *testing.T) {
177188 "password" : []byte ("pass1" ),
178189 },
179190 }
191+ ssh := & coreV1.Secret {
192+ ObjectMeta : metaV1.ObjectMeta {
193+ Name : fakeName ,
194+ Namespace : fakeNamespace ,
195+ },
196+ Data : map [string ][]byte {
197+ util .PrivateSShKeyName : []byte ("fake-ssh-key" ),
198+ },
199+ }
180200 scheme := runtime .NewScheme ()
181- scheme .AddKnownTypes (coreV1 .SchemeGroupVersion , s )
182- scheme .AddKnownTypes (codebaseApi .GroupVersion , c )
183- fakeCl := fake .NewClientBuilder ().WithScheme (scheme ).WithRuntimeObjects (s , c ).Build ()
201+ scheme .AddKnownTypes (coreV1 .SchemeGroupVersion , s , ssh )
202+ scheme .AddKnownTypes (codebaseApi .GroupVersion , c , gs )
203+ fakeCl := fake .NewClientBuilder ().WithScheme (scheme ).WithRuntimeObjects (s , c , gs , ssh ).Build ()
184204
185205 mGit := gitServerMocks .NewMockGit (t )
186206 mGit .On ("GetCurrentBranchName" , testify .Anything , "project-path" ).Return ("some-other-branch" , nil )
187207 mGit .On ("Checkout" , testify .Anything , "project-path" , "branch" , true ).Return (errors .New ("FATAL:FAILED" ))
188208
189- err := CheckoutBranch (context .Background (), repo , "project-path" , "branch" , mGit , c , fakeCl , func (config gitproviderv2.Config ) gitproviderv2.Git {
209+ err := CheckoutBranch (context .Background (), "branch" , & GitRepositoryContext {
210+ GitServer : gs ,
211+ GitServerSecret : ssh ,
212+ PrivateSSHKey : "fake-ssh-key" ,
213+ UserName : "user1" ,
214+ Token : "pass1" ,
215+ RepoGitUrl : "repo" ,
216+ WorkDir : "project-path" ,
217+ }, c , fakeCl , func (config gitproviderv2.Config ) gitproviderv2.Git {
190218 return mGit
191219 })
192220 assert .Error (t , err )
@@ -197,7 +225,6 @@ func TestCheckoutBranch_ShouldFailOnCheckout(t *testing.T) {
197225}
198226
199227func TestCheckoutBranch_ShouldPassForCloneStrategy (t * testing.T ) {
200- repo := "repo"
201228 c := & codebaseApi.Codebase {
202229 ObjectMeta : metaV1.ObjectMeta {
203230 Name : "fake-name" ,
@@ -251,7 +278,15 @@ func TestCheckoutBranch_ShouldPassForCloneStrategy(t *testing.T) {
251278 mGit .On ("GetCurrentBranchName" , testify .Anything , "project-path" ).Return ("some-other-branch" , nil )
252279 mGit .On ("CheckoutRemoteBranch" , testify .Anything , "project-path" , "branch" ).Return (nil )
253280
254- err := CheckoutBranch (context .Background (), repo , "project-path" , "branch" , mGit , c , fakeCl , func (config gitproviderv2.Config ) gitproviderv2.Git {
281+ err := CheckoutBranch (context .Background (), "branch" , & GitRepositoryContext {
282+ GitServer : gs ,
283+ GitServerSecret : ssh ,
284+ PrivateSSHKey : "fake" ,
285+ UserName : "user" ,
286+ Token : "pass" ,
287+ RepoGitUrl : "repo" ,
288+ WorkDir : "project-path" ,
289+ }, c , fakeCl , func (config gitproviderv2.Config ) gitproviderv2.Git {
255290 return mGit
256291 })
257292 assert .NoError (t , err )
0 commit comments