Skip to content

Commit 2beb456

Browse files
authored
Merge pull request #4713 from robertcal/issues_463_compose_port_linux_test.go
test: refactor compose_port_linux_test.go to use Tigron
2 parents efe8c5f + 248a714 commit 2beb456

1 file changed

Lines changed: 129 additions & 37 deletions

File tree

cmd/nerdctl/compose/compose_port_linux_test.go

Lines changed: 129 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,72 +18,166 @@ package compose
1818

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

2325
"github.com/containerd/nerdctl/mod/tigron/expect"
2426
"github.com/containerd/nerdctl/mod/tigron/test"
2527

2628
"github.com/containerd/nerdctl/v2/pkg/testutil"
2729
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
30+
"github.com/containerd/nerdctl/v2/pkg/testutil/portlock"
2831
)
2932

3033
func TestComposePort(t *testing.T) {
31-
base := testutil.NewBase(t)
34+
const portCount = 2
3235

33-
var dockerComposeYAML = fmt.Sprintf(`
36+
testCase := nerdtest.Setup()
37+
38+
testCase.Setup = func(data test.Data, helpers test.Helpers) {
39+
for i := 0; i < portCount; i++ {
40+
port, err := portlock.Acquire(0)
41+
if err != nil {
42+
helpers.T().Log(fmt.Sprintf("Failed to acquire port: %v", err))
43+
helpers.T().FailNow()
44+
}
45+
data.Labels().Set(fmt.Sprintf("hostPort%d", i), strconv.Itoa(port))
46+
}
47+
48+
dockerComposeYAML := fmt.Sprintf(`
3449
services:
3550
svc0:
3651
image: %s
3752
command: "sleep infinity"
3853
ports:
39-
- "12345:10000"
40-
- "12346:10001/udp"
41-
`, testutil.CommonImage)
54+
- "%s:10000"
55+
- "%s:10001/udp"
56+
`, testutil.CommonImage, data.Labels().Get("hostPort0"), data.Labels().Get("hostPort1"))
57+
58+
compYamlPath := data.Temp().Save(dockerComposeYAML, "compose.yaml")
59+
data.Labels().Set("composeYaml", compYamlPath)
60+
projectName := filepath.Base(filepath.Dir(compYamlPath))
61+
t.Logf("projectName=%q", projectName)
4262

43-
comp := testutil.NewComposeDir(t, dockerComposeYAML)
44-
defer comp.CleanUp()
45-
projectName := comp.ProjectName()
46-
t.Logf("projectName=%q", projectName)
63+
helpers.Ensure("compose", "-f", compYamlPath, "up", "-d")
64+
}
4765

48-
base.ComposeCmd("-f", comp.YAMLFullPath(), "up", "-d").AssertOK()
49-
defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").AssertOK()
66+
testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
67+
helpers.Anyhow("compose", "-f", data.Temp().Path("compose.yaml"), "down", "-v")
68+
for i := 0; i < portCount; i++ {
69+
port, _ := strconv.Atoi(data.Labels().Get(fmt.Sprintf("hostPort%d", i)))
70+
_ = portlock.Release(port)
71+
}
72+
}
5073

51-
// `port` should work for given port and protocol
52-
base.ComposeCmd("-f", comp.YAMLFullPath(), "port", "svc0", "10000").AssertOutExactly("0.0.0.0:12345\n")
53-
base.ComposeCmd("-f", comp.YAMLFullPath(), "port", "--protocol", "udp", "svc0", "10001").AssertOutExactly("0.0.0.0:12346\n")
74+
testCase.SubTests = []*test.Case{
75+
{
76+
Description: "port should return host port for TCP",
77+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
78+
return helpers.Command("compose", "-f", data.Labels().Get("composeYaml"), "port", "svc0", "10000")
79+
},
80+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
81+
return &test.Expected{
82+
ExitCode: expect.ExitCodeSuccess,
83+
Output: expect.Equals(fmt.Sprintf("0.0.0.0:%s\n", data.Labels().Get("hostPort0"))),
84+
}
85+
},
86+
},
87+
{
88+
Description: "port should return host port for UDP",
89+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
90+
return helpers.Command("compose", "-f", data.Labels().Get("composeYaml"), "port", "--protocol", "udp", "svc0", "10001")
91+
},
92+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
93+
return &test.Expected{
94+
ExitCode: expect.ExitCodeSuccess,
95+
Output: expect.Equals(fmt.Sprintf("0.0.0.0:%s\n", data.Labels().Get("hostPort1"))),
96+
}
97+
},
98+
},
99+
}
100+
101+
testCase.Run(t)
54102
}
55103

