mirror of
https://github.com/moby/moby.git
synced 2026-01-11 18:51:37 +00:00
Commit0e50d946a2introduced a feature to allow a custom stop-signal to be set. As part of this, existing code to parse the signal was extracted to `signal.ParseSignal()`, which accepts a string either containing a numeric value or a named signal. When failing to parse the given signal, it returns an error and a magic "-1" signal. The changes in0e50d946a2used the error when creating a container, but for existing container configs, it would ignore the error and instead check if the signal was "0", in which case it would fall back to use the default stop-signal (SIGTERM). Given that `signal.ParseSignal()` returns "-1" (not "0") for invalid signals, this would result in the failure going undetected and "-1" being used instead of the intended default (SIGTERM). In practice, this issues would unlikely be encountered, as custom signals are validated when creating the container, but it would be possible for an image to contain an invalid signal, which would be used by the container as default. This patch updates the logic to only use the custom value if no error is produced and a non-zero, positive signal is returned. A test-case was added that would fail before this patch: go test -v -run TestContainerStopSignal === RUN TestContainerStopSignal container_test.go:34: assertion failed: signal -1 (s syscall.Signal) != terminated (defaultStopSignal syscall.Signal) --- FAIL: TestContainerStopSignal (0.00s) Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
117 lines
2.5 KiB
Go
117 lines
2.5 KiB
Go
package container // import "github.com/docker/docker/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)
|
|
}
|