|
14 | 14 | Unit test suite for items module
|
15 | 15 | """
|
16 | 16 | import unittest
|
| 17 | +from datetime import timezone, datetime, timedelta |
| 18 | +from unittest.mock import Mock |
17 | 19 |
|
18 |
| -from aws_secretsmanager_caching.cache.items import SecretCacheObject |
| 20 | +from aws_secretsmanager_caching.cache.items import SecretCacheObject, SecretCacheItem |
19 | 21 | from aws_secretsmanager_caching.config import SecretCacheConfig
|
20 | 22 |
|
21 | 23 |
|
@@ -47,3 +49,89 @@ def test_simple_2(self):
|
47 | 49 | self.assertIsNone(sco.get_secret_value())
|
48 | 50 | sco._exception = Exception("test")
|
49 | 51 | self.assertRaises(Exception, sco.get_secret_value)
|
| 52 | + |
| 53 | + def test_datetime_fix_is_refresh_needed(self): |
| 54 | + secret_cached_object = TestSecretCacheObject.TestObject(SecretCacheConfig(), None, None) |
| 55 | + |
| 56 | + # Variable values set in order to be able to test modified line with assert statement (False is not None) |
| 57 | + secret_cached_object._next_retry_time = datetime.now(tz=timezone.utc) |
| 58 | + secret_cached_object._refresh_needed = False |
| 59 | + secret_cached_object._exception = not None |
| 60 | + |
| 61 | + self.assertTrue(secret_cached_object._is_refresh_needed()) |
| 62 | + |
| 63 | + def test_datetime_fix_refresh(self): |
| 64 | + exp_factor = 11 |
| 65 | + |
| 66 | + secret_cached_object = SecretCacheObject( |
| 67 | + SecretCacheConfig(exception_retry_delay_base=1, exception_retry_growth_factor=2), |
| 68 | + None, None |
| 69 | + ) |
| 70 | + secret_cached_object._set_result = Mock(side_effect=Exception("exception used for test")) |
| 71 | + secret_cached_object._refresh_needed = True |
| 72 | + secret_cached_object._exception_count = exp_factor # delay = min(1*(2^exp_factor) = 2048, 3600) |
| 73 | + |
| 74 | + t_before = datetime.now(tz=timezone.utc) |
| 75 | + secret_cached_object._SecretCacheObject__refresh() |
| 76 | + t_after = datetime.now(tz=timezone.utc) |
| 77 | + |
| 78 | + t_before_delay = t_before + timedelta( |
| 79 | + milliseconds=secret_cached_object._config.exception_retry_delay_base * ( |
| 80 | + secret_cached_object._config.exception_retry_growth_factor ** exp_factor |
| 81 | + ) |
| 82 | + ) |
| 83 | + self.assertLessEqual(t_before_delay, secret_cached_object._next_retry_time) |
| 84 | + |
| 85 | + t_after_delay = t_after + timedelta( |
| 86 | + milliseconds=secret_cached_object._config.exception_retry_delay_base * ( |
| 87 | + secret_cached_object._config.exception_retry_growth_factor ** exp_factor |
| 88 | + ) |
| 89 | + ) |
| 90 | + self.assertGreaterEqual(t_after_delay, secret_cached_object._next_retry_time) |
| 91 | + |
| 92 | +class TestSecretCacheItem(unittest.TestCase): |
| 93 | + def setUp(self): |
| 94 | + pass |
| 95 | + |
| 96 | + def tearDown(self): |
| 97 | + pass |
| 98 | + |
| 99 | + def test_datetime_fix_SCI_init(self): |
| 100 | + config = SecretCacheConfig() |
| 101 | + t_before = datetime.now(tz=timezone.utc) |
| 102 | + secret_cache_item = SecretCacheItem(config, None, None) |
| 103 | + t_after = datetime.now(tz=timezone.utc) |
| 104 | + |
| 105 | + self.assertGreaterEqual(secret_cache_item._next_refresh_time, t_before) |
| 106 | + self.assertLessEqual(secret_cache_item._next_refresh_time, t_after) |
| 107 | + |
| 108 | + def test_datetime_fix_refresh_needed(self): |
| 109 | + config = SecretCacheConfig() |
| 110 | + secret_cache_item = SecretCacheItem(config, None, None) |
| 111 | + |
| 112 | + # Variable values set in order to be able to test modified line with assert statement (False is not None) |
| 113 | + secret_cache_item._refresh_needed = False |
| 114 | + secret_cache_item._exception = False |
| 115 | + secret_cache_item._next_retry_time = None |
| 116 | + |
| 117 | + self.assertTrue(secret_cache_item._is_refresh_needed()) |
| 118 | + |
| 119 | + def test_datetime_fix_execute_refresh(self): |
| 120 | + client_mock = Mock() |
| 121 | + client_mock.describe_secret = Mock() |
| 122 | + client_mock.describe_secret.return_value = "test" |
| 123 | + |
| 124 | + config = SecretCacheConfig() |
| 125 | + secret_cache_item = SecretCacheItem(config, client_mock, None) |
| 126 | + |
| 127 | + t_before = datetime.now(tz=timezone.utc) |
| 128 | + secret_cache_item._execute_refresh() |
| 129 | + t_after = datetime.now(tz=timezone.utc) |
| 130 | + |
| 131 | + # Make sure that the timezone is correctly set |
| 132 | + self.assertEqual(secret_cache_item._next_refresh_time.tzinfo, timezone.utc) |
| 133 | + |
| 134 | + # Check that secret_refresh_interval addition works as intended |
| 135 | + self.assertGreaterEqual(secret_cache_item._next_refresh_time, t_before) |
| 136 | + t_max_after = t_after + timedelta(seconds=config.secret_refresh_interval) |
| 137 | + self.assertLessEqual(secret_cache_item._next_refresh_time, t_max_after) |
0 commit comments