56104
func TestComposePortFailure(t *testing.T) {
57-
base := testutil.NewBase(t)
105+
const portCount = 2
106+
107+
testCase := nerdtest.Setup()
58108

59-
var dockerComposeYAML = fmt.Sprintf(`
109+
testCase.Setup = func(data test.Data, helpers test.Helpers) {
110+
for i := 0; i < portCount; i++ {
111+
port, err := portlock.Acquire(0)
112+
if err != nil {
113+
helpers.T().Log(fmt.Sprintf("Failed to acquire port: %v", err))
114+
helpers.T().FailNow()
115+
}
116+
data.Labels().Set(fmt.Sprintf("hostPort%d", i), strconv.Itoa(port))
117+
}
118+
119+
dockerComposeYAML := fmt.Sprintf(`
60120
services:
61121
svc0:
62122
image: %s
63123
command: "sleep infinity"
64124
ports:
65-
- "12345:10000"
66-
- "12346:10001/udp"
67-
`, testutil.CommonImage)
68-
69-
comp := testutil.NewComposeDir(t, dockerComposeYAML)
70-
defer comp.CleanUp()
71-
projectName := comp.ProjectName()
72-
t.Logf("projectName=%q", projectName)
73-
74-
base.ComposeCmd("-f", comp.YAMLFullPath(), "up", "-d").AssertOK()
75-
defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").AssertOK()
76-
77-
// `port` should fail if given port and protocol don't exist
78-
base.ComposeCmd("-f", comp.YAMLFullPath(), "port", "svc0", "9999").AssertFail()
79-
base.ComposeCmd("-f", comp.YAMLFullPath(), "port", "--protocol", "udp", "svc0", "10000").AssertFail()
80-
base.ComposeCmd("-f", comp.YAMLFullPath(), "port", "--protocol", "tcp", "svc0", "10001").AssertFail()
125+
- "%s:10000"
126+
- "%s:10001/udp"
127+
`, testutil.CommonImage, data.Labels().Get("hostPort0"), data.Labels().Get("hostPort1"))
128+
129+
compYamlPath := data.Temp().Save(dockerComposeYAML, "compose.yaml")
130+
data.Labels().Set("composeYaml", compYamlPath)
131+
projectName := filepath.Base(filepath.Dir(compYamlPath))
132+
t.Logf("projectName=%q", projectName)
133+
134+
helpers.Ensure("compose", "-f", compYamlPath, "up", "-d")
135+
}
136+
137+
testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
138+
helpers.Anyhow("compose", "-f", data.Temp().Path("compose.yaml"), "down", "-v")
139+
for i := 0; i < portCount; i++ {
140+
port, _ := strconv.Atoi(data.Labels().Get(fmt.Sprintf("hostPort%d", i)))
141+
_ = portlock.Release(port)
142+
}
143+
}
144+
145+
testCase.SubTests = []*test.Case{
146+
{
147+
Description: "port should fail for non-existent port",
148+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
149+
return helpers.Command("compose", "-f", data.Labels().Get("composeYaml"), "port", "svc0", "9999")
150+
},
151+
Expected: test.Expects(expect.ExitCodeGenericFail, nil, nil),
152+
},
153+
{
154+
Description: "port should fail for wrong protocol (UDP on TCP port)",
155+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
156+
return helpers.Command("compose", "-f", data.Labels().Get("composeYaml"), "port", "--protocol", "udp", "svc0", "10000")
157+
},
158+
Expected: test.Expects(expect.ExitCodeGenericFail, nil, nil),
159+
},
160+
{
161+
Description: "port should fail for wrong protocol (TCP on UDP port)",
162+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
163+
return helpers.Command("compose", "-f", data.Labels().Get("composeYaml"), "port", "--protocol", "tcp", "svc0", "10001")
164+
},
165+
Expected: test.Expects(expect.ExitCodeGenericFail, nil, nil),
166+
},
167+
}
168+
169+
testCase.Run(t)
81170
}
82171

83172
// TestComposeMultiplePorts tests whether it is possible to allocate a large
84173
// number of ports. (https://github.com/containerd/nerdctl/issues/4027)
85174
func TestComposeMultiplePorts(t *testing.T) {
86-
var dockerComposeYAML = fmt.Sprintf(`
175+
testCase := nerdtest.Setup()
176+
177+
testCase.NoParallel = true
178+
179+
testCase.Setup = func(data test.Data, helpers test.Helpers) {
180+
dockerComposeYAML := fmt.Sprintf(`
87181
services:
88182
svc0:
89183
image: %s
@@ -92,11 +186,10 @@ services:
92186
- '32000-32060:32000-32060'
93187
`, testutil.AlpineImage)
94188

95-
testCase := nerdtest.Setup()
96-
97-
testCase.Setup = func(data test.Data, helpers test.Helpers) {
98189
compYamlPath := data.Temp().Save(dockerComposeYAML, "compose.yaml")
99190
data.Labels().Set("composeYaml", compYamlPath)
191+
projectName := filepath.Base(filepath.Dir(compYamlPath))
192+
t.Logf("projectName=%q", projectName)
100193

101194
helpers.Ensure("compose", "-f", compYamlPath, "up", "-d")
102195
}
@@ -108,7 +201,6 @@ services:
108201
testCase.SubTests = []*test.Case{
109202
{
110203
Description: "Issue #4027 - Allocate a large number of ports.",
111-
NoParallel: true,
112204
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
113205
return helpers.Command("compose", "-f", data.Labels().Get("composeYaml"), "port", "svc0", "32000")
114206
},

0 commit comments

Comments
 (0)