Skip to content

Commit

Permalink
Merge pull request #171 from QuanMPhm/bug/CRs
Browse files Browse the repository at this point in the history
Fixed billing with decreasing allocation change requests
  • Loading branch information
knikolla authored Aug 2, 2024
2 parents a9a7b01 + e523675 commit 07afcf0
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
import unittest
import pytz

import freezegun
Expand Down Expand Up @@ -177,6 +178,155 @@ def test_new_allocation_quota_never_approved(self):
)
self.assertEqual(value, 0)

def test_change_request_decrease(self):
"""Test for when a change request decreases the quota"""
self.resource = self.new_openshift_resource(
name="",
auth_url="",
)
user = self.new_user()
project = self.new_project(pi=user)
allocation = self.new_allocation(project, self.resource, 2)

with freezegun.freeze_time("2020-03-15 00:00:00"):
utils.set_attribute_on_allocation(
allocation, attributes.QUOTA_LIMITS_EPHEMERAL_STORAGE_GB, 2)

with freezegun.freeze_time("2020-03-17 00:00:00"):
cr = allocation_models.AllocationChangeRequest.objects.create(
allocation=allocation,
status = allocation_models.AllocationChangeStatusChoice.objects.filter(
name="Approved").first()
)
attr = allocation_models.AllocationAttribute.objects.filter(
allocation_attribute_type__name=attributes.QUOTA_LIMITS_EPHEMERAL_STORAGE_GB,
allocation=allocation
).first()
allocation_models.AllocationAttributeChangeRequest.objects.create(
allocation_change_request=cr,
allocation_attribute=attr,
new_value=0,
)

with freezegun.freeze_time("2020-03-19 00:00:00"):
utils.set_attribute_on_allocation(
allocation, attributes.QUOTA_LIMITS_EPHEMERAL_STORAGE_GB, 0)

allocation.refresh_from_db()

value = utils.calculate_quota_unit_hours(
allocation,
attributes.QUOTA_LIMITS_EPHEMERAL_STORAGE_GB,
pytz.utc.localize(datetime.datetime(2020, 3, 1, 0, 0, 1)),
pytz.utc.localize(datetime.datetime(2020, 3, 31, 23, 59, 59))
)
self.assertEqual(value, 96)

def test_change_request_increase(self):
"""Test for when a change request increases the quota"""
self.resource = self.new_openshift_resource(
name="",
auth_url="",
)
user = self.new_user()
project = self.new_project(pi=user)
allocation = self.new_allocation(project, self.resource, 2)

with freezegun.freeze_time("2020-03-15 00:00:00"):
utils.set_attribute_on_allocation(
allocation, attributes.QUOTA_LIMITS_EPHEMERAL_STORAGE_GB, 2)

with freezegun.freeze_time("2020-03-17 00:00:00"):
cr = allocation_models.AllocationChangeRequest.objects.create(
allocation=allocation,
status = allocation_models.AllocationChangeStatusChoice.objects.filter(
name="Approved").first()
)
attr = allocation_models.AllocationAttribute.objects.filter(
allocation_attribute_type__name=attributes.QUOTA_LIMITS_EPHEMERAL_STORAGE_GB,
allocation=allocation
).first()
allocation_models.AllocationAttributeChangeRequest.objects.create(
allocation_change_request=cr,
allocation_attribute=attr,
new_value=4,
)

with freezegun.freeze_time("2020-03-19 00:00:00"):
utils.set_attribute_on_allocation(
allocation, attributes.QUOTA_LIMITS_EPHEMERAL_STORAGE_GB, 4)

allocation.refresh_from_db()

value = utils.calculate_quota_unit_hours(
allocation,
attributes.QUOTA_LIMITS_EPHEMERAL_STORAGE_GB,
pytz.utc.localize(datetime.datetime(2020, 3, 1, 0, 0, 1)),
pytz.utc.localize(datetime.datetime(2020, 3, 20, 23, 59, 59))
)
self.assertEqual(value, 384)

def test_change_request_decrease_multiple(self):
"""Test for when multiple different change request decreases the quota"""
self.resource = self.new_openshift_resource(
name="",
auth_url="",
)
user = self.new_user()
project = self.new_project(pi=user)
allocation = self.new_allocation(project, self.resource, 2)

with freezegun.freeze_time("2020-03-15 00:00:00"):
utils.set_attribute_on_allocation(
allocation, attributes.QUOTA_LIMITS_EPHEMERAL_STORAGE_GB, 2)

# In this case, approved CR is the first CR submitted
with freezegun.freeze_time("2020-03-16 00:00:00"):
cr = allocation_models.AllocationChangeRequest.objects.create(
allocation=allocation,
status = allocation_models.AllocationChangeStatusChoice.objects.filter(
name="Approved").first()
)
attr = allocation_models.AllocationAttribute.objects.filter(
allocation_attribute_type__name=attributes.QUOTA_LIMITS_EPHEMERAL_STORAGE_GB,
allocation=allocation
).first()
allocation_models.AllocationAttributeChangeRequest.objects.create(
allocation_change_request=cr,
allocation_attribute=attr,
new_value=0,
)

with freezegun.freeze_time("2020-03-17 00:00:00"):
cr = allocation_models.AllocationChangeRequest.objects.create(
allocation=allocation,
status = allocation_models.AllocationChangeStatusChoice.objects.filter(
name="Approved").first()
)
attr = allocation_models.AllocationAttribute.objects.filter(
allocation_attribute_type__name=attributes.QUOTA_LIMITS_EPHEMERAL_STORAGE_GB,
allocation=allocation
).first()
allocation_models.AllocationAttributeChangeRequest.objects.create(
allocation_change_request=cr,
allocation_attribute=attr,
new_value=1,
)

with freezegun.freeze_time("2020-03-19 00:00:00"):
utils.set_attribute_on_allocation(
allocation, attributes.QUOTA_LIMITS_EPHEMERAL_STORAGE_GB, 0)

allocation.refresh_from_db()

value = utils.calculate_quota_unit_hours(
allocation,
attributes.QUOTA_LIMITS_EPHEMERAL_STORAGE_GB,
pytz.utc.localize(datetime.datetime(2020, 3, 1, 0, 0, 1)),
pytz.utc.localize(datetime.datetime(2020, 3, 31, 23, 59, 59))
)
self.assertEqual(value, 48)

def test_new_allocation_quota_change_request(self):
self.resource = self.new_openshift_resource(
name="",
Expand Down
2 changes: 1 addition & 1 deletion src/coldfront_plugin_cloud/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def calculate_quota_unit_hours(allocation: Allocation,
# find one that happened just before the next event.
cr_created_at = cr.history.first().created
if cr.history.first().created <= event_time:
if unbounded_last_event_time and unbounded_last_event_time < cr_created_at:
if unbounded_last_event_time and unbounded_last_event_time > cr_created_at:
# But after the unbounded last event time.
continue

Expand Down

0 comments on commit 07afcf0

Please sign in to comment.