From a488ed2cdc3c248e28996057e019be7888104c93 Mon Sep 17 00:00:00 2001 From: "James E. Blair" Date: Thu, 16 Jul 2020 12:53:03 -0700 Subject: [PATCH] Add a test that exercises the GCS Credentials class We subclass the GCS Credentials class. We can't completely test it without either hitting the live Google cloud or substantial mocking, but we should be able to exercise most of the functionality we typically use. Do that by asking it to load a token from disk and assert that it adds that token to a fake request headers dictionary. This also corrects a "problem" detected by the test. The current super() call uses the python3 form, which is fine in that all current uses of this code are using python3, but we still run python27 tests on this repo, so we'll use the python2/python3 compat syntax. Change-Id: Ifa4209617f4be52008b6294ebd10f0deb9bd6a51 --- roles/upload-logs-gcs/library/test-fixtures/auth.json | 1 + .../library/test_zuul_google_storage_upload.py | 11 +++++++++++ .../library/zuul_google_storage_upload.py | 2 +- 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 roles/upload-logs-gcs/library/test-fixtures/auth.json diff --git a/roles/upload-logs-gcs/library/test-fixtures/auth.json b/roles/upload-logs-gcs/library/test-fixtures/auth.json new file mode 100644 index 000000000..8df1606b6 --- /dev/null +++ b/roles/upload-logs-gcs/library/test-fixtures/auth.json @@ -0,0 +1 @@ +{"access_token": "something", "expires_in": 3599, "token_type": "Bearer"} diff --git a/roles/upload-logs-gcs/library/test_zuul_google_storage_upload.py b/roles/upload-logs-gcs/library/test_zuul_google_storage_upload.py index ed5c55627..16a7831d8 100644 --- a/roles/upload-logs-gcs/library/test_zuul_google_storage_upload.py +++ b/roles/upload-logs-gcs/library/test_zuul_google_storage_upload.py @@ -26,6 +26,7 @@ import fixtures from bs4 import BeautifulSoup from .zuul_google_storage_upload import FileList, Indexer, FileDetail +from .zuul_google_storage_upload import Credentials FIXTURE_DIR = os.path.join(os.path.dirname(__file__), @@ -404,3 +405,13 @@ class TestFileDetail(testtools.TestCase): self.assertEqual(time.gmtime(0), file_detail.last_modified) self.assertEqual(0, file_detail.size) + + +class TestCredential(testtools.TestCase): + + def test_credential(self): + path = os.path.join(FIXTURE_DIR, 'auth.json') + headers = {} + c = Credentials(path) + c.before_request(None, None, None, headers) + self.assertEqual("Bearer something", headers['authorization']) diff --git a/roles/upload-logs-gcs/library/zuul_google_storage_upload.py b/roles/upload-logs-gcs/library/zuul_google_storage_upload.py index b0c5eef7d..88ccd2eea 100755 --- a/roles/upload-logs-gcs/library/zuul_google_storage_upload.py +++ b/roles/upload-logs-gcs/library/zuul_google_storage_upload.py @@ -260,7 +260,7 @@ def sizeof_fmt(num, suffix='B'): class Credentials(gce_cred.Credentials): def __init__(self, path): - super().__init__() + super(Credentials, self).__init__() self._path = path self.refresh(None)