def test_specifies_host_port_with_host_port_range(self): service = Service('foo', image='foo', ports=["1000-2000:2000-3000"]) assert service.specifies_host_port()
def test_pull_image_no_tag(self): service = Service('foo', client=self.mock_client, image='ababab') service.pull() self.mock_client.pull.assert_called_once_with('ababab', tag='latest', stream=True)
def test_remove_image_local_with_image_name_doesnt_remove(self): web = Service('web', image='example', client=self.mock_client) assert not web.remove_image(ImageType.local) assert not self.mock_client.remove_image.called
def test_specifies_host_port_with_host_ip_and_port_range(self): service = Service('foo', image='foo', ports=["127.0.0.1:1000-2000:2000-3000"]) self.assertEqual(service.specifies_host_port(), True)
def test_get_container_not_found(self): self.mock_client.containers.return_value = [] service = Service('foo', client=self.mock_client, image='foo') self.assertRaises(ValueError, service.get_container)
def test_specifies_host_port_with_no_ports(self): service = Service('foo', image='foo') self.assertEqual(service.specifies_host_port(), False)
def test_specifies_host_port_with_host_ip_no_port(self): service = Service('foo', image='foo', ports=["127.0.0.1::2000"]) self.assertEqual(service.specifies_host_port(), False)
def test_name_validations(self): self.assertRaises(ConfigError, lambda: Service(name='', image='foo')) self.assertRaises(ConfigError, lambda: Service(name=' ', image='foo')) self.assertRaises(ConfigError, lambda: Service(name='/', image='foo')) self.assertRaises(ConfigError, lambda: Service(name='!', image='foo')) self.assertRaises(ConfigError, lambda: Service(name='\xe2', image='foo')) Service('a', image='foo') Service('foo', image='foo') Service('foo-bar', image='foo') Service('foo.bar', image='foo') Service('foo_bar', image='foo') Service('_', image='foo') Service('___', image='foo') Service('-', image='foo') Service('--', image='foo') Service('.__.', image='foo')
def test_create_container_no_build(self): service = Service('foo', client=self.mock_client, build='.') service.image = lambda: {'Id': 'abc123'} service.create_container(do_build=False) self.assertFalse(self.mock_client.build.called)
def test_split_domainname_none(self): service = Service('foo', image='foo', hostname='name', client=self.mock_client) self.mock_client.containers.return_value = [] opts = service._get_container_create_options({'image': 'foo'}, 1) self.assertEqual(opts['hostname'], 'name', 'hostname') self.assertFalse('domainname' in opts, 'domainname')
def test_memory_swap_limit(self): service = Service(name='foo', image='foo', hostname='name', client=self.mock_client, mem_limit=1000000000, memswap_limit=2000000000) self.mock_client.containers.return_value = [] opts = service._get_container_create_options({'some': 'overrides'}, 1) self.assertEqual(opts['host_config']['MemorySwap'], 2000000000) self.assertEqual(opts['host_config']['Memory'], 1000000000)
def test_self_reference_external_link(self): service = Service(name='foo', external_links=['default_foo_1']) with self.assertRaises(DependencyError): service.get_container_name(1)
def test_executor_name(): eb = EventBus() srv = Service(name='web1', cmd='fake') executor = Executor(eb, srv) executor.start() assert executor.name == 'web1'
def test_specifies_host_port_with_host_ip_no_port_range(self): service = Service('foo', image='foo', ports=["127.0.0.1::2000-3000"]) assert not service.specifies_host_port()
def test_create_container_no_build(self): service = Service('foo', client=self.mock_client, build='.') self.mock_client.inspect_image.return_value = {'Id': 'abc123'} service.create_container(do_build=False) self.assertFalse(self.mock_client.build.called)
def test_image_name_with_envvars(self): os.environ['USE_TAG'] = '12345' service = Service('foo', image='something:${USE_TAG}') self.assertEqual(service.image_name, 'something:12345')
def test_create_container_no_build_but_needs_build(self): service = Service('foo', client=self.mock_client, build='.') self.mock_client.inspect_image.side_effect = NoSuchImageError with self.assertRaises(NeedsBuildError): service.create_container(do_build=False)
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([]) with self.assertRaises(NeedsBuildError): service.create_container(do_build=False)
def test_specifies_host_port_with_host_port(self): service = Service('foo', image='foo', ports=["1000:2000"]) self.assertEqual(service.specifies_host_port(), True)
def test_remove_image_all_does_remove(self): web = Service('web', image='example', client=self.mock_client) assert web.remove_image(ImageType.all) self.mock_client.remove_image.assert_called_once_with(web.image_name)
def test_specifies_host_port_with_container_port_range(self): service = Service('foo', image='foo', ports=["2000-3000"]) self.assertEqual(service.specifies_host_port(), False)
def test_image_name_from_config(self): image_name = 'example/web:latest' service = Service('foo', image=image_name) assert service.image_name == image_name
def test_get_links_with_networking(self): service = Service('foo', image='foo', links=[(Service('one'), 'one')], use_networking=True) self.assertEqual(service._get_links(link_to_self=True), [])
def test_image_name_default(self): service = Service('foo', project='testing') assert service.image_name == 'testing_foo'
def test_containers(self): service = Service('db', self.mock_client, 'myproject', image='foo') self.mock_client.containers.return_value = [] self.assertEqual(list(service.containers()), [])
def test_events(self): services = [Service(name='web'), Service(name='db')] project = Project('test', services, self.mock_client) self.mock_client.api_version = '1.35' self.mock_client.events.return_value = iter([ { 'status': 'create', 'from': 'example/image', 'Type': 'container', 'Actor': { 'ID': 'abcde', 'Attributes': { 'com.docker.compose.project': 'test', 'com.docker.compose.service': 'web', 'image': 'example/image', 'name': 'test_web_1', } }, 'id': 'abcde', 'time': 1420092061, 'timeNano': 14200920610000002000, }, { 'status': 'attach', 'from': 'example/image', 'Type': 'container', 'Actor': { 'ID': 'abcde', 'Attributes': { 'com.docker.compose.project': 'test', 'com.docker.compose.service': 'web', 'image': 'example/image', 'name': 'test_web_1', } }, 'id': 'abcde', 'time': 1420092061, 'timeNano': 14200920610000003000, }, { 'status': 'create', 'from': 'example/other', 'Type': 'container', 'Actor': { 'ID': 'bdbdbd', 'Attributes': { 'image': 'example/other', 'name': 'shrewd_einstein', } }, 'id': 'bdbdbd', 'time': 1420092061, 'timeNano': 14200920610000005000, }, { 'status': 'create', 'from': 'example/db', 'Type': 'container', 'Actor': { 'ID': 'ababa', 'Attributes': { 'com.docker.compose.project': 'test', 'com.docker.compose.service': 'db', 'image': 'example/db', 'name': 'test_db_1', } }, 'id': 'ababa', 'time': 1420092061, 'timeNano': 14200920610000004000, }, { 'status': 'destroy', 'from': 'example/db', 'Type': 'container', 'Actor': { 'ID': 'eeeee', 'Attributes': { 'com.docker.compose.project': 'test', 'com.docker.compose.service': 'db', 'image': 'example/db', 'name': 'test_db_1', } }, 'id': 'eeeee', 'time': 1420092061, 'timeNano': 14200920610000004000, }, ]) def dt_with_microseconds(dt, us): return datetime.datetime.fromtimestamp(dt).replace(microsecond=us) def get_container(cid): if cid == 'eeeee': raise NotFound(None, None, "oops") if cid == 'abcde': name = 'web' labels = {LABEL_SERVICE: name} elif cid == 'ababa': name = 'db' labels = {LABEL_SERVICE: name} else: labels = {} name = '' return { 'Id': cid, 'Config': { 'Labels': labels }, 'Name': '/project_%s_1' % name, } self.mock_client.inspect_container.side_effect = get_container events = project.events() events_list = list(events) # Assert the return value is a generator assert not list(events) assert events_list == [ { 'type': 'container', 'service': 'web', 'action': 'create', 'id': 'abcde', 'attributes': { 'name': 'test_web_1', 'image': 'example/image', }, 'time': dt_with_microseconds(1420092061, 2), 'container': Container(None, get_container('abcde')), }, { 'type': 'container', 'service': 'web', 'action': 'attach', 'id': 'abcde', 'attributes': { 'name': 'test_web_1', 'image': 'example/image', }, 'time': dt_with_microseconds(1420092061, 3), 'container': Container(None, get_container('abcde')), }, { 'type': 'container', 'service': 'db', 'action': 'create', 'id': 'ababa', 'attributes': { 'name': 'test_db_1', 'image': 'example/db', }, 'time': dt_with_microseconds(1420092061, 4), 'container': Container(None, get_container('ababa')), }, { 'type': 'container', 'service': 'db', 'action': 'destroy', 'id': 'eeeee', 'attributes': { 'name': 'test_db_1', 'image': 'example/db', }, 'time': dt_with_microseconds(1420092061, 4), 'container': None, }, ]
def test_remove_image_none(self): web = Service('web', image='example', client=self.mock_client) assert not web.remove_image(ImageType.none) assert not self.mock_client.remove_image.called
def test_project_validation(self): self.assertRaises( ConfigError, lambda: Service(name='foo', project='>', image='foo')) Service(name='foo', project='bar.bar__', image='foo')
def test_remove_image_local_without_image_name_does_remove(self): web = Service('web', build='.', client=self.mock_client) assert web.remove_image(ImageType.local) self.mock_client.remove_image.assert_called_once_with(web.image_name)
def test_specifies_host_port_with_host_ip_and_port(self): service = Service('foo', image='foo', ports=["127.0.0.1:1000:2000"]) assert service.specifies_host_port()