11package chain
22
33import (
4+ "context"
45 "errors"
56 "strings"
67 "testing"
@@ -13,7 +14,8 @@ import (
1314 "sigs.k8s.io/controller-runtime/pkg/client/fake"
1415
1516 codebaseApi "github.com/epam/edp-codebase-operator/v2/api/v1"
16- gitServerMocks "github.com/epam/edp-codebase-operator/v2/pkg/git/mocks"
17+ gitproviderv2 "github.com/epam/edp-codebase-operator/v2/pkg/git/v2"
18+ gitServerMocks "github.com/epam/edp-codebase-operator/v2/pkg/git/v2/mocks"
1719 "github.com/epam/edp-codebase-operator/v2/pkg/util"
1820)
1921
@@ -89,6 +91,7 @@ func TestCheckoutBranch_ShouldFailOnGetSecret(t *testing.T) {
8991 Repository : & codebaseApi.Repository {
9092 Url : "repo" ,
9193 },
94+ Strategy : codebaseApi .Clone ,
9295 },
9396 }
9497
@@ -97,55 +100,18 @@ func TestCheckoutBranch_ShouldFailOnGetSecret(t *testing.T) {
97100 fakeCl := fake .NewClientBuilder ().WithScheme (scheme ).WithRuntimeObjects (c ).Build ()
98101
99102 mGit := gitServerMocks .NewMockGit (t )
103+ mGit .On ("GetCurrentBranchName" , testify .Anything , "project-path" ).Return ("some-other-branch" , nil )
100104
101- err := CheckoutBranch ("repo" , "project-path" , "branch" , mGit , c , fakeCl )
105+ err := CheckoutBranch (context .Background (), "repo" , "project-path" , "branch" , mGit , c , fakeCl , func (config gitproviderv2.Config ) gitproviderv2.Git {
106+ return mGit
107+ })
102108 assert .Error (t , err )
103109
104110 if ! strings .Contains (err .Error (), "failed to get secret repository-codebase-fake-name-temp" ) {
105111 t .Fatalf ("wrong error returned: %s" , err .Error ())
106112 }
107113}
108114
109- func TestCheckoutBranch_ShouldFailOnCheckPermission (t * testing.T ) {
110- c := & codebaseApi.Codebase {
111- ObjectMeta : metaV1.ObjectMeta {
112- Name : "fake-name" ,
113- Namespace : fakeNamespace ,
114- },
115- Spec : codebaseApi.CodebaseSpec {
116- Repository : & codebaseApi.Repository {
117- Url : "repo" ,
118- },
119- },
120- }
121- s := & coreV1.Secret {
122- ObjectMeta : metaV1.ObjectMeta {
123- Name : "repository-codebase-fake-name-temp" ,
124- Namespace : fakeNamespace ,
125- },
126- Data : map [string ][]byte {
127- "username" : []byte ("user" ),
128- "password" : []byte ("pass" ),
129- },
130- }
131- scheme := runtime .NewScheme ()
132-
133- scheme .AddKnownTypes (coreV1 .SchemeGroupVersion , s )
134- scheme .AddKnownTypes (codebaseApi .GroupVersion , c )
135-
136- fakeCl := fake .NewClientBuilder ().WithScheme (scheme ).WithRuntimeObjects (s , c ).Build ()
137-
138- mGit := gitServerMocks .NewMockGit (t )
139- mGit .On ("CheckPermissions" , testify .Anything , "repo" , util .GetStringP ("user" ), util .GetStringP ("pass" )).Return (false )
140-
141- err := CheckoutBranch ("repo" , "project-path" , "branch" , mGit , c , fakeCl )
142- assert .Error (t , err )
143-
144- if ! strings .Contains (err .Error (), "user user cannot get access to the repository repo" ) {
145- t .Fatalf ("wrong error returned: %s" , err .Error ())
146- }
147- }
148-
149115func TestCheckoutBranch_ShouldFailOnGetCurrentBranchName (t * testing.T ) {
150116 c := & codebaseApi.Codebase {
151117 ObjectMeta : metaV1.ObjectMeta {
@@ -156,6 +122,7 @@ func TestCheckoutBranch_ShouldFailOnGetCurrentBranchName(t *testing.T) {
156122 Repository : & codebaseApi.Repository {
157123 Url : "repo" ,
158124 },
125+ Strategy : codebaseApi .Clone ,
159126 },
160127 }
161128 s := & coreV1.Secret {
@@ -174,10 +141,11 @@ func TestCheckoutBranch_ShouldFailOnGetCurrentBranchName(t *testing.T) {
174141 fakeCl := fake .NewClientBuilder ().WithScheme (scheme ).WithRuntimeObjects (s , c ).Build ()
175142
176143 mGit := gitServerMocks .NewMockGit (t )
177- mGit .On ("CheckPermissions" , testify .Anything , "repo" , util .GetStringP ("user" ), util .GetStringP ("pass" )).Return (true )
178- mGit .On ("GetCurrentBranchName" , "project-path" ).Return ("" , errors .New ("FATAL:FAILED" ))
144+ mGit .On ("GetCurrentBranchName" , testify .Anything , "project-path" ).Return ("" , errors .New ("FATAL:FAILED" ))
179145
180- err := CheckoutBranch ("repo" , "project-path" , "branch" , mGit , c , fakeCl )
146+ err := CheckoutBranch (context .Background (), "repo" , "project-path" , "branch" , mGit , c , fakeCl , func (config gitproviderv2.Config ) gitproviderv2.Git {
147+ return mGit
148+ })
181149 assert .Error (t , err )
182150
183151 if ! strings .Contains (err .Error (), "FATAL:FAILED" ) {
@@ -187,8 +155,6 @@ func TestCheckoutBranch_ShouldFailOnGetCurrentBranchName(t *testing.T) {
187155
188156func TestCheckoutBranch_ShouldFailOnCheckout (t * testing.T ) {
189157 repo := "repo"
190- u := "user1"
191- p := "pass1"
192158 c := & codebaseApi.Codebase {
193159 ObjectMeta : metaV1.ObjectMeta {
194160 Name : "fake-name" ,
@@ -217,11 +183,12 @@ func TestCheckoutBranch_ShouldFailOnCheckout(t *testing.T) {
217183 fakeCl := fake .NewClientBuilder ().WithScheme (scheme ).WithRuntimeObjects (s , c ).Build ()
218184
219185 mGit := gitServerMocks .NewMockGit (t )
220- mGit .On ("CheckPermissions" , testify .Anything , "repo" , & u , & p ).Return (true )
221- mGit .On ("GetCurrentBranchName" , "project-path" ).Return ("some-other-branch" , nil )
222- mGit .On ("Checkout" , & u , & p , "project-path" , "branch" , true ).Return (errors .New ("FATAL:FAILED" ))
186+ mGit .On ("GetCurrentBranchName" , testify .Anything , "project-path" ).Return ("some-other-branch" , nil )
187+ mGit .On ("Checkout" , testify .Anything , "project-path" , "branch" , true ).Return (errors .New ("FATAL:FAILED" ))
223188
224- err := CheckoutBranch (repo , "project-path" , "branch" , mGit , c , fakeCl )
189+ err := CheckoutBranch (context .Background (), repo , "project-path" , "branch" , mGit , c , fakeCl , func (config gitproviderv2.Config ) gitproviderv2.Git {
190+ return mGit
191+ })
225192 assert .Error (t , err )
226193
227194 if ! strings .Contains (err .Error (), "FATAL:FAILED" ) {
@@ -231,8 +198,6 @@ func TestCheckoutBranch_ShouldFailOnCheckout(t *testing.T) {
231198
232199func TestCheckoutBranch_ShouldPassForCloneStrategy (t * testing.T ) {
233200 repo := "repo"
234- u := "user"
235- p := "pass"
236201 c := & codebaseApi.Codebase {
237202 ObjectMeta : metaV1.ObjectMeta {
238203 Name : "fake-name" ,
@@ -283,10 +248,11 @@ func TestCheckoutBranch_ShouldPassForCloneStrategy(t *testing.T) {
283248 fakeCl := fake .NewClientBuilder ().WithScheme (scheme ).WithRuntimeObjects (s , c , gs , ssh ).Build ()
284249
285250 mGit := gitServerMocks .NewMockGit (t )
286- mGit .On ("CheckPermissions" , testify .Anything , "repo" , & u , & p ).Return (true )
287- mGit .On ("GetCurrentBranchName" , "project-path" ).Return ("some-other-branch" , nil )
288- mGit .On ("CheckoutRemoteBranchBySSH" , "fake" , fakeName , "project-path" , "branch" ).Return (nil )
251+ mGit .On ("GetCurrentBranchName" , testify .Anything , "project-path" ).Return ("some-other-branch" , nil )
252+ mGit .On ("CheckoutRemoteBranch" , testify .Anything , "project-path" , "branch" ).Return (nil )
289253
290- err := CheckoutBranch (repo , "project-path" , "branch" , mGit , c , fakeCl )
254+ err := CheckoutBranch (context .Background (), repo , "project-path" , "branch" , mGit , c , fakeCl , func (config gitproviderv2.Config ) gitproviderv2.Git {
255+ return mGit
256+ })
291257 assert .NoError (t , err )
292258}
0 commit comments