Repository: moby/moby
File: integration/container/list_test.go (Go)

Code before:
package container

import (
	"fmt"
	"math/rand"
	"testing"

	containertypes "github.com/docker/docker/api/types/container"
	"github.com/docker/docker/api/types/filters"
	"github.com/docker/docker/client"
	"github.com/docker/docker/integration/internal/container"
	"github.com/docker/docker/testutil"
	"github.com/docker/docker/testutil/request"
	"gotest.tools/v3/assert"
	is "gotest.tools/v3/assert/cmp"
	"gotest.tools/v3/skip"
)

func TestList(t *testing.T) {
	ctx := setupTest(t)
	apiClient := request.NewAPIClient(t)

	// remove any existing containers
	container.RemoveAll(ctx, t, apiClient)

	// start a random number of containers (between 0->64)
	num := rand.Intn(64)
	containers := make([]string, num)
	for i := range num {
		id := container.Create(ctx, t, apiClient)
		defer container.Remove(ctx, t, apiClient, id, containertypes.RemoveOptions{Force: true})
		containers[i] = id
	}

	// list them and verify correctness
	containerList, err := apiClient.ContainerList(ctx, containertypes.ListOptions{All: true})
	assert.NilError(t, err)
	assert.Assert(t, is.Len(containerList, num))
	for i := range num {
		// container list should be ordered in descending creation order
		assert.Assert(t, is.Equal(containerList[i].ID, containers[num-1-i]))
	}
}

func TestListAnnotations(t *testing.T) {
	ctx := setupTest(t)
	apiClient := request.NewAPIClient(t)


Review comment (suggestion):
We should have a look why we ran into this issue; I don't _think_ we're running tests with `t.Parallel`, but it's _possible_ that on Windows (where we only have a single daemon to our availability) that test-suites may be running in parallel?

Generally, `setupTest(t)` should take care of cleaning up containers after tests complete, but it's possible that `t.Cleanup` is not guaranteed to be completed before the next test starts? https://github.com/moby/moby/blob/320db9d55d6769eaf8f83320ce27ca548ef9abfd/integration/container/main_test.go#L51-L56

Although our cleanup has a concept of "protected containers", which are containers that were present before the test ran, so _possibly_ if the previous cleanup didn't finish yet? https://github.com/moby/moby/blob/320db9d55d6769eaf8f83320ce27ca548ef9abfd/testutil/environment/clean.go#L76-L79

Code after:
package container

import (
	"fmt"
	"testing"

	containertypes "github.com/docker/docker/api/types/container"
	"github.com/docker/docker/api/types/filters"
	"github.com/docker/docker/client"
	"github.com/docker/docker/integration/internal/container"
	"github.com/docker/docker/testutil"
	"github.com/docker/docker/testutil/request"
	"gotest.tools/v3/assert"
	is "gotest.tools/v3/assert/cmp"
	"gotest.tools/v3/skip"
)

func TestContainerList(t *testing.T) {
	ctx := setupTest(t)
	apiClient := request.NewAPIClient(t)

	// remove all existing containers
	container.RemoveAll(ctx, t, apiClient)

	// create the containers
	num := 64
	containers := make([]string, num)
	for i := range num {
		id := container.Create(ctx, t, apiClient)
		defer container.Remove(ctx, t, apiClient, id, containertypes.RemoveOptions{Force: true})
		containers[i] = id
	}

	// list them and verify correctness
	containerList, err := apiClient.ContainerList(ctx, containertypes.ListOptions{All: true})
	assert.NilError(t, err)
	assert.Assert(t, is.Len(containerList, num))
	for i := range num {
		// container list should be ordered in descending creation order
		assert.Assert(t, is.Equal(containerList[i].ID, containers[num-1-i]))
	}
}

func TestContainerList_Annotations(t *testing.T) {
	ctx := setupTest(t)

	annotations := map[string]string{
		"foo":                       "bar",