@@ -37,15 +37,15 @@ func TestPromoteWithSuccessCustomMsg(t *testing.T) {
3737func promoteWithSuccess (t * testing.T , keepCache bool , repoType string , tlsVerify bool , msg string ) {
3838 dstBranch := "test-branch"
3939 author := & git.
Author {
Name :
"Testing User" ,
Email :
"[email protected] " ,
Token :
"test-token" }
40- devRepo , stagingRepo := mock .New ("/dev" , "master" ), mock .New ("/staging" , "master" )
40+ devRepo , stagingRepo := mock .New ("environments /dev" , "master" ), mock .New ("environments /staging" , "master" )
4141 repos := map [string ]* mock.Repository {
4242 mustAddCredentials (t , dev , author ): devRepo ,
4343 mustAddCredentials (t , staging , author ): stagingRepo ,
4444 }
4545 sm := New ("tmp" , author )
4646 sm .repoType = repoType
4747 sm .tlsVerify = tlsVerify
48- sm .clientFactory = func (s , ty , r string , v bool ) * scm.Client {
48+ sm .clientFactory = func (s , ty , r string , v bool ) * scm.Client {
4949 client , _ := fakescm .NewDefault ()
5050 if r != repoType {
5151 t .Fatalf ("repoType doesn't match %s != %s\n " , r , repoType )
@@ -61,8 +61,8 @@ func promoteWithSuccess(t *testing.T, keepCache bool, repoType string, tlsVerify
6161 }
6262 return git .Repo (repos [url ]), nil
6363 }
64- devRepo .AddFiles ("/ services/my-service/base/config/myfile.yaml" )
65-
64+ devRepo .AddFiles ("services/my-service/base/config/myfile.yaml" )
65+ stagingRepo . AddFiles ( "" )
6666 err := sm .Promote ("my-service" , dev , staging , dstBranch , msg , keepCache )
6767 if err != nil {
6868 t .Fatal (err )
@@ -71,11 +71,11 @@ func promoteWithSuccess(t *testing.T, keepCache bool, repoType string, tlsVerify
7171 expectedCommitMsg := msg
7272 if msg == "" {
7373 commit := devRepo .GetCommitID ()
74- expectedCommitMsg = fmt .Sprintf ("Promoting service ` my-service` at commit `%s` from branch ` master` in `%s` ." , commit , dev )
74+ expectedCommitMsg = fmt .Sprintf ("Promoting service my-service at commit %s from branch master in %s ." , commit , dev )
7575 }
7676
7777 stagingRepo .AssertBranchCreated (t , "master" , dstBranch )
78- stagingRepo .AssertFileCopiedInBranch (t , dstBranch , "/dev/services/my-service/base/config/myfile.yaml" , "/staging/services/my-service/base/config/myfile.yaml" )
78+ stagingRepo .AssertFileCopiedInBranch (t , dstBranch , "environments /dev/services/my-service/base/config/myfile.yaml" , "environments /staging/services/my-service/base/config/myfile.yaml" )
7979 stagingRepo .AssertCommit (t , dstBranch , expectedCommitMsg , author )
8080 stagingRepo .AssertPush (t , dstBranch )
8181
@@ -103,11 +103,11 @@ func TestPromoteLocalWithSuccessCustomMsg(t *testing.T) {
103103func promoteLocalWithSuccess (t * testing.T , keepCache bool , msg string ) {
104104 dstBranch := "test-branch"
105105 author := & git.
Author {
Name :
"Testing User" ,
Email :
"[email protected] " ,
Token :
"test-token" }
106- stagingRepo := mock .New ("/staging " , "master" )
106+ stagingRepo := mock .New ("environments " , "master" )
107107 devRepo := NewLocal ("/dev" )
108108
109109 sm := New ("tmp" , author )
110- sm .clientFactory = func (s , t , r string , v bool ) * scm.Client {
110+ sm .clientFactory = func (s , t , r string , v bool ) * scm.Client {
111111 client , _ := fakescm .NewDefault ()
112112 return client
113113 }
@@ -118,7 +118,8 @@ func promoteLocalWithSuccess(t *testing.T, keepCache bool, msg string) {
118118 return git .Source (devRepo )
119119 }
120120 sm .debug = true
121- devRepo .AddFiles ("/config/myfile.yaml" )
121+ devRepo .AddFiles ("config/myfile.yaml" )
122+ stagingRepo .AddFiles ("staging" )
122123
123124 err := sm .Promote ("my-service" , ldev , staging , dstBranch , msg , keepCache )
124125 if err != nil {
@@ -127,11 +128,11 @@ func promoteLocalWithSuccess(t *testing.T, keepCache bool, msg string) {
127128
128129 expectedCommitMsg := msg
129130 if expectedCommitMsg == "" {
130- expectedCommitMsg = "Promotion of service ` my-service` from local filesystem directory ` /root/repo` ."
131+ expectedCommitMsg = "Promotion of service my-service from local filesystem directory /root/repo."
131132 }
132133
133134 stagingRepo .AssertBranchCreated (t , "master" , dstBranch )
134- stagingRepo .AssertFileCopiedInBranch (t , dstBranch , "/dev/config/myfile.yaml" , "/staging/services/my-service/base/config/myfile.yaml" )
135+ stagingRepo .AssertFileCopiedInBranch (t , dstBranch , "/dev/config/myfile.yaml" , "environments /staging/services/my-service/base/config/myfile.yaml" )
135136 stagingRepo .AssertCommit (t , dstBranch , expectedCommitMsg , author )
136137 stagingRepo .AssertPush (t , dstBranch )
137138
@@ -142,6 +143,70 @@ func promoteLocalWithSuccess(t *testing.T, keepCache bool, msg string) {
142143 }
143144}
144145
146+ func TestPromoteLocalWithSuccessOneEnvAndIsUsed (t * testing.T ) {
147+ // Destination repo (GitOps repo) to have /environments/staging
148+ // Promotion should copy files into that staging directory
149+ dstBranch := "test-branch"
150+ author := & git.
Author {
Name :
"Testing User" ,
Email :
"[email protected] " ,
Token :
"test-token" }
151+ stagingRepo := mock .New ("environments" , "master" )
152+ devRepo := NewLocal ("/dev" )
153+
154+ sm := New ("tmp" , author )
155+ sm .clientFactory = func (s , t , r string , v bool ) * scm.Client {
156+ client , _ := fakescm .NewDefault ()
157+ return client
158+ }
159+ sm .repoFactory = func (url , _ string , _ bool , _ bool ) (git.Repo , error ) {
160+ return git .Repo (stagingRepo ), nil
161+ }
162+ sm .localFactory = func (path string , _ bool ) git.Source {
163+ return git .Source (devRepo )
164+ }
165+ sm .debug = true
166+ devRepo .AddFiles ("/config/myfile.yaml" )
167+ stagingRepo .AddFiles ("/staging" )
168+
169+ err := sm .Promote ("my-service" , ldev , staging , dstBranch , "" , false )
170+ if err != nil {
171+ t .Fatal (err )
172+ }
173+ expectedCommitMsg := "Promotion of service my-service from local filesystem directory /root/repo."
174+
175+ stagingRepo .AssertBranchCreated (t , "master" , dstBranch )
176+ stagingRepo .AssertFileCopiedInBranch (t , dstBranch , "/dev/config/myfile.yaml" , "environments/staging/services/my-service/base/config/myfile.yaml" )
177+ stagingRepo .AssertCommit (t , dstBranch , expectedCommitMsg , author )
178+ stagingRepo .AssertPush (t , dstBranch )
179+ }
180+
181+ func TestPromoteErrorsIfMultipleEnvironments (t * testing.T ) {
182+ dstBranch := "test-branch"
183+ author := & git.
Author {
Name :
"Testing User" ,
Email :
"[email protected] " ,
Token :
"test-token" }
184+ devRepo , stagingRepo := mock .New ("/" , "master" ), mock .New ("/environments" , "master" )
185+
186+ stagingRepo .AddFiles ("/staging" )
187+ stagingRepo .AddFiles ("/prod" )
188+
189+ repos := map [string ]* mock.Repository {
190+ mustAddCredentials (t , dev , author ): devRepo ,
191+ mustAddCredentials (t , staging , author ): stagingRepo ,
192+ }
193+ sm := New ("tmp" , author )
194+ sm .clientFactory = func (s , ty , r string , v bool ) * scm.Client {
195+ client , _ := fakescm .NewDefault ()
196+ return client
197+ }
198+ sm .repoFactory = func (url , _ string , v bool , _ bool ) (git.Repo , error ) {
199+ return git .Repo (repos [url ]), nil
200+ }
201+ devRepo .AddFiles ("services/my-service/base/config/myfile.yaml" )
202+
203+ msg := "foo message"
204+ err := sm .Promote ("my-service" , dev , staging , dstBranch , msg , false )
205+ if err == nil {
206+ t .Fail ()
207+ }
208+ }
209+
145210func TestAddCredentials (t * testing.T ) {
146211 testUser := & git.
Author {
Name :
"Test User" ,
Email :
"[email protected] " ,
Token :
"test-token" }
147212 tests := []struct {
@@ -177,32 +242,33 @@ func mustAddCredentials(t *testing.T, repoURL string, a *git.Author) string {
177242func TestPromoteWithCacheDeletionFailure (t * testing.T ) {
178243 dstBranch := "test-branch"
179244 author := & git.
Author {
Name :
"Testing User" ,
Email :
"[email protected] " ,
Token :
"test-token" }
180- devRepo , stagingRepo := mock .New ("/dev " , "master" ), mock .New ("/staging " , "master" )
245+ devRepo , stagingRepo := mock .New ("environments " , "master" ), mock .New ("environments " , "master" )
181246 stagingRepo .DeleteErr = errors .New ("failed test delete" )
182247 repos := map [string ]* mock.Repository {
183248 mustAddCredentials (t , dev , author ): devRepo ,
184249 mustAddCredentials (t , staging , author ): stagingRepo ,
185250 }
186251 sm := New ("tmp" , author )
187- sm .clientFactory = func (s , t , r string , v bool ) * scm.Client {
252+ sm .clientFactory = func (s , t , r string , v bool ) * scm.Client {
188253 client , _ := fakescm .NewDefault ()
189254 return client
190255 }
191256 sm .repoFactory = func (url , _ string , _ bool , _ bool ) (git.Repo , error ) {
192257 return git .Repo (repos [url ]), nil
193258 }
194- devRepo .AddFiles ("/services/my-service/base/config/myfile.yaml" )
259+ devRepo .AddFiles ("dev/services/my-service/base/config/myfile.yaml" )
260+ stagingRepo .AddFiles ("staging" )
195261
196262 err := sm .Promote ("my-service" , dev , staging , dstBranch , "" , false )
197263 if err != nil {
198264 t .Fatal (err )
199265 }
200266
201267 commit := devRepo .GetCommitID ()
202- expectedCommitMsg := fmt .Sprintf ("Promoting service ` my-service` at commit `%s` from branch ` master` in `%s` ." , commit , dev )
268+ expectedCommitMsg := fmt .Sprintf ("Promoting service my-service at commit %s from branch master in %s ." , commit , dev )
203269
204270 stagingRepo .AssertBranchCreated (t , "master" , dstBranch )
205- stagingRepo .AssertFileCopiedInBranch (t , dstBranch , "/dev/services/my-service/base/config/myfile.yaml" , "/staging/services/my-service/base/config/myfile.yaml" )
271+ stagingRepo .AssertFileCopiedInBranch (t , dstBranch , "environments /dev/services/my-service/base/config/myfile.yaml" , "environments /staging/services/my-service/base/config/myfile.yaml" )
206272 stagingRepo .AssertCommit (t , dstBranch , expectedCommitMsg , author )
207273 stagingRepo .AssertPush (t , dstBranch )
208274
@@ -298,7 +364,7 @@ func TestRepositoryCloneErrorOmitsToken(t *testing.T) {
298364 dstBranch := "test-branch"
299365 author := & git.
Author {
Name :
"Testing User" ,
Email :
"[email protected] " ,
Token :
"test-token" }
300366 client , _ := fakescm .NewDefault ()
301- fakeClientFactory := func (s , t , r string , v bool ) * scm.Client {
367+ fakeClientFactory := func (s , t , r string , v bool ) * scm.Client {
302368 return client
303369 }
304370 sm := New ("tmp" , author )
0 commit comments