Implement ability to specify external volumes

External volumes are created and managed by the user.
They are not namespaced.
They are expected to exist at the beginning of the up phase.

Signed-off-by: Joffrey F <joffrey@docker.com>
This commit is contained in:
Joffrey F 2016-01-12 16:53:49 -08:00
commit 9cb58b796e
6 changed files with 93 additions and 21 deletions

View file

@ -677,7 +677,7 @@ class ProjectTest(DockerClientTestCase):
vol_name
) in str(e.exception)
def test_initialize_volumes_user_created_volumes(self):
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)
@ -687,7 +687,7 @@ class ProjectTest(DockerClientTestCase):
'name': 'web',
'image': 'busybox:latest',
'command': 'top'
}], volumes={vol_name: {'driver': 'local'}}
}], volumes={vol_name: {'external': True}}
)
project = Project.from_config(
name='composetest',
@ -697,3 +697,23 @@ class ProjectTest(DockerClientTestCase):
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}}
)
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,10 @@ 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=False):
vol = Volume(
self.client, 'composetest', name, driver=driver, driver_opts=opts
self.client, 'composetest', name, driver=driver, driver_opts=opts,
external=external
)
self.tmp_volumes.append(vol)
return vol
@ -55,12 +56,19 @@ class VolumeTest(DockerClientTestCase):
volumes = self.client.volumes()['Volumes']
assert len([v for v in volumes if v['Name'] == vol.full_name]) == 0
def test_is_user_created(self):
vol = Volume(self.client, 'composetest', 'uservolume01')
try:
self.client.create_volume('uservolume01')
assert vol.is_user_created is True
finally:
self.client.remove_volume('uservolume01')
vol2 = Volume(self.client, 'composetest', 'volume01')
assert vol2.is_user_created is False
def test_external_volume(self):
vol = self.create_volume('volume01', 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 = 'alias01'
vol = self.create_volume('volume01', external={'name': alias_name})
assert vol.external is True
assert vol.full_name == alias_name
vol.create()
info = vol.inspect()
assert info['Name'] == alias_name

View file

@ -775,6 +775,24 @@ 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'
class PortsTest(unittest.TestCase):
INVALID_PORTS_TYPES = [