mirror of https://github.com/docker/compose.git
Merge pull request #2233 from dnephin/some_unit_test_cleanup
Cleanup some unit tests and whitespace
This commit is contained in:
commit
fb69391889
|
@ -300,9 +300,7 @@ class Service(object):
|
||||||
Create a container for this service. If the image doesn't exist, attempt to pull
|
Create a container for this service. If the image doesn't exist, attempt to pull
|
||||||
it.
|
it.
|
||||||
"""
|
"""
|
||||||
self.ensure_image_exists(
|
self.ensure_image_exists(do_build=do_build)
|
||||||
do_build=do_build,
|
|
||||||
)
|
|
||||||
|
|
||||||
container_options = self._get_container_create_options(
|
container_options = self._get_container_create_options(
|
||||||
override_options,
|
override_options,
|
||||||
|
@ -316,9 +314,7 @@ class Service(object):
|
||||||
|
|
||||||
return Container.create(self.client, **container_options)
|
return Container.create(self.client, **container_options)
|
||||||
|
|
||||||
def ensure_image_exists(self,
|
def ensure_image_exists(self, do_build=True):
|
||||||
do_build=True):
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.image()
|
self.image()
|
||||||
return
|
return
|
||||||
|
@ -403,9 +399,7 @@ class Service(object):
|
||||||
(action, containers) = plan
|
(action, containers) = plan
|
||||||
|
|
||||||
if action == 'create':
|
if action == 'create':
|
||||||
container = self.create_container(
|
container = self.create_container(do_build=do_build)
|
||||||
do_build=do_build,
|
|
||||||
)
|
|
||||||
self.start_container(container)
|
self.start_container(container)
|
||||||
|
|
||||||
return [container]
|
return [container]
|
||||||
|
|
|
@ -351,44 +351,37 @@ class ServiceTest(unittest.TestCase):
|
||||||
self.assertEqual(parse_repository_tag("user/repo@sha256:digest"), ("user/repo", "sha256:digest", "@"))
|
self.assertEqual(parse_repository_tag("user/repo@sha256:digest"), ("user/repo", "sha256:digest", "@"))
|
||||||
self.assertEqual(parse_repository_tag("url:5000/repo@sha256:digest"), ("url:5000/repo", "sha256:digest", "@"))
|
self.assertEqual(parse_repository_tag("url:5000/repo@sha256:digest"), ("url:5000/repo", "sha256:digest", "@"))
|
||||||
|
|
||||||
@mock.patch('compose.service.Container', autospec=True)
|
|
||||||
def test_create_container_latest_is_used_when_no_tag_specified(self, mock_container):
|
|
||||||
service = Service('foo', client=self.mock_client, image='someimage')
|
|
||||||
images = []
|
|
||||||
|
|
||||||
def pull(repo, tag=None, **kwargs):
|
|
||||||
self.assertEqual('someimage', repo)
|
|
||||||
self.assertEqual('latest', tag)
|
|
||||||
images.append({'Id': 'abc123'})
|
|
||||||
return []
|
|
||||||
|
|
||||||
service.image = lambda *args, **kwargs: mock_get_image(images)
|
|
||||||
self.mock_client.pull = pull
|
|
||||||
|
|
||||||
service.create_container()
|
|
||||||
self.assertEqual(1, len(images))
|
|
||||||
|
|
||||||
def test_create_container_with_build(self):
|
def test_create_container_with_build(self):
|
||||||
service = Service('foo', client=self.mock_client, build='.')
|
service = Service('foo', client=self.mock_client, build='.')
|
||||||
|
self.mock_client.inspect_image.side_effect = [
|
||||||
images = []
|
NoSuchImageError,
|
||||||
service.image = lambda *args, **kwargs: mock_get_image(images)
|
{'Id': 'abc123'},
|
||||||
service.build = lambda: images.append({'Id': 'abc123'})
|
]
|
||||||
|
self.mock_client.build.return_value = [
|
||||||
|
'{"stream": "Successfully built abcd"}',
|
||||||
|
]
|
||||||
|
|
||||||
service.create_container(do_build=True)
|
service.create_container(do_build=True)
|
||||||
self.assertEqual(1, len(images))
|
self.mock_client.build.assert_called_once_with(
|
||||||
|
tag='default_foo',
|
||||||
|
dockerfile=None,
|
||||||
|
stream=True,
|
||||||
|
path='.',
|
||||||
|
pull=False,
|
||||||
|
nocache=False,
|
||||||
|
rm=True,
|
||||||
|
)
|
||||||
|
|
||||||
def test_create_container_no_build(self):
|
def test_create_container_no_build(self):
|
||||||
service = Service('foo', client=self.mock_client, build='.')
|
service = Service('foo', client=self.mock_client, build='.')
|
||||||
service.image = lambda: {'Id': 'abc123'}
|
self.mock_client.inspect_image.return_value = {'Id': 'abc123'}
|
||||||
|
|
||||||
service.create_container(do_build=False)
|
service.create_container(do_build=False)
|
||||||
self.assertFalse(self.mock_client.build.called)
|
self.assertFalse(self.mock_client.build.called)
|
||||||
|
|
||||||
def test_create_container_no_build_but_needs_build(self):
|
def test_create_container_no_build_but_needs_build(self):
|
||||||
service = Service('foo', client=self.mock_client, build='.')
|
service = Service('foo', client=self.mock_client, build='.')
|
||||||
service.image = lambda *args, **kwargs: mock_get_image([])
|
self.mock_client.inspect_image.side_effect = NoSuchImageError
|
||||||
|
|
||||||
with self.assertRaises(NeedsBuildError):
|
with self.assertRaises(NeedsBuildError):
|
||||||
service.create_container(do_build=False)
|
service.create_container(do_build=False)
|
||||||
|
|
||||||
|
@ -488,13 +481,6 @@ class NetTestCase(unittest.TestCase):
|
||||||
self.assertEqual(net.service_name, service_name)
|
self.assertEqual(net.service_name, service_name)
|
||||||
|
|
||||||
|
|
||||||
def mock_get_image(images):
|
|
||||||
if images:
|
|
||||||
return images[0]
|
|
||||||
else:
|
|
||||||
raise NoSuchImageError()
|
|
||||||
|
|
||||||
|
|
||||||
class ServiceVolumesTest(unittest.TestCase):
|
class ServiceVolumesTest(unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
|
Loading…
Reference in New Issue