1
1
package context
2
2
3
3
import (
4
+ "fmt"
4
5
"io/ioutil"
5
6
"os"
6
7
"testing"
@@ -131,6 +132,11 @@ func TestCreateInvalids(t *testing.T) {
131
132
}
132
133
}
133
134
135
+ func assertContextCreateLogging (t * testing.T , cli * test.FakeCli , n string ) {
136
+ assert .Equal (t , n + "\n " , cli .OutBuffer ().String ())
137
+ assert .Equal (t , fmt .Sprintf ("Successfully created context %q\n " , n ), cli .ErrBuffer ().String ())
138
+ }
139
+
134
140
func TestCreateOrchestratorSwarm (t * testing.T ) {
135
141
cli , cleanup := makeFakeCli (t )
136
142
defer cleanup ()
@@ -141,8 +147,7 @@ func TestCreateOrchestratorSwarm(t *testing.T) {
141
147
Docker : map [string ]string {},
142
148
})
143
149
assert .NilError (t , err )
144
- assert .Equal (t , "test\n " , cli .OutBuffer ().String ())
145
- assert .Equal (t , "Successfully created context \" test\" \n " , cli .ErrBuffer ().String ())
150
+ assertContextCreateLogging (t , cli , "test" )
146
151
}
147
152
148
153
func TestCreateOrchestratorEmpty (t * testing.T ) {
@@ -154,6 +159,7 @@ func TestCreateOrchestratorEmpty(t *testing.T) {
154
159
Docker : map [string ]string {},
155
160
})
156
161
assert .NilError (t , err )
162
+ assertContextCreateLogging (t , cli , "test" )
157
163
}
158
164
159
165
func validateTestKubeEndpoint (t * testing.T , s store.Reader , name string ) {
@@ -189,6 +195,7 @@ func TestCreateOrchestratorAllKubernetesEndpointFromCurrent(t *testing.T) {
189
195
cli , cleanup := makeFakeCli (t )
190
196
defer cleanup ()
191
197
createTestContextWithKube (t , cli )
198
+ assertContextCreateLogging (t , cli , "test" )
192
199
validateTestKubeEndpoint (t , cli .ContextStore (), "test" )
193
200
}
194
201
@@ -225,6 +232,7 @@ func TestCreateFromContext(t *testing.T) {
225
232
defer cleanup ()
226
233
revert := env .Patch (t , "KUBECONFIG" , "./testdata/test-kubeconfig" )
227
234
defer revert ()
235
+ cli .ResetOutputBuffers ()
228
236
assert .NilError (t , RunCreate (cli , & CreateOptions {
229
237
Name : "original" ,
230
238
Description : "original description" ,
@@ -236,6 +244,9 @@ func TestCreateFromContext(t *testing.T) {
236
244
},
237
245
DefaultStackOrchestrator : "swarm" ,
238
246
}))
247
+ assertContextCreateLogging (t , cli , "original" )
248
+
249
+ cli .ResetOutputBuffers ()
239
250
assert .NilError (t , RunCreate (cli , & CreateOptions {
240
251
Name : "dummy" ,
241
252
Description : "dummy description" ,
@@ -247,11 +258,13 @@ func TestCreateFromContext(t *testing.T) {
247
258
},
248
259
DefaultStackOrchestrator : "swarm" ,
249
260
}))
261
+ assertContextCreateLogging (t , cli , "dummy" )
250
262
251
263
cli .SetCurrentContext ("dummy" )
252
264
253
265
for _ , c := range cases {
254
266
t .Run (c .name , func (t * testing.T ) {
267
+ cli .ResetOutputBuffers ()
255
268
err := RunCreate (cli , & CreateOptions {
256
269
From : "original" ,
257
270
Name : c .name ,
@@ -261,6 +274,7 @@ func TestCreateFromContext(t *testing.T) {
261
274
Kubernetes : c .kubernetes ,
262
275
})
263
276
assert .NilError (t , err )
277
+ assertContextCreateLogging (t , cli , c .name )
264
278
newContext , err := cli .ContextStore ().GetMetadata (c .name )
265
279
assert .NilError (t , err )
266
280
newContextTyped , err := command .GetDockerContext (newContext )
@@ -308,6 +322,7 @@ func TestCreateFromCurrent(t *testing.T) {
308
322
defer cleanup ()
309
323
revert := env .Patch (t , "KUBECONFIG" , "./testdata/test-kubeconfig" )
310
324
defer revert ()
325
+ cli .ResetOutputBuffers ()
311
326
assert .NilError (t , RunCreate (cli , & CreateOptions {
312
327
Name : "original" ,
313
328
Description : "original description" ,
@@ -319,17 +334,20 @@ func TestCreateFromCurrent(t *testing.T) {
319
334
},
320
335
DefaultStackOrchestrator : "swarm" ,
321
336
}))
337
+ assertContextCreateLogging (t , cli , "original" )
322
338
323
339
cli .SetCurrentContext ("original" )
324
340
325
341
for _ , c := range cases {
326
342
t .Run (c .name , func (t * testing.T ) {
343
+ cli .ResetOutputBuffers ()
327
344
err := RunCreate (cli , & CreateOptions {
328
345
Name : c .name ,
329
346
Description : c .description ,
330
347
DefaultStackOrchestrator : c .orchestrator ,
331
348
})
332
349
assert .NilError (t , err )
350
+ assertContextCreateLogging (t , cli , c .name )
333
351
newContext , err := cli .ContextStore ().GetMetadata (c .name )
334
352
assert .NilError (t , err )
335
353
newContextTyped , err := command .GetDockerContext (newContext )
0 commit comments