@@ -18,13 +18,18 @@ package compose
1818
1919import (
2020 "fmt"
21+ "regexp"
2122 "testing"
2223
24+ "github.com/containerd/nerdctl/mod/tigron/expect"
25+ "github.com/containerd/nerdctl/mod/tigron/test"
26+ "github.com/containerd/nerdctl/mod/tigron/tig"
27+
2328 "github.com/containerd/nerdctl/v2/pkg/testutil"
29+ "github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
2430)
2531
2632func TestComposeRestart (t * testing.T ) {
27- base := testutil .NewBase (t )
2833 var dockerComposeYAML = fmt .Sprintf (`
2934services:
3035 wordpress:
@@ -51,24 +56,65 @@ volumes:
5156 db:
5257` , testutil .WordpressImage , testutil .MariaDBImage )
5358
54- comp := testutil .NewComposeDir (t , dockerComposeYAML )
55- defer comp .CleanUp ()
56- projectName := comp .ProjectName ()
57- t .Logf ("projectName=%q" , projectName )
59+ testCase := nerdtest .Setup ()
60+
61+ testCase .Setup = func (data test.Data , helpers test.Helpers ) {
62+ data .Temp ().Save (dockerComposeYAML , "compose.yaml" )
63+ helpers .Ensure ("compose" , "-f" , data .Temp ().Path ("compose.yaml" ), "up" , "-d" )
64+ data .Labels ().Set ("yamlPath" , data .Temp ().Path ("compose.yaml" ))
65+ }
5866
59- base .ComposeCmd ("-f" , comp .YAMLFullPath (), "up" , "-d" ).AssertOK ()
60- defer base .ComposeCmd ("-f" , comp .YAMLFullPath (), "down" , "-v" ).Run ()
67+ testCase .Cleanup = func (data test.Data , helpers test.Helpers ) {
68+ helpers .Anyhow ("compose" , "-f" , data .Labels ().Get ("yamlPath" ), "down" , "-v" )
69+ }
6170
62- // stop and restart a single service.
63- base .ComposeCmd ("-f" , comp .YAMLFullPath (), "stop" , "db" ).AssertOK ()
64- base .ComposeCmd ("-f" , comp .YAMLFullPath (), "ps" , "db" , "-a" ).AssertOutContainsAny ("Exit" , "exited" )
65- base .ComposeCmd ("-f" , comp .YAMLFullPath (), "restart" , "db" ).AssertOK ()
66- base .ComposeCmd ("-f" , comp .YAMLFullPath (), "ps" , "db" ).AssertOutContainsAny ("Up" , "running" )
71+ testCase .SubTests = []* test.Case {
72+ {
73+ Description : "restart single service" ,
74+ NoParallel : true ,
75+ Setup : func (data test.Data , helpers test.Helpers ) {
76+ helpers .Ensure ("compose" , "-f" , data .Labels ().Get ("yamlPath" ), "stop" , "db" )
77+ ps := helpers .Capture ("compose" , "-f" , data .Labels ().Get ("yamlPath" ), "ps" , "db" , "-a" )
78+ expect .Match (regexp .MustCompile ("Exit|exited" ))(ps , helpers .T ())
79+ },
80+ Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
81+ return helpers .Command ("compose" , "-f" , data .Labels ().Get ("yamlPath" ), "restart" , "db" )
82+ },
83+ Expected : func (data test.Data , helpers test.Helpers ) * test.Expected {
84+ return & test.Expected {
85+ ExitCode : expect .ExitCodeSuccess ,
86+ Output : func (stdout string , t tig.T ) {
87+ ps := helpers .Capture ("compose" , "-f" , data .Labels ().Get ("yamlPath" ), "ps" , "db" )
88+ expect .Match (regexp .MustCompile ("Up|running" ))(ps , t )
89+ },
90+ }
91+ },
92+ },
93+ {
94+ Description : "stop one service and restart all with timeout" ,
95+ NoParallel : true ,
96+ Setup : func (data test.Data , helpers test.Helpers ) {
97+ helpers .Ensure ("compose" , "-f" , data .Labels ().Get ("yamlPath" ), "stop" , "db" )
98+ ps := helpers .Capture ("compose" , "-f" , data .Labels ().Get ("yamlPath" ), "ps" , "db" , "-a" )
99+ expect .Match (regexp .MustCompile ("Exit|exited" ))(ps , helpers .T ())
100+ },
101+ Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
102+ return helpers .Command ("compose" , "-f" , data .Labels ().Get ("yamlPath" ), "restart" , "--timeout" , "5" )
103+ },
104+ Expected : func (data test.Data , helpers test.Helpers ) * test.Expected {
105+ return & test.Expected {
106+ ExitCode : expect .ExitCodeSuccess ,
107+ Output : func (stdout string , t tig.T ) {
108+ db := helpers .Capture ("compose" , "-f" , data .Labels ().Get ("yamlPath" ), "ps" , "db" )
109+ wp := helpers .Capture ("compose" , "-f" , data .Labels ().Get ("yamlPath" ), "ps" , "wordpress" )
110+ comp := expect .Match (regexp .MustCompile ("Up|running" ))
111+ comp (db , t )
112+ comp (wp , t )
113+ },
114+ }
115+ },
116+ },
117+ }
67118
68- // stop one service and restart all (also check `--timeout` arg).
69- base .ComposeCmd ("-f" , comp .YAMLFullPath (), "stop" , "db" ).AssertOK ()
70- base .ComposeCmd ("-f" , comp .YAMLFullPath (), "ps" , "db" , "-a" ).AssertOutContainsAny ("Exit" , "exited" )
71- base .ComposeCmd ("-f" , comp .YAMLFullPath (), "restart" , "--timeout" , "5" ).AssertOK ()
72- base .ComposeCmd ("-f" , comp .YAMLFullPath (), "ps" , "db" ).AssertOutContainsAny ("Up" , "running" )
73- base .ComposeCmd ("-f" , comp .YAMLFullPath (), "ps" , "wordpress" ).AssertOutContainsAny ("Up" , "running" )
119+ testCase .Run (t )
74120}
0 commit comments