container_unit_test.go 2.9 KB

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