container_unit_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package container // import "github.com/docker/docker/container"
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. "github.com/docker/docker/api/types/container"
  9. swarmtypes "github.com/docker/docker/api/types/swarm"
  10. "github.com/docker/docker/daemon/logger/jsonfilelog"
  11. "github.com/moby/sys/signal"
  12. "gotest.tools/v3/assert"
  13. )
  14. func TestContainerStopSignal(t *testing.T) {
  15. c := &Container{
  16. Config: &container.Config{},
  17. }
  18. def, err := signal.ParseSignal(signal.DefaultStopSignal)
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. s := c.StopSignal()
  23. if s != int(def) {
  24. t.Fatalf("Expected %v, got %v", def, s)
  25. }
  26. c = &Container{
  27. Config: &container.Config{StopSignal: "SIGKILL"},
  28. }
  29. s = c.StopSignal()
  30. if s != 9 {
  31. t.Fatalf("Expected 9, got %v", s)
  32. }
  33. }
  34. func TestContainerStopTimeout(t *testing.T) {
  35. c := &Container{
  36. Config: &container.Config{},
  37. }
  38. s := c.StopTimeout()
  39. if s != DefaultStopTimeout {
  40. t.Fatalf("Expected %v, got %v", DefaultStopTimeout, s)
  41. }
  42. stopTimeout := 15
  43. c = &Container{
  44. Config: &container.Config{StopTimeout: &stopTimeout},
  45. }
  46. s = c.StopTimeout()
  47. if s != stopTimeout {
  48. t.Fatalf("Expected %v, got %v", stopTimeout, s)
  49. }
  50. }
  51. func TestContainerSecretReferenceDestTarget(t *testing.T) {
  52. ref := &swarmtypes.SecretReference{
  53. File: &swarmtypes.SecretReferenceFileTarget{
  54. Name: "app",
  55. },
  56. }
  57. d := getSecretTargetPath(ref)
  58. expected := filepath.Join(containerSecretMountPath, "app")
  59. if d != expected {
  60. t.Fatalf("expected secret dest %q; received %q", expected, d)
  61. }
  62. }
  63. func TestContainerLogPathSetForJSONFileLogger(t *testing.T) {
  64. containerRoot, err := ioutil.TempDir("", "TestContainerLogPathSetForJSONFileLogger")
  65. assert.NilError(t, err)
  66. defer os.RemoveAll(containerRoot)
  67. c := &Container{
  68. Config: &container.Config{},
  69. HostConfig: &container.HostConfig{
  70. LogConfig: container.LogConfig{
  71. Type: jsonfilelog.Name,
  72. },
  73. },
  74. ID: "TestContainerLogPathSetForJSONFileLogger",
  75. Root: containerRoot,
  76. }
  77. logger, err := c.StartLogger()
  78. assert.NilError(t, err)
  79. defer logger.Close()
  80. expectedLogPath, err := filepath.Abs(filepath.Join(containerRoot, fmt.Sprintf("%s-json.log", c.ID)))
  81. assert.NilError(t, err)
  82. assert.Equal(t, c.LogPath, expectedLogPath)
  83. }
  84. func TestContainerLogPathSetForRingLogger(t *testing.T) {
  85. containerRoot, err := ioutil.TempDir("", "TestContainerLogPathSetForRingLogger")
  86. assert.NilError(t, err)
  87. defer os.RemoveAll(containerRoot)
  88. c := &Container{
  89. Config: &container.Config{},
  90. HostConfig: &container.HostConfig{
  91. LogConfig: container.LogConfig{
  92. Type: jsonfilelog.Name,
  93. Config: map[string]string{
  94. "mode": string(container.LogModeNonBlock),
  95. },
  96. },
  97. },
  98. ID: "TestContainerLogPathSetForRingLogger",
  99. Root: containerRoot,
  100. }
  101. logger, err := c.StartLogger()
  102. assert.NilError(t, err)
  103. defer logger.Close()
  104. expectedLogPath, err := filepath.Abs(filepath.Join(containerRoot, fmt.Sprintf("%s-json.log", c.ID)))
  105. assert.NilError(t, err)
  106. assert.Equal(t, c.LogPath, expectedLogPath)
  107. }