Beispiel #1
0
    def test_report_running(self):
        """Verifies report running sequence."""
        manifest = {
            'vip': {
                'ip0': '192.168.0.1',
                'ip1': '192.168.0.2'
            },
            'task':
            't-0001',
            'name':
            'foo.test1#0001',
            'uniqueid':
            'AAAAAA',
            'proid':
            'andreik',
            'services': [{
                'command': '/usr/bin/python -m SimpleHTTPServer',
                'name': 'web_server',
                'restart': {
                    'interval': 60,
                    'limit': 3
                }
            }, {
                'command': 'sshd -D -f /etc/ssh/sshd_config',
                'name': 'sshd',
                'proid': None
            }],
            'endpoints': [{
                'port': 22,
                'name': 'ssh',
                'real_port': 5001
            }, {
                'port': 8000,
                'name': 'http',
                'real_port': 5000
            }]
        }
        treadmill.sysinfo.hostname.return_value = 'server1.xx.com'
        app_presence = presence.ServicePresence(manifest,
                                                container_dir=self.root,
                                                appevents_dir=self.events_dir)

        kazoo.client.KazooClient.exists.return_value = False
        app_presence.report_running('web_server')
        treadmill.appevents.post.assert_called_with(
            self.events_dir,
            events.ServiceRunningTraceEvent(instanceid='foo.test1#0001',
                                            uniqueid='AAAAAA',
                                            service='web_server'))

        kazoo.client.KazooClient.exists.return_value = True
        app_presence.report_running('web_server')
        treadmill.appevents.post.assert_called_with(
            self.events_dir,
            events.ServiceRunningTraceEvent(instanceid='foo.test1#0001',
                                            uniqueid='AAAAAA',
                                            service='web_server'))
Beispiel #2
0
    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.appevents.post.assert_called_with(
            self.tm_env.app_events_dir,
            events.ServiceRunningTraceEvent(instanceid='proid.app#001',
                                            uniqueid='abcdefghijklm',
                                            service='docker'))

        self.assertEqual(container.wait.call_count, 2)
        self.assertEqual(container.reload.call_count, 3)
Beispiel #3
0
 def report_running(self, service_name):
     """Creates ephemeral node indicating service has started."""
     _LOGGER.info('Service is running.')
     appevents.post(
         self.appevents_dir,
         traceevents.ServiceRunningTraceEvent(instanceid=self.appname,
                                              uniqueid=self.uniqueid,
                                              service=service_name))
Beispiel #4
0
    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)

            _sockets = runtime.allocate_network_ports(
                '0.0.0.0', manifest
            )

            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()

            try:
                client = self._get_client()

                try:
                    container = _create_container(
                        self._tm_env,
                        self._get_config(),
                        client,
                        app
                    )
                except docker.errors.ImageNotFound:
                    raise exc.ContainerSetupError(
                        'Image {0} was not found'.format(app.image),
                        app_abort.AbortedReason.IMAGE
                    )

                container.start()
                container.reload()

                _LOGGER.info('Container is running.')
                app_presence.register_endpoints()
                appevents.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()
            finally:
                _LOGGER.info('Stopping zookeeper.')
                context.GLOBAL.zk.conn.stop()
Beispiel #5
0
    def up(self, service):
        trace = self._get_trace(service)
        if trace is None:
            return

        appevents.post(
            self.tm_env.app_events_dir,
            traceevents.ServiceRunningTraceEvent(
                instanceid=trace['instanceid'],
                uniqueid=trace['uniqueid'],
                service=service.name))
Beispiel #6
0
    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')
Beispiel #7
0
def _start_service_sup(tm_env, manifest, container_dir):
    """Safely start services supervisor."""
    for service in manifest['services']:
        if service['trace']:
            appevents.post(
                tm_env.app_events_dir,
                traceevents.ServiceRunningTraceEvent(
                    instanceid=manifest['name'],
                    uniqueid=manifest['uniqueid'],
                    service=service['name']))

    try:
        supervisor.control_service(
            os.path.join(container_dir, 'sys', 'start_container'),
            supervisor.ServiceControlAction.once)
    except subproc.CalledProcessError:
        raise exc.ContainerSetupError('start_container')
Beispiel #8
0
 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'}
         )
     )