volume_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package loader
  2. import (
  3. "testing"
  4. "github.com/docker/docker/cli/compose/types"
  5. "github.com/docker/docker/pkg/testutil/assert"
  6. )
  7. func TestParseVolumeAnonymousVolume(t *testing.T) {
  8. for _, path := range []string{"/path", "/path/foo"} {
  9. volume, err := parseVolume(path)
  10. expected := types.ServiceVolumeConfig{Type: "volume", Target: path}
  11. assert.NilError(t, err)
  12. assert.DeepEqual(t, volume, expected)
  13. }
  14. }
  15. func TestParseVolumeAnonymousVolumeWindows(t *testing.T) {
  16. for _, path := range []string{"C:\\path", "Z:\\path\\foo"} {
  17. volume, err := parseVolume(path)
  18. expected := types.ServiceVolumeConfig{Type: "volume", Target: path}
  19. assert.NilError(t, err)
  20. assert.DeepEqual(t, volume, expected)
  21. }
  22. }
  23. func TestParseVolumeTooManyColons(t *testing.T) {
  24. _, err := parseVolume("/foo:/foo:ro:foo")
  25. assert.Error(t, err, "too many colons")
  26. }
  27. func TestParseVolumeShortVolumes(t *testing.T) {
  28. for _, path := range []string{".", "/a"} {
  29. volume, err := parseVolume(path)
  30. expected := types.ServiceVolumeConfig{Type: "volume", Target: path}
  31. assert.NilError(t, err)
  32. assert.DeepEqual(t, volume, expected)
  33. }
  34. }
  35. func TestParseVolumeMissingSource(t *testing.T) {
  36. for _, spec := range []string{":foo", "/foo::ro"} {
  37. _, err := parseVolume(spec)
  38. assert.Error(t, err, "empty section between colons")
  39. }
  40. }
  41. func TestParseVolumeBindMount(t *testing.T) {
  42. for _, path := range []string{"./foo", "~/thing", "../other", "/foo", "/home/user"} {
  43. volume, err := parseVolume(path + ":/target")
  44. expected := types.ServiceVolumeConfig{
  45. Type: "bind",
  46. Source: path,
  47. Target: "/target",
  48. }
  49. assert.NilError(t, err)
  50. assert.DeepEqual(t, volume, expected)
  51. }
  52. }
  53. func TestParseVolumeRelativeBindMountWindows(t *testing.T) {
  54. for _, path := range []string{
  55. "./foo",
  56. "~/thing",
  57. "../other",
  58. "D:\\path", "/home/user",
  59. } {
  60. volume, err := parseVolume(path + ":d:\\target")
  61. expected := types.ServiceVolumeConfig{
  62. Type: "bind",
  63. Source: path,
  64. Target: "d:\\target",
  65. }
  66. assert.NilError(t, err)
  67. assert.DeepEqual(t, volume, expected)
  68. }
  69. }
  70. func TestParseVolumeWithBindOptions(t *testing.T) {
  71. volume, err := parseVolume("/source:/target:slave")
  72. expected := types.ServiceVolumeConfig{
  73. Type: "bind",
  74. Source: "/source",
  75. Target: "/target",
  76. Bind: &types.ServiceVolumeBind{Propagation: "slave"},
  77. }
  78. assert.NilError(t, err)
  79. assert.DeepEqual(t, volume, expected)
  80. }
  81. func TestParseVolumeWithBindOptionsWindows(t *testing.T) {
  82. volume, err := parseVolume("C:\\source\\foo:D:\\target:ro,rprivate")
  83. expected := types.ServiceVolumeConfig{
  84. Type: "bind",
  85. Source: "C:\\source\\foo",
  86. Target: "D:\\target",
  87. ReadOnly: true,
  88. Bind: &types.ServiceVolumeBind{Propagation: "rprivate"},
  89. }
  90. assert.NilError(t, err)
  91. assert.DeepEqual(t, volume, expected)
  92. }
  93. func TestParseVolumeWithInvalidVolumeOptions(t *testing.T) {
  94. _, err := parseVolume("name:/target:bogus")
  95. assert.Error(t, err, "invalid spec: name:/target:bogus: unknown option: bogus")
  96. }
  97. func TestParseVolumeWithVolumeOptions(t *testing.T) {
  98. volume, err := parseVolume("name:/target:nocopy")
  99. expected := types.ServiceVolumeConfig{
  100. Type: "volume",
  101. Source: "name",
  102. Target: "/target",
  103. Volume: &types.ServiceVolumeVolume{NoCopy: true},
  104. }
  105. assert.NilError(t, err)
  106. assert.DeepEqual(t, volume, expected)
  107. }
  108. func TestParseVolumeWithReadOnly(t *testing.T) {
  109. for _, path := range []string{"./foo", "/home/user"} {
  110. volume, err := parseVolume(path + ":/target:ro")
  111. expected := types.ServiceVolumeConfig{
  112. Type: "bind",
  113. Source: path,
  114. Target: "/target",
  115. ReadOnly: true,
  116. }
  117. assert.NilError(t, err)
  118. assert.DeepEqual(t, volume, expected)
  119. }
  120. }
  121. func TestParseVolumeWithRW(t *testing.T) {
  122. for _, path := range []string{"./foo", "/home/user"} {
  123. volume, err := parseVolume(path + ":/target:rw")
  124. expected := types.ServiceVolumeConfig{
  125. Type: "bind",
  126. Source: path,
  127. Target: "/target",
  128. ReadOnly: false,
  129. }
  130. assert.NilError(t, err)
  131. assert.DeepEqual(t, volume, expected)
  132. }
  133. }