Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed billing with decreasing allocation change requests #171

Merged
merged 2 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
naved001 marked this conversation as resolved.
Show resolved Hide resolved

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
Loading