11package context
22
33import (
4+ "fmt"
45 "io/ioutil"
56 "os"
67 "testing"
@@ -131,6 +132,11 @@ func TestCreateInvalids(t *testing.T) {
131132 }
132133}
133134
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+
134140func TestCreateOrchestratorSwarm (t * testing.T ) {
135141 cli , cleanup := makeFakeCli (t )
136142 defer cleanup ()
@@ -141,8 +147,7 @@ func TestCreateOrchestratorSwarm(t *testing.T) {
141147 Docker : map [string ]string {},
142148 })
143149 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" )
146151}
147152
148153func TestCreateOrchestratorEmpty (t * testing.T ) {
@@ -154,6 +159,7 @@ func TestCreateOrchestratorEmpty(t *testing.T) {
154159 Docker : map [string ]string {},
155160 })
156161 assert .NilError (t , err )
162+ assertContextCreateLogging (t , cli , "test" )
157163}
158164
159165func validateTestKubeEndpoint (t * testing.T , s store.Reader , name string ) {
@@ -189,6 +195,7 @@ func TestCreateOrchestratorAllKubernetesEndpointFromCurrent(t *testing.T) {
189195 cli , cleanup := makeFakeCli (t )
190196 defer cleanup ()
191197 createTestContextWithKube (t , cli )
198+ assertContextCreateLogging (t , cli , "test" )
192199 validateTestKubeEndpoint (t , cli .ContextStore (), "test" )
193200}
194201
@@ -225,6 +232,7 @@ func TestCreateFromContext(t *testing.T) {
225232 defer cleanup ()
226233 revert := env .Patch (t , "KUBECONFIG" , "./testdata/test-kubeconfig" )
227234 defer revert ()
235+ cli .ResetOutputBuffers ()
228236 assert .NilError (t , RunCreate (cli , & CreateOptions {
229237 Name : "original" ,
230238 Description : "original description" ,
@@ -236,6 +244,9 @@ func TestCreateFromContext(t *testing.T) {
236244 },
237245 DefaultStackOrchestrator : "swarm" ,
238246 }))
247+ assertContextCreateLogging (t , cli , "original" )
248+
249+ cli .ResetOutputBuffers ()
239250 assert .NilError (t , RunCreate (cli , & CreateOptions {
240251 Name : "dummy" ,
241252 Description : "dummy description" ,
@@ -247,11 +258,13 @@ func TestCreateFromContext(t *testing.T) {
247258 },
248259 DefaultStackOrchestrator : "swarm" ,
249260 }))
261+ assertContextCreateLogging (t , cli , "dummy" )
250262
251263 cli .SetCurrentContext ("dummy" )
252264
253265 for _ , c := range cases {
254266 t .Run (c .name , func (t * testing.T ) {
267+ cli .ResetOutputBuffers ()
255268 err := RunCreate (cli , & CreateOptions {
256269 From : "original" ,
257270 Name : c .name ,
@@ -261,6 +274,7 @@ func TestCreateFromContext(t *testing.T) {
261274 Kubernetes : c .kubernetes ,
262275 })
263276 assert .NilError (t , err )
277+ assertContextCreateLogging (t , cli , c .name )
264278 newContext , err := cli .ContextStore ().GetMetadata (c .name )
265279 assert .NilError (t , err )
266280 newContextTyped , err := command .GetDockerContext (newContext )
@@ -308,6 +322,7 @@ func TestCreateFromCurrent(t *testing.T) {
308322 defer cleanup ()
309323 revert := env .Patch (t , "KUBECONFIG" , "./testdata/test-kubeconfig" )
310324 defer revert ()
325+ cli .ResetOutputBuffers ()
311326 assert .NilError (t , RunCreate (cli , & CreateOptions {
312327 Name : "original" ,
313328 Description : "original description" ,
@@ -319,17 +334,20 @@ func TestCreateFromCurrent(t *testing.T) {
319334 },
320335 DefaultStackOrchestrator : "swarm" ,
321336 }))
337+ assertContextCreateLogging (t , cli , "original" )
322338
323339 cli .SetCurrentContext ("original" )
324340
325341 for _ , c := range cases {
326342 t .Run (c .name , func (t * testing.T ) {
343+ cli .ResetOutputBuffers ()
327344 err := RunCreate (cli , & CreateOptions {
328345 Name : c .name ,
329346 Description : c .description ,
330347 DefaultStackOrchestrator : c .orchestrator ,
331348 })
332349 assert .NilError (t , err )
350+ assertContextCreateLogging (t , cli , c .name )
333351 newContext , err := cli .ContextStore ().GetMetadata (c .name )
334352 assert .NilError (t , err )
335353 newContextTyped , err := command .GetDockerContext (newContext )
0 commit comments