volume_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package convert
  2. import (
  3. "testing"
  4. volumetypes "github.com/docker/docker/api/types/volume"
  5. swarmapi "github.com/moby/swarmkit/v2/api"
  6. "gotest.tools/v3/assert"
  7. )
  8. func TestTopologyFromGRPC(t *testing.T) {
  9. nilTopology := topologyFromGRPC(nil)
  10. assert.DeepEqual(t, nilTopology, volumetypes.Topology{})
  11. swarmTop := &swarmapi.Topology{
  12. Segments: map[string]string{"foo": "bar"},
  13. }
  14. top := topologyFromGRPC(swarmTop)
  15. assert.DeepEqual(t, top.Segments, swarmTop.Segments)
  16. }
  17. func TestCapacityRangeFromGRPC(t *testing.T) {
  18. nilCapacity := capacityRangeFromGRPC(nil)
  19. assert.Assert(t, nilCapacity == nil)
  20. swarmZeroCapacity := &swarmapi.CapacityRange{}
  21. zeroCapacity := capacityRangeFromGRPC(swarmZeroCapacity)
  22. assert.Assert(t, zeroCapacity != nil)
  23. assert.Equal(t, zeroCapacity.RequiredBytes, int64(0))
  24. assert.Equal(t, zeroCapacity.LimitBytes, int64(0))
  25. swarmNonZeroCapacity := &swarmapi.CapacityRange{
  26. RequiredBytes: 1024,
  27. LimitBytes: 2048,
  28. }
  29. nonZeroCapacity := capacityRangeFromGRPC(swarmNonZeroCapacity)
  30. assert.Assert(t, nonZeroCapacity != nil)
  31. assert.Equal(t, nonZeroCapacity.RequiredBytes, int64(1024))
  32. assert.Equal(t, nonZeroCapacity.LimitBytes, int64(2048))
  33. }
  34. func TestVolumeAvailabilityFromGRPC(t *testing.T) {
  35. for _, tc := range []struct {
  36. name string
  37. in swarmapi.VolumeSpec_VolumeAvailability
  38. expected volumetypes.Availability
  39. }{
  40. {
  41. name: "Active",
  42. in: swarmapi.VolumeAvailabilityActive,
  43. expected: volumetypes.AvailabilityActive,
  44. }, {
  45. name: "Pause",
  46. in: swarmapi.VolumeAvailabilityPause,
  47. expected: volumetypes.AvailabilityPause,
  48. }, {
  49. name: "Drain",
  50. in: swarmapi.VolumeAvailabilityDrain,
  51. expected: volumetypes.AvailabilityDrain,
  52. },
  53. } {
  54. tc := tc
  55. t.Run(tc.name, func(t *testing.T) {
  56. actual := volumeAvailabilityFromGRPC(tc.in)
  57. assert.Equal(t, actual, tc.expected)
  58. })
  59. }
  60. }
  61. // TestAccessModeFromGRPC tests that the AccessMode type is correctly converted
  62. func TestAccessModeFromGRPC(t *testing.T) {
  63. for _, tc := range []struct {
  64. name string
  65. in *swarmapi.VolumeAccessMode
  66. expected *volumetypes.AccessMode
  67. }{
  68. {
  69. name: "MountVolume",
  70. in: &swarmapi.VolumeAccessMode{
  71. Scope: swarmapi.VolumeScopeSingleNode,
  72. Sharing: swarmapi.VolumeSharingNone,
  73. AccessType: &swarmapi.VolumeAccessMode_Mount{
  74. Mount: &swarmapi.VolumeAccessMode_MountVolume{
  75. FsType: "foo",
  76. // TODO(dperny): maybe don't convert this?
  77. MountFlags: []string{"one", "two"},
  78. },
  79. },
  80. },
  81. expected: &volumetypes.AccessMode{
  82. Scope: volumetypes.ScopeSingleNode,
  83. Sharing: volumetypes.SharingNone,
  84. MountVolume: &volumetypes.TypeMount{
  85. FsType: "foo",
  86. MountFlags: []string{"one", "two"},
  87. },
  88. },
  89. }, {
  90. name: "BlockVolume",
  91. in: &swarmapi.VolumeAccessMode{
  92. Scope: swarmapi.VolumeScopeSingleNode,
  93. Sharing: swarmapi.VolumeSharingNone,
  94. AccessType: &swarmapi.VolumeAccessMode_Block{
  95. Block: &swarmapi.VolumeAccessMode_BlockVolume{},
  96. },
  97. },
  98. expected: &volumetypes.AccessMode{
  99. Scope: volumetypes.ScopeSingleNode,
  100. Sharing: volumetypes.SharingNone,
  101. BlockVolume: &volumetypes.TypeBlock{},
  102. },
  103. },
  104. } {
  105. tc := tc
  106. t.Run(tc.name, func(t *testing.T) {
  107. out := accessModeFromGRPC(tc.in)
  108. assert.DeepEqual(t, tc.expected, out)
  109. })
  110. }
  111. }
  112. // TestVolumeCreateToGRPC tests that a docker-typed VolumeCreateBody is
  113. // correctly converted to a swarm-typed VolumeSpec.
  114. func TestVolumeCreateToGRPC(t *testing.T) {
  115. volume := &volumetypes.CreateOptions{
  116. Driver: "plug1",
  117. DriverOpts: map[string]string{"options": "yeah"},
  118. Labels: map[string]string{"labeled": "yeah"},
  119. Name: "volume1",
  120. }
  121. spec := &volumetypes.ClusterVolumeSpec{
  122. Group: "gronp",
  123. AccessMode: &volumetypes.AccessMode{
  124. Scope: volumetypes.ScopeMultiNode,
  125. Sharing: volumetypes.SharingAll,
  126. MountVolume: &volumetypes.TypeMount{
  127. FsType: "foo",
  128. MountFlags: []string{"one", "two"},
  129. },
  130. },
  131. Secrets: []volumetypes.Secret{
  132. {Key: "key1", Secret: "secret1"},
  133. {Key: "key2", Secret: "secret2"},
  134. },
  135. AccessibilityRequirements: &volumetypes.TopologyRequirement{
  136. Requisite: []volumetypes.Topology{
  137. {Segments: map[string]string{"top1": "yup"}},
  138. {Segments: map[string]string{"top2": "def"}},
  139. {Segments: map[string]string{"top3": "nah"}},
  140. },
  141. Preferred: []volumetypes.Topology{},
  142. },
  143. CapacityRange: &volumetypes.CapacityRange{
  144. RequiredBytes: 1,
  145. LimitBytes: 0,
  146. },
  147. }
  148. volume.ClusterVolumeSpec = spec
  149. swarmSpec := VolumeCreateToGRPC(volume)
  150. assert.Assert(t, swarmSpec != nil)
  151. expectedSwarmSpec := &swarmapi.VolumeSpec{
  152. Annotations: swarmapi.Annotations{
  153. Name: "volume1",
  154. Labels: map[string]string{
  155. "labeled": "yeah",
  156. },
  157. },
  158. Group: "gronp",
  159. Driver: &swarmapi.Driver{
  160. Name: "plug1",
  161. Options: map[string]string{
  162. "options": "yeah",
  163. },
  164. },
  165. AccessMode: &swarmapi.VolumeAccessMode{
  166. Scope: swarmapi.VolumeScopeMultiNode,
  167. Sharing: swarmapi.VolumeSharingAll,
  168. AccessType: &swarmapi.VolumeAccessMode_Mount{
  169. Mount: &swarmapi.VolumeAccessMode_MountVolume{
  170. FsType: "foo",
  171. MountFlags: []string{"one", "two"},
  172. },
  173. },
  174. },
  175. Secrets: []*swarmapi.VolumeSecret{
  176. {Key: "key1", Secret: "secret1"},
  177. {Key: "key2", Secret: "secret2"},
  178. },
  179. AccessibilityRequirements: &swarmapi.TopologyRequirement{
  180. Requisite: []*swarmapi.Topology{
  181. {Segments: map[string]string{"top1": "yup"}},
  182. {Segments: map[string]string{"top2": "def"}},
  183. {Segments: map[string]string{"top3": "nah"}},
  184. },
  185. Preferred: nil,
  186. },
  187. CapacityRange: &swarmapi.CapacityRange{
  188. RequiredBytes: 1,
  189. LimitBytes: 0,
  190. },
  191. }
  192. assert.DeepEqual(t, swarmSpec, expectedSwarmSpec)
  193. }