Skip to content

Commit 538c3bb

Browse files
authored
Merge pull request #4670 from haytok/issues_4613_compose_create_linux_test.go
test: refactor compose_create_linux_test.go to use Tigron
2 parents 53e7b27 + 4a95d73 commit 538c3bb

File tree

2 files changed

+149
-49
lines changed

2 files changed

+149
-49
lines changed

cmd/nerdctl/compose/compose_create_linux_test.go

Lines changed: 143 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@ package compose
1818

1919
import (
2020
"fmt"
21+
"path/filepath"
22+
"regexp"
2123
"strings"
2224
"testing"
2325

2426
"gotest.tools/v3/assert"
2527

2628
"github.com/containerd/nerdctl/mod/tigron/expect"
29+
"github.com/containerd/nerdctl/mod/tigron/require"
2730
"github.com/containerd/nerdctl/mod/tigron/test"
2831
"github.com/containerd/nerdctl/mod/tigron/tig"
2932

@@ -147,82 +150,174 @@ services:
147150
}
148151

149152
func TestComposeCreatePull(t *testing.T) {
153+
testCase := nerdtest.Setup()
150154

151-
base := testutil.NewBase(t)
152-
var dockerComposeYAML = fmt.Sprintf(`
155+
testCase.NoParallel = true
156+
testCase.Require = nerdtest.Private
157+
158+
testCase.Setup = func(data test.Data, helpers test.Helpers) {
159+
composeYAML := fmt.Sprintf(`
153160
services:
154161
svc0:
155162
image: %s
156163
`, testutil.CommonImage)
157164

158-
comp := testutil.NewComposeDir(t, dockerComposeYAML)
159-
defer comp.CleanUp()
160-
projectName := comp.ProjectName()
161-
t.Logf("projectName=%q", projectName)
162-
163-
defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").AssertOK()
164-
165-
// `compose create --pull never` should fail: no such image
166-
base.Cmd("rmi", "-f", testutil.CommonImage).Run()
167-
base.ComposeCmd("-f", comp.YAMLFullPath(), "create", "--pull", "never").AssertFail()
168-
// `compose create --pull missing(default)|always` should succeed: image is pulled and container is created
169-
base.Cmd("rmi", "-f", testutil.CommonImage).Run()
170-
base.ComposeCmd("-f", comp.YAMLFullPath(), "create").AssertOK()
171-
base.Cmd("rmi", "-f", testutil.CommonImage).Run()
172-
base.ComposeCmd("-f", comp.YAMLFullPath(), "create", "--pull", "always").AssertOK()
173-
base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "svc0", "-a").AssertOutContainsAny("Created", "created")
165+
composePath := data.Temp().Save(composeYAML, "compose.yaml")
166+
167+
projectName := filepath.Base(filepath.Dir(composePath))
168+
t.Logf("projectName=%q", projectName)
169+
170+
data.Labels().Set("composeYAML", composePath)
171+
}
172+
173+
testCase.SubTests = []*test.Case{
174+
{
175+
Description: "compose create --pull never fails when image missing",
176+
NoParallel: true,
177+
Setup: func(data test.Data, helpers test.Helpers) {
178+
helpers.Ensure("rmi", "-f", testutil.CommonImage)
179+
},
180+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
181+
return helpers.Command("compose", "-f", data.Labels().Get("composeYAML"), "create", "--pull", "never")
182+
},
183+
Expected: test.Expects(1, nil, nil),
184+
},
185+
{
186+
Description: "compose create --pull missing (default) pulls and creates a container",
187+
NoParallel: true,
188+
Setup: func(data test.Data, helpers test.Helpers) {
189+
helpers.Ensure("rmi", "-f", testutil.CommonImage)
190+
helpers.Ensure("compose", "-f", data.Labels().Get("composeYAML"), "create")
191+
},
192+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
193+
return helpers.Command("compose", "-f", data.Labels().Get("composeYAML"), "ps", "svc0", "-a")
194+
},
195+
Expected: test.Expects(0, nil, expect.Match(regexp.MustCompile(`Created|created`))),
196+
},
197+
{
198+
Description: "compose create --pull always pulls and creates a container",
199+
NoParallel: true,
200+
Setup: func(data test.Data, helpers test.Helpers) {
201+
helpers.Ensure("rmi", "-f", testutil.CommonImage)
202+
helpers.Ensure("compose", "-f", data.Labels().Get("composeYAML"), "create", "--pull", "always")
203+
},
204+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
205+
return helpers.Command("compose", "-f", data.Labels().Get("composeYAML"), "ps", "svc0", "-a")
206+
},
207+
Expected: test.Expects(0, nil, expect.Match(regexp.MustCompile(`Created|created`))),
208+
},
209+
}
210+
211+
testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
212+
if data.Labels().Get("composeYAML") != "" {
213+
helpers.Anyhow("compose", "-f", data.Labels().Get("composeYAML"), "down", "-v")
214+
}
215+
}
216+
217+
testCase.Run(t)
174218
}
175219

176220
func TestComposeCreateBuild(t *testing.T) {
177-
const imageSvc0 = "composebuild_svc0"
221+
testCase := nerdtest.Setup()
178222

179-
dockerComposeYAML := fmt.Sprintf(`
223+
testCase.NoParallel = true
224+
testCase.Require = require.All(
225+
nerdtest.Private,
226+
nerdtest.Build,
227+
)
228+
229+
testCase.Setup = func(data test.Data, helpers test.Helpers) {
230+
imageSvc0 := data.Identifier("composebuild_svc0")
231+
composeYAML := fmt.Sprintf(`
180232
services:
181233
svc0:
182234
build: .
183235
image: %s
184236
`, imageSvc0)
237+
dockerfile := fmt.Sprintf(`FROM %s`, testutil.CommonImage)
185238

186-
dockerfile := fmt.Sprintf(`FROM %s`, testutil.CommonImage)
239+
composePath := data.Temp().Save(composeYAML, "compose.yaml")
240+
data.Temp().Save(dockerfile, "Dockerfile")
187241

188-
testutil.RequiresBuild(t)
189-
testutil.RegisterBuildCacheCleanup(t)
190-
base := testutil.NewBase(t)
242+
projectName := filepath.Base(filepath.Dir(composePath))
243+
t.Logf("projectName=%q", projectName)
191244

192-
comp := testutil.NewComposeDir(t, dockerComposeYAML)
193-
defer comp.CleanUp()
194-
comp.WriteFile("Dockerfile", dockerfile)
195-
projectName := comp.ProjectName()
196-
t.Logf("projectName=%q", projectName)
245+
data.Labels().Set("composeYAML", composePath)
246+
data.Labels().Set("imageName", imageSvc0)
247+
}
197248

198-
defer base.Cmd("rmi", imageSvc0).Run()
199-
defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").AssertOK()
249+
testCase.SubTests = []*test.Case{
250+
{
251+
Description: "compose create --no-build fails when image needs to be built",
252+
NoParallel: true,
253+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
254+
return helpers.Command("compose", "-f", data.Labels().Get("composeYAML"), "create", "--no-build")
255+
},
256+
Expected: test.Expects(1, nil, nil),
257+
},
258+
{
259+
Description: "compose create --build builds image and creates container",
260+
NoParallel: true,
261+
Setup: func(data test.Data, helpers test.Helpers) {
262+
helpers.Ensure("compose", "-f", data.Labels().Get("composeYAML"), "create", "--build")
263+
helpers.Command("compose", "-f", data.Labels().Get("composeYAML"), "images", "svc0").Run(
264+
&test.Expected{
265+
ExitCode: 0,
266+
Output: func(stdout string, t tig.T) {
267+
assert.Assert(t, strings.Contains(stdout, data.Labels().Get("imageName")))
268+
},
269+
},
270+
)
271+
},
272+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
273+
return helpers.Command("compose", "-f", data.Labels().Get("composeYAML"), "ps", "svc0", "-a")
274+
},
275+
Expected: test.Expects(0, nil, expect.Match(regexp.MustCompile(`Created|created`))),
276+
},
277+
}
200278

