Skip to content

Commit 9ff75ea

Browse files
author
Jon Wayne Parrott
authored
Add tests for KMS quickstart (GoogleCloudPlatform#756)
1 parent 9c8e86d commit 9ff75ea

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

conftest.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import os
1616

17+
import mock
1718
import pytest
1819
import requests
1920

@@ -71,3 +72,21 @@ def remote_resource(cloud_config):
7172

7273
return lambda path, tmpdir: fetch_gcs_resource(
7374
remote_uri + path.strip('/'), tmpdir)
75+
76+
77+
@pytest.fixture
78+
def api_client_inject_project_id(cloud_config):
79+
"""Patches all googleapiclient requests to replace 'YOUR_PROJECT_ID' with
80+
the project ID from cloud_config."""
81+
import googleapiclient.http
82+
83+
class ProjectIdInjectingHttpRequest(googleapiclient.http.HttpRequest):
84+
def __init__(self, http, postproc, uri, *args, **kwargs):
85+
uri = uri.replace('YOUR_PROJECT_ID', cloud_config.project)
86+
super(ProjectIdInjectingHttpRequest, self).__init__(
87+
http, postproc, uri, *args, **kwargs)
88+
89+
with mock.patch(
90+
'googleapiclient.http.HttpRequest',
91+
new=ProjectIdInjectingHttpRequest):
92+
yield

kms/api/quickstart.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ def run_quickstart():
2525
# Lists keys in the "global" location.
2626
location = 'global'
2727

28-
# Instantiates a client
28+
# Creates an API client for the KMS API.
2929
kms_client = discovery.build('cloudkms', 'v1beta1')
3030

31-
# The resource name of the location associated with the KeyRings
31+
# The resource name of the location associated with the key rings.
3232
parent = 'projects/{}/locations/{}'.format(project_id, location)
3333

3434
# Lists key rings

kms/api/quickstart_test.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright 2017 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
def test_quickstart(api_client_inject_project_id, capsys):
17+
import quickstart
18+
19+
quickstart.run_quickstart()
20+
out, _ = capsys.readouterr()
21+
assert 'No key rings found' in out

0 commit comments

Comments
 (0)