Skip to content

Commit 8bfa44f

Browse files
authored
Merge pull request #4688 from haytok/issues_4613_compose_pause_linux_test.go
test: refactor compose_pause_linux_test.go to use Tigron
2 parents 0ea1dcf + f219a22 commit 8bfa44f

1 file changed

Lines changed: 55 additions & 19 deletions

File tree

cmd/nerdctl/compose/compose_pause_linux_test.go

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,26 @@ package compose
1818

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

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/composer/serviceparser"
2330
"github.com/containerd/nerdctl/v2/pkg/testutil"
31+
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
2432
)
2533

2634
func TestComposePauseAndUnpause(t *testing.T) {
27-
base := testutil.NewBase(t)
28-
switch base.Info().CgroupDriver {
29-
case "none", "":
30-
t.Skip("requires cgroup (for pausing)")
31-
}
35+
testCase := nerdtest.Setup()
3236

33-
var dockerComposeYAML = fmt.Sprintf(`
37+
testCase.Require = nerdtest.CGroup
38+
39+
testCase.Setup = func(data test.Data, helpers test.Helpers) {
40+
dockerComposeYAML := fmt.Sprintf(`
3441
services:
3542
svc0:
3643
image: %s
@@ -40,20 +47,49 @@ services:
4047
command: "sleep infinity"
4148
`, testutil.CommonImage, testutil.CommonImage)
4249

43-
comp := testutil.NewComposeDir(t, dockerComposeYAML)
44-
defer comp.CleanUp()
45-
projectName := comp.ProjectName()
46-
t.Logf("projectName=%q", projectName)
50+
composePath := data.Temp().Save(dockerComposeYAML, "compose.yaml")
51+
52+
projectName := filepath.Base(filepath.Dir(composePath))
53+
t.Logf("projectName=%q", projectName)
54+
55+
svc0Container := serviceparser.DefaultContainerName(projectName, "svc0", "1")
56+
svc1Container := serviceparser.DefaultContainerName(projectName, "svc1", "1")
57+
58+
data.Labels().Set("composeYAML", composePath)
59+
60+
helpers.Ensure("compose", "-f", composePath, "up", "-d")
61+
nerdtest.EnsureContainerStarted(helpers, svc0Container)
62+
nerdtest.EnsureContainerStarted(helpers, svc1Container)
63+
}
64+
65+
testCase.Command = func(data test.Data, helpers test.Helpers) test.TestableCommand {
66+
// pause a service should (only) pause its own container
67+
return helpers.Command("compose", "-f", data.Labels().Get("composeYAML"), "pause", "svc0")
68+
}
69+
70+
testCase.Expected = func(data test.Data, helpers test.Helpers) *test.Expected {
71+
return &test.Expected{
72+
ExitCode: expect.ExitCodeSuccess,
73+
Output: func(stdout string, t tig.T) {
74+
svc0Paused := helpers.Capture("compose", "-f", data.Labels().Get("composeYAML"), "ps", "svc0", "-a")
75+
expect.Match(regexp.MustCompile("Paused|paused"))(svc0Paused, t)
4776

48-
base.ComposeCmd("-f", comp.YAMLFullPath(), "up", "-d").AssertOK()
49-
defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").AssertOK()
77+
svc1Running := helpers.Capture("compose", "-f", data.Labels().Get("composeYAML"), "ps", "svc1")
78+
expect.Match(regexp.MustCompile("Up|running"))(svc1Running, t)
5079

51-
// pause a service should (only) pause its own container
52-
base.ComposeCmd("-f", comp.YAMLFullPath(), "pause", "svc0").AssertOK()
53-
base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "svc0", "-a").AssertOutContainsAny("Paused", "paused")
54-
base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "svc1").AssertOutContainsAny("Up", "running")
80+
// unpause should be able to recover the paused service container
81+
helpers.Ensure("compose", "-f", data.Labels().Get("composeYAML"), "unpause", "svc0")
82+
svc0Running := helpers.Capture("compose", "-f", data.Labels().Get("composeYAML"), "ps", "svc0")
83+
expect.Match(regexp.MustCompile("Up|running"))(svc0Running, t)
84+
},
85+
}
86+
}
87+
88+
testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
89+
if data.Labels().Get("composeYAML") != "" {
90+
helpers.Anyhow("compose", "-f", data.Labels().Get("composeYAML"), "down", "-v")
91+
}
92+
}
5593

56-
// unpause should be able to recover the paused service container
57-
base.ComposeCmd("-f", comp.YAMLFullPath(), "unpause", "svc0").AssertOK()
58-
base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "svc0").AssertOutContainsAny("Up", "running")
94+
testCase.Run(t)
5995
}

0 commit comments

Comments
 (0)