def test_server_state(self, stdout_mock): """Test printing ServerState event. """ event = events.ServerStateTraceEvent(timestamp=1, source='tests', servername='test.xx.com', state='up', payload={'foo': 'bar'}) self.trace_printer.process(event) self.assertEqual( stdout_mock.getvalue(), 'Thu, 01 Jan 1970 00:00:01+0000 - ' 'test.xx.com up\n')
def _record_server_state(self, servername): """Record server state.""" super(Master, self)._record_server_state(servername) server = self.servers.get(servername) if not server: _LOGGER.warning('Server not found: %s', servername) return placement_node = z.path.placement(servername) state, since = server.get_state() self.backend.put(placement_node, { 'state': state.value, 'since': since }) if self.server_events_dir: trace.post( self.server_events_dir, server_events.ServerStateTraceEvent(servername=servername, state=state.value))
def test_server_state(self): """ServerState event operations. """ event = events.ServerStateTraceEvent(state='up', timestamp=1, source='tests', servername='test.xx.com', payload={'foo': 'bar'}) self.assertEqual( event.to_dict(), { 'event_type': 'server_state', 'timestamp': 1, 'source': 'tests', 'servername': 'test.xx.com', 'payload': { 'foo': 'bar' }, 'state': 'up', }) self.assertEqual(event.to_data(), ( 1, 'tests', 'test.xx.com', 'server_state', 'up', { 'foo': 'bar' }, )) self.assertEqual( event, events.ServerStateTraceEvent.from_data(timestamp=1, source='tests', servername='test.xx.com', event_type='server_state', event_data='up', payload={'foo': 'bar'}))
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 )
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 )