validate_test.go 3.6 KB

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