1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/daemon/list_test.go

146 lines
4 KiB
Go
Raw Normal View History

package daemon
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/docker/docker/api/types"
containertypes "github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/container"
"github.com/docker/docker/image"
"github.com/opencontainers/go-digest"
"github.com/pborman/uuid"
"gotest.tools/assert"
is "gotest.tools/assert/cmp"
)
var root string
func TestMain(m *testing.M) {
var err error
root, err = ioutil.TempDir("", "docker-container-test-")
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 {
Fix regression when filtering container names using a leading slash Commit 5c8da2e96738addd8a175e5b9a23b8c37d4a4dfe updated the filtering behavior to match container-names without having to specify the leading slash. This change caused a regression in situations where a regex was provided as filter, using an explicit leading slash (`--filter name=^/mycontainername`). This fix changes the filters to match containers both with, and without the leading slash, effectively making the leading slash optional when filtering. With this fix, filters with and without a leading slash produce the same result: $ docker ps --filter name=^a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21afd6362b0c busybox "sh" 2 minutes ago Up 2 minutes a2 56e53770e316 busybox "sh" 2 minutes ago Up 2 minutes a1 $ docker ps --filter name=^/a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21afd6362b0c busybox "sh" 2 minutes ago Up 2 minutes a2 56e53770e316 busybox "sh" 3 minutes ago Up 3 minutes a1 $ docker ps --filter name=^b CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b69003b6a6fe busybox "sh" About a minute ago Up About a minute b1 $ docker ps --filter name=^/b CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b69003b6a6fe busybox "sh" 56 seconds ago Up 54 seconds b1 $ docker ps --filter name=/a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21afd6362b0c busybox "sh" 3 minutes ago Up 3 minutes a2 56e53770e316 busybox "sh" 4 minutes ago Up 4 minutes a1 $ docker ps --filter name=a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21afd6362b0c busybox "sh" 3 minutes ago Up 3 minutes a2 56e53770e316 busybox "sh" 4 minutes ago Up 4 minutes a1 Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2018-08-28 15:40:13 -04:00
t.Helper()
var (
id = uuid.New()
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
Fix regression when filtering container names using a leading slash Commit 5c8da2e96738addd8a175e5b9a23b8c37d4a4dfe updated the filtering behavior to match container-names without having to specify the leading slash. This change caused a regression in situations where a regex was provided as filter, using an explicit leading slash (`--filter name=^/mycontainername`). This fix changes the filters to match containers both with, and without the leading slash, effectively making the leading slash optional when filtering. With this fix, filters with and without a leading slash produce the same result: $ docker ps --filter name=^a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21afd6362b0c busybox "sh" 2 minutes ago Up 2 minutes a2 56e53770e316 busybox "sh" 2 minutes ago Up 2 minutes a1 $ docker ps --filter name=^/a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21afd6362b0c busybox "sh" 2 minutes ago Up 2 minutes a2 56e53770e316 busybox "sh" 3 minutes ago Up 3 minutes a1 $ docker ps --filter name=^b CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b69003b6a6fe busybox "sh" About a minute ago Up About a minute b1 $ docker ps --filter name=^/b CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b69003b6a6fe busybox "sh" 56 seconds ago Up 54 seconds b1 $ docker ps --filter name=/a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21afd6362b0c busybox "sh" 3 minutes ago Up 3 minutes a2 56e53770e316 busybox "sh" 4 minutes ago Up 4 minutes a1 $ docker ps --filter name=a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21afd6362b0c busybox "sh" 3 minutes ago Up 3 minutes a2 56e53770e316 busybox "sh" 4 minutes ago Up 4 minutes a1 Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2018-08-28 15:40:13 -04:00
if name[0] != '/' {
name = "/" + name
}
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 {
for _, container := range containers {
for _, containerName := range container.Names {
Fix regression when filtering container names using a leading slash Commit 5c8da2e96738addd8a175e5b9a23b8c37d4a4dfe updated the filtering behavior to match container-names without having to specify the leading slash. This change caused a regression in situations where a regex was provided as filter, using an explicit leading slash (`--filter name=^/mycontainername`). This fix changes the filters to match containers both with, and without the leading slash, effectively making the leading slash optional when filtering. With this fix, filters with and without a leading slash produce the same result: $ docker ps --filter name=^a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21afd6362b0c busybox "sh" 2 minutes ago Up 2 minutes a2 56e53770e316 busybox "sh" 2 minutes ago Up 2 minutes a1 $ docker ps --filter name=^/a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21afd6362b0c busybox "sh" 2 minutes ago Up 2 minutes a2 56e53770e316 busybox "sh" 3 minutes ago Up 3 minutes a1 $ docker ps --filter name=^b CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b69003b6a6fe busybox "sh" About a minute ago Up About a minute b1 $ docker ps --filter name=^/b CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b69003b6a6fe busybox "sh" 56 seconds ago Up 54 seconds b1 $ docker ps --filter name=/a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21afd6362b0c busybox "sh" 3 minutes ago Up 3 minutes a2 56e53770e316 busybox "sh" 4 minutes ago Up 4 minutes a1 $ docker ps --filter name=a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21afd6362b0c busybox "sh" 3 minutes ago Up 3 minutes a2 56e53770e316 busybox "sh" 4 minutes ago Up 4 minutes a1 Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2018-08-28 15:40:13 -04:00
if containerName == name {
return true
}
}
}
return false
}
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"))
_, err = d.Containers(&types.ContainerListOptions{
Filters: f,
})
assert.Assert(t, is.Error(err, "Invalid filter 'invalid'"))
}
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
containerList, err := d.Containers(&types.ContainerListOptions{
Filters: filters.NewArgs(filters.Arg("name", "^a")),
})
Fix regression when filtering container names using a leading slash Commit 5c8da2e96738addd8a175e5b9a23b8c37d4a4dfe updated the filtering behavior to match container-names without having to specify the leading slash. This change caused a regression in situations where a regex was provided as filter, using an explicit leading slash (`--filter name=^/mycontainername`). This fix changes the filters to match containers both with, and without the leading slash, effectively making the leading slash optional when filtering. With this fix, filters with and without a leading slash produce the same result: $ docker ps --filter name=^a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21afd6362b0c busybox "sh" 2 minutes ago Up 2 minutes a2 56e53770e316 busybox "sh" 2 minutes ago Up 2 minutes a1 $ docker ps --filter name=^/a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21afd6362b0c busybox "sh" 2 minutes ago Up 2 minutes a2 56e53770e316 busybox "sh" 3 minutes ago Up 3 minutes a1 $ docker ps --filter name=^b CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b69003b6a6fe busybox "sh" About a minute ago Up About a minute b1 $ docker ps --filter name=^/b CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b69003b6a6fe busybox "sh" 56 seconds ago Up 54 seconds b1 $ docker ps --filter name=/a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21afd6362b0c busybox "sh" 3 minutes ago Up 3 minutes a2 56e53770e316 busybox "sh" 4 minutes ago Up 4 minutes a1 $ docker ps --filter name=a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21afd6362b0c busybox "sh" 3 minutes ago Up 3 minutes a2 56e53770e316 busybox "sh" 4 minutes ago Up 4 minutes a1 Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2018-08-28 15:40:13 -04:00
assert.NilError(t, err)
assert.Assert(t, is.Len(containerList, 2))
assert.Assert(t, containerListContainsName(containerList, one.Name))
assert.Assert(t, containerListContainsName(containerList, two.Name))
Fix regression when filtering container names using a leading slash Commit 5c8da2e96738addd8a175e5b9a23b8c37d4a4dfe updated the filtering behavior to match container-names without having to specify the leading slash. This change caused a regression in situations where a regex was provided as filter, using an explicit leading slash (`--filter name=^/mycontainername`). This fix changes the filters to match containers both with, and without the leading slash, effectively making the leading slash optional when filtering. With this fix, filters with and without a leading slash produce the same result: $ docker ps --filter name=^a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21afd6362b0c busybox "sh" 2 minutes ago Up 2 minutes a2 56e53770e316 busybox "sh" 2 minutes ago Up 2 minutes a1 $ docker ps --filter name=^/a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21afd6362b0c busybox "sh" 2 minutes ago Up 2 minutes a2 56e53770e316 busybox "sh" 3 minutes ago Up 3 minutes a1 $ docker ps --filter name=^b CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b69003b6a6fe busybox "sh" About a minute ago Up About a minute b1 $ docker ps --filter name=^/b CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b69003b6a6fe busybox "sh" 56 seconds ago Up 54 seconds b1 $ docker ps --filter name=/a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21afd6362b0c busybox "sh" 3 minutes ago Up 3 minutes a2 56e53770e316 busybox "sh" 4 minutes ago Up 4 minutes a1 $ docker ps --filter name=a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21afd6362b0c busybox "sh" 3 minutes ago Up 3 minutes a2 56e53770e316 busybox "sh" 4 minutes ago Up 4 minutes a1 Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2018-08-28 15:40:13 -04:00
// Same as above but with slash prefix should produce the same result
containerListWithPrefix, err := d.Containers(&types.ContainerListOptions{
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))
// Same as above but make sure it works for exact names
containerList, err = d.Containers(&types.ContainerListOptions{
Filters: filters.NewArgs(filters.Arg("name", "b1")),
})
Fix regression when filtering container names using a leading slash Commit 5c8da2e96738addd8a175e5b9a23b8c37d4a4dfe updated the filtering behavior to match container-names without having to specify the leading slash. This change caused a regression in situations where a regex was provided as filter, using an explicit leading slash (`--filter name=^/mycontainername`). This fix changes the filters to match containers both with, and without the leading slash, effectively making the leading slash optional when filtering. With this fix, filters with and without a leading slash produce the same result: $ docker ps --filter name=^a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21afd6362b0c busybox "sh" 2 minutes ago Up 2 minutes a2 56e53770e316 busybox "sh" 2 minutes ago Up 2 minutes a1 $ docker ps --filter name=^/a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21afd6362b0c busybox "sh" 2 minutes ago Up 2 minutes a2 56e53770e316 busybox "sh" 3 minutes ago Up 3 minutes a1 $ docker ps --filter name=^b CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b69003b6a6fe busybox "sh" About a minute ago Up About a minute b1 $ docker ps --filter name=^/b CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b69003b6a6fe busybox "sh" 56 seconds ago Up 54 seconds b1 $ docker ps --filter name=/a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21afd6362b0c busybox "sh" 3 minutes ago Up 3 minutes a2 56e53770e316 busybox "sh" 4 minutes ago Up 4 minutes a1 $ docker ps --filter name=a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21afd6362b0c busybox "sh" 3 minutes ago Up 3 minutes a2 56e53770e316 busybox "sh" 4 minutes ago Up 4 minutes a1 Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2018-08-28 15:40:13 -04:00
assert.NilError(t, err)
assert.Assert(t, is.Len(containerList, 1))
assert.Assert(t, containerListContainsName(containerList, three.Name))
Fix regression when filtering container names using a leading slash Commit 5c8da2e96738addd8a175e5b9a23b8c37d4a4dfe updated the filtering behavior to match container-names without having to specify the leading slash. This change caused a regression in situations where a regex was provided as filter, using an explicit leading slash (`--filter name=^/mycontainername`). This fix changes the filters to match containers both with, and without the leading slash, effectively making the leading slash optional when filtering. With this fix, filters with and without a leading slash produce the same result: $ docker ps --filter name=^a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21afd6362b0c busybox "sh" 2 minutes ago Up 2 minutes a2 56e53770e316 busybox "sh" 2 minutes ago Up 2 minutes a1 $ docker ps --filter name=^/a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21afd6362b0c busybox "sh" 2 minutes ago Up 2 minutes a2 56e53770e316 busybox "sh" 3 minutes ago Up 3 minutes a1 $ docker ps --filter name=^b CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b69003b6a6fe busybox "sh" About a minute ago Up About a minute b1 $ docker ps --filter name=^/b CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b69003b6a6fe busybox "sh" 56 seconds ago Up 54 seconds b1 $ docker ps --filter name=/a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21afd6362b0c busybox "sh" 3 minutes ago Up 3 minutes a2 56e53770e316 busybox "sh" 4 minutes ago Up 4 minutes a1 $ docker ps --filter name=a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21afd6362b0c busybox "sh" 3 minutes ago Up 3 minutes a2 56e53770e316 busybox "sh" 4 minutes ago Up 4 minutes a1 Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2018-08-28 15:40:13 -04:00
// Same as above but with slash prefix should produce the same result
containerListWithPrefix, err = d.Containers(&types.ContainerListOptions{
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))
}