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

Code before:
	c.Config = &containertypes.Config{
		Image: computedImageID.String(),
	}

	// this is done here to avoid requiring these
	// operations n x number of containers in the
	// calling function
	daemon.containersReplica.Save(c)
	daemon.reserveName(id, name)

	return c
}

func containerListContainsName(containers []*containertypes.Summary, name string) bool {
	for _, ctr := range containers {
		for _, containerName := range ctr.Names {
			if containerName == name {
				return true
			}
		}
	}

	return false
}

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

	// test list with no containers
	containerList, err := d.Containers(context.Background(), &containertypes.ListOptions{All: true})
	assert.NilError(t, err)
	assert.Assert(t, is.Len(containerList, 0))

	// test list of one container
	one := setupContainerWithName(t, "a1", d)
	containerList, err = d.Containers(context.Background(), &containertypes.ListOptions{All: true})
	assert.NilError(t, err)
	assert.Assert(t, is.Len(containerList, 1))
	assert.Assert(t, is.Equal(containerList[0].Names[0], one.Name))

	// test list with random number of containers
	db2, err := container.NewViewDB() // new DB to ignore prior containers
	assert.NilError(t, err)
	d = &Daemon{
		containersReplica: db2,
	}


Review comment (suggestion):
Not a blocker, but perhaps now is a good opportunity to change course; I noticed that these tests are in `daemon/`, and "List" is rather ambiguous because we can list all kind of objects (volumes, networks, ...).


Before this, it looks like there was only two tests for listing (`TestListInvalidFilter`, `TestNameFilter`), both poorly named; this PR adds two more. Perhaps we should rename them to have a common prefix that clearly indicates it's for containers (which also allows `go test` to be used for filtering all "container-related" (list) tests;


- `TestList` -> `TestContainerList`
- `TestListInvalidFilter` -> `TestContainerListInvalidFilter` (or `TestContainerListFilterInvalid` to have a common `Filter` prefix)
- `TestNameFilter` -> `TestContainerListNameFilter` (or `TestContainerListFilterName`)
- `TestLimitFilter` -> `TestContainerListLimitFilter` (or `TestContainerListFilterLimit`)

Code after:
		Image: computedImageID.String(),
	}

	// this is done here to avoid requiring these
	// operations n x number of containers in the
	// calling function
	daemon.containersReplica.Save(c)
	daemon.reserveName(id, name)

	return c
}

func containerListContainsName(containers []*containertypes.Summary, name string) bool {
	for _, ctr := range containers {
		for _, containerName := range ctr.Names {
			if containerName == name {
				return true
			}
		}
	}

	return false
}

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

	// test list with different number of containers
	for _, num := range []int{0, 1, 2, 4, 8, 16, 32, 64, 100} {
		t.Run(fmt.Sprintf("%d containers", num), func(t *testing.T) {
			db, err := container.NewViewDB() // new DB to ignore prior containers
			assert.NilError(t, err)
			d = &Daemon{
				containersReplica: db,
			}

			// create the containers
			containers := make([]*container.Container, num)
			for i := range num {
				name := fmt.Sprintf("cont-%d", i)
				containers[i] = setupContainerWithName(t, name, d)
				// ensure container timestamps are separated enough so the
				// sort used by d.Containers() can deterministically sort them.
				if i > 0 {
					containers[i].Created = containers[i-1].Created.Add(time.Millisecond)
				}
			}