@@ -533,3 +533,123 @@ func TestRunRmTime(t *testing.T) {
533533 t .Fatalf ("expected to have completed in %v, took %v" , deadline , took )
534534 }
535535}
536+
537+ func runAttachStdin (t * testing.T , testStr string , args []string ) string {
538+ if runtime .GOOS == "windows" {
539+ t .Skip ("run attach test is not yet implemented on Windows" )
540+ }
541+
542+ t .Parallel ()
543+ base := testutil .NewBase (t )
544+ containerName := testutil .Identifier (t )
545+
546+ opts := []func (* testutil.Cmd ){
547+ testutil .WithStdin (strings .NewReader ("echo " + testStr + "\n exit\n " )),
548+ }
549+
550+ fullArgs := []string {"run" , "--rm" , "-i" }
551+ fullArgs = append (fullArgs , args ... )
552+ fullArgs = append (fullArgs ,
553+ "--name" ,
554+ containerName ,
555+ testutil .CommonImage ,
556+ )
557+
558+ defer base .Cmd ("rm" , "-f" , containerName ).AssertOK ()
559+ result := base .Cmd (fullArgs ... ).CmdOption (opts ... ).Run ()
560+
561+ return result .Combined ()
562+ }
563+
564+ func runAttach (t * testing.T , testStr string , args []string ) string {
565+ if runtime .GOOS == "windows" {
566+ t .Skip ("run attach test is not yet implemented on Windows" )
567+ }
568+
569+ t .Parallel ()
570+ base := testutil .NewBase (t )
571+ containerName := testutil .Identifier (t )
572+
573+ fullArgs := []string {"run" }
574+ fullArgs = append (fullArgs , args ... )
575+ fullArgs = append (fullArgs ,
576+ "--name" ,
577+ containerName ,
578+ testutil .CommonImage ,
579+ "sh" ,
580+ "-euxc" ,
581+ "echo " + testStr ,
582+ )
583+
584+ defer base .Cmd ("rm" , "-f" , containerName ).AssertOK ()
585+ result := base .Cmd (fullArgs ... ).Run ()
586+
587+ return result .Combined ()
588+ }
589+
590+ func TestRunAttachFlag (t * testing.T ) {
591+
592+ type testCase struct {
593+ name string
594+ args []string
595+ testFunc func (t * testing.T , testStr string , args []string ) string
596+ testStr string
597+ expectedOut string
598+ dockerOut string
599+ }
600+ testCases := []testCase {
601+ {
602+ name : "AttachFlagStdin" ,
603+ args : []string {"-a" , "STDIN" , "-a" , "STDOUT" },
604+ testFunc : runAttachStdin ,
605+ testStr : "test-run-stdio" ,
606+ expectedOut : "test-run-stdio" ,
607+ dockerOut : "test-run-stdio" ,
608+ },
609+ {
610+ name : "AttachFlagStdOut" ,
611+ args : []string {"-a" , "STDOUT" },
612+ testFunc : runAttach ,
613+ testStr : "foo" ,
614+ expectedOut : "foo" ,
615+ dockerOut : "foo" ,
616+ },
617+ {
618+ name : "AttachFlagMixedValue" ,
619+ args : []string {"-a" , "STDIN" , "-a" , "invalid-value" },
620+ testFunc : runAttach ,
621+ testStr : "foo" ,
622+ expectedOut : "invalid stream specified with -a flag. Valid streams are STDIN, STDOUT, and STDERR." ,
623+ dockerOut : "valid streams are STDIN, STDOUT and STDERR" ,
624+ },
625+ {
626+ name : "AttachFlagInvalidValue" ,
627+ args : []string {"-a" , "invalid-stream" },
628+ testFunc : runAttach ,
629+ testStr : "foo" ,
630+ expectedOut : "invalid stream specified with -a flag. Valid streams are STDIN, STDOUT, and STDERR." ,
631+ dockerOut : "valid streams are STDIN, STDOUT and STDERR" ,
632+ },
633+ {
634+ name : "AttachFlagCaseInsensitive" ,
635+ args : []string {"-a" , "stdin" , "-a" , "stdout" },
636+ testFunc : runAttachStdin ,
637+ testStr : "test-run-stdio" ,
638+ expectedOut : "test-run-stdio" ,
639+ dockerOut : "test-run-stdio" ,
640+ },
641+ }
642+
643+ for _ , tc := range testCases {
644+ tc := tc
645+ t .Run (tc .name , func (t * testing.T ) {
646+ actualOut := tc .testFunc (t , tc .testStr , tc .args )
647+ errorMsg := fmt .Sprintf ("%s failed;\n Expected: '%s'\n Actual: '%s'" , tc .name , tc .expectedOut , actualOut )
648+ if testutil .GetTarget () == testutil .Docker {
649+ assert .Equal (t , true , strings .Contains (actualOut , tc .dockerOut ), errorMsg )
650+ } else {
651+ assert .Equal (t , true , strings .Contains (actualOut , tc .expectedOut ), errorMsg )
652+ }
653+ })
654+ }
655+ }
0 commit comments