volume_test.go 4.0 KB

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