Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Volume QA part II: fixes and tests #3133

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions cmd/nerdctl/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ func namespaceLsAction(cmd *cobra.Command, args []string) error {
}
numImages = len(images)

volStore, err := volumestore.Path(dataStore, ns)
volStore, err := volumestore.New(dataStore, ns)
if err != nil {
log.L.Warn(err)
} else {
volEnts, err := os.ReadDir(volStore)
volEnts, err := volStore.List(false)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Replace direct access to underlying filesystem implementation by a proper api call.

if err != nil {
if !os.IsNotExist(err) {
log.L.Warn(err)
Expand Down
4 changes: 4 additions & 0 deletions cmd/nerdctl/system_prune_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ import (

func TestSystemPrune(t *testing.T) {
testutil.RequiresBuild(t)
// FIXME: using a dedicated namespace does not work with rootful (because of buildkit running)
// t.Parallel()
// namespaceID := testutil.Identifier(t)
// base := testutil.NewBaseWithNamespace(t, namespaceID)
base := testutil.NewBase(t)
base.Cmd("container", "prune", "-f").AssertOK()
base.Cmd("network", "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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Align with docker behavior. Empty labels are not allowed.

if label == "" {
return types.VolumeCreateOptions{}, fmt.Errorf("labels cannot be empty (%w)", errdefs.ErrInvalidArgument)
}
}

return types.VolumeCreateOptions{
GOptions: globalOptions,
Labels: labels,
Expand Down
161 changes: 136 additions & 25 deletions cmd/nerdctl/volume_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,151 @@ 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()
atMost := "at most 1 arg"
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
inspect func(t *testing.T, stdout string, stderr string)
dockerIncompatible bool
}{
{
description: "arg missing should create anonymous volume",
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 should fail",
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 should fail",
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: atMost,
}
},
},
{
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,
}
},
},
{
description: "creating already existing volume should succeed",
command: func(tID string) *testutil.Cmd {
base.Cmd("volume", "create", tID).AssertOK()
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,
}
},
},
}

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