Skip to content

Commit

Permalink
Volume QA: inspect, prune, create (fixes and tests)
Browse files Browse the repository at this point in the history
Signed-off-by: apostasie <spam_blackhole@farcloser.world>
  • Loading branch information
apostasie committed Jun 21, 2024
1 parent 873a087 commit a6cd38f
Show file tree
Hide file tree
Showing 15 changed files with 690 additions and 140 deletions.
3 changes: 2 additions & 1 deletion cmd/nerdctl/system_prune_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ import (

func TestSystemPrune(t *testing.T) {
testutil.RequiresBuild(t)
base := testutil.NewBase(t)
namespaceID := testutil.Identifier(t)
base := testutil.NewBaseWithNamespace(t, namespaceID)
base.Cmd("container", "prune", "-f").AssertOK()
base.Cmd("network", "prune", "-f").AssertOK()
base.Cmd("volume", "prune", "-f").AssertOK()
Expand Down
9 changes: 9 additions & 0 deletions cmd/nerdctl/volume_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
package main

import (
"fmt"

"github.com/containerd/containerd/errdefs"
"github.com/containerd/nerdctl/v2/pkg/api/types"
"github.com/containerd/nerdctl/v2/pkg/cmd/volume"

Expand Down Expand Up @@ -45,6 +48,12 @@ func processVolumeCreateOptions(cmd *cobra.Command) (types.VolumeCreateOptions,
if err != nil {
return types.VolumeCreateOptions{}, err
}
for _, label := range labels {
if label == "" {
return types.VolumeCreateOptions{}, fmt.Errorf("labels cannot be empty (%w)", errdefs.ErrInvalidArgument)
}
}

return types.VolumeCreateOptions{
GOptions: globalOptions,
Labels: labels,
Expand Down
142 changes: 117 additions & 25 deletions cmd/nerdctl/volume_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,132 @@ package main
import (
"testing"

"github.com/containerd/containerd/errdefs"
"github.com/containerd/nerdctl/v2/pkg/testutil"
"gotest.tools/v3/assert"
"gotest.tools/v3/icmd"
)

// Test TestVolumeCreate for creating volume with given name.
func TestVolumeCreate(t *testing.T) {
base := testutil.NewBase(t)
testVolume := testutil.Identifier(t)
t.Parallel()

base.Cmd("volume", "create", testVolume).AssertOK()
defer base.Cmd("volume", "rm", "-f", testVolume).Run()
base := testutil.NewBase(t)

base.Cmd("volume", "list").AssertOutContains(testVolume)
}
malformed := errdefs.ErrInvalidArgument.Error()
exitCodeVariant := 1
if base.Target == testutil.Docker {
malformed = "invalid"
exitCodeVariant = 125
}

// Test TestVolumeCreateTooManyArgs for creating volume with too many args.
func TestVolumeCreateTooManyArgs(t *testing.T) {
base := testutil.NewBase(t)
testCases := []struct {
description string
command func(tID string) *testutil.Cmd
tearUp func(tID string)
tearDown func(tID string)
expected func(tID string) icmd.Expected
}{
{
description: "arg missing",
command: func(tID string) *testutil.Cmd {
return base.Cmd("volume", "create")
},
expected: func(tID string) icmd.Expected {
return icmd.Expected{
ExitCode: 0,
}
},
},
{
description: "invalid identifier",
command: func(tID string) *testutil.Cmd {
return base.Cmd("volume", "create", "∞")
},
expected: func(tID string) icmd.Expected {
return icmd.Expected{
ExitCode: 1,
Err: malformed,
}
},
},
{
description: "too many args",
command: func(tID string) *testutil.Cmd {
return base.Cmd("volume", "create", "too", "many")
},
expected: func(tID string) icmd.Expected {
return icmd.Expected{
ExitCode: 1,
Err: "at most 1 arg",
}
},
},
{
description: "success",
command: func(tID string) *testutil.Cmd {
return base.Cmd("volume", "create", tID)
},
tearDown: func(tID string) {
base.Cmd("volume", "rm", "-f", tID).Run()
},
expected: func(tID string) icmd.Expected {
return icmd.Expected{
ExitCode: 0,
Out: tID,
}
},
},
{
description: "success with labels",
command: func(tID string) *testutil.Cmd {
return base.Cmd("volume", "create", "--label", "foo1=baz1", "--label", "foo2=baz2", tID)
},
tearDown: func(tID string) {
base.Cmd("volume", "rm", "-f", tID).Run()
},
expected: func(tID string) icmd.Expected {
return icmd.Expected{
ExitCode: 0,
Out: tID,
}
},
},
{
description: "invalid labels",
command: func(tID string) *testutil.Cmd {
// See https://github.com/containerd/nerdctl/issues/3126
return base.Cmd("volume", "create", "--label", "a", "--label", "", tID)
},
tearDown: func(tID string) {
base.Cmd("volume", "rm", "-f", tID).Run()
},
expected: func(tID string) icmd.Expected {
return icmd.Expected{
ExitCode: exitCodeVariant,
Err: malformed,
}
},
},
}

base.Cmd("volume", "create", "too", "many").AssertFail()
}
for _, test := range testCases {
currentTest := test
t.Run(currentTest.description, func(tt *testing.T) {
tt.Parallel()

// Test TestVolumeCreateWithLabels for creating volume with given labels.
func TestVolumeCreateWithLabels(t *testing.T) {
base := testutil.NewBase(t)
testVolume := testutil.Identifier(t)
tID := testutil.Identifier(tt)

base.Cmd("volume", "create", testVolume, "--label", "foo1=baz1", "--label", "foo2=baz2").AssertOK()
defer base.Cmd("volume", "rm", "-f", testVolume).Run()
if currentTest.tearDown != nil {
currentTest.tearDown(tID)
tt.Cleanup(func() {
currentTest.tearDown(tID)
})
}
if currentTest.tearUp != nil {
currentTest.tearUp(tID)
}

inspect := base.InspectVolume(testVolume)
inspectNerdctlLabels := *inspect.Labels
expected := make(map[string]string, 2)
expected["foo1"] = "baz1"
expected["foo2"] = "baz2"
assert.DeepEqual(base.T, expected, inspectNerdctlLabels)
cmd := currentTest.command(tID)
cmd.Assert(currentTest.expected(tID))
})
}
}
2 changes: 1 addition & 1 deletion cmd/nerdctl/volume_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func volumeInspectAction(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
return volume.Inspect(args, options)
return volume.Inspect(cmd.Context(), args, options)
}

func volumeInspectShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Expand Down
Loading

0 comments on commit a6cd38f

Please sign in to comment.