volume_test.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. from __future__ import absolute_import
  2. from __future__ import unicode_literals
  3. import six
  4. from docker.errors import DockerException
  5. from .testcases import DockerClientTestCase
  6. from .testcases import no_cluster
  7. from compose.const import LABEL_PROJECT
  8. from compose.const import LABEL_VOLUME
  9. from compose.volume import Volume
  10. class VolumeTest(DockerClientTestCase):
  11. def setUp(self):
  12. self.tmp_volumes = []
  13. def tearDown(self):
  14. for volume in self.tmp_volumes:
  15. try:
  16. self.client.remove_volume(volume.full_name)
  17. except DockerException:
  18. pass
  19. del self.tmp_volumes
  20. super(VolumeTest, self).tearDown()
  21. def create_volume(self, name, driver=None, opts=None, external=None, custom_name=False):
  22. if external:
  23. custom_name = True
  24. if isinstance(external, six.text_type):
  25. name = external
  26. vol = Volume(
  27. self.client, 'composetest', name, driver=driver, driver_opts=opts,
  28. external=bool(external), custom_name=custom_name
  29. )
  30. self.tmp_volumes.append(vol)
  31. return vol
  32. def test_create_volume(self):
  33. vol = self.create_volume('volume01')
  34. vol.create()
  35. info = self.get_volume_data(vol.full_name)
  36. assert info['Name'].split('/')[-1] == vol.full_name
  37. def test_create_volume_custom_name(self):
  38. vol = self.create_volume('volume01', custom_name=True)
  39. assert vol.name == vol.full_name
  40. vol.create()
  41. info = self.get_volume_data(vol.full_name)
  42. assert info['Name'].split('/')[-1] == vol.name
  43. def test_recreate_existing_volume(self):
  44. vol = self.create_volume('volume01')
  45. vol.create()
  46. info = self.get_volume_data(vol.full_name)
  47. assert info['Name'].split('/')[-1] == vol.full_name
  48. vol.create()
  49. info = self.get_volume_data(vol.full_name)
  50. assert info['Name'].split('/')[-1] == vol.full_name
  51. @no_cluster('inspect volume by name defect on Swarm Classic')
  52. def test_inspect_volume(self):
  53. vol = self.create_volume('volume01')
  54. vol.create()
  55. info = vol.inspect()
  56. assert info['Name'] == vol.full_name
  57. @no_cluster('remove volume by name defect on Swarm Classic')
  58. def test_remove_volume(self):
  59. vol = Volume(self.client, 'composetest', 'volume01')
  60. vol.create()
  61. vol.remove()
  62. volumes = self.client.volumes()['Volumes']
  63. assert len([v for v in volumes if v['Name'] == vol.full_name]) == 0
  64. @no_cluster('inspect volume by name defect on Swarm Classic')
  65. def test_external_volume(self):
  66. vol = self.create_volume('composetest_volume_ext', external=True)
  67. assert vol.external is True
  68. assert vol.full_name == vol.name
  69. vol.create()
  70. info = vol.inspect()
  71. assert info['Name'] == vol.name
  72. @no_cluster('inspect volume by name defect on Swarm Classic')
  73. def test_external_aliased_volume(self):
  74. alias_name = 'composetest_alias01'
  75. vol = self.create_volume('volume01', external=alias_name)
  76. assert vol.external is True
  77. assert vol.full_name == alias_name
  78. vol.create()
  79. info = vol.inspect()
  80. assert info['Name'] == alias_name
  81. @no_cluster('inspect volume by name defect on Swarm Classic')
  82. def test_exists(self):
  83. vol = self.create_volume('volume01')
  84. assert vol.exists() is False
  85. vol.create()
  86. assert vol.exists() is True
  87. @no_cluster('inspect volume by name defect on Swarm Classic')
  88. def test_exists_external(self):
  89. vol = self.create_volume('volume01', external=True)
  90. assert vol.exists() is False
  91. vol.create()
  92. assert vol.exists() is True
  93. @no_cluster('inspect volume by name defect on Swarm Classic')
  94. def test_exists_external_aliased(self):
  95. vol = self.create_volume('volume01', external='composetest_alias01')
  96. assert vol.exists() is False
  97. vol.create()
  98. assert vol.exists() is True
  99. @no_cluster('inspect volume by name defect on Swarm Classic')
  100. def test_volume_default_labels(self):
  101. vol = self.create_volume('volume01')
  102. vol.create()
  103. vol_data = vol.inspect()
  104. labels = vol_data['Labels']
  105. assert labels[LABEL_VOLUME] == vol.name
  106. assert labels[LABEL_PROJECT] == vol.project