DockerCLI/internal/test/builders/container.go

80 lines
1.7 KiB
Go
Raw Normal View History

package builders
import (
"time"
"github.com/docker/docker/api/types"
)
// Container creates a container with default values.
// Any number of container function builder can be passed to augment it.
func Container(name string, builders ...func(container *types.Container)) *types.Container {
// now := time.Now()
// onehourago := now.Add(-120 * time.Minute)
container := &types.Container{
ID: "container_id",
Names: []string{"/" + name},
Command: "top",
Image: "busybox:latest",
Status: "Up 1 second",
Fix container creation time in test/builders The created time of the containerd is initialized with nanoseconds, it seems to be a mistake. In other places of the code, this field is initialized with seconds: $ grep -rh 'time\.Now()\.Unix()' | grep Created Created: time.Now().Unix(), Created: time.Now().Unix(), return []image.HistoryResponseItem{{ID: img, Created: time.Now().Unix()}}, nil We can also see the the formatter assumes it to be seconds: cli/command/formatter/container.go ---- func (c *ContainerContext) CreatedAt() string { return time.Unix(c.c.Created, 0).String() } Interestingly, initializing the field with nanoseconds seems to work, except on mips architecture, where it causes some kind of overflow. ~~~~ === Failed === FAIL: cli/command/container TestContainerListWithoutFormat (0.00s) list_test.go:183: assertion failed: --- expected +++ actual @@ -1,7 +1,7 @@ CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES -container_id busybox:latest "top" Less than a second ago Up 1 second c1 -container_id busybox:latest "top" Less than a second ago Up 1 second c2 -container_id busybox:latest "top" Less than a second ago Up 1 second 80-82/tcp c3 -container_id busybox:latest "top" Less than a second ago Up 1 second 81/udp c4 -container_id busybox:latest "top" Less than a second ago Up 1 second 8.8.8.8:82->82/tcp c5 +container_id busybox:latest "top" -153722867 minutes ago Up 1 second c1 +container_id busybox:latest "top" -153722867 minutes ago Up 1 second c2 +container_id busybox:latest "top" -153722867 minutes ago Up 1 second 80-82/tcp c3 +container_id busybox:latest "top" -153722867 minutes ago Up 1 second 81/udp c4 +container_id busybox:latest "top" -153722867 minutes ago Up 1 second 8.8.8.8:82->82/tcp c5 === FAIL: cli/command/container TestContainerListNoTrunc (0.00s) list_test.go:198: assertion failed: --- expected +++ actual @@ -1,4 +1,4 @@ CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES -container_id busybox:latest "top" Less than a second ago Up 1 second c1 -container_id busybox:latest "top" Less than a second ago Up 1 second c2,foo/bar +container_id busybox:latest "top" -153722867 minutes ago Up 1 second c1 +container_id busybox:latest "top" -153722867 minutes ago Up 1 second c2,foo/bar ~~~~ Logs above taken from: https://buildd.debian.org/status/fetch.php?pkg=docker.io&arch=mipsel&ver=20.10.0%7Erc1%2Bdfsg3-1&stamp=1606895899 ~~~~ === RUN TestChtimesLinux chtimes_linux_test.go:87: Expected: 2262-04-11 23:47:16 +0000 UTC, got: 1990-01-27 10:50:44 +0000 UTC --- FAIL: TestChtimesLinux (0.00s) === RUN TestChtimes chtimes_test.go:92: Expected: 2262-04-11 23:47:16 +0000 UTC, got: 1990-01-27 10:50:44 +0000 UTC --- FAIL: TestChtimes (0.00s) ~~~~ Logs above taken from: https://buildd.debian.org/status/fetch.php?pkg=docker.io&arch=mips64el&ver=20.10.0%7Erc1%2Bdfsg3-1&stamp=1606895622 Signed-off-by: Arnaud Rebillout <elboulangero@gmail.com>
2020-12-04 14:01:53 -05:00
Created: time.Now().Unix(),
}
for _, builder := range builders {
builder(container)
}
return container
}
// WithLabel adds a label to the container
func WithLabel(key, value string) func(*types.Container) {
return func(c *types.Container) {
if c.Labels == nil {
c.Labels = map[string]string{}
}
c.Labels[key] = value
}
}
// WithName adds a name to the container
func WithName(name string) func(*types.Container) {
return func(c *types.Container) {
c.Names = append(c.Names, "/"+name)
}
}
// WithPort adds a port mapping to the container
func WithPort(privateport, publicport uint16, builders ...func(*types.Port)) func(*types.Container) {
return func(c *types.Container) {
if c.Ports == nil {
c.Ports = []types.Port{}
}
port := &types.Port{
PrivatePort: privateport,
PublicPort: publicport,
}
for _, builder := range builders {
builder(port)
}
c.Ports = append(c.Ports, *port)
}
}
// IP sets the ip of the port
func IP(ip string) func(*types.Port) {
return func(p *types.Port) {
p.IP = ip
}
}
// TCP sets the port to tcp
func TCP(p *types.Port) {
p.Type = "tcp"
}
// UDP sets the port to udp
func UDP(p *types.Port) {
p.Type = "udp"
}