@@ -467,53 +467,76 @@ func validateDockerEnv(ctx context.Context, t *testing.T, profile string) {
467
467
t .Skipf ("only validate docker env with docker container runtime, currently testing %s" , cr )
468
468
}
469
469
defer PostMortemLogs (t , profile )
470
- mctx , cancel := context .WithTimeout (ctx , Seconds (120 ))
471
- defer cancel ()
472
- var rr * RunResult
473
- var err error
474
- if runtime .GOOS == "windows" {
475
- c := exec .CommandContext (mctx , "powershell.exe" , "-NoProfile" , "-NonInteractive" , Target ()+ " -p " + profile + " docker-env | Invoke-Expression ;" + Target ()+ " status -p " + profile )
476
- rr , err = Run (t , c )
477
- } else {
478
- c := exec .CommandContext (mctx , "/bin/bash" , "-c" , "eval $(" + Target ()+ " -p " + profile + " docker-env) && " + Target ()+ " status -p " + profile )
479
- // we should be able to get minikube status with a bash which evaled docker-env
480
- rr , err = Run (t , c )
481
- }
482
- if mctx .Err () == context .DeadlineExceeded {
483
- t .Errorf ("failed to run the command by deadline. exceeded timeout. %s" , rr .Command ())
484
- }
485
- if err != nil {
486
- t .Fatalf ("failed to do status after eval-ing docker-env. error: %v" , err )
470
+
471
+ type ShellTest struct {
472
+ name string
473
+ commandPrefix []string
474
+ formatArg string
487
475
}
488
- if ! strings .Contains (rr .Output (), "Running" ) {
489
- t .Fatalf ("expected status output to include 'Running' after eval docker-env but got: *%s*" , rr .Output ())
476
+
477
+ windowsTests := []ShellTest {
478
+ {"powershell" , []string {"powershell.exe" , "-NoProfile" , "-NonInteractive" }, "%[1]s -p %[2]s docker-env | Invoke-Expression ; " },
490
479
}
491
- if ! strings . Contains ( rr . Output (), "in-use" ) {
492
- t . Fatalf ( "expected status output to include `in-use` after eval docker-env but got *%s*" , rr . Output ())
480
+ posixTests := [] ShellTest {
481
+ { "bash" , [] string { "/bin/bash" , "-c" }, "eval $(%[1]s -p %[2]s docker-env) && " },
493
482
}
494
483
495
- mctx , cancel = context .WithTimeout (ctx , Seconds (60 ))
496
- defer cancel ()
497
- // do a eval $(minikube -p profile docker-env) and check if we are point to docker inside minikube
498
- if runtime .GOOS == "windows" { // testing docker-env eval in powershell
499
- c := exec .CommandContext (mctx , "powershell.exe" , "-NoProfile" , "-NonInteractive" , Target ()+ " -p " + profile + " docker-env | Invoke-Expression ; docker images" )
500
- rr , err = Run (t , c )
501
- } else {
502
- c := exec .CommandContext (mctx , "/bin/bash" , "-c" , "eval $(" + Target ()+ " -p " + profile + " docker-env) && docker images" )
503
- rr , err = Run (t , c )
484
+ tests := posixTests
485
+ if runtime .GOOS == "windows" {
486
+ tests = windowsTests
504
487
}
488
+ for _ , tc := range tests {
489
+ t .Run (tc .name , func (t * testing.T ) {
490
+ mctx , cancel := context .WithTimeout (ctx , Seconds (120 ))
491
+ defer cancel ()
505
492
506
- if mctx .Err () == context .DeadlineExceeded {
507
- t .Errorf ("failed to run the command in 30 seconds. exceeded 30s timeout. %s" , rr .Command ())
508
- }
493
+ command := make ([]string , len (tc .commandPrefix )+ 1 )
494
+ // Would use "copy" built-in here, but that is shadowed by "copy" package
495
+ for i , v := range tc .commandPrefix {
496
+ command [i ] = v
497
+ }
509
498
510
- if err != nil {
511
- t .Fatalf ("failed to run minikube docker-env. args %q : %v " , rr .Command (), err )
512
- }
499
+ formattedArg := fmt .Sprintf (tc .formatArg , Target (), profile )
513
500
514
- expectedImgInside := "gcr.io/k8s-minikube/storage-provisioner"
515
- if ! strings .Contains (rr .Output (), expectedImgInside ) {
516
- t .Fatalf ("expected 'docker images' to have %q inside minikube. but the output is: *%s*" , expectedImgInside , rr .Output ())
501
+ // we should be able to get minikube status with a shell which evaled docker-env
502
+ command [len (command )- 1 ] = formattedArg + Target () + " status -p " + profile
503
+ c := exec .CommandContext (mctx , command [0 ], command [1 :]... )
504
+ rr , err := Run (t , c )
505
+
506
+ if mctx .Err () == context .DeadlineExceeded {
507
+ t .Errorf ("failed to run the command by deadline. exceeded timeout. %s" , rr .Command ())
508
+ }
509
+ if err != nil {
510
+ t .Fatalf ("failed to do status after eval-ing docker-env. error: %v" , err )
511
+ }
512
+ if ! strings .Contains (rr .Output (), "Running" ) {
513
+ t .Fatalf ("expected status output to include 'Running' after eval docker-env but got: *%s*" , rr .Output ())
514
+ }
515
+ if ! strings .Contains (rr .Output (), "in-use" ) {
516
+ t .Fatalf ("expected status output to include `in-use` after eval docker-env but got *%s*" , rr .Output ())
517
+ }
518
+
519
+ mctx , cancel = context .WithTimeout (ctx , Seconds (60 ))
520
+ defer cancel ()
521
+
522
+ // do a eval $(minikube -p profile docker-env) and check if we are point to docker inside minikube
523
+ command [len (command )- 1 ] = formattedArg + "docker images"
524
+ c = exec .CommandContext (mctx , command [0 ], command [1 :]... )
525
+ rr , err = Run (t , c )
526
+
527
+ if mctx .Err () == context .DeadlineExceeded {
528
+ t .Errorf ("failed to run the command in 30 seconds. exceeded 30s timeout. %s" , rr .Command ())
529
+ }
530
+
531
+ if err != nil {
532
+ t .Fatalf ("failed to run minikube docker-env. args %q : %v " , rr .Command (), err )
533
+ }
534
+
535
+ expectedImgInside := "gcr.io/k8s-minikube/storage-provisioner"
536
+ if ! strings .Contains (rr .Output (), expectedImgInside ) {
537
+ t .Fatalf ("expected 'docker images' to have %q inside minikube. but the output is: *%s*" , expectedImgInside , rr .Output ())
538
+ }
539
+ })
517
540
}
518
541
}
519
542
0 commit comments