def test_container_action_events_get_in_order(self): """Ensure retrived action events are in order.""" uuid1 = uuidutils.generate_uuid() action = dbapi.action_start(self.context, self._create_action_values(uuid1)) extra1 = { 'created_at': timeutils.utcnow() } extra2 = { 'created_at': timeutils.utcnow() + timedelta(seconds=5) } event_val1 = self._create_event_values(uuid1, 'fake1', extra=extra1) event_val2 = self._create_event_values(uuid1, 'fake2', extra=extra1) event_val3 = self._create_event_values(uuid1, 'fake3', extra=extra2) event1 = dbapi.action_event_start(self.context, event_val1) event2 = dbapi.action_event_start(self.context, event_val2) event3 = dbapi.action_event_start(self.context, event_val3) events = dbapi.action_events_get(self.context, action['id']) self.assertEqual(3, len(events)) self._assertEqualOrderedListOfObjects([event3, event2, event1], events, ['container_uuid', 'request_id'])
def event_start(cls, context, container_uuid, event_name, want_result=True): values = cls.pack_action_event_start(context, container_uuid, event_name) db_event = dbapi.action_event_start(context, values) if want_result: return cls._from_db_object(context, cls(context), db_event)
def test_container_action_event_start(self, mock_db_inst, mock__action_get_by_request_id, mock_write, mock_read): """Create a container action event.""" mock_db_inst.return_value = etcdapi.get_backend() mock_read.side_effect = etcd.EtcdKeyNotFound uuid = uuidutils.generate_uuid() action_values = self._create_action_values(uuid) action = dbapi.action_start(self.context, action_values) event_values = self._create_event_values(uuid) mock__action_get_by_request_id.return_value = action mock_read.side_effect = etcd.EtcdKeyNotFound event = dbapi.action_event_start(self.context, event_values) ignored_keys = self.IGNORED_FIELDS + ['finish_time', 'traceback', 'result', 'action_uuid', 'request_id', 'container_uuid', 'uuid'] self._assertEqualObjects(event_values, event, ignored_keys) event['start_time'] = datetime.isoformat(event['start_time']) mock_read.side_effect = lambda *args: FakeEtcdMultipleResult([event]) self._assertActionEventSaved(event, action['uuid'])
def test_container_action_events_get_in_order( self, mock_db_inst, mock__action_get_by_request_id, mock_write, mock_read): mock_db_inst.return_value = etcdapi.get_backend() mock_read.side_effect = etcd.EtcdKeyNotFound uuid1 = uuidutils.generate_uuid() action = dbapi.action_start(self.context, self._create_action_values(uuid1)) extra1 = { 'action_uuid': action['uuid'], 'created_at': timeutils.utcnow() } extra2 = { 'action_uuid': action['uuid'], 'created_at': timeutils.utcnow() + timedelta(seconds=5) } event_val1 = self._create_event_values(uuid1, 'fake1', extra=extra1) event_val2 = self._create_event_values(uuid1, 'fake2', extra=extra1) event_val3 = self._create_event_values(uuid1, 'fake3', extra=extra2) mock__action_get_by_request_id.return_value = action mock_read.side_effect = etcd.EtcdKeyNotFound event1 = dbapi.action_event_start(self.context, event_val1) event1['start_time'] = datetime.isoformat(event1['start_time']) mock__action_get_by_request_id.return_value = action mock_read.side_effect = etcd.EtcdKeyNotFound event2 = dbapi.action_event_start(self.context, event_val2) event2['start_time'] = datetime.isoformat(event2['start_time']) mock__action_get_by_request_id.return_value = action mock_read.side_effect = etcd.EtcdKeyNotFound event3 = dbapi.action_event_start(self.context, event_val3) event3['start_time'] = datetime.isoformat(event3['start_time']) mock_read.side_effect = lambda *args: FakeEtcdMultipleResult( [event1, event2, event3]) events = dbapi.action_events_get(self.context, action['uuid']) self.assertEqual(3, len(events)) self._assertEqualOrderedListOfObjects([event3, event2, event1], events, ['container_uuid', 'request_id'])
def test_container_action_event_finish_success(self): """Finish a container action event.""" uuid = uuidutils.generate_uuid() action = dbapi.action_start(self.context, self._create_action_values(uuid)) dbapi.action_event_start(self.context, self._create_event_values(uuid)) event_values = { 'finish_time': timeutils.utcnow() + datetime.timedelta(seconds=5), 'result': 'Success' } event_values = self._create_event_values(uuid, extra=event_values) event = dbapi.action_event_finish(self.context, event_values) self._assertActionEventSaved(event, action['id']) action = dbapi.action_get_by_request_id(self.context, uuid, self.context.request_id) self.assertNotEqual('Error', action['message'])
def test_container_action_event_finish_success(self): """Finish a container action event.""" uuid = uuidutils.generate_uuid() action = dbapi.action_start(self.context, self._create_action_values(uuid)) dbapi.action_event_start(self.context, self._create_event_values(uuid)) event_values = { 'finish_time': timeutils.utcnow() + timedelta(seconds=5), 'result': 'Success' } event_values = self._create_event_values(uuid, extra=event_values) event = dbapi.action_event_finish(self.context, event_values) self._assertActionEventSaved(event, action['id']) action = dbapi.action_get_by_request_id(self.context, uuid, self.context.request_id) self.assertNotEqual('Error', action['message'])
def test_container_action_event_start(self): """Create a container action event.""" uuid = uuidutils.generate_uuid() action_values = self._create_action_values(uuid) action = dbapi.action_start(self.context, action_values) event_values = self._create_event_values(uuid) event = dbapi.action_event_start(self.context, event_values) event_values['action_id'] = action['id'] ignored_keys = self.IGNORED_FIELDS + ['finish_time', 'traceback', 'result'] self._assertEqualObjects(event_values, event, ignored_keys) self._assertActionEventSaved(event, action['id'])
def test_container_action_event_finish_success( self, mock_db_inst, mock_get_event_by_name, mock__action_get_by_request_id, mock_update, mock_write, mock_read): """Finish a container action event.""" mock_db_inst.return_value = etcdapi.get_backend() mock_read.side_effect = etcd.EtcdKeyNotFound uuid = uuidutils.generate_uuid() action = dbapi.action_start(self.context, self._create_action_values(uuid)) event_values = self._create_event_values(uuid) event_values['action_uuid'] = action['uuid'] mock__action_get_by_request_id.return_value = action mock_read.side_effect = etcd.EtcdKeyNotFound event = dbapi.action_event_start(self.context, event_values) event_values = { 'finish_time': timeutils.utcnow() + timedelta(seconds=5), 'result': 'Success' } event_values = self._create_event_values(uuid, extra=event_values) mock__action_get_by_request_id.return_value = action mock_get_event_by_name.return_value = event mock_read.side_effect = lambda *args: FakeEtcdResult(event) event = dbapi.action_event_finish(self.context, event_values) event['start_time'] = datetime.isoformat(event['start_time']) mock_read.side_effect = lambda *args: FakeEtcdMultipleResult([event]) self._assertActionEventSaved(event, action['uuid']) mock_read.side_effect = lambda *args: FakeEtcdMultipleResult([action]) action = dbapi.action_get_by_request_id(self.context, uuid, self.context.request_id) self.assertNotEqual('Error', action['message'])