|
1 | 1 | import datetime
|
2 | 2 | import unittest
|
3 | 3 | import pytz
|
| 4 | +import tempfile |
4 | 5 |
|
5 | 6 | import freezegun
|
6 | 7 |
|
|
9 | 10 | from coldfront_plugin_cloud import utils
|
10 | 11 |
|
11 | 12 | from coldfront.core.allocation import models as allocation_models
|
| 13 | +from django.core.management import call_command |
12 | 14 |
|
13 | 15 |
|
14 | 16 | SECONDS_IN_DAY = 3600 * 24
|
@@ -42,6 +44,32 @@ def test_new_allocation_quota(self):
|
42 | 44 | )
|
43 | 45 | self.assertEqual(value, 96)
|
44 | 46 |
|
| 47 | + with tempfile.NamedTemporaryFile() as fp: |
| 48 | + call_command( |
| 49 | + 'calculate_storage_gb_hours', |
| 50 | + '--output', fp.name, |
| 51 | + '--start', '2020-03-01', |
| 52 | + '--end', '2020-03-31', |
| 53 | + '--openstack-gb-rate','0.0000087890625', |
| 54 | + '--openshift-gb-rate','0.0000087890625', |
| 55 | + '--invoice-month','2020-03' |
| 56 | + ) |
| 57 | + |
| 58 | + # Let's test a complete CLI call including excluded time, while we're at it. This is not for testing |
| 59 | + # the validity but just the unerrored execution of the complete pipeline. |
| 60 | + # Tests that verify the correct output are further down in the test file. |
| 61 | + with tempfile.NamedTemporaryFile() as fp: |
| 62 | + call_command( |
| 63 | + 'calculate_storage_gb_hours', |
| 64 | + '--output', fp.name, |
| 65 | + '--start', '2020-03-01', |
| 66 | + '--end', '2020-03-31', |
| 67 | + '--openstack-gb-rate','0.0000087890625', |
| 68 | + '--openshift-gb-rate','0.0000087890625', |
| 69 | + '--invoice-month','2020-03', |
| 70 | + '--excluded-time-ranges', '2020-03-02 00:00:00,2020-03-03 05:00:00' |
| 71 | + ) |
| 72 | + |
45 | 73 |
|
46 | 74 | def test_new_allocation_quota_expired(self):
|
47 | 75 | """Test that expiration doesn't affect invoicing."""
|
@@ -391,16 +419,17 @@ def get_excluded_interval_datetime_list(excluded_interval_list):
|
391 | 419 | ]
|
392 | 420 |
|
393 | 421 | # Single interval within active period
|
394 |
| - excluded_intervals = get_excluded_interval_datetime_list( |
395 |
| - (((2020, 3, 15), (2020, 3, 16)),) |
396 |
| - ) |
| 422 | + excluded_intervals = [ |
| 423 | + (datetime.datetime(2020, 3, 15, 9, 30, 0), |
| 424 | + datetime.datetime(2020, 3, 16, 10, 30, 0)), |
| 425 | + ] |
397 | 426 |
|
398 | 427 | value = utils.get_included_duration(
|
399 | 428 | datetime.datetime(2020, 3, 15, 0, 0, 0),
|
400 | 429 | datetime.datetime(2020, 3, 17, 0, 0, 0),
|
401 | 430 | excluded_intervals
|
402 | 431 | )
|
403 |
| - self.assertEqual(value, SECONDS_IN_DAY * 1) |
| 432 | + self.assertEqual(value, SECONDS_IN_DAY * 1 - 3600) |
404 | 433 |
|
405 | 434 | # Interval starts before active period
|
406 | 435 | excluded_intervals = get_excluded_interval_datetime_list(
|
@@ -470,21 +499,21 @@ def test_load_excluded_intervals(self):
|
470 | 499 | ]
|
471 | 500 | output = utils.load_excluded_intervals(interval_list)
|
472 | 501 | self.assertEqual(output, [
|
473 |
| - [datetime.datetime(2023, 1, 1, 0, 0, 0), |
474 |
| - datetime.datetime(2023, 1, 2, 0, 0, 0)] |
| 502 | + [pytz.utc.localize(datetime.datetime(2023, 1, 1, 0, 0, 0)), |
| 503 | + pytz.utc.localize(datetime.datetime(2023, 1, 2, 0, 0, 0))] |
475 | 504 | ])
|
476 | 505 |
|
477 | 506 | # More than 1 interval
|
478 | 507 | interval_list = [
|
479 | 508 | "2023-01-01,2023-01-02",
|
480 |
| - "2023-01-04,2023-01-15", |
| 509 | + "2023-01-04 09:00:00,2023-01-15 10:00:00", |
481 | 510 | ]
|
482 | 511 | output = utils.load_excluded_intervals(interval_list)
|
483 | 512 | self.assertEqual(output, [
|
484 |
| - [datetime.datetime(2023, 1, 1, 0, 0, 0), |
485 |
| - datetime.datetime(2023, 1, 2, 0, 0, 0)], |
486 |
| - [datetime.datetime(2023, 1, 4, 0, 0, 0), |
487 |
| - datetime.datetime(2023, 1, 15, 0, 0, 0)] |
| 513 | + [pytz.utc.localize(datetime.datetime(2023, 1, 1, 0, 0, 0)), |
| 514 | + pytz.utc.localize(datetime.datetime(2023, 1, 2, 0, 0, 0))], |
| 515 | + [pytz.utc.localize(datetime.datetime(2023, 1, 4, 9, 0, 0)), |
| 516 | + pytz.utc.localize(datetime.datetime(2023, 1, 15, 10, 0, 0))] |
488 | 517 | ])
|
489 | 518 |
|
490 | 519 | def test_load_excluded_intervals_invalid(self):
|
|
0 commit comments