Skip to content

Commit 0cd6b6c

Browse files
authored
Merge pull request #4196 from Shubhranshu153/fix-nerdctl-ps-filter
fix: nerdctl ps filter --status=created
2 parents ca64001 + 65f4412 commit 0cd6b6c

File tree

4 files changed

+46
-13
lines changed

4 files changed

+46
-13
lines changed

cmd/nerdctl/container/container_create_test.go

Lines changed: 5 additions & 13 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

@@ -41,29 +42,20 @@ func TestCreate(t *testing.T) {
4142
helpers.Anyhow("rm", "-f", data.Identifier("container"))
4243
}
4344

44-
testCase.Require = nerdtest.IsFlaky("https://github.com/containerd/nerdctl/issues/3717")
45-
4645
testCase.SubTests = []*test.Case{
4746
{
4847
Description: "ps -a",
4948
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,
5749
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
58-
return helpers.Command("start", data.Labels().Get("cID"))
50+
return helpers.Command("ps", "-a", "--filter", "status=created", "--filter", fmt.Sprintf("name=%s", data.Labels().Get("cID")))
5951
},
60-
Expected: test.Expects(0, nil, nil),
52+
Expected: test.Expects(0, nil, expect.Contains("Created")),
6153
},
6254
{
63-
Description: "logs",
55+
Description: "start",
6456
NoParallel: true,
6557
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
66-
return helpers.Command("logs", data.Labels().Get("cID"))
58+
return helpers.Command("start", "-a", data.Labels().Get("cID"))
6759
},
6860
Expected: test.Expects(0, nil, expect.Contains("foo")),
6961
},

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+
}

cmd/nerdctl/system/system_events_linux_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
)
3030

3131
func testEventFilterExecutor(data test.Data, helpers test.Helpers) test.TestableCommand {
32+
helpers.Ensure("pull", testutil.CommonImage)
3233
cmd := helpers.Command("events", "--filter", data.Labels().Get("filter"), "--format", "json")
3334
// 3 seconds is too short on slow rig (EL8)
3435
cmd.WithTimeout(10 * time.Second)

pkg/cmd/container/list_util.go

Lines changed: 6 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,6 +237,10 @@ 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
}

0 commit comments

Comments
 (0)