Пример #1
0
    def test_server_blackout(self, stdout_mock):
        """Test printing ServerBlackout event.
        """
        event = events.ServerBlackoutTraceEvent(timestamp=2,
                                                source='tests',
                                                servername='test.xx.com',
                                                payload={'foo': 'bar'})

        self.trace_printer.process(event)

        self.assertEqual(
            stdout_mock.getvalue(), 'Thu, 01 Jan 1970 00:00:02+0000 - '
            'test.xx.com blackout\n')
Пример #2
0
    def process_blackedout_servers(self, servers):
        """Callback invoked when server blacklist is modified."""
        events = []
        servers_blacklist = set(servers)

        for servername in servers_blacklist - self.servers_blacklist:
            _LOGGER.info('Server blackout: %s', servername)
            events.append(
                server_events.ServerBlackoutTraceEvent(servername=servername))

        for servername in self.servers_blacklist - servers_blacklist:
            _LOGGER.info('Server blackout cleared: %s', servername)
            events.append(
                server_events.ServerBlackoutClearedTraceEvent(
                    servername=servername))

        for event in events:
            if self.server_events_dir:
                trace.post(self.server_events_dir, event)
        self.servers_blacklist = servers_blacklist
Пример #3
0
 def test_server_blackout(self):
     """ServerBlackout event operations.
     """
     event = events.ServerBlackoutTraceEvent(timestamp=2,
                                             source='tests',
                                             servername='test.xx.com',
                                             payload={'foo': 'bar'})
     self.assertEqual(
         event.to_dict(), {
             'event_type': 'server_blackout',
             'timestamp': 2,
             'source': 'tests',
             'servername': 'test.xx.com',
             'payload': {
                 'foo': 'bar'
             },
         })
     self.assertEqual(event.to_data(), (
         2,
         'tests',
         'test.xx.com',
         'server_blackout',
         '',
         {
             'foo': 'bar'
         },
     ))
     self.assertEqual(
         event,
         events.ServerBlackoutTraceEvent.from_data(
             timestamp=2,
             source='tests',
             servername='test.xx.com',
             event_type='server_blackout',
             event_data='not used',
             payload={'foo': 'bar'}))
Пример #4
0
    def test_post(self):
        """Test trace.post."""
        # Disable W0212(protected-access)
        # pylint: disable=W0212
        zkclient_mock = mock.Mock()
        zkclient_mock.get_children.return_value = []
        publisher = events_publisher.EventsPublisher(
            zkclient_mock,
            app_events_dir=self.app_events_dir,
            server_events_dir=self.server_events_dir
        )

        trace.post(
            self.app_events_dir,
            app_events.PendingTraceEvent(
                instanceid='foo.bar#123',
                why='created',
            )
        )
        path = os.path.join(
            self.app_events_dir, '100,foo.bar#123,pending,created'
        )
        self.assertTrue(os.path.exists(path))
        publisher._on_created(path, app_zk.publish)
        zkclient_mock.create.assert_called_once_with(
            '/trace/007B/foo.bar#123,100,baz,pending,created',
            b'',
            ephemeral=False, makepath=True, sequence=False,
            acl=mock.ANY
        )

        zkclient_mock.reset_mock()
        trace.post(
            self.app_events_dir,
            app_events.PendingDeleteTraceEvent(
                instanceid='foo.bar#123',
                why='deleted'
            )
        )
        path = os.path.join(
            self.app_events_dir, '100,foo.bar#123,pending_delete,deleted'
        )
        self.assertTrue(os.path.exists(path))
        publisher._on_created(path, app_zk.publish)
        zkclient_mock.create.assert_called_once_with(
            '/trace/007B/foo.bar#123,100,baz,pending_delete,deleted',
            b'',
            ephemeral=False, makepath=True, sequence=False,
            acl=mock.ANY
        )

        zkclient_mock.reset_mock()
        trace.post(
            self.app_events_dir,
            app_events.AbortedTraceEvent(
                instanceid='foo.bar#123',
                why='test'
            )
        )
        path = os.path.join(
            self.app_events_dir, '100,foo.bar#123,aborted,test'
        )
        self.assertTrue(os.path.exists(path))
        publisher._on_created(path, app_zk.publish)
        self.assertEqual(zkclient_mock.create.call_args_list, [
            mock.call(
                '/trace/007B/foo.bar#123,100,baz,aborted,test',
                b'',
                ephemeral=False, makepath=True, sequence=False,
                acl=mock.ANY
            ),
            mock.call(
                '/finished/foo.bar#123',
                json.dumps({
                    'data': 'test',
                    'host': 'baz',
                    'state': 'aborted',
                    'when': '100'
                }, sort_keys=True).encode(),
                makepath=True,
                ephemeral=False,
                acl=mock.ANY,
                sequence=False
            )
        ])

        zkclient_mock.reset_mock()
        trace.post(
            self.server_events_dir,
            server_events.ServerStateTraceEvent(
                servername='test.xx.com',
                state='up'
            )
        )
        path = os.path.join(
            self.server_events_dir, '100,test.xx.com,server_state,up'
        )
        self.assertTrue(os.path.exists(path))
        publisher._on_created(path, server_zk.publish)
        zkclient_mock.create.assert_called_once_with(
            '/server-trace/005D/test.xx.com,100,baz,server_state,up',
            b'',
            ephemeral=False, makepath=True, sequence=False,
            acl=mock.ANY
        )

        zkclient_mock.reset_mock()
        trace.post(
            self.server_events_dir,
            server_events.ServerBlackoutTraceEvent(
                servername='test.xx.com'
            )
        )
        path = os.path.join(
            self.server_events_dir, '100,test.xx.com,server_blackout,'
        )
        self.assertTrue(os.path.exists(path))
        publisher._on_created(path, server_zk.publish)
        zkclient_mock.create.assert_called_once_with(
            '/server-trace/005D/test.xx.com,100,baz,server_blackout,',
            b'',
            ephemeral=False, makepath=True, sequence=False,
            acl=mock.ANY
        )
