Skip to content

Commit

Permalink
Add tests for KMS quickstart (#756)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Wayne Parrott authored Jan 12, 2017
1 parent 9c8e86d commit 9ff75ea
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
19 changes: 19 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import os

import mock
import pytest
import requests

Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions kms/api/quickstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions kms/api/quickstart_test.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9ff75ea

Please sign in to comment.