@@ -20,6 +20,7 @@ import (
2020	"fmt" 
2121	"os/exec" 
2222	"runtime" 
23+ 	"strconv" 
2324	"strings" 
2425	"testing" 
2526	"time" 
@@ -28,6 +29,8 @@ import (
2829	"gotest.tools/v3/icmd" 
2930
3031	"github.com/containerd/nerdctl/v2/pkg/testutil" 
32+ 	"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest" 
33+ 	"github.com/containerd/nerdctl/v2/pkg/testutil/test" 
3134)
3235
3336func  TestLogs (t  * testing.T ) {
@@ -255,3 +258,77 @@ func TestTailFollowRotateLogs(t *testing.T) {
255258	}
256259	assert .Equal (t , true , len (tailLogs ) >  linesPerFile , logRun .Stderr ())
257260}
261+ 
262+ func  TestLogsWithStartContainer (t  * testing.T ) {
263+ 	testCase  :=  nerdtest .Setup ()
264+ 	testCase .Require  =  test .Require (test .Not (test .Windows ),
265+ 		test .Not (nerdtest .Docker ))
266+ 	testCase .SubTests  =  []* test.Case {
267+ 		{
268+ 			Description : "Test logs are directed correctly for container start of a interactive container" ,
269+ 			Setup : func (data  test.Data , helpers  test.Helpers ) {
270+ 				cmd  :=  helpers .Command ("run" , "-it" , "--name" , data .Identifier (), testutil .CommonImage )
271+ 				cmd .WithWrapper ("unbuffer" , "-p" )
272+ 				cmd .WithStdin (testutil .NewDelayOnceReader (strings .NewReader ("echo foo\n exit\n " )))
273+ 				cmd .Run (& test.Expected {
274+ 					ExitCode : 0 ,
275+ 				})
276+ 
277+ 			},
278+ 			Cleanup : func (data  test.Data , helpers  test.Helpers ) {
279+ 				helpers .Anyhow ("rm" , "-f" , data .Identifier ())
280+ 			},
281+ 			Command : func (data  test.Data , helpers  test.Helpers ) test.TestableCommand  {
282+ 				cmd  :=  helpers .Command ("start" , "-a" , data .Identifier ())
283+ 				cmd .WithWrapper ("unbuffer" , "-p" )
284+ 				cmd .WithStdin (testutil .NewDelayOnceReader (strings .NewReader ("echo bar\n exit\n " )))
285+ 				cmd .Run (& test.Expected {
286+ 					ExitCode : 0 ,
287+ 				})
288+ 
289+ 				cmd  =  helpers .Command ("logs" , data .Identifier ())
290+ 
291+ 				return  cmd 
292+ 			},
293+ 			Expected : func (data  test.Data , helpers  test.Helpers ) * test.Expected  {
294+ 				return  & test.Expected {
295+ 					ExitCode : 0 ,
296+ 					Output : func (stdout  string , info  string , t  * testing.T ) {
297+ 						assert .Assert (t , strings .Contains (stdout , "foo" ))
298+ 						assert .Assert (t , strings .Contains (stdout , "bar" ))
299+ 					},
300+ 				}
301+ 			},
302+ 		},
303+ 		{
304+ 			Description : "Test logs are captured after stopping and starting a non-interactive container and continue capturing new logs" ,
305+ 			Setup : func (data  test.Data , helpers  test.Helpers ) {
306+ 				helpers .Ensure ("run" , "-d" , "--name" , data .Identifier (), testutil .CommonImage , "sh" , "-c" , "while true; do echo foo; sleep 1; done" )
307+ 			},
308+ 			Cleanup : func (data  test.Data , helpers  test.Helpers ) {
309+ 				helpers .Anyhow ("rm" , "-f" , data .Identifier ())
310+ 			},
311+ 			Command : func (data  test.Data , helpers  test.Helpers ) test.TestableCommand  {
312+ 
313+ 				helpers .Anyhow ("stop" , data .Identifier ())
314+ 				initialLogs  :=  helpers .Capture ("logs" , data .Identifier ())
315+ 				initialFooCount  :=  strings .Count (initialLogs , "foo" )
316+ 				data .Set ("initialFooCount" , strconv .Itoa (initialFooCount ))
317+ 				helpers .Ensure ("start" , data .Identifier ())
318+ 				nerdtest .EnsureContainerStarted (helpers , data .Identifier ())
319+ 				return  helpers .Command ("logs" , data .Identifier ())
320+ 			},
321+ 			Expected : func (data  test.Data , helpers  test.Helpers ) * test.Expected  {
322+ 				return  & test.Expected {
323+ 					ExitCode : 0 ,
324+ 					Output : func (stdout  string , info  string , t  * testing.T ) {
325+ 						finalLogsCount  :=  strings .Count (stdout , "foo" )
326+ 						initialFooCount , _  :=  strconv .Atoi (data .Get ("initialFooCount" ))
327+ 						assert .Assert (t , finalLogsCount  >  initialFooCount , "Expected 'foo' count to increase after restart" , info )
328+ 					},
329+ 				}
330+ 			},
331+ 		},
332+ 	}
333+ 	testCase .Run (t )
334+ }
0 commit comments