Esempio n. 1
0
 def test_should_allow_milliseconds_in_expiration(self):
     """Moto has milliseconds in the expiration date, AWS does not"""
     credentials = {'test_role': '{"Expiration": "1970-01-01T00:00:59.123Z"}'}
     scheduler = Scheduler(credentials, None)
     expected = 59
     received = scheduler.extract_refresh_delta()
     self.assertEqual(expected, received)
Esempio n. 2
0
 def test_should_acquire_valid_credentials(self, scheduler_mock):
     credentials_mock = OrderedDict()
     credentials_provider_mock = Mock()
     expected = OrderedDict({'test_role': '{"Expiration": "1970-01-01T00:00:00Z"}'})
     credentials_provider_mock.get_credentials_for_all_roles.return_value = expected
     scheduler = Scheduler(credentials_mock, credentials_provider_mock)
     scheduler.refresh_credentials()
     self.assertEqual(expected, credentials_mock)
Esempio n. 3
0
 def setUp(self):
     self.credentials_mock = {}
     self.credentials_provider_mock = Mock()
     self.scheduler = Scheduler(self.credentials_mock, self.credentials_provider_mock)
Esempio n. 4
0
class RefreshCredentialsTest(unittest.TestCase):

    def setUp(self):
        self.credentials_mock = {}
        self.credentials_provider_mock = Mock()
        self.scheduler = Scheduler(self.credentials_mock, self.credentials_provider_mock)

    @patch('alppaca.scheduler.Scheduler.build_trigger')
    def test_should_get_valid_credentials_when_called_with_correct_date(self, build_trigger_mock):

        self.credentials_provider_mock.get_credentials_for_all_roles.return_value = {
            'test_role': '{"Expiration": "1970-01-01T00:00:00Z"}'
        }

        self.scheduler.refresh_credentials()
        build_trigger_mock.assert_called_with(0)

    @patch('alppaca.scheduler.Scheduler.build_trigger')
    def test_should_enter_backoff_state_on_empty_credentials(self, build_trigger_mock):
        self.credentials_provider_mock.get_credentials_for_all_roles.return_value = {}

        self.assertIsNone(self.scheduler.backoff)
        self.scheduler.refresh_credentials()
        self.assertIsNotNone(self.scheduler.backoff)

    @patch('alppaca.scheduler.Scheduler.build_trigger')
    def test_should_increment_refresh_delta_when_in_backoff_state(self, build_trigger_mock):
        self.credentials_provider_mock.get_credentials_for_all_roles.return_value = {}
        self.scheduler.refresh_credentials()
        build_trigger_mock.assert_called_with(0)
        self.scheduler.refresh_credentials()
        build_trigger_mock.assert_called_with(1)
        self.scheduler.refresh_credentials()
        build_trigger_mock.assert_called_with(2)

    @patch('datetime.datetime', FixedDateTime)
    @patch('alppaca.scheduler.Scheduler.build_trigger')
    def test_should_exit_backoff_state_on_valid_credentials(self, build_trigger_mock):
        self.credentials_provider_mock.get_credentials_for_all_roles.return_value = {
            'test_role': '{"Expiration": "1970-01-01T00:00:59Z"}'
        }
        self.scheduler.backoff = True
        self.scheduler.refresh_credentials()
        self.assertIsNone(self.scheduler.backoff)

    @patch('alppaca.scheduler.Scheduler.build_trigger')
    def test_should_enter_backoff_state_on_expired_credentials(self, build_trigger_mock):
        self.credentials_provider_mock.get_credentials_for_all_roles.return_value = {
            'test_role': '{"Expiration": "1969-12-24T00:00:00Z"}'
        }
        self.scheduler.refresh_credentials()

        self.assertIsNotNone(self.scheduler.backoff)

    @patch('alppaca.scheduler.Scheduler.build_trigger')
    def test_should_enter_backoff_state_on_exception(self, build_trigger_mock):
        self.credentials_provider_mock.get_credentials_for_all_roles.side_effect = Exception()
        self.scheduler.refresh_credentials()

        self.assertIsNotNone(self.scheduler.backoff)
Esempio n. 5
0
 def test_should_extract_correct_seconds_from_credentials(self):
     credentials = {'test_role': '{"Expiration": "1970-01-01T00:00:59Z"}'}
     scheduler = Scheduler(credentials, None)
     expected = 59
     received = scheduler.extract_refresh_delta()
     self.assertEqual(expected, received)