Skip to content

Commit 01d2815

Browse files
committed
Added test for nerc-rates loading. Mocks nerc-rates integration, sets up mock outages, creates test data, verifies call is called with correct parameters, verifies output is actually reflects reduced hours
1 parent e6798af commit 01d2815

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

src/coldfront_plugin_cloud/tests/unit/test_calculate_quota_unit_hours.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import csv
12
import datetime
23
import pytz
34
import tempfile
5+
from decimal import Decimal
46
from unittest.mock import patch
57

68
import freezegun
@@ -604,3 +606,67 @@ def test_load_excluded_intervals_invalid(self):
604606
]
605607
with self.assertRaises(AssertionError):
606608
utils.load_excluded_intervals(invalid_interval)
609+
610+
@patch(
611+
"coldfront_plugin_cloud.management.commands.calculate_storage_gb_hours.get_rates"
612+
)
613+
@patch("coldfront_plugin_cloud.utils.load_outages_from_nerc_rates")
614+
def test_calculate_storage_loads_nerc_outages(
615+
self, mock_load_outages, mock_rates_loader
616+
):
617+
"""Test calculate_storage_gb_hours loads outages from nerc-rates with correct parameters."""
618+
mock_outages = [
619+
(
620+
pytz.utc.localize(datetime.datetime(2020, 3, 10, 0, 0, 0)),
621+
pytz.utc.localize(datetime.datetime(2020, 3, 12, 0, 0, 0)),
622+
)
623+
]
624+
mock_load_outages.return_value = mock_outages
625+
mock_rates_loader.return_value.get_value_at.return_value = Decimal("0.001")
626+
627+
resource = self.new_openstack_resource(name="NERC")
628+
with freezegun.freeze_time("2020-03-01"):
629+
user = self.new_user()
630+
project = self.new_project(pi=user)
631+
allocation = self.new_allocation(project, resource, 100)
632+
utils.set_attribute_on_allocation(
633+
allocation, attributes.ALLOCATION_PROJECT_NAME, "test"
634+
)
635+
utils.set_attribute_on_allocation(
636+
allocation, attributes.ALLOCATION_PROJECT_ID, "123"
637+
)
638+
utils.set_attribute_on_allocation(
639+
allocation, attributes.QUOTA_VOLUMES_GB, 10
640+
)
641+
642+
with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".csv") as fp:
643+
output_file = fp.name
644+
645+
call_command(
646+
"calculate_storage_gb_hours",
647+
"--output",
648+
output_file,
649+
"--start",
650+
"2020-03-01",
651+
"--end",
652+
"2020-03-31",
653+
"--invoice-month",
654+
"2020-03",
655+
)
656+
657+
# Verify load_outages_from_nerc_rates was called with correct parameters
658+
start = pytz.utc.localize(datetime.datetime(2020, 3, 1, 0, 0, 0))
659+
end = pytz.utc.localize(datetime.datetime(2020, 3, 31, 0, 0, 0))
660+
mock_load_outages.assert_called_once_with(start, end, "stack")
661+
662+
# Verify the mocked outages were actually used to reduce billable hours
663+
with open(output_file, "r") as f:
664+
reader = csv.DictReader(f)
665+
rows = list(reader)
666+
667+
self.assertEqual(len(rows), 1)
668+
billable_hours = int(rows[0]["SU Hours (GBhr or SUhr)"])
669+
expected_hours = (
670+
(30 - 2) * 24 * 10
671+
) # 28 days × 24 hrs/day × 10 GB = 6,720 GB-hours
672+
self.assertEqual(billable_hours, expected_hours)

0 commit comments

Comments
 (0)