1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
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)
}
|