@@ -18,15 +18,23 @@ package compose
1818
1919import (
2020 "fmt"
21+ "path/filepath"
22+ "regexp"
2123 "testing"
22- "time"
2324
25+ "github.com/containerd/nerdctl/mod/tigron/expect"
26+ "github.com/containerd/nerdctl/mod/tigron/test"
27+
28+ "github.com/containerd/nerdctl/v2/pkg/composer/serviceparser"
2429 "github.com/containerd/nerdctl/v2/pkg/testutil"
30+ "github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
2531)
2632
2733func TestComposeKill (t * testing.T ) {
28- base := testutil .NewBase (t )
29- var dockerComposeYAML = fmt .Sprintf (`
34+ testCase := nerdtest .Setup ()
35+
36+ testCase .Setup = func (data test.Data , helpers test.Helpers ) {
37+ dockerComposeYAML := fmt .Sprintf (`
3038services:
3139
3240 wordpress:
@@ -54,17 +62,52 @@ volumes:
5462 db:
5563` , testutil .WordpressImage , testutil .MariaDBImage )
5664
57- comp := testutil .NewComposeDir (t , dockerComposeYAML )
58- defer comp .CleanUp ()
59- projectName := comp .ProjectName ()
60- t .Logf ("projectName=%q" , projectName )
65+ composePath := data .Temp ().Save (dockerComposeYAML , "compose.yaml" )
66+
67+ projectName := filepath .Base (filepath .Dir (composePath ))
68+ t .Logf ("projectName=%q" , projectName )
69+
70+ wordpressContainerName := serviceparser .DefaultContainerName (projectName , "wordpress" , "1" )
71+ dbContainerName := serviceparser .DefaultContainerName (projectName , "db" , "1" )
72+
73+ data .Labels ().Set ("composeYAML" , composePath )
74+ data .Labels ().Set ("wordpressContainer" , wordpressContainerName )
75+ data .Labels ().Set ("dbContainer" , dbContainerName )
76+
77+ helpers .Ensure ("compose" , "-f" , composePath , "up" , "-d" )
78+ nerdtest .EnsureContainerStarted (helpers , wordpressContainerName )
79+ nerdtest .EnsureContainerStarted (helpers , dbContainerName )
80+ }
81+
82+ testCase .SubTests = []* test.Case {
83+ {
84+ Description : "kill db container and exit with 137" ,
85+ NoParallel : true ,
86+ Setup : func (data test.Data , helpers test.Helpers ) {
87+ helpers .Ensure ("compose" , "-f" , data .Labels ().Get ("composeYAML" ), "kill" , "db" )
88+ nerdtest .EnsureContainerExited (helpers , data .Labels ().Get ("dbContainer" ), expect .ExitCodeSigkill )
89+ },
90+ Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
91+ return helpers .Command ("compose" , "-f" , data .Labels ().Get ("composeYAML" ), "ps" , "db" , "-a" )
92+ },
93+ // Docker Compose v1: "Exit 137", v2: "exited (137)"
94+ Expected : test .Expects (expect .ExitCodeSuccess , nil , expect .Match (regexp .MustCompile (` 137|\(137\)` ))),
95+ },
96+ {
97+ Description : "wordpress container is still running" ,
98+ NoParallel : true ,
99+ Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
100+ return helpers .Command ("compose" , "-f" , data .Labels ().Get ("composeYAML" ), "ps" , "wordpress" )
101+ },
102+ Expected : test .Expects (expect .ExitCodeSuccess , nil , expect .Match (regexp .MustCompile ("Up|running" ))),
103+ },
104+ }
61105
62- base .ComposeCmd ("-f" , comp .YAMLFullPath (), "up" , "-d" ).AssertOK ()
63- defer base .ComposeCmd ("-f" , comp .YAMLFullPath (), "down" , "-v" ).Run ()
106+ testCase .Cleanup = func (data test.Data , helpers test.Helpers ) {
107+ if data .Labels ().Get ("composeYAML" ) != "" {
108+ helpers .Anyhow ("compose" , "-f" , data .Labels ().Get ("composeYAML" ), "down" , "-v" )
109+ }
110+ }
64111
65- base .ComposeCmd ("-f" , comp .YAMLFullPath (), "kill" , "db" ).AssertOK ()
66- time .Sleep (3 * time .Second )
67- // Docker Compose v1: "Exit 137", v2: "exited (137)"
68- base .ComposeCmd ("-f" , comp .YAMLFullPath (), "ps" , "db" , "-a" ).AssertOutContainsAny (" 137" , "(137)" )
69- base .ComposeCmd ("-f" , comp .YAMLFullPath (), "ps" , "wordpress" ).AssertOutContainsAny ("Up" , "running" )
112+ testCase .Run (t )
70113}
0 commit comments