Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for KMS quickstart #756

Merged
merged 1 commit into from
Jan 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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