@@ -19,8 +19,10 @@ package container
1919import  (
2020	"errors" 
2121	"fmt" 
22+ 	"io" 
2223	"os/exec" 
2324	"runtime" 
25+ 	"strconv" 
2426	"strings" 
2527	"testing" 
2628	"time" 
@@ -383,3 +385,78 @@ func TestLogsWithDetails(t *testing.T) {
383385
384386	testCase .Run (t )
385387}
388+ 
389+ func  TestLogsWithStartContainer (t  * testing.T ) {
390+ 	testCase  :=  nerdtest .Setup ()
391+ 
392+ 	// For windows we  havent added support for dual logging so not adding the test. 
393+ 	testCase .Require  =  require .Not (require .Windows )
394+ 
395+ 	testCase .SubTests  =  []* test.Case {
396+ 		{
397+ 			Description : "Test logs are directed correctly for container start of a interactive container" ,
398+ 			Setup : func (data  test.Data , helpers  test.Helpers ) {
399+ 				cmd  :=  helpers .Command ("run" , "-it" , "--name" , data .Identifier (), testutil .CommonImage )
400+ 				cmd .WithPseudoTTY ()
401+ 				cmd .WithFeeder (func () io.Reader  {
402+ 					// Note: Without the sleep the test seems flaky. 
403+ 					time .Sleep (time .Second )
404+ 					return  strings .NewReader ("echo foo\n exit\n " )
405+ 				})
406+ 
407+ 				cmd .Run (& test.Expected {
408+ 					ExitCode : 0 ,
409+ 				})
410+ 
411+ 			},
412+ 			Cleanup : func (data  test.Data , helpers  test.Helpers ) {
413+ 				helpers .Anyhow ("rm" , "-f" , data .Identifier ())
414+ 			},
415+ 			Command : func (data  test.Data , helpers  test.Helpers ) test.TestableCommand  {
416+ 				cmd  :=  helpers .Command ("start" , "-ia" , data .Identifier ())
417+ 				cmd .WithPseudoTTY ()
418+ 				cmd .WithFeeder (func () io.Reader  {
419+ 					// Note: Without the sleep the test seems flaky. 
420+ 					time .Sleep (time .Second )
421+ 					return  strings .NewReader ("echo bar\n exit\n " )
422+ 				})
423+ 				cmd .Run (& test.Expected {
424+ 					ExitCode : 0 ,
425+ 				})
426+ 				cmd  =  helpers .Command ("logs" , data .Identifier ())
427+ 
428+ 				return  cmd 
429+ 			},
430+ 			Expected : test .Expects (0 , nil , expect .Contains ("foo" , "bar" )),
431+ 		},
432+ 		{
433+ 			Description : "Test logs are captured after stopping and starting a non-interactive container and continue capturing new logs" ,
434+ 			Setup : func (data  test.Data , helpers  test.Helpers ) {
435+ 				helpers .Ensure ("run" , "-d" , "--name" , data .Identifier (), testutil .CommonImage , "sh" , "-c" , "while true; do echo foo; sleep 1; done" )
436+ 			},
437+ 			Cleanup : func (data  test.Data , helpers  test.Helpers ) {
438+ 				helpers .Anyhow ("rm" , "-f" , data .Identifier ())
439+ 			},
440+ 			Command : func (data  test.Data , helpers  test.Helpers ) test.TestableCommand  {
441+ 				helpers .Ensure ("stop" , data .Identifier ())
442+ 				initialLogs  :=  helpers .Capture ("logs" , data .Identifier ())
443+ 				initialFooCount  :=  strings .Count (initialLogs , "foo" )
444+ 				data .Labels ().Set ("initialFooCount" , strconv .Itoa (initialFooCount ))
445+ 				helpers .Ensure ("start" , data .Identifier ())
446+ 				nerdtest .EnsureContainerStarted (helpers , data .Identifier ())
447+ 				return  helpers .Command ("logs" , data .Identifier ())
448+ 			},
449+ 			Expected : func (data  test.Data , helpers  test.Helpers ) * test.Expected  {
450+ 				return  & test.Expected {
451+ 					ExitCode : 0 ,
452+ 					Output : func (stdout  string , info  string , t  * testing.T ) {
453+ 						finalLogsCount  :=  strings .Count (stdout , "foo" )
454+ 						initialFooCount , _  :=  strconv .Atoi (data .Labels ().Get ("initialFooCount" ))
455+ 						assert .Assert (t , finalLogsCount  >  initialFooCount , "Expected 'foo' count to increase after restart" , info )
456+ 					},
457+ 				}
458+ 			},
459+ 		},
460+ 	}
461+ 	testCase .Run (t )
462+ }
0 commit comments