201-
// `compose create --no-build` should fail if service image needs build
202-
base.ComposeCmd("-f", comp.YAMLFullPath(), "create", "--no-build").AssertFail()
203-
// `compose create --build` should succeed: image is built and container is created
204-
base.ComposeCmd("-f", comp.YAMLFullPath(), "create", "--build").AssertOK()
205-
base.ComposeCmd("-f", comp.YAMLFullPath(), "images", "svc0").AssertOutContains(imageSvc0)
206-
base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "svc0", "-a").AssertOutContainsAny("Created", "created")
279+
testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
280+
if data.Labels().Get("composeYAML") != "" {
281+
helpers.Anyhow("compose", "-f", data.Labels().Get("composeYAML"), "down", "-v")
282+
}
283+
helpers.Anyhow("rmi", "-f", data.Labels().Get("imageName"))
284+
helpers.Anyhow("builder", "prune", "--all", "--force")
285+
}
286+
287+
testCase.Run(t)
207288
}
208289

209290
func TestComposeCreateWritesConfigHashLabel(t *testing.T) {
210-
var dockerComposeYAML = fmt.Sprintf(`
291+
testCase := nerdtest.Setup()
292+
293+
testCase.Setup = func(data test.Data, helpers test.Helpers) {
294+
var composeYAML = fmt.Sprintf(`
211295
services:
212296
svc0:
213297
image: %s
214298
`, testutil.CommonImage)
299+
composePath := data.Temp().Save(composeYAML, "compose.yaml")
300+
301+
projectName := filepath.Base(filepath.Dir(composePath))
302+
t.Logf("projectName=%q", projectName)
215303

216-
base := testutil.NewBase(t)
217-
comp := testutil.NewComposeDir(t, dockerComposeYAML)
218-
defer comp.CleanUp()
219-
projectName := comp.ProjectName()
220-
t.Logf("projectName=%q", projectName)
304+
data.Labels().Set("composeYAML", composePath)
305+
data.Labels().Set("containerName", serviceparser.DefaultContainerName(projectName, "svc0", "1"))
221306

222-
base.ComposeCmd("-f", comp.YAMLFullPath(), "create").AssertOK()
223-
defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").Run()
307+
helpers.Ensure("compose", "-f", composePath, "create")
308+
}
309+
310+
testCase.Command = func(data test.Data, helpers test.Helpers) test.TestableCommand {
311+
return helpers.Command("inspect", "--format", "{{json .Config.Labels}}", data.Labels().Get("containerName"))
312+
}
313+
314+
testCase.Expected = test.Expects(0, nil, expect.Contains("com.docker.compose.config-hash"))
224315

225-
container := serviceparser.DefaultContainerName(projectName, "svc0", "1")
226-
base.Cmd("inspect", "--format", "{{json .Config.Labels}}", container).
227-
AssertOutContains("com.docker.compose.config-hash")
316+
testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
317+
if path := data.Labels().Get("composeYAML"); path != "" {
318+
helpers.Anyhow("compose", "-f", path, "down", "-v")
319+
}
320+
}
321+
322+
testCase.Run(t)
228323
}

pkg/testutil/nerdtest/requirements.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,12 @@ var Build = &test.Requirement{
337337
mess := "buildkitd is enabled"
338338

339339
if isTargetNerdish() {
340-
bkHostAddr, err := buildkitutil.GetBuildkitHost(defaultNamespace)
340+
namespace := defaultNamespace
341+
if ns := helpers.Read(Namespace); ns != "" {
342+
namespace = string(ns)
343+
}
344+
345+
bkHostAddr, err := buildkitutil.GetBuildkitHost(namespace)
341346
if err != nil {
342347
ret = false
343348
mess = fmt.Sprintf("buildkitd is not enabled: %+v", err)

0 commit comments

Comments
 (0)