validate_test.go 3.0 KB

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