VIRT-2874: Using terminated_at value instead of deleted_at from the request to use as the deleted_at field in the DB. Unit test changes also included

PHASE-II Changed test cases since the current ones were logically incorrect.

Change-Id: Idb3d42a245711fc72c32ab66b395a7aa67e1bb87
This commit is contained in:
Isham Ibrahim 2017-07-18 18:18:33 +05:30
parent b7aa9c8980
commit 89ff15391b
2 changed files with 66 additions and 68 deletions

View File

@ -267,10 +267,10 @@ def _process_delete(raw, notification):
if notification.launched_at and notification.launched_at != '': if notification.launched_at and notification.launched_at != '':
instance_id = notification.instance instance_id = notification.instance
deleted_at = None deleted_at = None
if notification.deleted_at: if notification.terminated_at:
deleted_at = utils.str_time_to_unix(notification.deleted_at)
elif notification.terminated_at:
deleted_at = utils.str_time_to_unix(notification.terminated_at) deleted_at = utils.str_time_to_unix(notification.terminated_at)
elif notification.deleted_at:
deleted_at = utils.str_time_to_unix(notification.deleted_at)
launched_at = utils.str_time_to_unix(notification.launched_at) launched_at = utils.str_time_to_unix(notification.launched_at)
values = { values = {
'instance': instance_id, 'instance': instance_id,

View File

@ -18,6 +18,7 @@ import datetime
import json import json
import mox import mox
from mock import MagicMock, patch
import utils import utils
from utils import BANDWIDTH_PUBLIC_OUTBOUND from utils import BANDWIDTH_PUBLIC_OUTBOUND
@ -748,96 +749,93 @@ class StacktachUsageParsingTestCase(StacktachBaseTestCase):
self.mox.VerifyAll() self.mox.VerifyAll()
def test_process_delete(self): @patch('stacktach.views.STACKDB')
def test_process_delete(self, stackdb_mock):
delete_time = datetime.datetime.utcnow() delete_time = datetime.datetime.utcnow()
terminated_time = delete_time-datetime.timedelta(seconds=1) terminated_time = delete_time-datetime.timedelta(seconds=1)
launch_time = delete_time-datetime.timedelta(days=1) launch_time = delete_time-datetime.timedelta(days=1)
launch_decimal = utils.decimal_utc(launch_time) launch_decimal = utils.decimal_utc(launch_time)
delete_decimal = utils.decimal_utc(delete_time) terminated_decimal = utils.decimal_utc(terminated_time)
notification = self.mox.CreateMockAnything() notification = MagicMock()
notification.instance = INSTANCE_ID_1 notification.instance = INSTANCE_ID_1
notification.deleted_at = str(delete_time) notification.deleted_at = str(delete_time)
notification.terminated_at = str(terminated_time) notification.terminated_at = str(terminated_time)
notification.launched_at = str(launch_time) notification.launched_at = str(launch_time)
raw = MagicMock()
raw = self.mox.CreateMockAnything() delete = MagicMock()
delete = self.mox.CreateMockAnything()
delete.instance = INSTANCE_ID_1 delete.instance = INSTANCE_ID_1
delete.launched_at = launch_decimal delete.launched_at = launch_decimal
delete.deleted_at = delete_decimal # Since there is a terminated_at arg present, it will be used as the deleted_at value.
views.STACKDB.get_or_create_instance_delete( delete.deleted_at = terminated_decimal
instance=INSTANCE_ID_1, deleted_at=delete_decimal,
launched_at=launch_decimal)\
.AndReturn((delete, True))
views.STACKDB.save(delete)
self.mox.ReplayAll()
stack_db_obj = MagicMock()
stackdb_mock.get_or_create_instance_delete.return_value = (stack_db_obj, False)
from stacktach import views
views._process_delete(raw, notification) views._process_delete(raw, notification)
self.assertEqual(delete.instance, INSTANCE_ID_1) stackdb_mock.get_or_create_instance_delete.assert_called_with(instance=delete.instance,
self.assertEqual(delete.launched_at, launch_decimal) deleted_at=delete.deleted_at,
self.assertEqual(delete.deleted_at, delete_decimal) launched_at=delete.launched_at)
self.mox.VerifyAll() stackdb_mock.save.assert_called_with(stack_db_obj)
def test_process_delete_with_only_terminated_at(self): @patch('stacktach.views.STACKDB')
def test_process_delete_with_only_terminated_at(self, stackdb_mock):
# 1. Creating test values
terminated_time = datetime.datetime.utcnow()
launch_time = terminated_time-datetime.timedelta(days=1)
launch_decimal = utils.decimal_utc(launch_time)
terminated_decimal = utils.decimal_utc(terminated_time)
notification = MagicMock()
notification.instance = INSTANCE_ID_1
notification.deleted_at = ''
notification.terminated_at = str(terminated_time)
notification.launched_at = str(launch_time)
raw = MagicMock()
delete = MagicMock()
delete.instance = INSTANCE_ID_1
delete.launched_at = launch_decimal
# Since there is a terminated_at arg present, it will be used as the deleted_at value.
delete.deleted_at = terminated_decimal
# 2. Mocking methods
stack_db_obj = MagicMock()
stackdb_mock.get_or_create_instance_delete.return_value = (stack_db_obj, False)
from stacktach import views
views._process_delete(raw, notification)
# 3. Assert statements
stackdb_mock.get_or_create_instance_delete.assert_called_with(instance=delete.instance,
deleted_at=delete.deleted_at,
launched_at=delete.launched_at)
stackdb_mock.save.assert_called_with(stack_db_obj)
@patch('stacktach.views.STACKDB')
def test_process_delete_with_only_deleted_at(self, stackdb_mock):
# 1. Creating test values
delete_time = datetime.datetime.utcnow() delete_time = datetime.datetime.utcnow()
launch_time = delete_time-datetime.timedelta(days=1) launch_time = delete_time-datetime.timedelta(days=1)
launch_decimal = utils.decimal_utc(launch_time) launch_decimal = utils.decimal_utc(launch_time)
delete_decimal = utils.decimal_utc(delete_time) delete_decimal = utils.decimal_utc(delete_time)
notification = self.mox.CreateMockAnything() notification = MagicMock()
notification.instance = INSTANCE_ID_1 notification.instance = INSTANCE_ID_1
notification.deleted_at = '' notification.deleted_at = str(delete_time)
notification.terminated_at = str(delete_time) notification.terminated_at = ''
notification.launched_at = str(launch_time) notification.launched_at = str(launch_time)
raw = self.mox.CreateMockAnything() raw = MagicMock()
delete = self.mox.CreateMockAnything() delete = MagicMock()
delete.instance = INSTANCE_ID_1 delete.instance = INSTANCE_ID_1
delete.launched_at = launch_decimal delete.launched_at = launch_decimal
# Since there is no terminated_at arg present, the deleted_at arg will be used as the deleted_at value.
delete.deleted_at = delete_decimal delete.deleted_at = delete_decimal
views.STACKDB.get_or_create_instance_delete( # 2. Mocking methods
instance=INSTANCE_ID_1, deleted_at=delete_decimal, stack_db_obj = MagicMock()
launched_at=launch_decimal)\ stackdb_mock.get_or_create_instance_delete.return_value = (stack_db_obj, False)
.AndReturn((delete, True)) from stacktach import views
views.STACKDB.save(delete)
self.mox.ReplayAll()
views._process_delete(raw, notification) views._process_delete(raw, notification)
# 3. Assert statements
self.assertEqual(delete.instance, INSTANCE_ID_1) stackdb_mock.get_or_create_instance_delete.assert_called_with(instance=delete.instance,
self.assertEqual(delete.launched_at, launch_decimal) deleted_at=delete.deleted_at,
self.assertEqual(delete.deleted_at, delete_decimal) launched_at=delete.launched_at)
self.mox.VerifyAll() stackdb_mock.save.assert_called_with(stack_db_obj)
def test_process_delete_with_neither(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)
notification = self.mox.CreateMockAnything()
notification.instance = INSTANCE_ID_1
notification.deleted_at = ''
notification.terminated_at = str(delete_time)
notification.launched_at = str(launch_time)
raw = self.mox.CreateMockAnything()
delete = self.mox.CreateMockAnything()
delete.instance = INSTANCE_ID_1
delete.launched_at = launch_decimal
delete.deleted_at = delete_decimal
views.STACKDB.get_or_create_instance_delete(
instance=INSTANCE_ID_1, deleted_at=delete_decimal,
launched_at=launch_decimal)\
.AndReturn((delete, True))
views.STACKDB.save(delete)
self.mox.ReplayAll()
views._process_delete(raw, notification)
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_delete_no_launch(self): def test_process_delete_no_launch(self):
delete_time = datetime.datetime.utcnow() delete_time = datetime.datetime.utcnow()