Skip to content

Commit cf22a11

Browse files
authored
tests: pre-scrub old HMAC keys before testing creation (#467)
Avoids hitting 5-key-per-service-account quota. Closes #334.
1 parent 3b06f9e commit cf22a11

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

tests/system/test_system.py

+26-5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import mock
2828

2929
import requests
30+
import pytest
3031
import six
3132

3233
from google.cloud import exceptions
@@ -35,6 +36,7 @@
3536
from google.cloud.storage._helpers import _base64_md5hash
3637
from google.cloud.storage.bucket import LifecycleRuleDelete
3738
from google.cloud.storage.bucket import LifecycleRuleSetStorageClass
39+
from google.cloud import _helpers
3840
from google.cloud import kms
3941
from google import resumable_media
4042
import google.auth
@@ -147,23 +149,43 @@ def test_get_service_account_email(self):
147149

148150
self.assertTrue(any(match for match in matches if match is not None))
149151

152+
@staticmethod
153+
def _get_before_hmac_keys(client):
154+
from google.cloud.storage.hmac_key import HMACKeyMetadata
155+
156+
before_hmac_keys = set(client.list_hmac_keys())
157+
158+
now = datetime.datetime.utcnow().replace(tzinfo=_helpers.UTC)
159+
yesterday = now - datetime.timedelta(days=1)
160+
161+
# Delete any HMAC keys older than a day.
162+
for hmac_key in list(before_hmac_keys):
163+
if hmac_key.time_created < yesterday:
164+
if hmac_key.state != HMACKeyMetadata.INACTIVE_STATE:
165+
hmac_key.state = HMACKeyMetadata.INACTIVE_STATE
166+
hmac_key.update()
167+
hmac_key.delete()
168+
before_hmac_keys.remove(hmac_key)
169+
170+
return before_hmac_keys
171+
150172
def test_hmac_key_crud(self):
151173
from google.cloud.storage.hmac_key import HMACKeyMetadata
152174

153175
credentials = Config.CLIENT._credentials
154176
email = credentials.service_account_email
155177

156-
before_keys = set(Config.CLIENT.list_hmac_keys())
178+
before_hmac_keys = self._get_before_hmac_keys(Config.CLIENT)
157179

158180
metadata, secret = Config.CLIENT.create_hmac_key(email)
159181
self.case_hmac_keys_to_delete.append(metadata)
160182

161183
self.assertIsInstance(secret, six.text_type)
162184
self.assertEqual(len(secret), 40)
163185

164-
after_keys = set(Config.CLIENT.list_hmac_keys())
165-
self.assertFalse(metadata in before_keys)
166-
self.assertTrue(metadata in after_keys)
186+
after_hmac_keys = set(Config.CLIENT.list_hmac_keys())
187+
self.assertFalse(metadata in before_hmac_keys)
188+
self.assertTrue(metadata in after_hmac_keys)
167189

168190
another = HMACKeyMetadata(Config.CLIENT)
169191

@@ -309,7 +331,6 @@ def test_bucket_update_labels(self):
309331
self.assertEqual(bucket.labels, {})
310332

311333
def test_get_set_iam_policy(self):
312-
import pytest
313334
from google.cloud.storage.iam import STORAGE_OBJECT_VIEWER_ROLE
314335
from google.api_core.exceptions import BadRequest, PreconditionFailed
315336

0 commit comments

Comments
 (0)