Merge pull request #2647 from shin-/preexisting_volume_detection_rb1

Support for external volumes
This commit is contained in:
Joffrey F 2016-01-13 17:02:36 -08:00
commit 153185eadb
7 changed files with 188 additions and 17 deletions

View file

@ -4,6 +4,7 @@ from __future__ import unicode_literals
import random
import py
from docker.errors import NotFound
from .testcases import DockerClientTestCase
from compose.config import config
@ -624,7 +625,7 @@ class ProjectTest(DockerClientTestCase):
self.assertEqual(volume_data['Name'], full_vol_name)
self.assertEqual(volume_data['Driver'], 'local')
def test_project_up_invalid_volume_driver(self):
def test_initialize_volumes_invalid_volume_driver(self):
vol_name = '{0:x}'.format(random.getrandbits(32))
config_data = config.Config(
@ -642,7 +643,7 @@ class ProjectTest(DockerClientTestCase):
with self.assertRaises(config.ConfigurationError):
project.initialize_volumes()
def test_project_up_updated_driver(self):
def test_initialize_volumes_updated_driver(self):
vol_name = '{0:x}'.format(random.getrandbits(32))
full_vol_name = 'composetest_{0}'.format(vol_name)
@ -675,3 +676,48 @@ class ProjectTest(DockerClientTestCase):
assert 'Configuration for volume {0} specifies driver smb'.format(
vol_name
) in str(e.exception)
def test_initialize_volumes_external_volumes(self):
# Use composetest_ prefix so it gets garbage-collected in tearDown()
vol_name = 'composetest_{0:x}'.format(random.getrandbits(32))
full_vol_name = 'composetest_{0}'.format(vol_name)
self.client.create_volume(vol_name)
config_data = config.Config(
version=2, services=[{
'name': 'web',
'image': 'busybox:latest',
'command': 'top'
}], volumes={
vol_name: {'external': True, 'external_name': vol_name}
}
)
project = Project.from_config(
name='composetest',
config_data=config_data, client=self.client
)
project.initialize_volumes()
with self.assertRaises(NotFound):
self.client.inspect_volume(full_vol_name)
def test_initialize_volumes_inexistent_external_volume(self):
vol_name = '{0:x}'.format(random.getrandbits(32))
config_data = config.Config(
version=2, services=[{
'name': 'web',
'image': 'busybox:latest',
'command': 'top'
}], volumes={
vol_name: {'external': True, 'external_name': vol_name}
}
)
project = Project.from_config(
name='composetest',
config_data=config_data, client=self.client
)
with self.assertRaises(config.ConfigurationError) as e:
project.initialize_volumes()
assert 'Volume {0} declared as external'.format(
vol_name
) in str(e.exception)

View file

@ -18,9 +18,12 @@ class VolumeTest(DockerClientTestCase):
except DockerException:
pass
def create_volume(self, name, driver=None, opts=None):
def create_volume(self, name, driver=None, opts=None, external=None):
if external and isinstance(external, bool):
external = name
vol = Volume(
self.client, 'composetest', name, driver=driver, driver_opts=opts
self.client, 'composetest', name, driver=driver, driver_opts=opts,
external_name=external
)
self.tmp_volumes.append(vol)
return vol
@ -54,3 +57,38 @@ class VolumeTest(DockerClientTestCase):
vol.remove()
volumes = self.client.volumes()['Volumes']
assert len([v for v in volumes if v['Name'] == vol.full_name]) == 0
def test_external_volume(self):
vol = self.create_volume('composetest_volume_ext', external=True)
assert vol.external is True
assert vol.full_name == vol.name
vol.create()
info = vol.inspect()
assert info['Name'] == vol.name
def test_external_aliased_volume(self):
alias_name = 'composetest_alias01'
vol = self.create_volume('volume01', external=alias_name)
assert vol.external is True
assert vol.full_name == alias_name
vol.create()
info = vol.inspect()
assert info['Name'] == alias_name
def test_exists(self):
vol = self.create_volume('volume01')
assert vol.exists() is False
vol.create()
assert vol.exists() is True
def test_exists_external(self):
vol = self.create_volume('volume01', external=True)
assert vol.exists() is False
vol.create()
assert vol.exists() is True
def test_exists_external_aliased(self):
vol = self.create_volume('volume01', external='composetest_alias01')
assert vol.exists() is False
vol.create()
assert vol.exists() is True

View file

@ -775,6 +775,37 @@ class ConfigTest(unittest.TestCase):
'extends': {'service': 'foo'}
}
def test_external_volume_config(self):
config_details = build_config_details({
'version': 2,
'services': {
'bogus': {'image': 'busybox'}
},
'volumes': {
'ext': {'external': True},
'ext2': {'external': {'name': 'aliased'}}
}
})
config_result = config.load(config_details)
volumes = config_result.volumes
assert 'ext' in volumes
assert volumes['ext']['external'] is True
assert 'ext2' in volumes
assert volumes['ext2']['external']['name'] == 'aliased'
def test_external_volume_invalid_config(self):
config_details = build_config_details({
'version': 2,
'services': {
'bogus': {'image': 'busybox'}
},
'volumes': {
'ext': {'external': True, 'driver': 'foo'}
}
})
with self.assertRaises(ConfigurationError):
config.load(config_details)
class PortsTest(unittest.TestCase):
INVALID_PORTS_TYPES = [