2018-02-05 16:05:59 -05:00
|
|
|
package container // import "github.com/docker/docker/container"
|
2015-11-12 14:55:17 -05:00
|
|
|
|
|
|
|
import (
|
2018-02-09 19:03:47 -05:00
|
|
|
"fmt"
|
2018-02-27 14:07:06 -05:00
|
|
|
"os"
|
2017-04-11 13:34:19 -04:00
|
|
|
"path/filepath"
|
2015-11-12 14:55:17 -05:00
|
|
|
"testing"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2017-04-11 13:34:19 -04:00
|
|
|
swarmtypes "github.com/docker/docker/api/types/swarm"
|
2018-02-09 19:03:47 -05:00
|
|
|
"github.com/docker/docker/daemon/logger/jsonfilelog"
|
2021-07-09 18:11:57 -04:00
|
|
|
"github.com/moby/sys/signal"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
2015-11-12 14:55:17 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestContainerStopSignal(t *testing.T) {
|
|
|
|
c := &Container{
|
2017-04-25 15:03:45 -04:00
|
|
|
Config: &container.Config{},
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
|
|
|
|
pkg/signal: remove DefaultStopSignal const
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>
2021-08-06 12:50:56 -04:00
|
|
|
def, err := signal.ParseSignal(defaultStopSignal)
|
2015-11-12 14:55:17 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s := c.StopSignal()
|
2022-05-01 19:16:56 -04:00
|
|
|
if s != def {
|
2015-11-12 14:55:17 -05:00
|
|
|
t.Fatalf("Expected %v, got %v", def, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
c = &Container{
|
2017-04-25 15:03:45 -04:00
|
|
|
Config: &container.Config{StopSignal: "SIGKILL"},
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
|
|
|
s = c.StopSignal()
|
|
|
|
if s != 9 {
|
|
|
|
t.Fatalf("Expected 9, got %v", s)
|
|
|
|
}
|
|
|
|
}
|
2016-05-26 16:34:48 -04:00
|
|
|
|
|
|
|
func TestContainerStopTimeout(t *testing.T) {
|
|
|
|
c := &Container{
|
2017-04-25 15:03:45 -04:00
|
|
|
Config: &container.Config{},
|
2016-05-26 16:34:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
s := c.StopTimeout()
|
2021-08-06 04:24:09 -04:00
|
|
|
if s != defaultStopTimeout {
|
|
|
|
t.Fatalf("Expected %v, got %v", defaultStopTimeout, s)
|
2016-05-26 16:34:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
stopTimeout := 15
|
|
|
|
c = &Container{
|
2017-04-25 15:03:45 -04:00
|
|
|
Config: &container.Config{StopTimeout: &stopTimeout},
|
2016-05-26 16:34:48 -04:00
|
|
|
}
|
2018-04-17 13:52:54 -04:00
|
|
|
s = c.StopTimeout()
|
|
|
|
if s != stopTimeout {
|
|
|
|
t.Fatalf("Expected %v, got %v", stopTimeout, s)
|
2016-05-26 16:34:48 -04:00
|
|
|
}
|
|
|
|
}
|
2017-04-11 13:34:19 -04:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2018-02-09 19:03:47 -05:00
|
|
|
|
|
|
|
func TestContainerLogPathSetForJSONFileLogger(t *testing.T) {
|
2021-08-24 06:10:50 -04:00
|
|
|
containerRoot, err := os.MkdirTemp("", "TestContainerLogPathSetForJSONFileLogger")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-02-27 14:07:06 -05:00
|
|
|
defer os.RemoveAll(containerRoot)
|
2018-02-09 19:03:47 -05:00
|
|
|
|
|
|
|
c := &Container{
|
|
|
|
Config: &container.Config{},
|
|
|
|
HostConfig: &container.HostConfig{
|
|
|
|
LogConfig: container.LogConfig{
|
|
|
|
Type: jsonfilelog.Name,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ID: "TestContainerLogPathSetForJSONFileLogger",
|
|
|
|
Root: containerRoot,
|
|
|
|
}
|
|
|
|
|
2018-02-27 14:07:06 -05:00
|
|
|
logger, err := c.StartLogger()
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-02-27 14:07:06 -05:00
|
|
|
defer logger.Close()
|
2018-02-09 19:03:47 -05:00
|
|
|
|
|
|
|
expectedLogPath, err := filepath.Abs(filepath.Join(containerRoot, fmt.Sprintf("%s-json.log", c.ID)))
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, c.LogPath, expectedLogPath)
|
2018-02-09 19:03:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainerLogPathSetForRingLogger(t *testing.T) {
|
2021-08-24 06:10:50 -04:00
|
|
|
containerRoot, err := os.MkdirTemp("", "TestContainerLogPathSetForRingLogger")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-02-27 14:07:06 -05:00
|
|
|
defer os.RemoveAll(containerRoot)
|
2018-02-09 19:03:47 -05:00
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2018-02-27 14:07:06 -05:00
|
|
|
logger, err := c.StartLogger()
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-02-27 14:07:06 -05:00
|
|
|
defer logger.Close()
|
2018-02-09 19:03:47 -05:00
|
|
|
|
|
|
|
expectedLogPath, err := filepath.Abs(filepath.Join(containerRoot, fmt.Sprintf("%s-json.log", c.ID)))
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, c.LogPath, expectedLogPath)
|
2018-02-09 19:03:47 -05:00
|
|
|
}
|