@@ -37,97 +37,118 @@ import (
3737 "github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
3838)
3939
40- func TestLogs (t * testing.T ) {
41- const expected = `foo
40+ const expected = `foo
4241bar
4342`
4443
44+ func newLogTestCase (name string ) * test.Case {
4545 testCase := nerdtest .Setup ()
46-
47- testCase .Require = nerdtest .IsFlaky ("https://github.com/containerd/nerdctl/issues/4782" )
46+ testCase .NoParallel = true
4847 if runtime .GOOS == "windows" {
4948 testCase .Require = nerdtest .NerdctlNeedsFixing ("https://github.com/containerd/nerdctl/issues/4237" )
5049 }
51-
5250 testCase .Cleanup = func (data test.Data , helpers test.Helpers ) {
53- helpers .Anyhow ("rm" , "-f" , data . Identifier () )
51+ helpers .Anyhow ("rm" , "-f" , name )
5452 }
55-
5653 testCase .Setup = func (data test.Data , helpers test.Helpers ) {
57- helpers .Ensure ("run" , "--quiet" , "--name" , data . Identifier () , testutil .CommonImage , "sh" , "-euxc" , "echo foo; echo bar;" )
58- data . Labels (). Set ( "cID " , data . Identifier () )
54+ helpers .Ensure ("run" , "--quiet" , "--name" , name , testutil .CommonImage ,
55+ "sh " , "-euxc" , "echo foo; echo bar;" )
5956 }
57+ return testCase
58+ }
6059
61- testCase .SubTests = []* test.Case {
62- {
63- Description : "since 1s" ,
64- Setup : func (data test.Data , helpers test.Helpers ) {
65- // Ensure at least 2 seconds have elapsed since the container ran,
66- // so that --since 1s does not include the container's output.
67- time .Sleep (2 * time .Second )
68- },
69- Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
70- return helpers .Command ("logs" , "--since" , "1s" , data .Labels ().Get ("cID" ))
71- },
72- Expected : test .Expects (0 , nil , expect .DoesNotContain (expected )),
60+ func TestLogs_Since1s (t * testing.T ) {
61+ testCase := newLogTestCase (t .Name ())
62+ testCase .SubTests = []* test.Case {{
63+ Description : "since 1s" ,
64+ Setup : func (data test.Data , helpers test.Helpers ) {
65+ // Ensure at least 2 seconds have elapsed since the container ran,
66+ // so that --since 1s does not include the container's output.
67+ time .Sleep (2 * time .Second )
7368 },
74- {
75- Description : "since 60s" ,
76- Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
77- return helpers .Command ("logs" , "--since" , "60s" , data .Labels ().Get ("cID" ))
78- },
79- Expected : test .Expects (0 , nil , expect .Equals (expected )),
69+ Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
70+ return helpers .Command ("logs" , "--since" , "1s" , t .Name ())
8071 },
81- {
82- Description : "until 60s" ,
83- Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
84- return helpers .Command ("logs" , "--until" , "60s" , data .Labels ().Get ("cID" ))
85- },
86- Expected : test .Expects (0 , nil , expect .DoesNotContain (expected )),
72+ Expected : test .Expects (0 , nil , expect .DoesNotContain (expected )),
73+ }}
74+ testCase .Run (t )
75+ }
76+
77+ func TestLogs_Since60s (t * testing.T ) {
78+ testCase := newLogTestCase (t .Name ())
79+ testCase .SubTests = []* test.Case {{
80+ Description : "since 60s" ,
81+ Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
82+ return helpers .Command ("logs" , "--since" , "60s" , t .Name ())
8783 },
88- {
89- Description : "until 1s" ,
90- Setup : func ( data test. Data , helpers test. Helpers ) {
91- // Ensure at least 2 seconds have elapsed since the container ran,
92- // so that --until 1s includes the container's output.
93- time . Sleep ( 2 * time . Second )
94- },
95- Command : func ( data test. Data , helpers test.Helpers ) test. TestableCommand {
96- return helpers . Command ( "logs" , "-- until" , "1s" , data . Labels (). Get ( "cID" ))
97- },
98- Expected : test . Expects ( 0 , nil , expect . Equals ( expected )),
84+ Expected : test . Expects ( 0 , nil , expect . Equals ( expected )),
85+ }}
86+ testCase . Run ( t )
87+ }
88+
89+ func TestLogs_Until60s ( t * testing. T ) {
90+ testCase := newLogTestCase ( t . Name ())
91+ testCase . SubTests = [] * test.Case { {
92+ Description : " until 60s" ,
93+ Command : func ( data test. Data , helpers test. Helpers ) test. TestableCommand {
94+ return helpers . Command ( "logs" , "--until" , "60s" , t . Name ())
9995 },
100- {
101- Description : "follow" ,
102- Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
103- return helpers .Command ("logs" , "-f" , data .Labels ().Get ("cID" ))
104- },
105- Expected : test .Expects (0 , nil , expect .Equals (expected )),
96+ Expected : test .Expects (0 , nil , expect .DoesNotContain (expected )),
97+ }}
98+ testCase .Run (t )
99+ }
100+
101+ func TestLogs_Until1s (t * testing.T ) {
102+ testCase := newLogTestCase (t .Name ())
103+ testCase .SubTests = []* test.Case {{
104+ Description : "until 1s" ,
105+ // Ensure at least 2 seconds have elapsed since the container ran,
106+ // so that --until 1s includes the container's output.
107+ Setup : func (data test.Data , helpers test.Helpers ) {
108+ time .Sleep (2 * time .Second )
106109 },
107- {
108- Description : "timestamp" ,
109- Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
110- return helpers .Command ("logs" , "-t" , data .Labels ().Get ("cID" ))
111- },
112- Expected : test .Expects (0 , nil , expect .Contains (time .Now ().UTC ().Format ("2006-01-02" ))),
110+ Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
111+ return helpers .Command ("logs" , "--until" , "1s" , t .Name ())
113112 },
114- {
115- Description : "tail flag" ,
116- Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
117- return helpers .Command ("logs" , "-n" , "all" , data .Labels ().Get ("cID" ))
118- },
119- Expected : test .Expects (0 , nil , expect .Equals (expected )),
113+ Expected : test .Expects (0 , nil , expect .Equals (expected )),
114+ }}
115+ testCase .Run (t )
116+ }
117+
118+ func TestLogs_Follow (t * testing.T ) {
119+ testCase := newLogTestCase (t .Name ())
120+ testCase .SubTests = []* test.Case {{
121+ Description : "follow" ,
122+ Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
123+ return helpers .Command ("logs" , "-f" , t .Name ())
120124 },
121- {
122- Description : "tail flag" ,
123- Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
124- return helpers .Command ("logs" , "-n" , "1" , data .Labels ().Get ("cID" ))
125- },
126- // FIXME: why?
127- Expected : test .Expects (0 , nil , expect .Match (regexp .MustCompile ("^(?:bar\n |)$" ))),
125+ Expected : test .Expects (0 , nil , expect .Equals (expected )),
126+ }}
127+ testCase .Run (t )
128+ }
129+
130+ func TestLogs_Timestamp (t * testing.T ) {
131+ testCase := newLogTestCase (t .Name ())
132+ testCase .SubTests = []* test.Case {{
133+ Description : "timestamp" ,
134+ Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
135+ return helpers .Command ("logs" , "-t" , t .Name ())
128136 },
129- }
137+ Expected : test .Expects (0 , nil , expect .Contains (time .Now ().UTC ().Format ("2006-01-02" ))),
138+ }}
139+ testCase .Run (t )
140+ }
130141
142+ func TestLogs_Tail1 (t * testing.T ) {
143+ testCase := newLogTestCase (t .Name ())
144+ testCase .SubTests = []* test.Case {{
145+ Description : "tail flag" ,
146+ Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
147+ return helpers .Command ("logs" , "-n" , "1" , t .Name ())
148+ },
149+ // FIXME: why?
150+ Expected : test .Expects (0 , nil , expect .Match (regexp .MustCompile ("^(?:bar\n |)$" ))),
151+ }}
131152 testCase .Run (t )
132153}
133154
0 commit comments