Skip to content
Open
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
97 changes: 74 additions & 23 deletions cmd/nerdctl/compose/compose_port_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ import (
)

func TestComposePort(t *testing.T) {
base := testutil.NewBase(t)

var dockerComposeYAML = fmt.Sprintf(`
services:
svc0:
Expand All @@ -40,22 +38,44 @@ services:
- "12346:10001/udp"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please re-write to get host port using portlock package ?

https://github.com/containerd/nerdctl/blob/main/pkg/testutil/portlock/portlock.go

Please correct the affected areas.

Additionally, please verify whether other sections can also be rewritten.

`, testutil.CommonImage)

comp := testutil.NewComposeDir(t, dockerComposeYAML)
defer comp.CleanUp()
projectName := comp.ProjectName()
t.Logf("projectName=%q", projectName)
Comment on lines -45 to -46
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you write these in testCase.Setup ?
The same applies to other test functions.

testCase := nerdtest.Setup()

testCase.NoParallel = true

testCase.Setup = func(data test.Data, helpers test.Helpers) {
compYamlPath := data.Temp().Save(dockerComposeYAML, "compose.yaml")
data.Labels().Set("composeYaml", compYamlPath)

base.ComposeCmd("-f", comp.YAMLFullPath(), "up", "-d").AssertOK()
defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").AssertOK()
helpers.Ensure("compose", "-f", compYamlPath, "up", "-d")
}

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

testCase.SubTests = []*test.Case{
{
Description: "port should return host port for TCP",
NoParallel: true,
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
return helpers.Command("compose", "-f", data.Labels().Get("composeYaml"), "port", "svc0", "10000")
},
Expected: test.Expects(expect.ExitCodeSuccess, nil, expect.Equals("0.0.0.0:12345\n")),
},
{
Description: "port should return host port for UDP",
NoParallel: true,
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
return helpers.Command("compose", "-f", data.Labels().Get("composeYaml"), "port", "--protocol", "udp", "svc0", "10001")
},
Expected: test.Expects(expect.ExitCodeSuccess, nil, expect.Equals("0.0.0.0:12346\n")),
},
}

// `port` should work for given port and protocol
base.ComposeCmd("-f", comp.YAMLFullPath(), "port", "svc0", "10000").AssertOutExactly("0.0.0.0:12345\n")
base.ComposeCmd("-f", comp.YAMLFullPath(), "port", "--protocol", "udp", "svc0", "10001").AssertOutExactly("0.0.0.0:12346\n")
testCase.Run(t)
}

func TestComposePortFailure(t *testing.T) {
base := testutil.NewBase(t)

var dockerComposeYAML = fmt.Sprintf(`
services:
svc0:
Expand All @@ -66,18 +86,49 @@ services:
- "12346:10001/udp"
`, testutil.CommonImage)

comp := testutil.NewComposeDir(t, dockerComposeYAML)
defer comp.CleanUp()
projectName := comp.ProjectName()
t.Logf("projectName=%q", projectName)
testCase := nerdtest.Setup()

base.ComposeCmd("-f", comp.YAMLFullPath(), "up", "-d").AssertOK()
defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").AssertOK()
testCase.NoParallel = true

// `port` should fail if given port and protocol don't exist
base.ComposeCmd("-f", comp.YAMLFullPath(), "port", "svc0", "9999").AssertFail()
base.ComposeCmd("-f", comp.YAMLFullPath(), "port", "--protocol", "udp", "svc0", "10000").AssertFail()
base.ComposeCmd("-f", comp.YAMLFullPath(), "port", "--protocol", "tcp", "svc0", "10001").AssertFail()
testCase.Setup = func(data test.Data, helpers test.Helpers) {
compYamlPath := data.Temp().Save(dockerComposeYAML, "compose.yaml")
data.Labels().Set("composeYaml", compYamlPath)

helpers.Ensure("compose", "-f", compYamlPath, "up", "-d")
}

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

testCase.SubTests = []*test.Case{
{
Description: "port should fail for non-existent port",
NoParallel: true,
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
return helpers.Command("compose", "-f", data.Labels().Get("composeYaml"), "port", "svc0", "9999")
},
Expected: test.Expects(expect.ExitCodeGenericFail, nil, nil),
},
{
Description: "port should fail for wrong protocol (UDP on TCP port)",
NoParallel: true,
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
return helpers.Command("compose", "-f", data.Labels().Get("composeYaml"), "port", "--protocol", "udp", "svc0", "10000")
},
Expected: test.Expects(expect.ExitCodeGenericFail, nil, nil),
},
{
Description: "port should fail for wrong protocol (TCP on UDP port)",
NoParallel: true,
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
return helpers.Command("compose", "-f", data.Labels().Get("composeYaml"), "port", "--protocol", "tcp", "svc0", "10001")
},
Expected: test.Expects(expect.ExitCodeGenericFail, nil, nil),
},
}

testCase.Run(t)
}

// TestComposeMultiplePorts tests whether it is possible to allocate a large
Expand Down
Loading