From f0bfb58056fca9e32ff055a2a1a65a9dd1944843 Mon Sep 17 00:00:00 2001 From: Jon Wayne Parrott Date: Thu, 12 Jan 2017 10:47:06 -0800 Subject: [PATCH] Add tests for KMS quickstart Change-Id: I5697275f8fc19c9f7416b34c859a61d37ab99e6e --- conftest.py | 19 +++++++++++++++++++ kms/api/quickstart.py | 4 ++-- kms/api/quickstart_test.py | 21 +++++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 kms/api/quickstart_test.py diff --git a/conftest.py b/conftest.py index 8ac8451e0ef..c8ecd63d0c3 100644 --- a/conftest.py +++ b/conftest.py @@ -14,6 +14,7 @@ import os +import mock import pytest import requests @@ -71,3 +72,21 @@ def remote_resource(cloud_config): return lambda path, tmpdir: fetch_gcs_resource( remote_uri + path.strip('/'), tmpdir) + + +@pytest.fixture +def api_client_inject_project_id(cloud_config): + """Patches all googleapiclient requests to replace 'YOUR_PROJECT_ID' with + the project ID from cloud_config.""" + import googleapiclient.http + + class ProjectIdInjectingHttpRequest(googleapiclient.http.HttpRequest): + def __init__(self, http, postproc, uri, *args, **kwargs): + uri = uri.replace('YOUR_PROJECT_ID', cloud_config.project) + super(ProjectIdInjectingHttpRequest, self).__init__( + http, postproc, uri, *args, **kwargs) + + with mock.patch( + 'googleapiclient.http.HttpRequest', + new=ProjectIdInjectingHttpRequest): + yield diff --git a/kms/api/quickstart.py b/kms/api/quickstart.py index 59b76f30737..4e94a669118 100644 --- a/kms/api/quickstart.py +++ b/kms/api/quickstart.py @@ -25,10 +25,10 @@ def run_quickstart(): # Lists keys in the "global" location. location = 'global' - # Instantiates a client + # Creates an API client for the KMS API. kms_client = discovery.build('cloudkms', 'v1beta1') - # The resource name of the location associated with the KeyRings + # The resource name of the location associated with the key rings. parent = 'projects/{}/locations/{}'.format(project_id, location) # Lists key rings diff --git a/kms/api/quickstart_test.py b/kms/api/quickstart_test.py new file mode 100644 index 00000000000..d5a196eb395 --- /dev/null +++ b/kms/api/quickstart_test.py @@ -0,0 +1,21 @@ +# Copyright 2017 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +def test_quickstart(api_client_inject_project_id, capsys): + import quickstart + + quickstart.run_quickstart() + out, _ = capsys.readouterr() + assert 'No key rings found' in out