This fix adds an integration test for `before` and `since` filter for `docker ps`
Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,64 @@ |
| 0 |
+package container |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "context" |
|
| 4 |
+ "testing" |
|
| 5 |
+ |
|
| 6 |
+ "github.com/docker/docker/api/types" |
|
| 7 |
+ "github.com/docker/docker/api/types/container" |
|
| 8 |
+ "github.com/docker/docker/api/types/filters" |
|
| 9 |
+ "github.com/docker/docker/api/types/network" |
|
| 10 |
+ "github.com/docker/docker/integration/util/request" |
|
| 11 |
+ "github.com/stretchr/testify/assert" |
|
| 12 |
+ "github.com/stretchr/testify/require" |
|
| 13 |
+) |
|
| 14 |
+ |
|
| 15 |
+func TestPsFilter(t *testing.T) {
|
|
| 16 |
+ defer setupTest(t)() |
|
| 17 |
+ client := request.NewAPIClient(t) |
|
| 18 |
+ ctx := context.Background() |
|
| 19 |
+ |
|
| 20 |
+ createContainerForFilter := func(ctx context.Context, name string) string {
|
|
| 21 |
+ body, err := client.ContainerCreate(ctx, |
|
| 22 |
+ &container.Config{
|
|
| 23 |
+ Cmd: []string{"top"},
|
|
| 24 |
+ Image: "busybox", |
|
| 25 |
+ }, |
|
| 26 |
+ &container.HostConfig{},
|
|
| 27 |
+ &network.NetworkingConfig{},
|
|
| 28 |
+ name, |
|
| 29 |
+ ) |
|
| 30 |
+ require.NoError(t, err) |
|
| 31 |
+ return body.ID |
|
| 32 |
+ } |
|
| 33 |
+ |
|
| 34 |
+ prev := createContainerForFilter(ctx, "prev") |
|
| 35 |
+ createContainerForFilter(ctx, "top") |
|
| 36 |
+ next := createContainerForFilter(ctx, "next") |
|
| 37 |
+ |
|
| 38 |
+ containerIDs := func(containers []types.Container) []string {
|
|
| 39 |
+ entries := []string{}
|
|
| 40 |
+ for _, container := range containers {
|
|
| 41 |
+ entries = append(entries, container.ID) |
|
| 42 |
+ } |
|
| 43 |
+ return entries |
|
| 44 |
+ } |
|
| 45 |
+ |
|
| 46 |
+ f1 := filters.NewArgs() |
|
| 47 |
+ f1.Add("since", "top")
|
|
| 48 |
+ q1, err := client.ContainerList(ctx, types.ContainerListOptions{
|
|
| 49 |
+ All: true, |
|
| 50 |
+ Filters: f1, |
|
| 51 |
+ }) |
|
| 52 |
+ require.NoError(t, err) |
|
| 53 |
+ assert.Contains(t, containerIDs(q1), next) |
|
| 54 |
+ |
|
| 55 |
+ f2 := filters.NewArgs() |
|
| 56 |
+ f2.Add("before", "top")
|
|
| 57 |
+ q2, err := client.ContainerList(ctx, types.ContainerListOptions{
|
|
| 58 |
+ All: true, |
|
| 59 |
+ Filters: f2, |
|
| 60 |
+ }) |
|
| 61 |
+ require.NoError(t, err) |
|
| 62 |
+ assert.Contains(t, containerIDs(q2), prev) |
|
| 63 |
+} |