@@ -37,97 +37,116 @@ 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`
43+ const ExitSuccess = 0
4444
45+ func newLogTestCase (name string ) * test.Case {
4546 testCase := nerdtest .Setup ()
46-
47- testCase .Require = nerdtest .IsFlaky ("https://github.com/containerd/nerdctl/issues/4782" )
48- if runtime .GOOS == "windows" {
49- testCase .Require = nerdtest .NerdctlNeedsFixing ("https://github.com/containerd/nerdctl/issues/4237" )
50- }
51-
47+ testCase .NoParallel = true
5248 testCase .Cleanup = func (data test.Data , helpers test.Helpers ) {
53- helpers .Anyhow ("rm" , "-f" , data . Identifier () )
49+ helpers .Anyhow ("rm" , "-f" , name )
5450 }
55-
5651 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 () )
52+ helpers .Ensure ("run" , "--quiet" , "--name" , name , testutil .CommonImage ,
53+ "sh " , "-euxc" , "echo foo; echo bar;" )
5954 }
55+ return testCase
56+ }
6057
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 )),
58+ func TestLogs_Since1s (t * testing.T ) {
59+ testCase := newLogTestCase (t .Name ())
60+ testCase .SubTests = []* test.Case {{
61+ Description : "since 1s" ,
62+ Setup : func (data test.Data , helpers test.Helpers ) {
63+ // Ensure at least 2 seconds have elapsed since the container ran,
64+ // so that --since 1s does not include the container's output.
65+ time .Sleep (2 * time .Second )
7366 },
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 )),
67+ Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
68+ return helpers .Command ("logs" , "--since" , "1s" , t .Name ())
8069 },
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 )),
70+ Expected : test .Expects (ExitSuccess , nil , expect .DoesNotContain (expected )),
71+ }}
72+ testCase .Run (t )
73+ }
74+
75+ func TestLogs_Since60s (t * testing.T ) {
76+ testCase := newLogTestCase (t .Name ())
77+ testCase .SubTests = []* test.Case {{
78+ Description : "since 60s" ,
79+ Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
80+ return helpers .Command ("logs" , "--since" , "60s" , t .Name ())
8781 },
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 )),
82+ Expected : test . Expects ( ExitSuccess , nil , expect . Equals ( expected )),
83+ }}
84+ testCase . Run ( t )
85+ }
86+
87+ func TestLogs_Until60s ( t * testing. T ) {
88+ testCase := newLogTestCase ( t . Name ())
89+ testCase . SubTests = [] * test.Case { {
90+ Description : " until 60s" ,
91+ Command : func ( data test. Data , helpers test. Helpers ) test. TestableCommand {
92+ return helpers . Command ( "logs" , "--until" , "60s" , t . Name ())
9993 },
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 )),
94+ Expected : test .Expects (ExitSuccess , nil , expect .DoesNotContain (expected )),
95+ }}
96+ testCase .Run (t )
97+ }
98+
99+ func TestLogs_Until1s (t * testing.T ) {
100+ testCase := newLogTestCase (t .Name ())
101+ testCase .SubTests = []* test.Case {{
102+ Description : "until 1s" ,
103+ // Ensure at least 2 seconds have elapsed since the container ran,
104+ // so that --until 1s includes the container's output.
105+ Setup : func (data test.Data , helpers test.Helpers ) {
106+ time .Sleep (2 * time .Second )
106107 },
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" ))),
108+ Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
109+ return helpers .Command ("logs" , "--until" , "1s" , t .Name ())
113110 },
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 )),
111+ Expected : test .Expects (ExitSuccess , nil , expect .Equals (expected )),
112+ }}
113+ testCase .Run (t )
114+ }
115+
116+ func TestLogs_Follow (t * testing.T ) {
117+ testCase := newLogTestCase (t .Name ())
118+ testCase .SubTests = []* test.Case {{
119+ Description : "follow" ,
120+ Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
121+ return helpers .Command ("logs" , "-f" , t .Name ())
120122 },
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 |)$" ))),
123+ Expected : test .Expects (ExitSuccess , nil , expect .Equals (expected )),
124+ }}
125+ testCase .Run (t )
126+ }
127+
128+ func TestLogs_Timestamp (t * testing.T ) {
129+ testCase := newLogTestCase (t .Name ())
130+ testCase .SubTests = []* test.Case {{
131+ Description : "timestamp" ,
132+ Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
133+ return helpers .Command ("logs" , "-t" , t .Name ())
128134 },
129- }
135+ Expected : test .Expects (ExitSuccess , nil , expect .Contains (time .Now ().UTC ().Format ("2006-01-02" ))),
136+ }}
137+ testCase .Run (t )
138+ }
130139
140+ func TestLogs_Tail1 (t * testing.T ) {
141+ testCase := newLogTestCase (t .Name ())
142+ testCase .SubTests = []* test.Case {{
143+ Description : "tail flag" ,
144+ Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
145+ return helpers .Command ("logs" , "-n" , "1" , t .Name ())
146+ },
147+ // FIXME: why?
148+ Expected : test .Expects (ExitSuccess , nil , expect .Match (regexp .MustCompile ("^(?:bar\n |)$" ))),
149+ }}
131150 testCase .Run (t )
132151}
133152
0 commit comments