Skip to content

Commit fcaa66c

Browse files
authored
Merge pull request #5116 from subotac/fix/compose-test-cleanup
test: use compose rm for run cleanup
2 parents 497045b + 5432faa commit fcaa66c

1 file changed

Lines changed: 40 additions & 48 deletions

File tree

cmd/nerdctl/compose/compose_run_linux_test.go

Lines changed: 40 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package compose
1919
import (
2020
"fmt"
2121
"io"
22+
"os"
2223
"path/filepath"
2324
"strconv"
2425
"strings"
@@ -38,6 +39,28 @@ import (
3839
"github.com/containerd/nerdctl/v2/pkg/testutil/testregistry"
3940
)
4041

42+
func composeRunCleanup() test.Butler {
43+
return func(data test.Data, helpers test.Helpers) {
44+
composePath := data.Temp().Path("compose.yaml")
45+
// Tigron runs cleanup before setup too. A fresh temp project has no
46+
// manifest or resources yet, so avoid waiting for the global compose lock.
47+
if _, err := os.Stat(composePath); os.IsNotExist(err) {
48+
return
49+
}
50+
// A background compose run holds the global compose lock. Stop its exact
51+
// test container first so the process exits before compose rm acquires it.
52+
helpers.Anyhow("stop", data.Identifier())
53+
helpers.Anyhow("compose", "-f", composePath, "rm", "-f", "-s", "-v")
54+
// Docker Compose excludes one-off containers from `compose rm`, while
55+
// nerdctl Compose selects every container with the project and service labels.
56+
// Remove the explicit `compose run --name` container in compatibility runs.
57+
if nerdtest.IsDocker() {
58+
helpers.Anyhow("rm", "-f", "-v", data.Identifier())
59+
}
60+
helpers.Anyhow("compose", "-f", composePath, "down", "-v")
61+
}
62+
}
63+
4164
func TestComposeRun(t *testing.T) {
4265
const expectedOutput = "speed 38400 baud"
4366

@@ -71,10 +94,7 @@ services:
7194
return cmd
7295
},
7396
Expected: test.Expects(expect.ExitCodeSuccess, nil, expect.Contains(expectedOutput)),
74-
Cleanup: func(data test.Data, helpers test.Helpers) {
75-
helpers.Anyhow("rm", "-f", "-v", data.Identifier())
76-
helpers.Anyhow("compose", "-f", data.Temp().Path("compose.yaml"), "down", "-v")
77-
},
97+
Cleanup: composeRunCleanup(),
7898
},
7999
{
80100
Description: "pty run with --rm",
@@ -104,10 +124,7 @@ services:
104124
Output: expect.Contains(expectedOutput),
105125
}
106126
},
107-
Cleanup: func(data test.Data, helpers test.Helpers) {
108-
helpers.Anyhow("rm", "-f", "-v", data.Identifier())
109-
helpers.Anyhow("compose", "-f", data.Temp().Path("compose.yaml"), "down", "-v")
110-
},
127+
Cleanup: composeRunCleanup(),
111128
},
112129
}
113130

@@ -116,6 +133,9 @@ services:
116133

