2018-02-15 10:24:26 -05:00
|
|
|
package daemon
|
|
|
|
|
2018-02-15 10:44:00 -05:00
|
|
|
import (
|
2022-10-26 12:13:17 -04:00
|
|
|
"context"
|
2018-07-15 06:34:18 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-02-15 10:24:26 -05:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2018-07-15 06:34:18 -04:00
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
2018-02-15 10:24:26 -05:00
|
|
|
"github.com/docker/docker/api/types/filters"
|
2018-02-15 10:44:00 -05:00
|
|
|
"github.com/docker/docker/container"
|
2018-07-15 06:34:18 -04:00
|
|
|
"github.com/docker/docker/image"
|
2019-03-13 07:34:27 -04:00
|
|
|
"github.com/google/uuid"
|
2022-03-04 08:49:42 -05:00
|
|
|
"github.com/opencontainers/go-digest"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2018-02-15 10:24:26 -05:00
|
|
|
)
|
|
|
|
|
2018-07-15 06:34:18 -04:00
|
|
|
var root string
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
var err error
|
2021-08-24 06:10:50 -04:00
|
|
|
root, err = os.MkdirTemp("", "docker-container-test-")
|
2018-07-15 06:34:18 -04:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(root)
|
|
|
|
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
|
|
|
// This sets up a container with a name so that name filters
|
|
|
|
// work against it. It takes in a pointer to Daemon so that
|
|
|
|
// minor operations are not repeated by the caller
|
|
|
|
func setupContainerWithName(t *testing.T, name string, daemon *Daemon) *container.Container {
|
2018-08-28 15:40:13 -04:00
|
|
|
t.Helper()
|
2018-07-15 06:34:18 -04:00
|
|
|
var (
|
2019-03-13 07:34:27 -04:00
|
|
|
id = uuid.New().String()
|
2018-07-15 06:34:18 -04:00
|
|
|
computedImageID = digest.FromString(id)
|
|
|
|
cRoot = filepath.Join(root, id)
|
|
|
|
)
|
|
|
|
if err := os.MkdirAll(cRoot, 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c := container.NewBaseContainer(id, cRoot)
|
|
|
|
// these are for passing includeContainerInList
|
2018-08-28 15:40:13 -04:00
|
|
|
if name[0] != '/' {
|
|
|
|
name = "/" + name
|
|
|
|
}
|
2018-07-15 06:34:18 -04:00
|
|
|
c.Name = name
|
|
|
|
c.Running = true
|
|
|
|
c.HostConfig = &containertypes.HostConfig{}
|
|
|
|
|
|
|
|
// these are for passing the refreshImage reducer
|
|
|
|
c.ImageID = image.IDFromDigest(computedImageID)
|
|
|
|
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 []*types.Container, name string) bool {
|
2019-08-09 08:10:07 -04:00
|
|
|
for _, ctr := range containers {
|
|
|
|
for _, containerName := range ctr.Names {
|
2018-08-28 15:40:13 -04:00
|
|
|
if containerName == name {
|
2018-07-15 06:34:18 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-02-15 10:24:26 -05:00
|
|
|
func TestListInvalidFilter(t *testing.T) {
|
|
|
|
db, err := container.NewViewDB()
|
|
|
|
assert.Assert(t, err == nil)
|
|
|
|
d := &Daemon{
|
|
|
|
containersReplica: db,
|
|
|
|
}
|
|
|
|
|
|
|
|
f := filters.NewArgs(filters.Arg("invalid", "foo"))
|
|
|
|
|
2022-10-26 12:13:17 -04:00
|
|
|
_, err = d.Containers(context.Background(), &types.ContainerListOptions{
|
2018-02-15 10:24:26 -05:00
|
|
|
Filters: f,
|
|
|
|
})
|
2022-03-02 12:56:06 -05:00
|
|
|
assert.Assert(t, is.Error(err, "invalid filter 'invalid'"))
|
2018-02-15 10:44:00 -05:00
|
|
|
}
|
2018-07-15 06:34:18 -04:00
|
|
|
|
|
|
|
func TestNameFilter(t *testing.T) {
|
|
|
|
db, err := container.NewViewDB()
|
|
|
|
assert.Assert(t, err == nil)
|
|
|
|
d := &Daemon{
|
|
|
|
containersReplica: db,
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
one = setupContainerWithName(t, "a1", d)
|
|
|
|
two = setupContainerWithName(t, "a2", d)
|
|
|
|
three = setupContainerWithName(t, "b1", d)
|
|
|
|
)
|
|
|
|
|
|
|
|
// moby/moby #37453 - ^ regex not working due to prefix slash
|
|
|
|
// not being stripped
|
2022-10-26 12:13:17 -04:00
|
|
|
containerList, err := d.Containers(context.Background(), &types.ContainerListOptions{
|
2018-07-15 06:34:18 -04:00
|
|
|
Filters: filters.NewArgs(filters.Arg("name", "^a")),
|
|
|
|
})
|
2018-08-28 15:40:13 -04:00
|
|
|
assert.NilError(t, err)
|
2018-07-15 06:34:18 -04:00
|
|
|
assert.Assert(t, is.Len(containerList, 2))
|
|
|
|
assert.Assert(t, containerListContainsName(containerList, one.Name))
|
|
|
|
assert.Assert(t, containerListContainsName(containerList, two.Name))
|
|
|
|
|
2018-08-28 15:40:13 -04:00
|
|
|
// Same as above but with slash prefix should produce the same result
|
2022-10-26 12:13:17 -04:00
|
|
|
containerListWithPrefix, err := d.Containers(context.Background(), &types.ContainerListOptions{
|
2018-08-28 15:40:13 -04:00
|
|
|
Filters: filters.NewArgs(filters.Arg("name", "^/a")),
|
|
|
|
})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, is.Len(containerListWithPrefix, 2))
|
|
|
|
assert.Assert(t, containerListContainsName(containerListWithPrefix, one.Name))
|
|
|
|
assert.Assert(t, containerListContainsName(containerListWithPrefix, two.Name))
|
|
|
|
|
2018-07-15 06:34:18 -04:00
|
|
|
// Same as above but make sure it works for exact names
|
2022-10-26 12:13:17 -04:00
|
|
|
containerList, err = d.Containers(context.Background(), &types.ContainerListOptions{
|
2018-07-15 06:34:18 -04:00
|
|
|
Filters: filters.NewArgs(filters.Arg("name", "b1")),
|
|
|
|
})
|
2018-08-28 15:40:13 -04:00
|
|
|
assert.NilError(t, err)
|
2018-07-15 06:34:18 -04:00
|
|
|
assert.Assert(t, is.Len(containerList, 1))
|
|
|
|
assert.Assert(t, containerListContainsName(containerList, three.Name))
|
2018-08-28 15:40:13 -04:00
|
|
|
|
|
|
|
// Same as above but with slash prefix should produce the same result
|
2022-10-26 12:13:17 -04:00
|
|
|
containerListWithPrefix, err = d.Containers(context.Background(), &types.ContainerListOptions{
|
2018-08-28 15:40:13 -04:00
|
|
|
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))
|
2018-07-15 06:34:18 -04:00
|
|
|
}
|