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"
|
|
|
|
"io/ioutil"
|
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"
|
2015-11-12 14:55:17 -05:00
|
|
|
"github.com/docker/docker/pkg/signal"
|
2018-03-13 15:28:34 -04:00
|
|
|
"github.com/gotestyourself/gotestyourself/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
|
|
|
}
|
|
|
|
|
|
|
|
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 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()
|
2016-06-06 23:29:05 -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) {
|
|
|
|
containerRoot, err := ioutil.TempDir("", "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) {
|
|
|
|
containerRoot, err := ioutil.TempDir("", "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
|
|
|
}
|