17
17
package compose
18
18
19
19
import (
20
- "encoding/json"
21
20
"fmt"
22
- "strings"
23
21
"testing"
24
22
23
+ "gotest.tools/v3/assert"
24
+
25
+ "github.com/containerd/nerdctl/mod/tigron/expect"
26
+ "github.com/containerd/nerdctl/mod/tigron/test"
27
+ "github.com/containerd/nerdctl/mod/tigron/tig"
28
+
29
+ "github.com/containerd/nerdctl/v2/pkg/referenceutil"
25
30
"github.com/containerd/nerdctl/v2/pkg/testutil"
31
+ "github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
26
32
)
27
33
28
34
func TestComposeImages (t * testing.T ) {
29
- base := testutil .NewBase (t )
30
35
var dockerComposeYAML = fmt .Sprintf (`
31
- version: '3.1'
32
-
33
36
services:
34
37
wordpress:
35
38
image: %s
36
- ports:
37
- - 8080:80
39
+ container_name: wordpress
38
40
environment:
39
41
WORDPRESS_DB_HOST: db
40
42
WORDPRESS_DB_USER: exampleuser
@@ -44,6 +46,7 @@ services:
44
46
- wordpress:/var/www/html
45
47
db:
46
48
image: %s
49
+ container_name: db
47
50
environment:
48
51
MYSQL_DATABASE: exampledb
49
52
MYSQL_USER: exampleuser
@@ -57,95 +60,71 @@ volumes:
57
60
db:
58
61
` , testutil .WordpressImage , testutil .MariaDBImage )
59
62
60
- comp := testutil .NewComposeDir (t , dockerComposeYAML )
61
- defer comp .CleanUp ()
62
- projectName := comp .ProjectName ()
63
- t .Logf ("projectName=%q" , projectName )
64
-
65
- base .ComposeCmd ("-f" , comp .YAMLFullPath (), "up" , "-d" ).AssertOK ()
66
- defer base .ComposeCmd ("-f" , comp .YAMLFullPath (), "down" , "-v" ).Run ()
67
-
68
- wordpressImageName := strings .Split (testutil .WordpressImage , ":" )[0 ]
69
- dbImageName := strings .Split (testutil .MariaDBImage , ":" )[0 ]
70
-
71
- // check one service image
72
- base .ComposeCmd ("-f" , comp .YAMLFullPath (), "images" , "db" ).AssertOutContains (dbImageName )
73
- base .ComposeCmd ("-f" , comp .YAMLFullPath (), "images" , "db" ).AssertOutNotContains (wordpressImageName )
74
-
75
- // check all service images
76
- base .ComposeCmd ("-f" , comp .YAMLFullPath (), "images" ).AssertOutContains (dbImageName )
77
- base .ComposeCmd ("-f" , comp .YAMLFullPath (), "images" ).AssertOutContains (wordpressImageName )
78
- }
63
+ wordpressImageName , _ := referenceutil .Parse (testutil .WordpressImage )
64
+ dbImageName , _ := referenceutil .Parse (testutil .MariaDBImage )
79
65
80
- func TestComposeImagesJson (t * testing.T ) {
81
- base := testutil .NewBase (t )
82
- var dockerComposeYAML = fmt .Sprintf (`
83
- version: '3.1'
66
+ testCase := nerdtest .Setup ()
84
67
85
- services:
86
- wordpress:
87
- image: %s
88
- container_name: wordpress
89
- ports:
90
- - 8080:80
91
- environment:
92
- WORDPRESS_DB_HOST: db
93
- WORDPRESS_DB_USER: exampleuser
94
- WORDPRESS_DB_PASSWORD: examplepass
95
- WORDPRESS_DB_NAME: exampledb
96
- volumes:
97
- - wordpress:/var/www/html
98
- db:
99
- image: %s
100
- container_name: db
101
- environment:
102
- MYSQL_DATABASE: exampledb
103
- MYSQL_USER: exampleuser
104
- MYSQL_PASSWORD: examplepass
105
- MYSQL_RANDOM_ROOT_PASSWORD: '1'
106
- volumes:
107
- - db:/var/lib/mysql
108
-
109
- volumes:
110
- wordpress:
111
- db:
112
- ` , testutil .WordpressImage , testutil .MariaDBImage )
68
+ testCase .Setup = func (data test.Data , helpers test.Helpers ) {
69
+ data .Temp ().Save (dockerComposeYAML , "compose.yaml" )
70
+ data .Labels ().Set ("composeYaml" , data .Temp ().Path ("compose.yaml" ))
71
+ helpers .Ensure ("compose" , "-f" , data .Temp ().Path ("compose.yaml" ), "up" , "-d" )
72
+ }
113
73
114
- comp := testutil .NewComposeDir (t , dockerComposeYAML )
115
- defer comp .CleanUp ()
116
- projectName := comp .ProjectName ()
117
- t .Logf ("projectName=%q" , projectName )
118
-
119
- base .ComposeCmd ("-f" , comp .YAMLFullPath (), "up" , "-d" ).AssertOK ()
120
- defer base .ComposeCmd ("-f" , comp .YAMLFullPath (), "down" , "-v" ).Run ()
121
-
122
- assertHandler := func (svc string , count int , fields ... string ) func (stdout string ) error {
123
- return func (stdout string ) error {
124
- // 1. check json output can be unmarshalled back to printables.
125
- var printables []composeContainerPrintable
126
- if err := json .Unmarshal ([]byte (stdout ), & printables ); err != nil {
127
- return fmt .Errorf ("[service: %s]failed to unmarshal json output from `compose images`: %s" , svc , stdout )
128
- }
129
- // 2. check #printables matches expected count.
130
- if len (printables ) != count {
131
- return fmt .Errorf ("[service: %s]unmarshal generates %d printables, expected %d: %s" , svc , len (printables ), count , stdout )
132
- }
133
- // 3. check marshalled json string has all expected substrings.
134
- for _ , field := range fields {
135
- if ! strings .Contains (stdout , field ) {
136
- return fmt .Errorf ("[service: %s]marshalled json output doesn't have expected string (%s): %s" , svc , field , stdout )
137
- }
138
- }
139
- return nil
140
- }
74
+ testCase .Cleanup = func (data test.Data , helpers test.Helpers ) {
75
+ helpers .Anyhow ("compose" , "-f" , data .Temp ().Path ("compose.yaml" ), "down" )
141
76
}
142
77
143
- // check other formats are not supported
144
- base .ComposeCmd ("-f" , comp .YAMLFullPath (), "images" , "--format" , "yaml" ).AssertFail ()
145
- // check all services are up (can be marshalled and unmarshalled)
146
- base .ComposeCmd ("-f" , comp .YAMLFullPath (), "images" , "--format" , "json" ).
147
- AssertOutWithFunc (assertHandler ("all" , 2 , `"ContainerName":"wordpress"` , `"ContainerName":"db"` ))
78
+ testCase .SubTests = []* test.Case {
79
+ {
80
+ Description : "images db" ,
81
+ Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
82
+ return helpers .Command ("compose" , "-f" , data .Labels ().Get ("composeYaml" ), "images" , "db" )
83
+ },
84
+ Expected : test .Expects (expect .ExitCodeSuccess , nil , expect .All (
85
+ expect .Contains (dbImageName .Name ()),
86
+ expect .DoesNotContain (wordpressImageName .Name ()),
87
+ )),
88
+ },
89
+ {
90
+ Description : "images" ,
91
+ Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
92
+ return helpers .Command ("compose" , "-f" , data .Labels ().Get ("composeYaml" ), "images" )
93
+ },
94
+ Expected : test .Expects (expect .ExitCodeSuccess , nil , expect .Contains (dbImageName .Name (), wordpressImageName .Name ())),
95
+ },
96
+ {
97
+ Description : "images --format yaml" ,
98
+ Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
99
+ return helpers .Command ("compose" , "-f" , data .Labels ().Get ("composeYaml" ), "images" , "--format" , "yaml" )
100
+ },
101
+ Expected : test .Expects (expect .ExitCodeGenericFail , nil , nil ),
102
+ },
103
+ {
104
+ Description : "images --format json" ,
105
+ Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
106
+ return helpers .Command ("compose" , "-f" , data .Labels ().Get ("composeYaml" ), "images" , "--format" , "json" )
107
+ },
108
+ Expected : test .Expects (expect .ExitCodeSuccess , nil , expect .All (
109
+ expect .JSON ([]composeContainerPrintable {}, func (printables []composeContainerPrintable , s string , t tig.T ) {
110
+ assert .Equal (t , len (printables ), 2 )
111
+ }),
112
+ expect .Contains (`"ContainerName":"wordpress"` , `"ContainerName":"db"` ),
113
+ )),
114
+ },
115
+ {
116
+ Description : "images --format json wordpress" ,
117
+ Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
118
+ return helpers .Command ("compose" , "-f" , data .Labels ().Get ("composeYaml" ), "images" , "--format" , "json" , "wordpress" )
119
+ },
120
+ Expected : test .Expects (expect .ExitCodeSuccess , nil , expect .All (
121
+ expect .JSON ([]composeContainerPrintable {}, func (printables []composeContainerPrintable , s string , t tig.T ) {
122
+ assert .Equal (t , len (printables ), 1 )
123
+ }),
124
+ expect .Contains (`"ContainerName":"wordpress"` ),
125
+ )),
126
+ },
127
+ }
148
128
149
- base .ComposeCmd ("-f" , comp .YAMLFullPath (), "images" , "--format" , "json" , "wordpress" ).
150
- AssertOutWithFunc (assertHandler ("wordpress" , 1 , `"ContainerName":"wordpress"` ))
129
+ testCase .Run (t )
151
130
}
0 commit comments