volume_test.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from __future__ import absolute_import
  2. from __future__ import unicode_literals
  3. from docker.errors import DockerException
  4. from .testcases import DockerClientTestCase
  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):
  20. if external and isinstance(external, bool):
  21. external = name
  22. vol = Volume(
  23. self.client, 'composetest', name, driver=driver, driver_opts=opts,
  24. external_name=external
  25. )
  26. self.tmp_volumes.append(vol)
  27. return vol
  28. def test_create_volume(self):
  29. vol = self.create_volume('volume01')
  30. vol.create()
  31. info = self.client.inspect_volume(vol.full_name)
  32. assert info['Name'] == vol.full_name
  33. def test_recreate_existing_volume(self):
  34. vol = self.create_volume('volume01')
  35. vol.create()
  36. info = self.client.inspect_volume(vol.full_name)
  37. assert info['Name'] == vol.full_name
  38. vol.create()
  39. info = self.client.inspect_volume(vol.full_name)
  40. assert info['Name'] == vol.full_name
  41. def test_inspect_volume(self):
  42. vol = self.create_volume('volume01')
  43. vol.create()
  44. info = vol.inspect()
  45. assert info['Name'] == vol.full_name
  46. def test_remove_volume(self):
  47. vol = Volume(self.client, 'composetest', 'volume01')
  48. vol.create()
  49. vol.remove()
  50. volumes = self.client.volumes()['Volumes']
  51. assert len([v for v in volumes if v['Name'] == vol.full_name]) == 0
  52. def test_external_volume(self):
  53. vol = self.create_volume('composetest_volume_ext', external=True)
  54. assert vol.external is True
  55. assert vol.full_name == vol.name
  56. vol.create()
  57. info = vol.inspect()
  58. assert info['Name'] == vol.name
  59. def test_external_aliased_volume(self):
  60. alias_name = 'composetest_alias01'
  61. vol = self.create_volume('volume01', external=alias_name)
  62. assert vol.external is True
  63. assert vol.full_name == alias_name
  64. vol.create()
  65. info = vol.inspect()
  66. assert info['Name'] == alias_name
  67. def test_exists(self):
  68. vol = self.create_volume('volume01')
  69. assert vol.exists() is False
  70. vol.create()
  71. assert vol.exists() is True
  72. def test_exists_external(self):
  73. vol = self.create_volume('volume01', external=True)
  74. assert vol.exists() is False
  75. vol.create()
  76. assert vol.exists() is True
  77. def test_exists_external_aliased(self):
  78. vol = self.create_volume('volume01', external='composetest_alias01')
  79. assert vol.exists() is False
  80. vol.create()
  81. assert vol.exists() is True
  82. def test_volume_default_labels(self):
  83. vol = self.create_volume('volume01')
  84. vol.create()
  85. vol_data = vol.inspect()
  86. labels = vol_data['Labels']
  87. assert labels[LABEL_VOLUME] == vol.name
  88. assert labels[LABEL_PROJECT] == vol.project