def test_process_delete(self): delete_time = datetime.datetime.utcnow() launch_time = delete_time-datetime.timedelta(days=1) launch_decimal = utils.decimal_utc(launch_time) delete_decimal = utils.decimal_utc(delete_time) notif = utils.create_nova_notif(request_id=REQUEST_ID_1, launched=str(launch_time), deleted=str(delete_time)) json_str = json.dumps(notif) event = 'compute.instance.delete.end' raw = utils.create_raw(self.mox, delete_decimal, event=event, json_str=json_str) delete = self.mox.CreateMockAnything() delete.instance = INSTANCE_ID_1 delete.launched_at = launch_decimal delete.deleted_at = delete_decimal views.STACKDB.create_instance_delete(instance=INSTANCE_ID_1, launched_at=launch_decimal, deleted_at=delete_decimal, raw=raw)\ .AndReturn(delete) views.STACKDB.save(delete) self.mox.ReplayAll() views._process_delete(raw, notif[1]) self.assertEqual(delete.instance, INSTANCE_ID_1) self.assertEqual(delete.launched_at, launch_decimal) self.assertEqual(delete.deleted_at, delete_decimal) self.mox.VerifyAll()
def test_process_usage_for_updates_revert_end(self): when_time = datetime.datetime.utcnow() when_decimal = utils.decimal_utc(when_time) notif = utils.create_nova_notif(request_id=REQUEST_ID_1, launched=str(when_time)) json_str = json.dumps(notif) event = 'compute.instance.resize.revert.end' raw = utils.create_raw(self.mox, when_decimal, event=event, json_str=json_str) usage = self.mox.CreateMockAnything() usage.instance = INSTANCE_ID_1 usage.request_id = REQUEST_ID_1 usage.instance_type_id = '1' views.STACKDB.get_or_create_instance_usage(instance=INSTANCE_ID_1, request_id=REQUEST_ID_1)\ .AndReturn((usage, True)) views.STACKDB.save(usage) self.mox.ReplayAll() views._process_usage_for_updates(raw, notif[1]) self.assertEqual(usage.instance, INSTANCE_ID_1) self.assertEqual(usage.request_id, REQUEST_ID_1) self.assertEqual(usage.instance_type_id, '1') self.assertEqual(usage.launched_at, when_decimal) self.mox.VerifyAll()
def test_process_exists_with_deleted_at(self): launch_time = datetime.datetime.utcnow()-datetime.timedelta(hours=23) launch_decimal = utils.decimal_utc(launch_time) deleted_time = datetime.datetime.utcnow()-datetime.timedelta(hours=12) deleted_decimal = utils.decimal_utc(deleted_time) current_time = datetime.datetime.utcnow() current_decimal = utils.decimal_utc(current_time) notif = utils.create_nova_notif(launched=str(launch_time), deleted=str(deleted_time)) json_str = json.dumps(notif) event = 'compute.instance.exists' raw = utils.create_raw(self.mox, current_decimal, event=event, json_str=json_str) usage = self.mox.CreateMockAnything() views.STACKDB.get_instance_usage(instance=INSTANCE_ID_1, launched_at=launch_decimal)\ .AndReturn(usage) exists_values = { 'message_id': MESSAGE_ID_1, 'instance': INSTANCE_ID_1, 'launched_at': launch_decimal, 'deleted_at': deleted_decimal, 'instance_type_id': '1', 'usage': usage, 'raw': raw, } exists = self.mox.CreateMockAnything() views.STACKDB.create_instance_exists(**exists_values).AndReturn(exists) views.STACKDB.save(exists) self.mox.ReplayAll() views._process_exists(raw) self.mox.VerifyAll()
def test_process_exists_with_deleted_at(self): current_time = datetime.datetime.utcnow() launch_time = current_time - datetime.timedelta(hours=23) launch_decimal = utils.decimal_utc(launch_time) deleted_time = current_time - datetime.timedelta(hours=12) deleted_decimal = utils.decimal_utc(deleted_time) current_decimal = utils.decimal_utc(current_time) audit_beginning = current_time - datetime.timedelta(hours=20) audit_beginning_decimal = utils.decimal_utc(audit_beginning) audit_ending_decimal = utils.decimal_utc(current_time) notif = utils.create_nova_notif(launched=str(launch_time), deleted=str(deleted_time), audit_period_beginning=str(audit_beginning), audit_period_ending=str(current_time), tenant_id=TENANT_ID_1, os_architecture=OS_ARCH_1, os_version=OS_VERSION_1, os_distro=OS_DISTRO_1, rax_options=RAX_OPTIONS_1) json_str = json.dumps(notif) event = 'compute.instance.exists' raw = utils.create_raw(self.mox, current_decimal, event=event, json_str=json_str) usage = self.mox.CreateMockAnything() launched_range = (launch_decimal, launch_decimal+1) views.STACKDB.get_instance_usage(instance=INSTANCE_ID_1, launched_at__range=launched_range)\ .AndReturn(usage) delete = self.mox.CreateMockAnything() views.STACKDB.get_instance_delete(instance=INSTANCE_ID_1, launched_at__range=launched_range)\ .AndReturn(delete) exists_values = { 'message_id': MESSAGE_ID_1, 'instance': INSTANCE_ID_1, 'launched_at': launch_decimal, 'deleted_at': deleted_decimal, 'audit_period_beginning': audit_beginning_decimal, 'audit_period_ending': audit_ending_decimal, 'instance_type_id': '1', 'usage': usage, 'delete': delete, 'raw': raw, 'tenant': TENANT_ID_1, 'rax_options': RAX_OPTIONS_1, 'os_architecture': OS_ARCH_1, 'os_version': OS_VERSION_1, 'os_distro': OS_DISTRO_1 } exists = self.mox.CreateMockAnything() views.STACKDB.create_instance_exists(**exists_values).AndReturn(exists) views.STACKDB.save(exists) self.mox.ReplayAll() views._process_exists(raw, notif[1]) self.mox.VerifyAll()
def test_process_usage_for_updates_create_end(self): kwargs = {'launched': str(DUMMY_TIME), 'tenant_id': TENANT_ID_1} notification = utils.create_nova_notif(request_id=REQUEST_ID_1, **kwargs) event = 'compute.instance.create.end' raw, usage = self._setup_process_usage_mocks(event, notification) views._process_usage_for_updates(raw, notification[1]) self.assertEqual(usage.launched_at, utils.decimal_utc(DUMMY_TIME)) self.assertEqual(usage.tenant, TENANT_ID_1) self.mox.VerifyAll()
def test_process_usage_for_new_launch_rebuild_start(self): kwargs = {'launched': str(DUMMY_TIME), 'tenant_id': TENANT_ID_1} notification = utils.create_nova_notif(request_id=REQUEST_ID_1, **kwargs) event = 'compute.instance.rebuild.start' raw, usage = self._setup_process_usage_mocks(event, notification) views._process_usage_for_new_launch(raw, notification[1]) self.assertEquals(usage.instance_type_id, '1') self.assertEquals(usage.tenant, TENANT_ID_1) self.mox.VerifyAll()
def test_process_usage_for_updates_prep_end(self): kwargs = {'launched': str(DUMMY_TIME), 'new_type_id': INSTANCE_TYPE_ID_2, 'tenant_id': TENANT_ID_1} notification = utils.create_nova_notif(request_id=REQUEST_ID_1, **kwargs) event = 'compute.instance.resize.prep.end' raw, usage = self._setup_process_usage_mocks(event, notification) views._process_usage_for_updates(raw, notification[1]) self.assertEqual(usage.instance_type_id, INSTANCE_TYPE_ID_2) self.assertEquals(usage.tenant, TENANT_ID_1) self.mox.VerifyAll()
def test_process_usage_for_new_launch_resize_revert_start_when_no_launched_at_in_db(self): kwargs = {'launched': str(DUMMY_TIME), 'tenant_id': TENANT_ID_1} notification = utils.create_nova_notif(request_id=REQUEST_ID_1, **kwargs) event = 'compute.instance.resize.revert.start' raw, usage = self._setup_process_usage_mocks(event, notification) usage.launched_at = None views._process_usage_for_new_launch(raw, notification[1]) self.assertEquals(usage.tenant, TENANT_ID_1) self.assertEqual(usage.launched_at, utils.decimal_utc(DUMMY_TIME)) self.mox.VerifyAll()
def test_process_usage_for_new_launch_rebuild_start_when_no_launched_at_in_db(self): kwargs = {'launched': str(DUMMY_TIME), 'tenant_id': TENANT_ID_1} notification = utils.create_nova_notif(request_id=REQUEST_ID_1, **kwargs) event = 'compute.instance.rebuild.start' raw, usage = self._setup_process_usage_mocks(event, notification) usage.launched_at = None views._process_usage_for_new_launch(raw, notification[1]) self.assertEqual(usage.launched_at, utils.decimal_utc(DUMMY_TIME)) self.assertEquals(usage.tenant, TENANT_ID_1) self.mox.VerifyAll()
def test_process_usage_for_new_launch_resize_prep_start_when_launched_at_in_db(self): kwargs = {'launched': str(DUMMY_TIME), 'tenant_id': TENANT_ID_1} notification = utils.create_nova_notif(request_id=REQUEST_ID_1, **kwargs) event = 'compute.instance.resize.prep.start' raw, usage = self._setup_process_usage_mocks(event, notification) orig_launched_at = utils.decimal_utc(DUMMY_TIME - datetime.timedelta(days=1)) usage.launched_at = orig_launched_at views._process_usage_for_new_launch(raw, notification[1]) self.assertEqual(usage.launched_at, orig_launched_at) self.assertEqual(usage.tenant, TENANT_ID_1) self.mox.VerifyAll()
def test_process_usage_for_new_launch(self): when = utils.decimal_utc() notif = utils.create_nova_notif(request_id=REQUEST_ID_1) json_str = json.dumps(notif) event = 'compute.instance.create.start' raw = utils.create_raw(self.mox, when, event=event, json_str=json_str) usage = self.mox.CreateMockAnything() views.STACKDB.get_or_create_instance_usage(instance=INSTANCE_ID_1, request_id=REQUEST_ID_1)\ .AndReturn((usage, True)) views.STACKDB.save(usage) self.mox.ReplayAll() views._process_usage_for_new_launch(raw, notif[1]) self.assertEquals(usage.instance_type_id, '1') self.mox.VerifyAll()
def test_process_usage_for_updates_create_end_error_message(self): kwargs = {'launched': str(DUMMY_TIME), 'tenant_id': TENANT_ID_1} notification = utils.create_nova_notif(request_id=REQUEST_ID_1, **kwargs) notification[1]['payload']['message'] = "Error" event = 'compute.instance.create.end' when_time = DUMMY_TIME when_decimal = utils.decimal_utc(when_time) json_str = json.dumps(notification) raw = utils.create_raw(self.mox, when_decimal, event=event, json_str=json_str) self.mox.ReplayAll() views._process_usage_for_updates(raw, notification[1]) self.mox.VerifyAll()
def test_process_usage_for_new_launch_rebuild_start(self): kwargs = {'launched': str(DUMMY_TIME), 'tenant_id': TENANT_ID_1, 'rax_options': RAX_OPTIONS_1, 'os_architecture': OS_ARCH_1, 'os_version': OS_VERSION_1, 'os_distro': OS_DISTRO_1 } notification = utils.create_nova_notif(request_id=REQUEST_ID_1, **kwargs) event = 'compute.instance.rebuild.start' raw, usage = self._setup_process_usage_mocks(event, notification) views._process_usage_for_new_launch(raw, notification[1]) self.assertEquals(usage.instance_type_id, '1') self.assertEquals(usage.tenant, TENANT_ID_1) self.assertEquals(usage.os_architecture, OS_ARCH_1) self.assertEquals(usage.os_version, OS_VERSION_1) self.assertEquals(usage.os_distro, OS_DISTRO_1) self.assertEquals(usage.rax_options, RAX_OPTIONS_1) self.mox.VerifyAll()
def test_process_exists_with_deleted_at(self): current_time = datetime.datetime.utcnow() launch_time = current_time - datetime.timedelta(hours=23) launch_decimal = utils.decimal_utc(launch_time) deleted_time = current_time - datetime.timedelta(hours=12) deleted_decimal = utils.decimal_utc(deleted_time) current_decimal = utils.decimal_utc(current_time) audit_beginning = current_time - datetime.timedelta(hours=20) audit_beginning_decimal = utils.decimal_utc(audit_beginning) audit_ending_decimal = utils.decimal_utc(current_time) notif = utils.create_nova_notif(launched=str(launch_time), deleted=str(deleted_time), audit_period_beginning=str(audit_beginning), audit_period_ending=str(current_time), tenant_id=TENANT_ID_1) json_str = json.dumps(notif) event = 'compute.instance.exists' raw = utils.create_raw(self.mox, current_decimal, event=event, json_str=json_str) usage = self.mox.CreateMockAnything() launched_range = (launch_decimal, launch_decimal+1) views.STACKDB.get_instance_usage(instance=INSTANCE_ID_1, launched_at__range=launched_range)\ .AndReturn(usage) delete = self.mox.CreateMockAnything() views.STACKDB.get_instance_delete(instance=INSTANCE_ID_1, launched_at__range=launched_range)\ .AndReturn(delete) exists_values = { 'message_id': MESSAGE_ID_1, 'instance': INSTANCE_ID_1, 'launched_at': launch_decimal, 'deleted_at': deleted_decimal, 'audit_period_beginning': audit_beginning_decimal, 'audit_period_ending': audit_ending_decimal, 'instance_type_id': '1', 'usage': usage, 'delete': delete, 'raw': raw, 'tenant': TENANT_ID_1 } exists = self.mox.CreateMockAnything() views.STACKDB.create_instance_exists(**exists_values).AndReturn(exists) views.STACKDB.save(exists) self.mox.ReplayAll() views._process_exists(raw, notif[1]) self.mox.VerifyAll()
def test_process_exists_no_launched_at(self): current_time = datetime.datetime.utcnow() current_decimal = utils.decimal_utc(current_time) audit_beginning = current_time - datetime.timedelta(hours=20) notif = utils.create_nova_notif(audit_period_beginning=str(audit_beginning), audit_period_ending=str(current_time), tenant_id=TENANT_ID_1) json_str = json.dumps(notif) event = 'compute.instance.exists' raw = utils.create_raw(self.mox, current_decimal, event=event, json_str=json_str) raw.id = 1 self.setup_mock_log() self.log.warn('Ignoring exists without launched_at. RawData(1)') self.mox.ReplayAll() views._process_exists(raw, notif[1]) self.mox.VerifyAll()
def test_process_usage_for_new_launch_resize_prep_start_when_no_launched_at_in_db(self): kwargs = {'launched': str(DUMMY_TIME), 'tenant_id': TENANT_ID_1, 'rax_options': RAX_OPTIONS_1, 'os_architecture': OS_ARCH_1, 'os_version': OS_VERSION_1, 'os_distro': OS_DISTRO_1 } notification = utils.create_nova_notif(request_id=REQUEST_ID_1, **kwargs) event = 'compute.instance.resize.prep.start' raw, usage = self._setup_process_usage_mocks(event, notification) usage.launched_at = None views._process_usage_for_new_launch(raw, notification[1]) self.assertEqual(usage.launched_at, utils.decimal_utc(DUMMY_TIME)) self.assertEquals(usage.tenant, TENANT_ID_1) self.assertEquals(usage.os_architecture, OS_ARCH_1) self.assertEquals(usage.os_version, OS_VERSION_1) self.assertEquals(usage.os_distro, OS_DISTRO_1) self.assertEquals(usage.rax_options, RAX_OPTIONS_1) self.mox.VerifyAll()
def test_process_usage_for_new_launch_resize_no_launched_at_in_db(self): now = datetime.datetime.utcnow() when = utils.decimal_utc(now) notif = utils.create_nova_notif(request_id=REQUEST_ID_1, launched=str(now)) json_str = json.dumps(notif) event = 'compute.instance.resize.prep.start' raw = utils.create_raw(self.mox, when, event=event, json_str=json_str) usage = self.mox.CreateMockAnything() usage.launched_at = None views.STACKDB.get_or_create_instance_usage(instance=INSTANCE_ID_1, request_id=REQUEST_ID_1) \ .AndReturn((usage, True)) views.STACKDB.save(usage) self.mox.ReplayAll() views._process_usage_for_new_launch(raw, notif[1]) self.assertEqual(usage.launched_at, when) self.mox.VerifyAll()
def test_process_usage_for_updates_create_end_success_message(self): kwargs = {'launched': str(DUMMY_TIME), 'tenant_id': TENANT_ID_1, 'rax_options': RAX_OPTIONS_1, 'os_architecture': OS_ARCH_1, 'os_version': OS_VERSION_1, 'os_distro': OS_DISTRO_1 } notification = utils.create_nova_notif(request_id=REQUEST_ID_1, **kwargs) notification[1]['payload']['message'] = "Success" event = 'compute.instance.create.end' raw, usage = self._setup_process_usage_mocks(event, notification) views._process_usage_for_updates(raw, notification[1]) self.assertEqual(usage.launched_at, utils.decimal_utc(DUMMY_TIME)) self.assertEqual(usage.tenant, TENANT_ID_1) self.assertEquals(usage.os_architecture, OS_ARCH_1) self.assertEquals(usage.os_version, OS_VERSION_1) self.assertEquals(usage.os_distro, OS_DISTRO_1) self.assertEquals(usage.rax_options, RAX_OPTIONS_1) self.mox.VerifyAll()
def test_process_exists(self): current_time = datetime.datetime.utcnow() launch_time = current_time - datetime.timedelta(hours=23) launch_decimal = utils.decimal_utc(launch_time) current_decimal = utils.decimal_utc(current_time) audit_beginning = current_time - datetime.timedelta(hours=20) audit_beginning_decimal = utils.decimal_utc(audit_beginning) audit_ending_decimal = utils.decimal_utc(current_time) notif = utils.create_nova_notif(launched=str(launch_time), audit_period_beginning=str(audit_beginning), audit_period_ending=str(current_time)) json_str = json.dumps(notif) event = 'compute.instance.exists' raw = utils.create_raw(self.mox, current_decimal, event=event, json_str=json_str) usage = self.mox.CreateMockAnything() launched_range = (launch_decimal, launch_decimal+1) views.STACKDB.get_instance_usage(instance=INSTANCE_ID_1, launched_at__range=launched_range)\ .AndReturn(usage) views.STACKDB.get_instance_delete(instance=INSTANCE_ID_1, launched_at__range=launched_range)\ .AndReturn(None) exists_values = { 'message_id': MESSAGE_ID_1, 'instance': INSTANCE_ID_1, 'launched_at': launch_decimal, 'audit_period_beginning': audit_beginning_decimal, 'audit_period_ending': audit_ending_decimal, 'instance_type_id': '1', 'usage': usage, 'raw': raw, } exists = self.mox.CreateMockAnything() views.STACKDB.create_instance_exists(**exists_values).AndReturn(exists) views.STACKDB.save(exists) self.mox.ReplayAll() views._process_exists(raw, notif[1]) self.mox.VerifyAll()
def test_process_delete_no_launch(self): delete_time = datetime.datetime.utcnow() delete_decimal = utils.decimal_utc(delete_time) notif = utils.create_nova_notif(request_id=REQUEST_ID_1, deleted=str(delete_time)) json_str = json.dumps(notif) event = 'compute.instance.delete.end' raw = utils.create_raw(self.mox, delete_decimal, event=event, json_str=json_str) delete = self.mox.CreateMockAnything() delete.instance = INSTANCE_ID_1 delete.deleted_at = delete_decimal views.STACKDB.get_or_create_instance_delete(instance=INSTANCE_ID_1, deleted_at=delete_decimal)\ .AndReturn((delete, True)) views.STACKDB.save(delete) self.mox.ReplayAll() views._process_delete(raw, notif[1]) self.assertEqual(delete.instance, INSTANCE_ID_1) self.assertEqual(delete.deleted_at, delete_decimal) self.mox.VerifyAll()