Skip to content

Commit 2c26839

Browse files
committed
Rewrite some compose tests
Signed-off-by: apostasie <[email protected]>
1 parent 848062e commit 2c26839

11 files changed

+352
-249
lines changed

cmd/nerdctl/compose/compose_build_linux_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ services:
4646
svc0:
4747
build: .
4848
image: %s
49-
ports:
50-
- 8080:80
5149
depends_on:
5250
- svc1
5351
svc1:

cmd/nerdctl/compose/compose_create_linux_test.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ import (
3232

3333
func TestComposeCreate(t *testing.T) {
3434
var dockerComposeYAML = fmt.Sprintf(`
35-
version: '3.1'
36-
3735
services:
3836
svc0:
3937
image: %s
@@ -87,8 +85,6 @@ services:
8785

8886
func TestComposeCreateDependency(t *testing.T) {
8987
var dockerComposeYAML = fmt.Sprintf(`
90-
version: '3.1'
91-
9288
services:
9389
svc0:
9490
image: %s
@@ -152,8 +148,6 @@ func TestComposeCreatePull(t *testing.T) {
152148

153149
base := testutil.NewBase(t)
154150
var dockerComposeYAML = fmt.Sprintf(`
155-
version: '3.1'
156-
157151
services:
158152
svc0:
159153
image: %s

cmd/nerdctl/compose/compose_images_linux_test.go

Lines changed: 71 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,26 @@
1717
package compose
1818

1919
import (
20-
"encoding/json"
2120
"fmt"
22-
"strings"
2321
"testing"
2422

23+
"gotest.tools/v3/assert"
24+
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/referenceutil"
2530
"github.com/containerd/nerdctl/v2/pkg/testutil"
31+
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
2632
)
2733

2834
func TestComposeImages(t *testing.T) {
29-
base := testutil.NewBase(t)
3035
var dockerComposeYAML = fmt.Sprintf(`
31-
version: '3.1'
32-
3336
services:
3437
wordpress:
3538
image: %s
36-
ports:
37-
- 8080:80
39+
container_name: wordpress
3840
environment:
3941
WORDPRESS_DB_HOST: db
4042
WORDPRESS_DB_USER: exampleuser
@@ -44,6 +46,7 @@ services:
4446
- wordpress:/var/www/html
4547
db:
4648
image: %s
49+
container_name: db
4750
environment:
4851
MYSQL_DATABASE: exampledb
4952
MYSQL_USER: exampleuser
@@ -57,95 +60,71 @@ volumes:
5760
db:
5861
`, testutil.WordpressImage, testutil.MariaDBImage)
5962

60-
comp := testutil.NewComposeDir(t, dockerComposeYAML)
61-
defer comp.CleanUp()
62-
projectName := comp.ProjectName()
63-
t.Logf("projectName=%q", projectName)
64-
65-
base.ComposeCmd("-f", comp.YAMLFullPath(), "up", "-d").AssertOK()
66-
defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").Run()
67-
68-
wordpressImageName := strings.Split(testutil.WordpressImage, ":")[0]
69-
dbImageName := strings.Split(testutil.MariaDBImage, ":")[0]
70-
71-
// check one service image
72-
base.ComposeCmd("-f", comp.YAMLFullPath(), "images", "db").AssertOutContains(dbImageName)
73-
base.ComposeCmd("-f", comp.YAMLFullPath(), "images", "db").AssertOutNotContains(wordpressImageName)
74-
75-
// check all service images
76-
base.ComposeCmd("-f", comp.YAMLFullPath(), "images").AssertOutContains(dbImageName)
77-
base.ComposeCmd("-f", comp.YAMLFullPath(), "images").AssertOutContains(wordpressImageName)
78-
}
63+
wordpressImageName, _ := referenceutil.Parse(testutil.WordpressImage)
64+
dbImageName, _ := referenceutil.Parse(testutil.MariaDBImage)
7965

80-
func TestComposeImagesJson(t *testing.T) {
81-
base := testutil.NewBase(t)
82-
var dockerComposeYAML = fmt.Sprintf(`
83-
version: '3.1'
66+
testCase := nerdtest.Setup()
8467

85-
services:
86-
wordpress:
87-
image: %s
88-
container_name: wordpress
89-
ports:
90-
- 8080:80
91-
environment:
92-
WORDPRESS_DB_HOST: db
93-
WORDPRESS_DB_USER: exampleuser
94-
WORDPRESS_DB_PASSWORD: examplepass
95-
WORDPRESS_DB_NAME: exampledb
96-
volumes:
97-
- wordpress:/var/www/html
98-
db:
99-
image: %s
100-
container_name: db
101-
environment:
102-
MYSQL_DATABASE: exampledb
103-
MYSQL_USER: exampleuser
104-
MYSQL_PASSWORD: examplepass
105-
MYSQL_RANDOM_ROOT_PASSWORD: '1'
106-
volumes:
107-
- db:/var/lib/mysql
108-
109-
volumes:
110-
wordpress:
111-
db:
112-
`, testutil.WordpressImage, testutil.MariaDBImage)
68+
testCase.Setup = func(data test.Data, helpers test.Helpers) {
69+
data.Temp().Save(dockerComposeYAML, "compose.yaml")
70+
data.Labels().Set("composeYaml", data.Temp().Path("compose.yaml"))
71+
helpers.Ensure("compose", "-f", data.Temp().Path("compose.yaml"), "up", "-d")
72+
}
11373

114-
comp := testutil.NewComposeDir(t, dockerComposeYAML)
115-
defer comp.CleanUp()
116-
projectName := comp.ProjectName()
117-
t.Logf("projectName=%q", projectName)
118-
119-
base.ComposeCmd("-f", comp.YAMLFullPath(), "up", "-d").AssertOK()
120-
defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").Run()
121-
122-
assertHandler := func(svc string, count int, fields ...string) func(stdout string) error {
123-
return func(stdout string) error {
124-
// 1. check json output can be unmarshalled back to printables.
125-
var printables []composeContainerPrintable
126-
if err := json.Unmarshal([]byte(stdout), &printables); err != nil {
127-
return fmt.Errorf("[service: %s]failed to unmarshal json output from `compose images`: %s", svc, stdout)
128-
}
129-
// 2. check #printables matches expected count.
130-
if len(printables) != count {
131-
return fmt.Errorf("[service: %s]unmarshal generates %d printables, expected %d: %s", svc, len(printables), count, stdout)
132-
}
133-
// 3. check marshalled json string has all expected substrings.
134-
for _, field := range fields {
135-
if !strings.Contains(stdout, field) {
136-
return fmt.Errorf("[service: %s]marshalled json output doesn't have expected string (%s): %s", svc, field, stdout)
137-
}
138-
}
139-
return nil
140-
}
74+
testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
75+
helpers.Anyhow("compose", "-f", data.Temp().Path("compose.yaml"), "down")
14176
}
14277

143-
// check other formats are not supported
144-
base.ComposeCmd("-f", comp.YAMLFullPath(), "images", "--format", "yaml").AssertFail()
145-
// check all services are up (can be marshalled and unmarshalled)
146-
base.ComposeCmd("-f", comp.YAMLFullPath(), "images", "--format", "json").
147-
AssertOutWithFunc(assertHandler("all", 2, `"ContainerName":"wordpress"`, `"ContainerName":"db"`))
78+
testCase.SubTests = []*test.Case{
79+
{
80+
Description: "images db",
81+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
82+
return helpers.Command("compose", "-f", data.Labels().Get("composeYaml"), "images", "db")
83+
},
84+
Expected: test.Expects(expect.ExitCodeSuccess, nil, expect.All(
85+
expect.Contains(dbImageName.Name()),
86+
expect.DoesNotContain(wordpressImageName.Name()),
87+
)),
88+
},
89+
{
90+
Description: "images",
91+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
92+
return helpers.Command("compose", "-f", data.Labels().Get("composeYaml"), "images")
93+
},
94+
Expected: test.Expects(expect.ExitCodeSuccess, nil, expect.Contains(dbImageName.Name(), wordpressImageName.Name())),
95+
},
96+
{
97+
Description: "images --format yaml",
98+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
99+
return helpers.Command("compose", "-f", data.Labels().Get("composeYaml"), "images", "--format", "yaml")
100+
},
101+
Expected: test.Expects(expect.ExitCodeGenericFail, nil, nil),
102+
},
103+
{
104+
Description: "images --format json",
105+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
106+
return helpers.Command("compose", "-f", data.Labels().Get("composeYaml"), "images", "--format", "json")
107+
},
108+
Expected: test.Expects(expect.ExitCodeSuccess, nil, expect.All(
109+
expect.JSON([]composeContainerPrintable{}, func(printables []composeContainerPrintable, s string, t tig.T) {
110+
assert.Equal(t, len(printables), 2)
111+
}),
112+
expect.Contains(`"ContainerName":"wordpress"`, `"ContainerName":"db"`),
113+
)),
114+
},
115+
{
116+
Description: "images --format json wordpress",
117+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
118+
return helpers.Command("compose", "-f", data.Labels().Get("composeYaml"), "images", "--format", "json", "wordpress")
119+
},
120+
Expected: test.Expects(expect.ExitCodeSuccess, nil, expect.All(
121+
expect.JSON([]composeContainerPrintable{}, func(printables []composeContainerPrintable, s string, t tig.T) {
122+
assert.Equal(t, len(printables), 1)
123+
}),
124+
expect.Contains(`"ContainerName":"wordpress"`),
125+
)),
126+
},
127+
}
148128

149-
base.ComposeCmd("-f", comp.YAMLFullPath(), "images", "--format", "json", "wordpress").
150-
AssertOutWithFunc(assertHandler("wordpress", 1, `"ContainerName":"wordpress"`))
129+
testCase.Run(t)
151130
}

cmd/nerdctl/compose/compose_pull_linux_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,10 @@ import (
2626
func TestComposePullWithService(t *testing.T) {
2727
base := testutil.NewBase(t)
2828
var dockerComposeYAML = fmt.Sprintf(`
29-
version: '3.1'
30-
3129
services:
3230
3331
wordpress:
3432
image: %s
35-
ports:
36-
- 8080:80
3733
environment:
3834
WORDPRESS_DB_HOST: db
3935
WORDPRESS_DB_USER: exampleuser

cmd/nerdctl/compose/compose_restart_linux_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,9 @@ import (
2626
func TestComposeRestart(t *testing.T) {
2727
base := testutil.NewBase(t)
2828
var dockerComposeYAML = fmt.Sprintf(`
29-
version: '3.1'
30-
3129
services:
3230
wordpress:
3331
image: %s
34-
ports:
35-
- 8080:80
3632
environment:
3733
WORDPRESS_DB_HOST: db
3834
WORDPRESS_DB_USER: exampleuser

cmd/nerdctl/compose/compose_rm_linux_test.go

Lines changed: 72 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,22 @@ package compose
1818

1919
import (
2020
"fmt"
21+
"regexp"
2122
"testing"
22-
"time"
23+
24+
"github.com/containerd/nerdctl/mod/tigron/expect"
25+
"github.com/containerd/nerdctl/mod/tigron/test"
2326

2427
"github.com/containerd/nerdctl/v2/pkg/testutil"
28+
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
2529
)
2630

2731
func TestComposeRemove(t *testing.T) {
28-
base := testutil.NewBase(t)
2932
var dockerComposeYAML = fmt.Sprintf(`
30-
version: '3.1'
31-
3233
services:
3334
3435
wordpress:
3536
image: %s
36-
ports:
37-
- 8080:80
3837
environment:
3938
WORDPRESS_DB_HOST: db
4039
WORDPRESS_DB_USER: exampleuser
@@ -58,27 +57,71 @@ volumes:
5857
db:
5958
`, testutil.WordpressImage, testutil.MariaDBImage)
6059

61-
comp := testutil.NewComposeDir(t, dockerComposeYAML)
62-
defer comp.CleanUp()
63-
projectName := comp.ProjectName()
64-
t.Logf("projectName=%q", projectName)
65-
66-
base.ComposeCmd("-f", comp.YAMLFullPath(), "up", "-d").AssertOK()
67-
defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").Run()
68-
69-
// no stopped containers
70-
base.ComposeCmd("-f", comp.YAMLFullPath(), "rm", "-f").AssertOK()
71-
time.Sleep(3 * time.Second)
72-
base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "wordpress").AssertOutContainsAny("Up", "running")
73-
base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "db").AssertOutContainsAny("Up", "running")
74-
// remove one stopped service
75-
base.ComposeCmd("-f", comp.YAMLFullPath(), "stop", "wordpress").AssertOK()
76-
base.ComposeCmd("-f", comp.YAMLFullPath(), "rm", "-f", "wordpress").AssertOK()
77-
time.Sleep(3 * time.Second)
78-
base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "wordpress").AssertOutNotContains("wordpress")
79-
base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "db").AssertOutContainsAny("Up", "running")
80-
// remove all services with `--stop`
81-
base.ComposeCmd("-f", comp.YAMLFullPath(), "rm", "-f", "-s").AssertOK()
82-
time.Sleep(3 * time.Second)
83-
base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "db").AssertOutNotContains("db")
60+
testCase := nerdtest.Setup()
61+
62+
testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
63+
helpers.Anyhow("compose", "-f", data.Temp().Path("compose.yaml"), "down")
64+
}
65+
66+
testCase.Setup = func(data test.Data, helpers test.Helpers) {
67+
data.Temp().Save(dockerComposeYAML, "compose.yaml")
68+
helpers.Ensure("compose", "-f", data.Temp().Path("compose.yaml"), "up", "-d")
69+
data.Labels().Set("yamlPath", data.Temp().Path("compose.yaml"))
70+
}
71+
72+
testCase.SubTests = []*test.Case{
73+
{
74+
Description: "All services are still up",
75+
NoParallel: true,
76+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
77+
return helpers.Command("compose", "-f", data.Labels().Get("yamlPath"), "rm", "-f")
78+
},
79+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
80+
return &test.Expected{
81+
Output: func(stdout, info string, t *testing.T) {
82+
wp := helpers.Capture("compose", "-f", data.Labels().Get("yamlPath"), "ps", "wordpress")
83+
db := helpers.Capture("compose", "-f", data.Labels().Get("yamlPath"), "ps", "db")
84+
comp := expect.Match(regexp.MustCompile("Up|running"))
85+
comp(wp, "", t)
86+
comp(db, "", t)
87+
},
88+
}
89+
},
90+
},
91+
{
92+
Description: "Remove stopped service",
93+
NoParallel: true,
94+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
95+
helpers.Ensure("compose", "-f", data.Labels().Get("yamlPath"), "stop", "wordpress")
96+
return helpers.Command("compose", "-f", data.Labels().Get("yamlPath"), "rm", "-f")
97+
},
98+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
99+
return &test.Expected{
100+
Output: func(stdout, info string, t *testing.T) {
101+
wp := helpers.Capture("compose", "-f", data.Labels().Get("yamlPath"), "ps", "wordpress")
102+
db := helpers.Capture("compose", "-f", data.Labels().Get("yamlPath"), "ps", "db")
103+
expect.DoesNotContain("wordpress")(wp, "", t)
104+
expect.Match(regexp.MustCompile("Up|running"))(db, "", t)
105+
},
106+
}
107+
},
108+
},
109+
{
110+
Description: "Remove all services with stop",
111+
NoParallel: true,
112+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
113+
return helpers.Command("compose", "-f", data.Labels().Get("yamlPath"), "rm", "-f", "-s")
114+
},
115+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
116+
return &test.Expected{
117+
Output: func(stdout, info string, t *testing.T) {
118+
db := helpers.Capture("compose", "-f", data.Labels().Get("yamlPath"), "ps", "db")
119+
expect.DoesNotContain("db")(db, "", t)
120+
},
121+
}
122+
},
123+
},
124+
}
125+
126+
testCase.Run(t)
84127
}

0 commit comments

Comments
 (0)