def test__run(self): """Tests docker runtime run.""" # Access to a protected member # pylint: disable=W0212 docker_runtime = runtime.DockerRuntime(self.tm_env, self.container_dir, {'version': '1.24'}) container = mock.Mock() treadmill.runtime.docker.runtime._create_container.return_value = \ container type(container).status = mock.PropertyMock( side_effect=['running', 'running', 'exited']) docker_runtime._run(self.manifest) runtime._create_container.assert_called_once() container.start.assert_called_once() treadmill.trace.post.assert_called_with( self.tm_env.app_events_dir, events.ServiceRunningTraceEvent(instanceid='proid.app#001', uniqueid='abcdefghijklm', service='docker')) self.assertEqual(container.reload.call_count, 1)
def _run(self, manifest): context.GLOBAL.zk.conn.add_listener(zkutils.exit_on_lost) with lc.LogContext(_LOGGER, self._service.name, lc.ContainerAdapter) as log: log.info('Running %r', self._service.directory) manifest['ephemeral_ports']['tcp'] = [] manifest['ephemeral_ports']['udp'] = [] _create_docker_log_symlink(self._service.data_dir) app = runtime.save_app(manifest, self._service.data_dir) volume_mapping = self._get_volume_mapping() app_presence = presence.EndpointPresence(context.GLOBAL.zk.conn, manifest) app_presence.register_identity() app_presence.register_running() client = self._get_client() try: container = _create_container(self._tm_env, self._get_config(), client, app, volume_mapping) except docker.errors.ImageNotFound: raise exc.ContainerSetupError( 'Image {0} was not found'.format(app.image), app_abort.AbortedReason.IMAGE) container.start() container.reload() _update_network_info_in_manifest(container, manifest) # needs to share manifest with container if volume_mapping: container_data_dir = next(iter(volume_mapping)) runtime.save_app(manifest, container_data_dir, app_json='app.json') _LOGGER.info('Container is running.') app_presence.register_endpoints() trace.post( self._tm_env.app_events_dir, events.ServiceRunningTraceEvent(instanceid=app.name, uniqueid=app.uniqueid, service='docker')) _print_container_logs(container)
def test_service_running(self, stdout_mock): """Test printing ServiceRunning event. """ event = events.ServiceRunningTraceEvent(timestamp=1, source='tests', instanceid='proid.foo#123', uniqueid='AAAA', service='web.web', payload={'foo': 'bar'}) self.trace_printer.process(event) self.assertEqual( stdout_mock.getvalue(), 'Thu, 01 Jan 1970 00:00:01+0000 - ' 'proid.foo#123/AAAA/service/web.web running\n')
def test_service_running(self): """ServiceRunning event operations. """ event = events.ServiceRunningTraceEvent( timestamp=1, source='tests', instanceid='proid.foo#123', uniqueid='AAAA', service='web.web', payload={'foo': 'bar'} ) self.assertEqual( event.to_dict(), { 'event_type': 'service_running', 'timestamp': 1, 'source': 'tests', 'instanceid': 'proid.foo#123', 'uniqueid': 'AAAA', 'service': 'web.web', 'payload': {'foo': 'bar'}, } ) self.assertEqual( event.to_data(), ( 1, 'tests', 'proid.foo#123', 'service_running', 'AAAA.web.web', {'foo': 'bar'}, ) ) self.assertEqual( event, events.ServiceRunningTraceEvent.from_data( timestamp=1, source='tests', instanceid='proid.foo#123', event_type='service_running', event_data='AAAA.web.web', payload={'foo': 'bar'} ) )
def _run(self, manifest): context.GLOBAL.zk.conn.add_listener(zkutils.exit_on_lost) with lc.LogContext(_LOGGER, self._service.name, lc.ContainerAdapter) as log: log.info('Running %r', self._service.directory) manifest['ephemeral_ports']['tcp'] = [] manifest['ephemeral_ports']['udp'] = [] # create container_data dir container_data_dir = os.path.join(self._service.data_dir, 'container_data') log.info('container_data %r', container_data_dir) fs.mkdir_safe(container_data_dir) # volume mapping config : read-only mapping volume_mapping = { container_data_dir: { 'bind': 'c:\\container_data', 'mode': 'ro' } } app = runtime.save_app(manifest, self._service.data_dir) app_presence = presence.EndpointPresence(context.GLOBAL.zk.conn, manifest) app_presence.register_identity() app_presence.register_running() client = self._get_client() try: container = _create_container(self._tm_env, self._get_config(), client, app, volume_mapping) except docker.errors.ImageNotFound: raise exc.ContainerSetupError( 'Image {0} was not found'.format(app.image), app_abort.AbortedReason.IMAGE) container.start() container.reload() _update_network_info_in_manifest(container, manifest) runtime.save_app(manifest, container_data_dir, app_json='app.json') _LOGGER.info('Container is running.') app_presence.register_endpoints() trace.post( self._tm_env.app_events_dir, events.ServiceRunningTraceEvent(instanceid=app.name, uniqueid=app.uniqueid, service='docker')) while container.status == 'running': container.wait(timeout=10) container.reload()