Cleanup some unit tests and whitespace.

Remove some unnecessary newlines.
Remove a unittest that was attempting to test behaviour that was removed a while ago, so isn't testing anything.
Updated some unit tests to use mocks instead of a custom fake.

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2015-10-20 13:10:32 -04:00
parent 5ed9f9bdf7
commit b1f8ed84a3
2 changed files with 21 additions and 41 deletions

View File

@ -300,9 +300,7 @@ class Service(object):
Create a container for this service. If the image doesn't exist, attempt to pull
it.
"""
self.ensure_image_exists(
do_build=do_build,
)
self.ensure_image_exists(do_build=do_build)
container_options = self._get_container_create_options(
override_options,
@ -316,9 +314,7 @@ class Service(object):
return Container.create(self.client, **container_options)
def ensure_image_exists(self,
do_build=True):
def ensure_image_exists(self, do_build=True):
try:
self.image()
return
@ -403,9 +399,7 @@ class Service(object):
(action, containers) = plan
if action == 'create':
container = self.create_container(
do_build=do_build,
)
container = self.create_container(do_build=do_build)
self.start_container(container)
return [container]

View File

@ -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("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):
service = Service('foo', client=self.mock_client, build='.')
images = []
service.image = lambda *args, **kwargs: mock_get_image(images)
service.build = lambda: images.append({'Id': 'abc123'})
self.mock_client.inspect_image.side_effect = [
NoSuchImageError,
{'Id': 'abc123'},
]
self.mock_client.build.return_value = [
'{"stream": "Successfully built abcd"}',
]
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):
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)
self.assertFalse(self.mock_client.build.called)
def test_create_container_no_build_but_needs_build(self):
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):
service.create_container(do_build=False)
@ -488,13 +481,6 @@ class NetTestCase(unittest.TestCase):
self.assertEqual(net.service_name, service_name)
def mock_get_image(images):
if images:
return images[0]
else:
raise NoSuchImageError()
class ServiceVolumesTest(unittest.TestCase):
def setUp(self):