コード例 #1
0
 def test_advisory_refresh_needed(self):
     creds = IntegerRefresher(
         # These values will immediately trigger
         # a manadatory refresh.
         creds_last_for=4,
         mandatory_refresh=2,
         advisory_refresh=5)
     temp = creds.get_frozen_credentials()
     self.assertEqual(temp, credentials.ReadOnlyCredentials('1', '1', '1'))
コード例 #2
0
 def test_advisory_refresh_needed(self):
     creds = IntegerRefresher(
         # These values will immediately trigger
         # a manadatory refresh.
         creds_last_for=4,
         mandatory_refresh=2,
         advisory_refresh=5)
     temp = creds.get_frozen_credentials()
     self.assertEqual(
         temp, credentials.ReadOnlyCredentials('1', '1', '1'))
コード例 #3
0
 def test_exception_propogated_on_error_during_mandatory_period(self):
     fail_refresh = mock.Mock(side_effect=Exception("refresh failed"))
     creds = IntegerRefresher(
         creds_last_for=5,
         advisory_refresh=10,
         # Note we're in the mandatory period now (5 < 7< 10).
         mandatory_refresh=7,
         refresh_function=fail_refresh)
     with self.assertRaisesRegexp(Exception, 'refresh failed'):
         creds.get_frozen_credentials()
コード例 #4
0
 def test_exception_propogated_on_error_during_mandatory_period(self):
     fail_refresh = mock.Mock(side_effect=Exception("refresh failed"))
     creds = IntegerRefresher(
         creds_last_for=5,
         advisory_refresh=10,
         # Note we're in the mandatory period now (5 < 7< 10).
         mandatory_refresh=7,
         refresh_function=fail_refresh
     )
     with self.assertRaisesRegexp(Exception, 'refresh failed'):
         creds.get_frozen_credentials()
コード例 #5
0
 def test_exception_propogated_on_expired_credentials(self):
     fail_refresh = mock.Mock(side_effect=Exception("refresh failed"))
     creds = IntegerRefresher(
         # Setting this to 0 mean the credentials are immediately
         # expired.
         creds_last_for=0,
         advisory_refresh=10,
         mandatory_refresh=7,
         refresh_function=fail_refresh)
     with self.assertRaisesRegexp(Exception, 'refresh failed'):
         # Because credentials are actually expired, any
         # failure to refresh should be propagated.
         creds.get_frozen_credentials()
コード例 #6
0
 def test_exception_propogated_on_expired_credentials(self):
     fail_refresh = mock.Mock(side_effect=Exception("refresh failed"))
     creds = IntegerRefresher(
         # Setting this to 0 mean the credentials are immediately
         # expired.
         creds_last_for=0,
         advisory_refresh=10,
         mandatory_refresh=7,
         refresh_function=fail_refresh
     )
     with self.assertRaisesRegexp(Exception, 'refresh failed'):
         # Because credentials are actually expired, any
         # failure to refresh should be propagated.
         creds.get_frozen_credentials()
コード例 #7
0
 def test_refresh_fails_is_not_an_error_during_advisory_period(self):
     fail_refresh = mock.Mock(side_effect=Exception("refresh failed"))
     creds = IntegerRefresher(creds_last_for=5,
                              advisory_refresh=7,
                              mandatory_refresh=3,
                              refresh_function=fail_refresh)
     temp = creds.get_frozen_credentials()
     # We should have called the refresh function.
     self.assertTrue(fail_refresh.called)
     # The fail_refresh function will raise an exception.
     # Because we're in the advisory period we'll not propogate
     # the exception and return the current set of credentials
     # (generation '1').
     self.assertEqual(temp, credentials.ReadOnlyCredentials('0', '0', '0'))
コード例 #8
0
 def test_refresh_fails_is_not_an_error_during_advisory_period(self):
     fail_refresh = mock.Mock(side_effect=Exception("refresh failed"))
     creds = IntegerRefresher(
         creds_last_for=5,
         advisory_refresh=7,
         mandatory_refresh=3,
         refresh_function=fail_refresh
     )
     temp = creds.get_frozen_credentials()
     # We should have called the refresh function.
     self.assertTrue(fail_refresh.called)
     # The fail_refresh function will raise an exception.
     # Because we're in the advisory period we'll not propogate
     # the exception and return the current set of credentials
     # (generation '1').
     self.assertEqual(
         temp, credentials.ReadOnlyCredentials('0', '0', '0'))
コード例 #9
0
 def test_refresh_giving_expired_credentials_raises_exception(self):
     # This verifies an edge cases where refreshed credentials
     # still give expired credentials:
     # 1. We see credentials are expired.
     # 2. We try to refresh the credentials.
     # 3. The "refreshed" credentials are still expired.
     #
     # In this case, we hard fail and let the user know what
     # happened.
     creds = IntegerRefresher(
         # Negative number indicates that the credentials
         # have already been expired for 2 seconds, even
         # on refresh.
         creds_last_for=-2, )
     err_msg = 'refreshed credentials are still expired'
     with self.assertRaisesRegexp(RuntimeError, err_msg):
         # Because credentials are actually expired, any
         # failure to refresh should be propagated.
         creds.get_frozen_credentials()
コード例 #10
0
 def test_refresh_giving_expired_credentials_raises_exception(self):
     # This verifies an edge cases where refreshed credentials
     # still give expired credentials:
     # 1. We see credentials are expired.
     # 2. We try to refresh the credentials.
     # 3. The "refreshed" credentials are still expired.
     #
     # In this case, we hard fail and let the user know what
     # happened.
     creds = IntegerRefresher(
         # Negative number indicates that the credentials
         # have already been expired for 2 seconds, even
         # on refresh.
         creds_last_for=-2,
     )
     err_msg = 'refreshed credentials are still expired'
     with self.assertRaisesRegexp(RuntimeError, err_msg):
         # Because credentials are actually expired, any
         # failure to refresh should be propagated.
         creds.get_frozen_credentials()
コード例 #11
0
    def test_no_race_for_immediate_advisory_expiration(self):
        creds = IntegerRefresher(creds_last_for=1,
                                 advisory_refresh=1,
                                 mandatory_refresh=0)

        def _run_in_thread(collected):
            for _ in range(100):
                frozen = creds.get_frozen_credentials()
                collected.append(
                    (frozen.access_key, frozen.secret_key, frozen.token))

        self.assert_consistent_credentials_seen(creds, _run_in_thread)
コード例 #12
0
    def test_has_no_race_conditions(self):
        creds = IntegerRefresher(creds_last_for=2,
                                 advisory_refresh=1,
                                 mandatory_refresh=0)

        def _run_in_thread(collected):
            for _ in range(4000):
                frozen = creds.get_frozen_credentials()
                collected.append(
                    (frozen.access_key, frozen.secret_key, frozen.token))

        start = time.time()
        self.assert_consistent_credentials_seen(creds, _run_in_thread)
        end = time.time()
        # creds_last_for = 2 seconds (from above)
        # So, for example, if execution time took 6.1 seconds, then
        # we should see a maximum number of refreshes being (6 / 2.0) + 1 = 4
        max_calls_allowed = math.ceil((end - start) / 2.0) + 1
        self.assertTrue(
            creds.refresh_counter <= max_calls_allowed,
            "Too many cred refreshes, max: %s, actual: %s, "
            "time_delta: %.4f" % (max_calls_allowed, creds.refresh_counter,
                                  (end - start)))