Skip to content

Commit e3d2710

Browse files
committed
test: refactor TestRunWithInvalidPortThenCleanUp
This commit refactors TestRunWithInvalidPortThenCleanUp in cmd/nerdctl/container/container_run_network_linux_test.go based on the principles in the following document. - https://github.com/containerd/nerdctl/tree/main/docs/testing#principles Signed-off-by: Hayato Kiwata <[email protected]>
1 parent 1259a55 commit e3d2710

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

cmd/nerdctl/container/container_run_network_linux_test.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
"github.com/containerd/nerdctl/v2/pkg/testutil"
4242
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
4343
"github.com/containerd/nerdctl/v2/pkg/testutil/nettestutil"
44+
"github.com/containerd/nerdctl/v2/pkg/testutil/test"
4445
)
4546

4647
func extractHostPort(portMapping string, port string) (string, error) {
@@ -350,15 +351,29 @@ func TestRunPort(t *testing.T) {
350351
}
351352

352353
func TestRunWithInvalidPortThenCleanUp(t *testing.T) {
354+
testCase := nerdtest.Setup()
353355
// docker does not set label restriction to 4096 bytes
354-
testutil.DockerIncompatible(t)
355-
t.Parallel()
356-
base := testutil.NewBase(t)
357-
containerName := testutil.Identifier(t)
358-
defer base.Cmd("rm", "-f", containerName).Run()
359-
base.Cmd("run", "--rm", "--name", containerName, "-p", "22200-22299:22200-22299", testutil.CommonImage).AssertFail()
360-
base.Cmd("run", "--rm", "--name", containerName, "-p", "22200-22299:22200-22299", testutil.CommonImage).AssertCombinedOutContains(errdefs.ErrInvalidArgument.Error())
361-
base.Cmd("run", "--rm", "--name", containerName, testutil.CommonImage).AssertOK()
356+
testCase.Require = test.Not(nerdtest.Docker)
357+
358+
testCase.SubTests = []*test.Case{
359+
{
360+
Description: "Run a container with invalid ports, and then clean up.",
361+
Cleanup: func(data test.Data, helpers test.Helpers) {
362+
helpers.Anyhow("rm", "--data-root", data.TempDir(), "-f", data.Identifier())
363+
},
364+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
365+
return helpers.Command("run", "--data-root", data.TempDir(), "--rm", "--name", data.Identifier(), "-p", "22200-22299:22200-22299", testutil.CommonImage)
366+
},
367+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
368+
return &test.Expected{
369+
ExitCode: 1,
370+
Errors: []error{errdefs.ErrInvalidArgument},
371+
}
372+
},
373+
},
374+
}
375+
376+
testCase.Run(t)
362377
}
363378

364379
func TestRunContainerWithStaticIP(t *testing.T) {

0 commit comments

Comments
 (0)