From edf62f4e296edf6cbac5c3291f4e5ee1a972f63e Mon Sep 17 00:00:00 2001 From: Chris Wilcox Date: Thu, 5 Mar 2020 09:59:15 -0800 Subject: [PATCH 1/4] fix: Reuse HMAC key as we have a limit of 5 --- storage/cloud-client/hmac_samples_test.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/storage/cloud-client/hmac_samples_test.py b/storage/cloud-client/hmac_samples_test.py index e852d31bf20..51d328ed366 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 @@ -35,8 +36,7 @@ SERVICE_ACCOUNT_EMAIL = os.environ["HMAC_KEY_TEST_SERVICE_ACCOUNT"] 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 @@ -100,7 +100,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 From 8413d8cf4de93cda886ba9c1ee5c9b713c16f11d Mon Sep 17 00:00:00 2001 From: Chris Wilcox Date: Thu, 5 Mar 2020 10:09:18 -0800 Subject: [PATCH 2/4] fix: lint --- storage/cloud-client/hmac_samples_test.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/storage/cloud-client/hmac_samples_test.py b/storage/cloud-client/hmac_samples_test.py index 51d328ed366..9073203e0aa 100644 --- a/storage/cloud-client/hmac_samples_test.py +++ b/storage/cloud-client/hmac_samples_test.py @@ -36,6 +36,7 @@ SERVICE_ACCOUNT_EMAIL = os.environ["HMAC_KEY_TEST_SERVICE_ACCOUNT"] STORAGE_CLIENT = storage.Client(project=PROJECT_ID) + @pytest.fixture(scope="module") def new_hmac_key(): """ @@ -107,6 +108,6 @@ def test_delete_key(capsys, new_hmac_key): 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 From 688091a87800ccda06a48ca9499dc5d48dea3c88 Mon Sep 17 00:00:00 2001 From: Chris Wilcox Date: Thu, 5 Mar 2020 10:18:08 -0800 Subject: [PATCH 3/4] fix: modify comment for fixture to be clear about significant test order --- storage/cloud-client/hmac_samples_test.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/storage/cloud-client/hmac_samples_test.py b/storage/cloud-client/hmac_samples_test.py index 9073203e0aa..8da4a88c33b 100644 --- a/storage/cloud-client/hmac_samples_test.py +++ b/storage/cloud-client/hmac_samples_test.py @@ -41,7 +41,9 @@ 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 @@ -92,7 +94,7 @@ def test_activate_key(capsys, new_hmac_key): assert hmac_key.state == "ACTIVE" -def test_deactivate_key(capsys, new_hmac_key): +def test_deactivate_key(capsys, new_hmac_key): hmac_key = storage_deactivate_hmac_key.deactivate_key( new_hmac_key.access_id, PROJECT_ID ) From 764048a95ba14c79039cfd3e9ca2e7b5a391810f Mon Sep 17 00:00:00 2001 From: Chris Wilcox Date: Thu, 5 Mar 2020 10:21:50 -0800 Subject: [PATCH 4/4] fix: oops whitespace --- storage/cloud-client/hmac_samples_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/storage/cloud-client/hmac_samples_test.py b/storage/cloud-client/hmac_samples_test.py index 8da4a88c33b..9712ed15944 100644 --- a/storage/cloud-client/hmac_samples_test.py +++ b/storage/cloud-client/hmac_samples_test.py @@ -94,7 +94,7 @@ def test_activate_key(capsys, new_hmac_key): assert hmac_key.state == "ACTIVE" -def test_deactivate_key(capsys, new_hmac_key): +def test_deactivate_key(capsys, new_hmac_key): hmac_key = storage_deactivate_hmac_key.deactivate_key( new_hmac_key.access_id, PROJECT_ID )