@@ -28,6 +28,8 @@ import (
2828 "gotest.tools/v3/icmd"
2929
3030 "github.com/containerd/nerdctl/v2/pkg/testutil"
31+ "github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
32+ "github.com/containerd/nerdctl/v2/pkg/testutil/test"
3133)
3234
3335func TestLogs (t * testing.T ) {
@@ -160,65 +162,86 @@ func TestLogsWithFailingContainer(t *testing.T) {
160162}
161163
162164func TestLogsWithForegroundContainers (t * testing.T ) {
163- t .Parallel ()
164- if runtime .GOOS == "windows" {
165- t .Skip ("dual logging is not supported on Windows" )
166- }
167- base := testutil .NewBase (t )
168- tid := testutil .Identifier (t )
165+ testCase := nerdtest .Setup ()
166+ // dual logging is not supported on Windows
167+ testCase .Require = test .Not (test .Windows )
169168
170- // unbuffer(1) emulates tty, which is required by `nerdctl run -t`.
171- // unbuffer(1) can be installed with `apt-get install expect`.
172- unbuffer := []string {"unbuffer" }
169+ testCase .Run (t )
173170
174- testCases := []struct {
175- name string
176- flags []string
177- tty bool
178- }{
171+ testCase .SubTests = []* test.Case {
179172 {
180- name : "foreground" ,
181- flags : nil ,
182- tty : false ,
173+ Description : "foreground" ,
174+ Cleanup : func (data test.Data , helpers test.Helpers ) {
175+ helpers .Anyhow ("rm" , "-f" , data .Identifier ())
176+ },
177+ Setup : func (data test.Data , helpers test.Helpers ) {
178+ helpers .Ensure ("run" , "--name" , data .Identifier (), testutil .CommonImage , "sh" , "-euxc" , "echo foo; echo bar" )
179+ },
180+ Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
181+ return helpers .Command ("logs" , data .Identifier ())
182+ },
183+ Expected : test .Expects (0 , nil , test .All (
184+ test .Contains ("foo" ),
185+ test .Contains ("bar" ),
186+ test .DoesNotContain ("baz" ),
187+ )),
183188 },
184189 {
185- name : "interactive" ,
186- flags : []string {"-i" },
187- tty : false ,
190+ Description : "interactive" ,
191+ Cleanup : func (data test.Data , helpers test.Helpers ) {
192+ helpers .Anyhow ("rm" , "-f" , data .Identifier ())
193+ },
194+ Setup : func (data test.Data , helpers test.Helpers ) {
195+ helpers .Ensure ("run" , "-i" , "--name" , data .Identifier (), testutil .CommonImage , "sh" , "-euxc" , "echo foo; echo bar" )
196+ },
197+ Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
198+ return helpers .Command ("logs" , data .Identifier ())
199+ },
200+ Expected : test .Expects (0 , nil , test .All (
201+ test .Contains ("foo" ),
202+ test .Contains ("bar" ),
203+ test .DoesNotContain ("baz" ),
204+ )),
188205 },
189206 {
190- name : "PTY" ,
191- flags : []string {"-t" },
192- tty : true ,
207+ Description : "PTY" ,
208+ Cleanup : func (data test.Data , helpers test.Helpers ) {
209+ helpers .Anyhow ("rm" , "-f" , data .Identifier ())
210+ },
211+ Setup : func (data test.Data , helpers test.Helpers ) {
212+ cmd := helpers .Command ("run" , "-t" , "--name" , data .Identifier (), testutil .CommonImage , "sh" , "-euxc" , "echo foo; echo bar" )
213+ cmd .WithPseudoTTY ()
214+ cmd .Run (& test.Expected {ExitCode : 0 })
215+ },
216+ Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
217+ return helpers .Command ("logs" , data .Identifier ())
218+ },
219+ Expected : test .Expects (0 , nil , test .All (
220+ test .Contains ("foo" ),
221+ test .Contains ("bar" ),
222+ test .DoesNotContain ("baz" ),
223+ )),
193224 },
194225 {
195- name : "interactivePTY" ,
196- flags : []string {"-i" , "-t" },
197- tty : true ,
226+ Description : "interactivePTY" ,
227+ Cleanup : func (data test.Data , helpers test.Helpers ) {
228+ helpers .Anyhow ("rm" , "-f" , data .Identifier ())
229+ },
230+ Setup : func (data test.Data , helpers test.Helpers ) {
231+ cmd := helpers .Command ("run" , "-i" , "-t" , "--name" , data .Identifier (), testutil .CommonImage , "sh" , "-euxc" , "echo foo; echo bar" )
232+ cmd .WithPseudoTTY ()
233+ cmd .Run (& test.Expected {ExitCode : 0 })
234+ },
235+ Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
236+ return helpers .Command ("logs" , data .Identifier ())
237+ },
238+ Expected : test .Expects (0 , nil , test .All (
239+ test .Contains ("foo" ),
240+ test .Contains ("bar" ),
241+ test .DoesNotContain ("baz" ),
242+ )),
198243 },
199244 }
200-
201- for _ , tc := range testCases {
202- tc := tc
203- func (t * testing.T ) {
204- containerName := tid + "-" + tc .name
205- var cmdArgs []string
206- defer base .Cmd ("rm" , "-f" , containerName ).Run ()
207- cmdArgs = append (cmdArgs , "run" , "--name" , containerName )
208- cmdArgs = append (cmdArgs , tc .flags ... )
209- cmdArgs = append (cmdArgs , testutil .CommonImage , "sh" , "-euxc" , "echo foo; echo bar" )
210-
211- if tc .tty {
212- base .CmdWithHelper (unbuffer , cmdArgs ... ).AssertOK ()
213- } else {
214- base .Cmd (cmdArgs ... ).AssertOK ()
215- }
216-
217- base .Cmd ("logs" , containerName ).AssertOutContains ("foo" )
218- base .Cmd ("logs" , containerName ).AssertOutContains ("bar" )
219- base .Cmd ("logs" , containerName ).AssertOutNotContains ("baz" )
220- }(t )
221- }
222245}
223246
224247func TestTailFollowRotateLogs (t * testing.T ) {
0 commit comments