Repository: moby/moby
File: daemon/list_test.go (Go)

Code before:
	assert.Assert(t, containerListContainsName(containerList, three.Name))

	// Same as above but with slash prefix should produce the same result
	containerListWithPrefix, err = d.Containers(context.Background(), &containertypes.ListOptions{
		Filters: filters.NewArgs(filters.Arg("name", "/b1")),
	})
	assert.NilError(t, err)
	assert.Assert(t, is.Len(containerListWithPrefix, 1))
	assert.Assert(t, containerListContainsName(containerListWithPrefix, three.Name))
}

func TestContainerList_LimitFilter(t *testing.T) {
	db, err := container.NewViewDB()
	assert.NilError(t, err)
	d := &Daemon{
		containersReplica: db,
	}

	// start containers
	num := 32
	containers := make([]*container.Container, num)
	for i := range num {
		name := fmt.Sprintf("cont-%d", i)
		containers[i] = setupContainerWithName(t, name, d)
	}

	t.Run("limit 1 container", func(t *testing.T) {
		limit := 1
		containerList, err := d.Containers(context.Background(), &containertypes.ListOptions{Limit: limit})
		assert.NilError(t, err)
		expectedListLen := min(num, limit)
		assert.Assert(t, is.Len(containerList, expectedListLen))
	})

	t.Run("limit less than num containers", func(t *testing.T) {
		limit := 20
		containerList, err := d.Containers(context.Background(), &containertypes.ListOptions{Limit: limit})
		assert.NilError(t, err)
		expectedListLen := min(num, limit)
		assert.Assert(t, is.Len(containerList, expectedListLen))
	})

	t.Run("limit equal num containers", func(t *testing.T) {
		limit := 32
		containerList, err := d.Containers(context.Background(), &containertypes.ListOptions{Limit: limit})
		assert.NilError(t, err)
		expectedListLen := min(num, limit)
		assert.Assert(t, is.Len(containerList, expectedListLen))
	})

	t.Run("limit greater than num containers", func(t *testing.T) {

Review comment (suggestion):
Just an idea; would it be good to check the actual list in the containersReplica db (as that's the source of truth used)?

```go
	const num = 32
	for i := range num {
		setupContainerWithName(t, fmt.Sprintf("cont-%d", i), d)
	}
	containers, err := db.Snapshot().All()
	assert.NilError(t, err)
	assert.Assert(t, is.Len(containers, num))
```

Code after:
	assert.NilError(t, err)
	assert.Assert(t, is.Len(containerList, 1))
	assert.Assert(t, containerListContainsName(containerList, three.Name))

	// Same as above but with slash prefix should produce the same result
	containerListWithPrefix, err = d.Containers(context.Background(), &containertypes.ListOptions{
		Filters: filters.NewArgs(filters.Arg("name", "/b1")),
	})
	assert.NilError(t, err)
	assert.Assert(t, is.Len(containerListWithPrefix, 1))
	assert.Assert(t, containerListContainsName(containerListWithPrefix, three.Name))
}

func TestContainerList_LimitFilter(t *testing.T) {
	db, err := container.NewViewDB()
	assert.NilError(t, err)
	d := &Daemon{
		containersReplica: db,
	}

	// start containers
	num := 32
	for i := range num {
		name := fmt.Sprintf("cont-%d", i)
		setupContainerWithName(t, name, d)
	}

	containers, err := db.Snapshot().All()
	assert.NilError(t, err)
	assert.Assert(t, is.Len(containers, num))

	tests := []struct {
		limit int
		doc   string
	}{
		{limit: 0, doc: "no limit"},
		{limit: -1, doc: "negative limit doesn't limit"},
		{limit: 1, doc: "limit 1 container"},
		{limit: 20, doc: "limit less than num containers"},
		{limit: 32, doc: "limit equal num containers"},
		{limit: 40, doc: "limit greater than num containers"},
	}

	for _, tc := range tests {
		t.Run(tc.doc, func(t *testing.T) {
			containerList, err := d.Containers(context.Background(), &containertypes.ListOptions{Limit: tc.limit})
			assert.NilError(t, err)
			expectedListLen := num
			if tc.limit > 0 {
				expectedListLen = min(num, tc.limit)
			}