Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 40 additions & 48 deletions cmd/nerdctl/compose/compose_run_linux_test.go
Comment thread
subotac marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package compose
import (
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"strings"
Expand All @@ -38,6 +39,28 @@ import (
"github.com/containerd/nerdctl/v2/pkg/testutil/testregistry"
)

func composeRunCleanup() test.Butler {
return func(data test.Data, helpers test.Helpers) {
composePath := data.Temp().Path("compose.yaml")
// Tigron runs cleanup before setup too. A fresh temp project has no
// manifest or resources yet, so avoid waiting for the global compose lock.
if _, err := os.Stat(composePath); os.IsNotExist(err) {
return
}
// A background compose run holds the global compose lock. Stop its exact
// test container first so the process exits before compose rm acquires it.
helpers.Anyhow("stop", data.Identifier())
helpers.Anyhow("compose", "-f", composePath, "rm", "-f", "-s", "-v")
// Docker Compose excludes one-off containers from `compose rm`, while
// nerdctl Compose selects every container with the project and service labels.
// Remove the explicit `compose run --name` container in compatibility runs.
if nerdtest.IsDocker() {
helpers.Anyhow("rm", "-f", "-v", data.Identifier())
}
helpers.Anyhow("compose", "-f", composePath, "down", "-v")
}
}

func TestComposeRun(t *testing.T) {
const expectedOutput = "speed 38400 baud"

Expand Down Expand Up @@ -71,10 +94,7 @@ services:
return cmd
},
Expected: test.Expects(expect.ExitCodeSuccess, nil, expect.Contains(expectedOutput)),
Cleanup: func(data test.Data, helpers test.Helpers) {
helpers.Anyhow("rm", "-f", "-v", data.Identifier())
helpers.Anyhow("compose", "-f", data.Temp().Path("compose.yaml"), "down", "-v")
},
Cleanup: composeRunCleanup(),
},
{
Description: "pty run with --rm",
Expand Down Expand Up @@ -104,10 +124,7 @@ services:
Output: expect.Contains(expectedOutput),
}
},
Cleanup: func(data test.Data, helpers test.Helpers) {
helpers.Anyhow("rm", "-f", "-v", data.Identifier())
helpers.Anyhow("compose", "-f", data.Temp().Path("compose.yaml"), "down", "-v")
},
Cleanup: composeRunCleanup(),
},
}

Expand All @@ -116,6 +133,9 @@ services:

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

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

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

testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
helpers.Anyhow("rm", "-f", "-v", data.Identifier())
if composeYAML := data.Labels().Get("composeYAML"); composeYAML != "" {
helpers.Anyhow("compose", "-f", composeYAML, "down", "-v")
}
cleanup(data, helpers)
if portStr := data.Labels().Get("hostPort"); portStr != "" {
if port, err := strconv.Atoi(portStr); err == nil {
_ = portlock.Release(port)
Expand Down Expand Up @@ -183,6 +198,9 @@ services:

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

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

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

testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
helpers.Anyhow("rm", "-f", "-v", data.Identifier())
if composeYAML := data.Labels().Get("composeYAML"); composeYAML != "" {
helpers.Anyhow("compose", "-f", composeYAML, "down", "-v")
}
cleanup(data, helpers)
if portStr := data.Labels().Get("hostPort"); portStr != "" {
if port, err := strconv.Atoi(portStr); err == nil {
_ = portlock.Release(port)
Expand Down Expand Up @@ -285,10 +298,7 @@ services:

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

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

testCase.Run(t)
}
Expand Down Expand Up @@ -331,10 +341,7 @@ services:

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

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

testCase.Run(t)
}
Expand Down Expand Up @@ -376,10 +383,7 @@ services:

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

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

testCase.Run(t)
}
Expand Down Expand Up @@ -434,10 +438,7 @@ services:
}
}

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

testCase.Run(t)
}
Expand Down Expand Up @@ -478,10 +479,7 @@ services:

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

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

testCase.Run(t)
}
Expand Down Expand Up @@ -524,10 +522,7 @@ services:

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

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

testCase.Run(t)
}
Expand Down Expand Up @@ -582,10 +577,7 @@ services:
}
}

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

testCase.Run(t)
}
Expand Down
Loading