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