def test_post_zk(self): """Test appevents.post.zk.""" zkclient_mock = mock.Mock() zkclient_mock.get_children.return_value = [] appevents.post_zk( zkclient_mock, 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() appevents.post_zk( zkclient_mock, 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() appevents.post_zk( zkclient_mock, 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', b'{data: test, host: baz, state: aborted, when: \'100\'}\n', ephemeral=False, makepath=True, sequence=False, acl=mock.ANY ) ])
def delete_apps(zkclient, app_ids, deleted_by=None): """Unschedules apps.""" for app_id in app_ids: zkutils.ensure_deleted(zkclient, _app_node(app_id)) appevents.post_zk( zkclient, traceevents.PendingDeleteTraceEvent( instanceid=app_id, why='%s:deleted' % deleted_by if deleted_by else 'deleted'))
def test_pending_delete(self, stdout_mock): """Test printing PendingDelete event. """ event = events.PendingDeleteTraceEvent(why='deleted', timestamp=2, source='tests', instanceid='proid.foo#123', payload={'foo': 'bar'}) self.trace_printer.process(event) self.assertEqual( stdout_mock.getvalue(), 'Thu, 01 Jan 1970 00:00:02+0000 - ' 'proid.foo#123 pending delete: deleted\n')
def test_pending_delete(self): """PendingDelete event operations. """ event = events.PendingDeleteTraceEvent(why='deleted', timestamp=2, source='tests', instanceid='proid.foo#123', payload={'foo': 'bar'}) self.assertEqual( event.to_dict(), { 'event_type': 'pending_delete', 'timestamp': 2, 'source': 'tests', 'instanceid': 'proid.foo#123', 'payload': { 'foo': 'bar' }, 'why': 'deleted', }) self.assertEqual(event.to_data(), ( 2, 'tests', 'proid.foo#123', 'pending_delete', 'deleted', { 'foo': 'bar' }, )) self.assertEqual( event, events.PendingDeleteTraceEvent.from_data( timestamp=2, source='tests', instanceid='proid.foo#123', event_type='pending_delete', event_data='deleted', payload={'foo': 'bar'}))
def test_post(self): """Test appevents.post.""" # Disable W0212(protected-access) # pylint: disable=W0212 zkclient_mock = mock.Mock() zkclient_mock.get_children.return_value = [] watcher = appevents.AppEventsWatcher(zkclient_mock, self.root) appevents.post( self.root, events.PendingTraceEvent( instanceid='foo.bar#123', why='created', )) path = os.path.join(self.root, '100,foo.bar#123,pending,created') self.assertTrue(os.path.exists(path)) watcher._on_created(path) 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() appevents.post( self.root, events.PendingDeleteTraceEvent(instanceid='foo.bar#123', why='deleted')) path = os.path.join(self.root, '100,foo.bar#123,pending_delete,deleted') self.assertTrue(os.path.exists(path)) watcher._on_created(path) 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() appevents.post( self.root, events.AbortedTraceEvent(instanceid='foo.bar#123', why='test')) path = os.path.join(self.root, '100,foo.bar#123,aborted,test') self.assertTrue(os.path.exists(path)) watcher._on_created(path) 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', b'{data: test, host: baz, state: aborted, when: \'100\'}\n', makepath=True, ephemeral=False, acl=mock.ANY, sequence=False) ])