17
17
package compose
18
18
19
19
import (
20
+ "errors"
20
21
"fmt"
21
22
"testing"
22
23
24
+ "github.com/containerd/nerdctl/mod/tigron/expect"
25
+ "github.com/containerd/nerdctl/mod/tigron/test"
26
+
23
27
"github.com/containerd/nerdctl/v2/pkg/testutil"
28
+ "github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
24
29
)
25
30
26
31
func TestComposeBuild (t * testing.T ) {
27
- const imageSvc0 = "composebuild_svc0"
28
- const imageSvc1 = "composebuild_svc1"
32
+ dockerfile := "FROM " + testutil .AlpineImage
33
+
34
+ testCase := nerdtest .Setup ()
35
+
36
+ testCase .Require = nerdtest .Build
37
+
38
+ testCase .Setup = func (data test.Data , helpers test.Helpers ) {
39
+ // Make sure we shard the image name to something unique to the test to avoid conflicts with other tests
40
+ imageSvc0 := data .Identifier ("svc0" )
41
+ imageSvc1 := data .Identifier ("svc1" )
29
42
30
- dockerComposeYAML := fmt .Sprintf (`
43
+ // We are not going to run them, so, ports conflicts should not matter here
44
+ dockerComposeYAML := fmt .Sprintf (`
31
45
services:
32
46
svc0:
33
47
build: .
@@ -43,31 +57,81 @@ services:
43
57
- 8081:80
44
58
` , imageSvc0 , imageSvc1 )
45
59
46
- dockerfile := fmt .Sprintf (`FROM %s` , testutil .AlpineImage )
47
-
48
- testutil .RequiresBuild (t )
49
- testutil .RegisterBuildCacheCleanup (t )
50
- base := testutil .NewBase (t )
51
-
52
- comp := testutil .NewComposeDir (t , dockerComposeYAML )
53
- defer comp .CleanUp ()
54
- comp .WriteFile ("Dockerfile" , dockerfile )
55
- projectName := comp .ProjectName ()
56
- t .Logf ("projectName=%q" , projectName )
57
-
58
- defer base .Cmd ("rmi" , imageSvc0 ).Run ()
59
- defer base .Cmd ("rmi" , imageSvc1 ).Run ()
60
-
61
- // 1. build only 1 service without triggering the dependency service build
62
- base .ComposeCmd ("-f" , comp .YAMLFullPath (), "build" , "svc0" ).AssertOK ()
63
- base .Cmd ("images" ).AssertOutContains (imageSvc0 )
64
- base .Cmd ("images" ).AssertOutNotContains (imageSvc1 )
65
- // 2. build multiple services
66
- base .ComposeCmd ("-f" , comp .YAMLFullPath (), "build" , "svc0" , "svc1" ).AssertOK ()
67
- base .Cmd ("images" ).AssertOutContains (imageSvc0 )
68
- base .Cmd ("images" ).AssertOutContains (imageSvc1 )
69
- // 3. build all if no args are given
70
- base .ComposeCmd ("-f" , comp .YAMLFullPath (), "build" ).AssertOK ()
71
- // 4. fail if some services args not exist in compose.yml
72
- base .ComposeCmd ("-f" , comp .YAMLFullPath (), "build" , "svc0" , "svc100" ).AssertFail ()
60
+ data .Temp ().Save (dockerComposeYAML , "compose.yaml" )
61
+ data .Temp ().Save (dockerfile , "Dockerfile" )
62
+
63
+ data .Labels ().Set ("composeYaml" , data .Temp ().Path ("compose.yaml" ))
64
+ data .Labels ().Set ("imageSvc0" , imageSvc0 )
65
+ data .Labels ().Set ("imageSvc1" , imageSvc1 )
66
+ }
67
+
68
+ testCase .SubTests = []* test.Case {
69
+ {
70
+ Description : "build svc0" ,
71
+ NoParallel : true ,
72
+ Setup : func (data test.Data , helpers test.Helpers ) {
73
+ helpers .Ensure ("compose" , "-f" , data .Labels ().Get ("composeYaml" ), "build" , "svc0" )
74
+ },
75
+
76
+ Command : test .Command ("images" ),
77
+
78
+ Expected : func (data test.Data , helpers test.Helpers ) * test.Expected {
79
+ return & test.Expected {
80
+ Output : expect .All (
81
+ expect .Contains (data .Labels ().Get ("imageSvc0" )),
82
+ expect .DoesNotContain (data .Labels ().Get ("imageSvc1" )),
83
+ ),
84
+ }
85
+ },
86
+ },
87
+ {
88
+ Description : "build svc0 and svc1" ,
89
+ NoParallel : true ,
90
+ Setup : func (data test.Data , helpers test.Helpers ) {
91
+ helpers .Ensure ("compose" , "-f" , data .Labels ().Get ("composeYaml" ), "build" , "svc0" , "svc1" )
92
+ },
93
+
94
+ Command : test .Command ("images" ),
95
+
96
+ Expected : func (data test.Data , helpers test.Helpers ) * test.Expected {
97
+ return & test.Expected {
98
+ Output : expect .All (
99
+ expect .Contains (data .Labels ().Get ("imageSvc0" )),
100
+ expect .Contains (data .Labels ().Get ("imageSvc1" )),
101
+ ),
102
+ }
103
+ },
104
+ },
105
+ {
106
+ Description : "build no arg" ,
107
+ Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
108
+ return helpers .Command ("compose" , "-f" , data .Labels ().Get ("composeYaml" ), "build" )
109
+ },
110
+
111
+ Expected : test .Expects (expect .ExitCodeSuccess , nil , nil ),
112
+ },
113
+ {
114
+ Description : "build bogus" ,
115
+ Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
116
+ return helpers .Command (
117
+ "compose" ,
118
+ "-f" ,
119
+ data .Labels ().Get ("composeYaml" ),
120
+ "build" ,
121
+ "svc0" ,
122
+ "svc100" ,
123
+ )
124
+ },
125
+
126
+ Expected : test .Expects (1 , []error {errors .New ("no such service: svc100" )}, nil ),
127
+ },
128
+ }
129
+
130
+ testCase .Cleanup = func (data test.Data , helpers test.Helpers ) {
131
+ if data .Labels ().Get ("imageSvc0" ) != "" {
132
+ helpers .Anyhow ("rmi" , data .Labels ().Get ("imageSvc0" ), data .Labels ().Get ("imageSvc1" ))
133
+ }
134
+ }
135
+
136
+ testCase .Run (t )
73
137
}
0 commit comments