117134
func TestComposeRunWithServicePorts(t *testing.T) {
118135
testCase := nerdtest.Setup()
136+
// A background compose run holds the global compose lock until cleanup.
137+
testCase.NoParallel = true
138+
cleanup := composeRunCleanup()
119139

120140
testCase.Setup = func(data test.Data, helpers test.Helpers) {
121141
hostPort, err := portlock.Acquire(0)
@@ -139,19 +159,14 @@ services:
139159
data.Labels().Set("composeYAML", composePath)
140160
data.Labels().Set("hostPort", strconv.Itoa(hostPort))
141161

142-
// specify the name of container in order to remove
143-
// TODO: when `compose rm` is implemented, replace it.
144162
cmd := helpers.Command("compose", "-f", composePath, "run", "--service-ports", "--name", data.Identifier(), "web")
145163
cmd.WithPseudoTTY()
146164
cmd.Background()
147165
nerdtest.EnsureContainerStarted(helpers, data.Identifier())
148166
}
149167

150168
testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
151-
helpers.Anyhow("rm", "-f", "-v", data.Identifier())
152-
if composeYAML := data.Labels().Get("composeYAML"); composeYAML != "" {
153-
helpers.Anyhow("compose", "-f", composeYAML, "down", "-v")
154-
}
169+
cleanup(data, helpers)
155170
if portStr := data.Labels().Get("hostPort"); portStr != "" {
156171
if port, err := strconv.Atoi(portStr); err == nil {
157172
_ = portlock.Release(port)
@@ -183,6 +198,9 @@ services:
183198

184199
func TestComposeRunWithPublish(t *testing.T) {
185200
testCase := nerdtest.Setup()
201+
// A background compose run holds the global compose lock until cleanup.
202+
testCase.NoParallel = true
203+
cleanup := composeRunCleanup()
186204

187205
testCase.Setup = func(data test.Data, helpers test.Helpers) {
188206
hostPort, err := portlock.Acquire(0)
@@ -204,19 +222,14 @@ services:
204222
data.Labels().Set("composeYAML", composePath)
205223
data.Labels().Set("hostPort", strconv.Itoa(hostPort))
206224

207-
// specify the name of container in order to remove
208-
// TODO: when `compose rm` is implemented, replace it.
209225
cmd := helpers.Command("compose", "-f", composePath, "run", "--publish", fmt.Sprintf("%d:80", hostPort), "--name", data.Identifier(), "web")
210226
cmd.WithPseudoTTY()
211227
cmd.Background()
212228
nerdtest.EnsureContainerStarted(helpers, data.Identifier())
213229
}
214230

215231
testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
216-
helpers.Anyhow("rm", "-f", "-v", data.Identifier())
217-
if composeYAML := data.Labels().Get("composeYAML"); composeYAML != "" {
218-
helpers.Anyhow("compose", "-f", composeYAML, "down", "-v")
219-
}
232+
cleanup(data, helpers)
220233
if portStr := data.Labels().Get("hostPort"); portStr != "" {
221234
if port, err := strconv.Atoi(portStr); err == nil {
222235
_ = portlock.Release(port)
@@ -285,10 +298,7 @@ services:
285298

286299
testCase.Expected = test.Expects(expect.ExitCodeSuccess, nil, expect.Contains(partialOutput))
287300

288-
testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
289-
helpers.Anyhow("rm", "-f", "-v", data.Identifier())
290-
helpers.Anyhow("compose", "-f", data.Temp().Path("compose.yaml"), "down", "-v")
291-
}
301+
testCase.Cleanup = composeRunCleanup()
292302

293303
testCase.Run(t)
294304
}
@@ -331,10 +341,7 @@ services:
331341

332342
testCase.Expected = test.Expects(expect.ExitCodeSuccess, nil, expect.Contains(partialOutput))
333343

334-
testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
335-
helpers.Anyhow("rm", "-f", "-v", data.Identifier())
336-
helpers.Anyhow("compose", "-f", data.Temp().Path("compose.yaml"), "down", "-v")
337-
}
344+
testCase.Cleanup = composeRunCleanup()
338345

339346
testCase.Run(t)
340347
}
@@ -376,10 +383,7 @@ services:
376383

377384
testCase.Expected = test.Expects(expect.ExitCodeSuccess, nil, expect.Contains(expectedOutput))
378385

379-
testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
380-
helpers.Anyhow("rm", "-f", "-v", data.Identifier())
381-
helpers.Anyhow("compose", "-f", data.Temp().Path("compose.yaml"), "down", "-v")
382-
}
386+
testCase.Cleanup = composeRunCleanup()
383387

384388
testCase.Run(t)
385389
}
@@ -434,10 +438,7 @@ services:
434438
}
435439
}
436440

437-
testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
438-
helpers.Anyhow("rm", "-f", "-v", data.Identifier())
439-
helpers.Anyhow("compose", "-f", data.Temp().Path("compose.yaml"), "down", "-v")
440-
}
441+
testCase.Cleanup = composeRunCleanup()
441442

442443
testCase.Run(t)
443444
}
@@ -478,10 +479,7 @@ services:
478479

479480
testCase.Expected = test.Expects(expect.ExitCodeSuccess, nil, expect.Contains(partialOutput))
480481

481-
testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
482-
helpers.Anyhow("rm", "-f", "-v", data.Identifier())
483-
helpers.Anyhow("compose", "-f", data.Temp().Path("compose.yaml"), "down", "-v")
484-
}
482+
testCase.Cleanup = composeRunCleanup()
485483

486484
testCase.Run(t)
487485
}
@@ -524,10 +522,7 @@ services:
524522

525523
testCase.Expected = test.Expects(expect.ExitCodeSuccess, nil, expect.Contains(partialOutput))
526524

527-
testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
528-
helpers.Anyhow("rm", "-f", "-v", data.Identifier())
529-
helpers.Anyhow("compose", "-f", data.Temp().Path("compose.yaml"), "down", "-v")
530-
}
525+
testCase.Cleanup = composeRunCleanup()
531526

532527
testCase.Run(t)
533528
}
@@ -582,10 +577,7 @@ services:
582577
}
583578
}
584579

585-
testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
586-
helpers.Anyhow("rm", "-f", "-v", data.Identifier())
587-
helpers.Anyhow("compose", "-f", data.Temp().Path("compose.yaml"), "down", "-v")
588-
}
580+
testCase.Cleanup = composeRunCleanup()
589581

590582
testCase.Run(t)
591583
}

0 commit comments

Comments
 (0)