Skip to content

Commit 2967ba5

Browse files
fix: test nerdctl ps filter created.
Signed-off-by: Shubharanshu Mahapatra <[email protected]>
1 parent b957f47 commit 2967ba5

File tree

3 files changed

+46
-11
lines changed

3 files changed

+46
-11
lines changed

cmd/nerdctl/container/container_create_test.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package container
1818

1919
import (
2020
"encoding/json"
21+
"fmt"
2122
"testing"
2223
"time"
2324

@@ -47,23 +48,16 @@ func TestCreate(t *testing.T) {
4748
{
4849
Description: "ps -a",
4950
NoParallel: true,
50-
Command: test.Command("ps", "-a"),
51-
// FIXME: this might get a false positive if other tests have created a container
52-
Expected: test.Expects(0, nil, expect.Contains("Created")),
53-
},
54-
{
55-
Description: "start",
56-
NoParallel: true,
5751
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
58-
return helpers.Command("start", data.Labels().Get("cID"))
52+
return helpers.Command("ps", "-a", "--filter", "status=created", "--filter", fmt.Sprintf("name=%s", data.Labels().Get("cID")))
5953
},
60-
Expected: test.Expects(0, nil, nil),
54+
Expected: test.Expects(0, nil, expect.Contains("Created")),
6155
},
6256
{
63-
Description: "logs",
57+
Description: "start",
6458
NoParallel: true,
6559
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
66-
return helpers.Command("logs", data.Labels().Get("cID"))
60+
return helpers.Command("start", "-a", data.Labels().Get("cID"))
6761
},
6862
Expected: test.Expects(0, nil, expect.Contains("foo")),
6963
},

cmd/nerdctl/container/container_list_linux_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import (
2626

2727
"gotest.tools/v3/assert"
2828

29+
"github.com/containerd/nerdctl/mod/tigron/test"
30+
2931
"github.com/containerd/nerdctl/v2/pkg/formatter"
3032
"github.com/containerd/nerdctl/v2/pkg/strutil"
3133
"github.com/containerd/nerdctl/v2/pkg/tabutil"
@@ -628,3 +630,35 @@ func TestContainerListCheckCreatedTime(t *testing.T) {
628630
t.Errorf("expected containers in decending order")
629631
}
630632
}
633+
634+
func TestContainerListStatusFilter(t *testing.T) {
635+
testCase := nerdtest.Setup()
636+
testCase.Setup = func(data test.Data, helpers test.Helpers) {
637+
helpers.Ensure("create", "--name", data.Identifier("container"), testutil.CommonImage, "echo", "foo")
638+
data.Labels().Set("cID", data.Identifier("container"))
639+
}
640+
testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
641+
helpers.Anyhow("rm", "-f", data.Identifier("container"))
642+
}
643+
644+
testCase.SubTests = []*test.Case{
645+
// TODO: Refactor other filter tests
646+
{
647+
Description: "ps filter with status=created",
648+
NoParallel: true,
649+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
650+
return helpers.Command("ps", "-a", "--filter", "status=created", "--filter", fmt.Sprintf("name=%s", data.Labels().Get("cID")))
651+
},
652+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
653+
return &test.Expected{
654+
ExitCode: 0,
655+
Output: func(stdout, info string, t *testing.T) {
656+
assert.Assert(t, strings.Contains(stdout, data.Labels().Get("cID")), "No container found with status created")
657+
},
658+
}
659+
},
660+
},
661+
}
662+
663+
testCase.Run(t)
664+
}

pkg/cmd/container/list_util.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
containerd "github.com/containerd/containerd/v2/client"
2727
"github.com/containerd/containerd/v2/core/containers"
28+
"github.com/containerd/errdefs"
2829
"github.com/containerd/log"
2930

3031
"github.com/containerd/nerdctl/v2/pkg/containerutil"
@@ -106,6 +107,7 @@ func (cl *containerFilterContext) foldExitedFilter(_ context.Context, filter, va
106107
if err != nil {
107108
return err
108109
}
110+
log.L.Infof("checking exit status %v %v", filter, value)
109111
cl.exitedFilterFuncs = append(cl.exitedFilterFuncs, func(exitStatus int) bool {
110112
return exited == exitStatus
111113
})
@@ -235,9 +237,14 @@ func (cl *containerFilterContext) matchesTaskFilters(ctx context.Context, contai
235237
defer cancel()
236238
task, err := container.Task(ctx, nil)
237239
if err != nil {
240+
if errdefs.IsNotFound(err) {
241+
// Check if we want to filter created containers
242+
return cl.matchesExitedFilter(containerd.Status{Status: containerd.Created}) && cl.matchesStatusFilter(containerd.Status{Status: containerd.Created})
243+
}
238244
log.G(ctx).Warn(err)
239245
return false
240246
}
247+
241248
status, err := task.Status(ctx)
242249
if err != nil {
243250
log.G(ctx).Warn(err)

0 commit comments

Comments
 (0)