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