test_docker_volume.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Unit tests for the docker state
  4. '''
  5. # Import Python Libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Testing Libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.unit import TestCase
  10. from tests.support.mock import (
  11. Mock,
  12. patch
  13. )
  14. # Import Salt Libs
  15. import salt.modules.dockermod as docker_mod
  16. import salt.states.docker_volume as docker_state
  17. class DockerVolumeTestCase(TestCase, LoaderModuleMockMixin):
  18. '''
  19. Test docker_volume states
  20. '''
  21. def setup_loader_modules(self):
  22. return {
  23. docker_mod: {
  24. '__context__': {'docker.docker_version': ''}
  25. },
  26. docker_state: {
  27. '__opts__': {'test': False}
  28. }
  29. }
  30. def test_present(self):
  31. '''
  32. Test docker_volume.present
  33. '''
  34. volumes = []
  35. default_driver = 'dummy_default'
  36. def create_volume(name, driver=None, driver_opts=None):
  37. for v in volumes:
  38. # present should never try to add a conflicting
  39. # volume
  40. self.assertNotEqual(v['Name'], name)
  41. if driver is None:
  42. driver = default_driver
  43. new = {'Name': name, 'Driver': driver}
  44. volumes.append(new)
  45. return new
  46. def remove_volume(name):
  47. old_len = len(volumes)
  48. removed = [v for v in volumes if v['Name'] == name]
  49. # present should not have tried to remove a volume
  50. # that didn't exist
  51. self.assertEqual(1, len(removed))
  52. volumes.remove(removed[0])
  53. return removed[0]
  54. docker_create_volume = Mock(side_effect=create_volume)
  55. __salt__ = {
  56. 'docker.create_volume': docker_create_volume,
  57. 'docker.volumes': Mock(return_value={'Volumes': volumes}),
  58. 'docker.remove_volume': Mock(side_effect=remove_volume)
  59. }
  60. with patch.dict(docker_state.__dict__, {'__salt__': __salt__}):
  61. ret = docker_state.present('volume_foo')
  62. docker_create_volume.assert_called_with('volume_foo',
  63. driver=None,
  64. driver_opts=None)
  65. self.assertEqual(
  66. {
  67. 'name': 'volume_foo',
  68. 'comment': '',
  69. 'changes': {
  70. 'created': {
  71. 'Driver': default_driver,
  72. 'Name': 'volume_foo',
  73. },
  74. },
  75. 'result': True,
  76. },
  77. ret)
  78. self.assertEqual(len(volumes), 1)
  79. self.assertEqual(volumes[0]['Name'], 'volume_foo')
  80. self.assertIs(volumes[0]['Driver'], default_driver)
  81. # run it again with the same arguments
  82. orig_volumes = [volumes[0].copy()]
  83. ret = docker_state.present('volume_foo')
  84. self.assertEqual(
  85. {
  86. 'name': 'volume_foo',
  87. 'comment': "Volume 'volume_foo' already exists.",
  88. 'changes': {},
  89. 'result': True,
  90. },
  91. ret)
  92. self.assertEqual(orig_volumes, volumes)
  93. # run it again with a different driver but don't force
  94. ret = docker_state.present('volume_foo', driver='local')
  95. self.assertEqual(
  96. {
  97. 'name': 'volume_foo',
  98. 'comment': ("Driver for existing volume 'volume_foo'"
  99. " ('dummy_default') does not match specified"
  100. " driver ('local') and force is False"),
  101. 'changes': {},
  102. 'result': False,
  103. },
  104. ret)
  105. self.assertEqual(orig_volumes, volumes)
  106. # run it again with a different driver and force
  107. ret = docker_state.present(
  108. 'volume_foo', driver='local', force=True)
  109. self.assertEqual(
  110. {
  111. 'name': 'volume_foo',
  112. 'comment': "",
  113. 'changes': {
  114. 'removed': {
  115. 'Driver': default_driver,
  116. 'Name': 'volume_foo',
  117. },
  118. 'created': {
  119. 'Driver': 'local',
  120. 'Name': 'volume_foo',
  121. },
  122. },
  123. 'result': True,
  124. },
  125. ret)
  126. mod_orig_volumes = [orig_volumes[0].copy()]
  127. mod_orig_volumes[0]['Driver'] = 'local'
  128. self.assertEqual(mod_orig_volumes, volumes)
  129. def test_present_with_another_driver(self):
  130. '''
  131. Test docker_volume.present
  132. '''
  133. docker_create_volume = Mock(return_value='created')
  134. docker_remove_volume = Mock(return_value='removed')
  135. __salt__ = {'docker.create_volume': docker_create_volume,
  136. 'docker.remove_volume': docker_remove_volume,
  137. 'docker.volumes': Mock(return_value={
  138. 'Volumes': [{'Name': 'volume_foo',
  139. 'Driver': 'foo'}]}),
  140. }
  141. with patch.dict(docker_state.__dict__,
  142. {'__salt__': __salt__}):
  143. ret = docker_state.present(
  144. 'volume_foo',
  145. driver='bar',
  146. force=True,
  147. )
  148. docker_remove_volume.assert_called_with('volume_foo')
  149. docker_create_volume.assert_called_with('volume_foo',
  150. driver='bar',
  151. driver_opts=None)
  152. self.assertEqual(ret, {'name': 'volume_foo',
  153. 'comment': '',
  154. 'changes': {'created': 'created',
  155. 'removed': 'removed'},
  156. 'result': True})
  157. def test_present_wo_existing_volumes(self):
  158. '''
  159. Test docker_volume.present without existing volumes.
  160. '''
  161. docker_create_volume = Mock(return_value='created')
  162. docker_remove_volume = Mock(return_value='removed')
  163. __salt__ = {'docker.create_volume': docker_create_volume,
  164. 'docker.remove_volume': docker_remove_volume,
  165. 'docker.volumes': Mock(return_value={'Volumes': None}),
  166. }
  167. with patch.dict(docker_state.__dict__,
  168. {'__salt__': __salt__}):
  169. ret = docker_state.present(
  170. 'volume_foo',
  171. driver='bar',
  172. force=True,
  173. )
  174. docker_create_volume.assert_called_with('volume_foo',
  175. driver='bar',
  176. driver_opts=None)
  177. self.assertEqual(ret, {'name': 'volume_foo',
  178. 'comment': '',
  179. 'changes': {'created': 'created'},
  180. 'result': True})
  181. def test_absent(self):
  182. '''
  183. Test docker_volume.absent
  184. '''
  185. docker_remove_volume = Mock(return_value='removed')
  186. __salt__ = {'docker.remove_volume': docker_remove_volume,
  187. 'docker.volumes': Mock(return_value={
  188. 'Volumes': [{'Name': 'volume_foo'}]}),
  189. }
  190. with patch.dict(docker_state.__dict__,
  191. {'__salt__': __salt__}):
  192. ret = docker_state.absent(
  193. 'volume_foo',
  194. )
  195. docker_remove_volume.assert_called_with('volume_foo')
  196. self.assertEqual(ret, {'name': 'volume_foo',
  197. 'comment': '',
  198. 'changes': {'removed': 'removed'},
  199. 'result': True})