Пример #5
0
    def test_post_zk(self):
        """Test trace.post_zk."""
        zkclient_mock = mock.Mock()
        zkclient_mock.get_children.return_value = []

        trace.post_zk(
            zkclient_mock,
            app_events.PendingTraceEvent(
                instanceid='foo.bar#123',
                why='created',
                payload=''
            )
        )
        zkclient_mock.create.assert_called_once_with(
            '/trace/007B/foo.bar#123,100,baz,pending,created',
            b'',
            ephemeral=False, makepath=True, sequence=False,
            acl=mock.ANY
        )
        zkclient_mock.reset_mock()

        trace.post_zk(
            zkclient_mock,
            app_events.PendingDeleteTraceEvent(
                instanceid='foo.bar#123',
                why='deleted'
            )
        )
        zkclient_mock.create.assert_called_once_with(
            '/trace/007B/foo.bar#123,100,baz,pending_delete,deleted',
            b'',
            ephemeral=False, makepath=True, sequence=False,
            acl=mock.ANY
        )
        zkclient_mock.reset_mock()

        trace.post_zk(
            zkclient_mock,
            app_events.AbortedTraceEvent(
                instanceid='foo.bar#123',
                why='test'
            )
        )
        zkclient_mock.create.assert_has_calls([
            mock.call(
                '/trace/007B/foo.bar#123,100,baz,aborted,test',
                b'',
                ephemeral=False, makepath=True, sequence=False,
                acl=mock.ANY
            ),
            mock.call(
                '/finished/foo.bar#123',
                json.dumps({
                    'data': 'test',
                    'state': 'aborted',
                    'when': '100',
                    'host': 'baz'
                }, sort_keys=True).encode(),
                ephemeral=False, makepath=True, sequence=False,
                acl=mock.ANY
            )
        ])
        zkclient_mock.reset_mock()

        trace.post_zk(
            zkclient_mock,
            server_events.ServerStateTraceEvent(
                servername='test.xx.com',
                state='up'
            )
        )
        zkclient_mock.create.assert_called_once_with(
            '/server-trace/005D/test.xx.com,100,baz,server_state,up',
            b'',
            ephemeral=False, makepath=True, sequence=False,
            acl=mock.ANY
        )
        zkclient_mock.reset_mock()

        trace.post_zk(
            zkclient_mock,
            server_events.ServerBlackoutTraceEvent(
                servername='test.xx.com'
            )
        )
        zkclient_mock.create.assert_called_once_with(
            '/server-trace/005D/test.xx.com,100,baz,server_blackout,',
            b'',
            ephemeral=False, makepath=True, sequence=False,
            acl=mock.ANY
        )