-
Notifications
You must be signed in to change notification settings - Fork 13
Integrate nerc-rates for dynamic outage information loading #261
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
f687493
634e3f2
e6798af
01d2815
85d324b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,9 @@ | ||
| import csv | ||
| import datetime | ||
| import pytz | ||
| import tempfile | ||
| from decimal import Decimal | ||
| from unittest.mock import patch | ||
|
|
||
| import freezegun | ||
|
|
||
|
|
@@ -16,7 +19,11 @@ | |
|
|
||
|
|
||
| class TestCalculateAllocationQuotaHours(base.TestBase): | ||
| def test_new_allocation_quota(self): | ||
| @patch("coldfront_plugin_cloud.utils.load_outages_from_nerc_rates") | ||
| def test_new_allocation_quota(self, mock_load_outages): | ||
| """Test quota calculation with nerc-rates outages mocked.""" | ||
| mock_load_outages.return_value = [] | ||
|
|
||
| self.resource = self.new_openshift_resource( | ||
| name="", | ||
| ) | ||
|
|
@@ -63,7 +70,7 @@ def test_new_allocation_quota(self): | |
| "2020-03", | ||
| ) | ||
|
|
||
| # Let's test a complete CLI call including excluded time, while we're at it. This is not for testing | ||
| # Let's test a complete CLI call. This is not for testing | ||
| # the validity but just the unerrored execution of the complete pipeline. | ||
| # Tests that verify the correct output are further down in the test file. | ||
| with tempfile.NamedTemporaryFile() as fp: | ||
|
|
@@ -83,10 +90,12 @@ def test_new_allocation_quota(self): | |
| "0.00001", | ||
| "--invoice-month", | ||
| "2020-03", | ||
| "--excluded-time-ranges", | ||
| "2020-03-02 00:00:00,2020-03-03 05:00:00", | ||
| ) | ||
|
|
||
| # Verify that load_outages_from_nerc_rates is not called when resource name | ||
| # doesn't match NERC service mapping | ||
| mock_load_outages.assert_not_called() | ||
|
|
||
| def test_new_allocation_quota_expired(self): | ||
| """Test that expiration doesn't affect invoicing.""" | ||
| self.resource = self.new_openshift_resource( | ||
|
|
@@ -597,3 +606,67 @@ def test_load_excluded_intervals_invalid(self): | |
| ] | ||
| with self.assertRaises(AssertionError): | ||
| utils.load_excluded_intervals(invalid_interval) | ||
|
|
||
| @patch( | ||
| "coldfront_plugin_cloud.management.commands.calculate_storage_gb_hours.get_rates" | ||
| ) | ||
| @patch("coldfront_plugin_cloud.utils.load_outages_from_nerc_rates") | ||
| def test_calculate_storage_loads_nerc_outages( | ||
| self, mock_load_outages, mock_rates_loader | ||
| ): | ||
| """Test calculate_storage_gb_hours loads outages from nerc-rates with correct parameters.""" | ||
| mock_outages = [ | ||
| ( | ||
| pytz.utc.localize(datetime.datetime(2020, 3, 10, 0, 0, 0)), | ||
| pytz.utc.localize(datetime.datetime(2020, 3, 12, 0, 0, 0)), | ||
| ) | ||
| ] | ||
| mock_load_outages.return_value = mock_outages | ||
| mock_rates_loader.return_value.get_value_at.return_value = Decimal("0.001") | ||
|
|
||
| resource = self.new_openstack_resource(name="NERC") | ||
|
||
| with freezegun.freeze_time("2020-03-01"): | ||
| user = self.new_user() | ||
| project = self.new_project(pi=user) | ||
| allocation = self.new_allocation(project, resource, 100) | ||
| utils.set_attribute_on_allocation( | ||
| allocation, attributes.ALLOCATION_PROJECT_NAME, "test" | ||
| ) | ||
| utils.set_attribute_on_allocation( | ||
| allocation, attributes.ALLOCATION_PROJECT_ID, "123" | ||
| ) | ||
| utils.set_attribute_on_allocation( | ||
| allocation, attributes.QUOTA_VOLUMES_GB, 10 | ||
| ) | ||
|
|
||
| with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".csv") as fp: | ||
| output_file = fp.name | ||
|
|
||
| call_command( | ||
| "calculate_storage_gb_hours", | ||
| "--output", | ||
| output_file, | ||
| "--start", | ||
| "2020-03-01", | ||
| "--end", | ||
| "2020-03-31", | ||
| "--invoice-month", | ||
| "2020-03", | ||
| ) | ||
|
|
||
| # Verify load_outages_from_nerc_rates was called with correct parameters | ||
| start = pytz.utc.localize(datetime.datetime(2020, 3, 1, 0, 0, 0)) | ||
| end = pytz.utc.localize(datetime.datetime(2020, 3, 31, 0, 0, 0)) | ||
| mock_load_outages.assert_called_once_with(start, end, "stack") | ||
|
|
||
| # Verify the mocked outages were actually used to reduce billable hours | ||
| with open(output_file, "r") as f: | ||
| reader = csv.DictReader(f) | ||
| rows = list(reader) | ||
|
|
||
| self.assertEqual(len(rows), 1) | ||
| billable_hours = int(rows[0]["SU Hours (GBhr or SUhr)"]) | ||
| expected_hours = ( | ||
| (30 - 2) * 24 * 10 | ||
| ) # 28 days × 24 hrs/day × 10 GB = 6,720 GB-hours | ||
| self.assertEqual(billable_hours, expected_hours) | ||
Uh oh!
There was an error while loading. Please reload this page.