Add get_secret_volumes unit tests

Signed-off-by: Joffrey F <joffrey@docker.com>
This commit is contained in:
Joffrey F 2017-10-13 17:02:01 -07:00 committed by Joffrey F
parent edcb8aea7b
commit 08714ef796
2 changed files with 61 additions and 6 deletions

View File

@ -881,12 +881,12 @@ class Service(object):
def get_secret_volumes(self): def get_secret_volumes(self):
def build_spec(secret): def build_spec(secret):
if secret['secret'].target is not None and secret['secret'].target.startswith('/'): target = secret['secret'].target
target = secret['secret'].target if target is None:
else: target = '{}/{}'.format(const.SECRETS_PATH, secret['secret'].source)
target = '{}/{}'.format( elif not os.path.isabs(target):
const.SECRETS_PATH, target = '{}/{}'.format(const.SECRETS_PATH, target)
secret['secret'].target or secret['secret'].source)
return VolumeSpec(secret['file'], target, 'ro') return VolumeSpec(secret['file'], target, 'ro')
return [build_spec(secret) for secret in self.secrets] return [build_spec(secret) for secret in self.secrets]

View File

@ -9,12 +9,14 @@ from .. import mock
from .. import unittest from .. import unittest
from compose.config.errors import DependencyError from compose.config.errors import DependencyError
from compose.config.types import ServicePort from compose.config.types import ServicePort
from compose.config.types import ServiceSecret
from compose.config.types import VolumeFromSpec from compose.config.types import VolumeFromSpec
from compose.config.types import VolumeSpec from compose.config.types import VolumeSpec
from compose.const import LABEL_CONFIG_HASH from compose.const import LABEL_CONFIG_HASH
from compose.const import LABEL_ONE_OFF from compose.const import LABEL_ONE_OFF
from compose.const import LABEL_PROJECT from compose.const import LABEL_PROJECT
from compose.const import LABEL_SERVICE from compose.const import LABEL_SERVICE
from compose.const import SECRETS_PATH
from compose.container import Container from compose.container import Container
from compose.project import OneOffFilter from compose.project import OneOffFilter
from compose.service import build_ulimits from compose.service import build_ulimits
@ -1089,3 +1091,56 @@ class ServiceVolumesTest(unittest.TestCase):
self.assertEqual( self.assertEqual(
self.mock_client.create_host_config.call_args[1]['binds'], self.mock_client.create_host_config.call_args[1]['binds'],
[volume]) [volume])
class ServiceSecretTest(unittest.TestCase):
def setUp(self):
self.mock_client = mock.create_autospec(docker.APIClient)
def test_get_secret_volumes(self):
secret1 = {
'secret': ServiceSecret.parse({'source': 'secret1', 'target': 'b.txt'}),
'file': 'a.txt'
}
service = Service(
'web',
client=self.mock_client,
image='busybox',
secrets=[secret1]
)
volumes = service.get_secret_volumes()
assert volumes[0].external == secret1['file']
assert volumes[0].internal == '{}/{}'.format(SECRETS_PATH, secret1['secret'].target)
def test_get_secret_volumes_abspath(self):
secret1 = {
'secret': ServiceSecret.parse({'source': 'secret1', 'target': '/d.txt'}),
'file': 'c.txt'
}
service = Service(
'web',
client=self.mock_client,
image='busybox',
secrets=[secret1]
)
volumes = service.get_secret_volumes()
assert volumes[0].external == secret1['file']
assert volumes[0].internal == secret1['secret'].target
def test_get_secret_volumes_no_target(self):
secret1 = {
'secret': ServiceSecret.parse({'source': 'secret1'}),
'file': 'c.txt'
}
service = Service(
'web',
client=self.mock_client,
image='busybox',
secrets=[secret1]
)
volumes = service.get_secret_volumes()
assert volumes[0].external == secret1['file']
assert volumes[0].internal == '{}/{}'.format(SECRETS_PATH, secret1['secret'].source)