Files
moby/container/container_test.go
Sebastiaan van Stijn 89aa33001e container: remove // import comments
These comments were added to enforce using the correct import path for
our packages ("github.com/docker/docker", not "github.com/moby/moby").
However, when working in go module mode (not GOPATH / vendor), they have
no effect, so their impact is limited.

Remove these imports in preparation of migrating our code to become an
actual go module.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-05-30 15:59:11 +02:00

117 lines
2.5 KiB
Go

package container
import (
"fmt"
"path/filepath"
"syscall"
"testing"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/daemon/logger/jsonfilelog"
"gotest.tools/v3/assert"
)
func TestContainerStopSignal(t *testing.T) {
c := &Container{
Config: &container.Config{},
}
s := c.StopSignal()
assert.Equal(t, s, defaultStopSignal)
c = &Container{
Config: &container.Config{StopSignal: "SIGKILL"},
}
s = c.StopSignal()
expected := syscall.SIGKILL
assert.Equal(t, s, expected)
c = &Container{
Config: &container.Config{StopSignal: "NOSUCHSIGNAL"},
}
s = c.StopSignal()
assert.Equal(t, s, defaultStopSignal)
}
func TestContainerStopTimeout(t *testing.T) {
c := &Container{
Config: &container.Config{},
}
s := c.StopTimeout()
assert.Equal(t, s, defaultStopTimeout)
stopTimeout := 15
c = &Container{
Config: &container.Config{StopTimeout: &stopTimeout},
}
s = c.StopTimeout()
assert.Equal(t, s, stopTimeout)
}
func TestContainerSecretReferenceDestTarget(t *testing.T) {
ref := &swarm.SecretReference{
File: &swarm.SecretReferenceFileTarget{
Name: "app",
},
}
d := getSecretTargetPath(ref)
expected := filepath.Join(containerSecretMountPath, "app")
assert.Equal(t, d, expected)
}
func TestContainerLogPathSetForJSONFileLogger(t *testing.T) {
containerRoot := t.TempDir()
c := &Container{
Config: &container.Config{},
HostConfig: &container.HostConfig{
LogConfig: container.LogConfig{
Type: jsonfilelog.Name,
},
},
ID: t.Name(),
Root: containerRoot,
}
logger, err := c.StartLogger()
assert.NilError(t, err)
defer func() {
assert.NilError(t, logger.Close())
}()
expectedLogPath, err := filepath.Abs(filepath.Join(containerRoot, fmt.Sprintf("%s-json.log", c.ID)))
assert.NilError(t, err)
assert.Equal(t, c.LogPath, expectedLogPath)
}
func TestContainerLogPathSetForRingLogger(t *testing.T) {
containerRoot := t.TempDir()
c := &Container{
Config: &container.Config{},
HostConfig: &container.HostConfig{
LogConfig: container.LogConfig{
Type: jsonfilelog.Name,
Config: map[string]string{
"mode": string(container.LogModeNonBlock),
},
},
},
ID: t.Name(),
Root: containerRoot,
}
logger, err := c.StartLogger()
assert.NilError(t, err)
defer func() {
assert.NilError(t, logger.Close())
}()
expectedLogPath, err := filepath.Abs(filepath.Join(containerRoot, fmt.Sprintf("%s-json.log", c.ID)))
assert.NilError(t, err)
assert.Equal(t, c.LogPath, expectedLogPath)
}