diff --git a/storage/cloud-client/hmac_samples_test.py b/storage/cloud-client/hmac_samples_test.py index e852d31bf20..9712ed15944 100644 --- a/storage/cloud-client/hmac_samples_test.py +++ b/storage/cloud-client/hmac_samples_test.py @@ -21,6 +21,7 @@ import os from google.cloud import storage +import google.api_core.exceptions import pytest import storage_activate_hmac_key @@ -36,11 +37,13 @@ STORAGE_CLIENT = storage.Client(project=PROJECT_ID) -@pytest.fixture +@pytest.fixture(scope="module") def new_hmac_key(): """ Fixture to create a new HMAC key, and to guarantee all keys are deleted at - the end of each test. + the end of the module. + + NOTE: Due to the module scope, test order in this file is significant """ hmac_key, secret = STORAGE_CLIENT.create_hmac_key( service_account_email=SERVICE_ACCOUNT_EMAIL, project_id=PROJECT_ID @@ -100,7 +103,13 @@ def test_deactivate_key(capsys, new_hmac_key): def test_delete_key(capsys, new_hmac_key): - new_hmac_key.state = "INACTIVE" - new_hmac_key.update() + # Due to reuse of the HMAC key for each test function, the previous + # test has deactivated the key already. + try: + new_hmac_key.state = "INACTIVE" + new_hmac_key.update() + except google.api_core.exceptions.BadRequest: + pass + storage_delete_hmac_key.delete_key(new_hmac_key.access_id, PROJECT_ID) assert "The key is deleted" in capsys.readouterr().out