volume_test.py 4.2 KB

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