mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
e53f65a916
This const was previously living in pkg/signal, but with that package being moved to its own module, it didn't make much sense to put docker's defaults in a generic module. The const from the "signal" package is currenlty used *both* by the CLI and the daemon as a default value when creating containers. This put up some questions: a. should the default be non-exported, and private to the container package? After all, it's a _default_ (so should be used if _NOT_ set). b. should the client actually setting a default, or instead just omit the value, unless specified by the user? having the client set a default also means that the daemon cannot change the default value because the client (or older clients) will override it. c. consider defaults from the client and defaults of the daemon to be separate things, and create a default const in the CLI. This patch implements option "a" (option "b" will be done separately, as it involves the CLI code). This still leaves "c" open as an option, if the CLI wants to set its own default. Unfortunately, this change means we'll have to drop the alias for the deprecated pkg/signal.DefaultStopSignal const, but a comment was left instead, which can assist consumers of the const to find why it's no longer there (a search showed the Docker CLI as the only consumer though). Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
126 lines
2.9 KiB
Go
126 lines
2.9 KiB
Go
package container // import "github.com/docker/docker/container"
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/docker/docker/api/types/container"
|
|
swarmtypes "github.com/docker/docker/api/types/swarm"
|
|
"github.com/docker/docker/daemon/logger/jsonfilelog"
|
|
"github.com/moby/sys/signal"
|
|
"gotest.tools/v3/assert"
|
|
)
|
|
|
|
func TestContainerStopSignal(t *testing.T) {
|
|
c := &Container{
|
|
Config: &container.Config{},
|
|
}
|
|
|
|
def, err := signal.ParseSignal(defaultStopSignal)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
s := c.StopSignal()
|
|
if s != int(def) {
|
|
t.Fatalf("Expected %v, got %v", def, s)
|
|
}
|
|
|
|
c = &Container{
|
|
Config: &container.Config{StopSignal: "SIGKILL"},
|
|
}
|
|
s = c.StopSignal()
|
|
if s != 9 {
|
|
t.Fatalf("Expected 9, got %v", s)
|
|
}
|
|
}
|
|
|
|
func TestContainerStopTimeout(t *testing.T) {
|
|
c := &Container{
|
|
Config: &container.Config{},
|
|
}
|
|
|
|
s := c.StopTimeout()
|
|
if s != defaultStopTimeout {
|
|
t.Fatalf("Expected %v, got %v", defaultStopTimeout, s)
|
|
}
|
|
|
|
stopTimeout := 15
|
|
c = &Container{
|
|
Config: &container.Config{StopTimeout: &stopTimeout},
|
|
}
|
|
s = c.StopTimeout()
|
|
if s != stopTimeout {
|
|
t.Fatalf("Expected %v, got %v", stopTimeout, s)
|
|
}
|
|
}
|
|
|
|
func TestContainerSecretReferenceDestTarget(t *testing.T) {
|
|
ref := &swarmtypes.SecretReference{
|
|
File: &swarmtypes.SecretReferenceFileTarget{
|
|
Name: "app",
|
|
},
|
|
}
|
|
|
|
d := getSecretTargetPath(ref)
|
|
expected := filepath.Join(containerSecretMountPath, "app")
|
|
if d != expected {
|
|
t.Fatalf("expected secret dest %q; received %q", expected, d)
|
|
}
|
|
}
|
|
|
|
func TestContainerLogPathSetForJSONFileLogger(t *testing.T) {
|
|
containerRoot, err := ioutil.TempDir("", "TestContainerLogPathSetForJSONFileLogger")
|
|
assert.NilError(t, err)
|
|
defer os.RemoveAll(containerRoot)
|
|
|
|
c := &Container{
|
|
Config: &container.Config{},
|
|
HostConfig: &container.HostConfig{
|
|
LogConfig: container.LogConfig{
|
|
Type: jsonfilelog.Name,
|
|
},
|
|
},
|
|
ID: "TestContainerLogPathSetForJSONFileLogger",
|
|
Root: containerRoot,
|
|
}
|
|
|
|
logger, err := c.StartLogger()
|
|
assert.NilError(t, err)
|
|
defer 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, err := ioutil.TempDir("", "TestContainerLogPathSetForRingLogger")
|
|
assert.NilError(t, err)
|
|
defer os.RemoveAll(containerRoot)
|
|
|
|
c := &Container{
|
|
Config: &container.Config{},
|
|
HostConfig: &container.HostConfig{
|
|
LogConfig: container.LogConfig{
|
|
Type: jsonfilelog.Name,
|
|
Config: map[string]string{
|
|
"mode": string(container.LogModeNonBlock),
|
|
},
|
|
},
|
|
},
|
|
ID: "TestContainerLogPathSetForRingLogger",
|
|
Root: containerRoot,
|
|
}
|
|
|
|
logger, err := c.StartLogger()
|
|
assert.NilError(t, err)
|
|
defer 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)
|
|
}
|