validate_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package container // import "github.com/docker/docker/daemon/cluster/executor/container"
  2. import (
  3. "os"
  4. "strings"
  5. "testing"
  6. "github.com/docker/docker/daemon"
  7. "github.com/docker/docker/pkg/stringid"
  8. "github.com/moby/swarmkit/v2/api"
  9. )
  10. func newTestControllerWithMount(m api.Mount) (*controller, error) {
  11. return newController(&daemon.Daemon{}, nil, nil, &api.Task{
  12. ID: stringid.GenerateRandomID(),
  13. ServiceID: stringid.GenerateRandomID(),
  14. Spec: api.TaskSpec{
  15. Runtime: &api.TaskSpec_Container{
  16. Container: &api.ContainerSpec{
  17. Image: "image_name",
  18. Labels: map[string]string{
  19. "com.docker.swarm.task.id": "id",
  20. },
  21. Mounts: []api.Mount{m},
  22. },
  23. },
  24. },
  25. }, nil,
  26. nil)
  27. }
  28. func TestControllerValidateMountBind(t *testing.T) {
  29. // with improper source
  30. if _, err := newTestControllerWithMount(api.Mount{
  31. Type: api.MountTypeBind,
  32. Source: "foo",
  33. Target: testAbsPath,
  34. }); err == nil || !strings.Contains(err.Error(), "invalid bind mount source") {
  35. t.Fatalf("expected error, got: %v", err)
  36. }
  37. // with non-existing source
  38. if _, err := newTestControllerWithMount(api.Mount{
  39. Type: api.MountTypeBind,
  40. Source: testAbsNonExistent,
  41. Target: testAbsPath,
  42. }); err != nil {
  43. t.Fatalf("controller should not error at creation: %v", err)
  44. }
  45. // with proper source
  46. tmpdir, err := os.MkdirTemp("", "TestControllerValidateMountBind")
  47. if err != nil {
  48. t.Fatalf("failed to create temp dir: %v", err)
  49. }
  50. defer os.Remove(tmpdir)
  51. if _, err := newTestControllerWithMount(api.Mount{
  52. Type: api.MountTypeBind,
  53. Source: tmpdir,
  54. Target: testAbsPath,
  55. }); err != nil {
  56. t.Fatalf("expected error, got: %v", err)
  57. }
  58. }
  59. func TestControllerValidateMountVolume(t *testing.T) {
  60. // with improper source
  61. if _, err := newTestControllerWithMount(api.Mount{
  62. Type: api.MountTypeVolume,
  63. Source: testAbsPath,
  64. Target: testAbsPath,
  65. }); err == nil || !strings.Contains(err.Error(), "invalid volume mount source") {
  66. t.Fatalf("expected error, got: %v", err)
  67. }
  68. // with proper source
  69. if _, err := newTestControllerWithMount(api.Mount{
  70. Type: api.MountTypeVolume,
  71. Source: "foo",
  72. Target: testAbsPath,
  73. }); err != nil {
  74. t.Fatalf("expected error, got: %v", err)
  75. }
  76. }
  77. func TestControllerValidateMountTarget(t *testing.T) {
  78. tmpdir, err := os.MkdirTemp("", "TestControllerValidateMountTarget")
  79. if err != nil {
  80. t.Fatalf("failed to create temp dir: %v", err)
  81. }
  82. defer os.Remove(tmpdir)
  83. // with improper target
  84. if _, err := newTestControllerWithMount(api.Mount{
  85. Type: api.MountTypeBind,
  86. Source: testAbsPath,
  87. Target: "foo",
  88. }); err == nil || !strings.Contains(err.Error(), "invalid mount target") {
  89. t.Fatalf("expected error, got: %v", err)
  90. }
  91. // with proper target
  92. if _, err := newTestControllerWithMount(api.Mount{
  93. Type: api.MountTypeBind,
  94. Source: tmpdir,
  95. Target: testAbsPath,
  96. }); err != nil {
  97. t.Fatalf("expected no error, got: %v", err)
  98. }
  99. }
  100. func TestControllerValidateMountTmpfs(t *testing.T) {
  101. // with improper target
  102. if _, err := newTestControllerWithMount(api.Mount{
  103. Type: api.MountTypeTmpfs,
  104. Source: "foo",
  105. Target: testAbsPath,
  106. }); err == nil || !strings.Contains(err.Error(), "invalid tmpfs source") {
  107. t.Fatalf("expected error, got: %v", err)
  108. }
  109. // with proper target
  110. if _, err := newTestControllerWithMount(api.Mount{
  111. Type: api.MountTypeTmpfs,
  112. Target: testAbsPath,
  113. }); err != nil {
  114. t.Fatalf("expected no error, got: %v", err)
  115. }
  116. }
  117. func TestControllerValidateMountInvalidType(t *testing.T) {
  118. // with improper target
  119. if _, err := newTestControllerWithMount(api.Mount{
  120. Type: api.Mount_MountType(9999),
  121. Source: "foo",
  122. Target: testAbsPath,
  123. }); err == nil || !strings.Contains(err.Error(), "invalid mount type") {
  124. t.Fatalf("expected error, got: %v", err)
  125. }
  126. }