forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
184 lines (147 loc) · 5.82 KB
/
Copy pathutils.py
File metadata and controls
184 lines (147 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# Copyright 2015, Google, Inc.
# 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.
#
"""
Common testing utilities between samples
"""
import contextlib
import os
import sys
import tempfile
import unittest
from flaky import flaky
import gcloud
import httplib2
from nose.plugins.attrib import attr
from nose.plugins.skip import SkipTest
from six.moves import cStringIO
try:
APPENGINE_AVAILABLE = True
from google.appengine.datastore import datastore_stub_util
from google.appengine.ext import testbed
from google.appengine.api import namespace_manager
except ImportError:
APPENGINE_AVAILABLE = False
RESOURCE_PATH = os.path.join(
os.path.abspath(os.path.dirname(__file__)), 'resources')
PROJECT_ID_ENV_VAR = 'TEST_PROJECT_ID'
BUCKET_NAME_ENV_VAR = 'TEST_BUCKET_NAME'
def flaky_filter(e, *args):
exception_class, exception_instance, traceback = e
return isinstance(
exception_instance,
(gcloud.exceptions.GCloudError,))
def mark_flaky(f):
return flaky(max_runs=3, rerun_filter=flaky_filter)(
attr('flaky')(f))
class CloudBaseTest(unittest.TestCase):
def setUp(self):
self.resource_path = RESOURCE_PATH
self.project_id = os.environ.get(PROJECT_ID_ENV_VAR)
if not self.project_id:
raise EnvironmentError(
'You must set the {} environment variable to a valid Google '
'Cloud project ID.'.format(PROJECT_ID_ENV_VAR))
self.bucket_name = os.environ.get(BUCKET_NAME_ENV_VAR)
if not self.bucket_name:
raise EnvironmentError(
'You must set the {} environment variable to a valid Google '
'Cloud Storage bucket.'.format(BUCKET_NAME_ENV_VAR))
class AppEngineTestbedCase(CloudBaseTest):
"""A base test case for common setup/teardown tasks for test."""
def setUp(self):
super(AppEngineTestbedCase, self).setUp()
if not APPENGINE_AVAILABLE:
raise SkipTest()
# A hack to prevent get_application_default from going GAE route.
self._server_software_org = os.environ.get('SERVER_SOFTWARE')
os.environ['SERVER_SOFTWARE'] = ''
# Setup the datastore and memcache stub.
# First, create an instance of the Testbed class.
self.testbed = testbed.Testbed()
# Then activate the testbed, which prepares the service stubs for
# use.
self.testbed.activate()
# Create a consistency policy that will simulate the High
# Replication consistency model.
self.policy = datastore_stub_util.PseudoRandomHRConsistencyPolicy(
probability=0)
# Initialize the datastore stub with this policy.
self.testbed.init_datastore_v3_stub(
datastore_file=tempfile.mkstemp()[1],
consistency_policy=self.policy)
self.testbed.init_memcache_stub()
# Setup remaining stubs.
self.testbed.init_app_identity_stub()
self.testbed.init_blobstore_stub()
self.testbed.init_user_stub()
self.testbed.init_taskqueue_stub(root_path='tests/resources')
self.taskqueue_stub = self.testbed.get_stub(
testbed.TASKQUEUE_SERVICE_NAME)
self.testbed.init_logservice_stub()
def tearDown(self):
super(AppEngineTestbedCase, self).tearDown()
if self._server_software_org:
os.environ['SERVER_SOFTWARE'] = self._server_software_org
self.testbed.deactivate()
def loginUser(self, email='user@example.com', id='123', is_admin=False):
self.testbed.setup_env(
user_email=email,
user_id=id,
user_is_admin='1' if is_admin else '0',
overwrite=True)
def runTasks(self):
tasks = self.taskqueue_stub.get_filtered_tasks()
for task in tasks:
namespace = task.headers.get('X-AppEngine-Current-Namespace', '')
previous_namespace = namespace_manager.get_namespace()
try:
namespace_manager.set_namespace(namespace)
self.app.post(
task.url,
task.extract_params(),
headers={
k: v for k, v in task.headers.iteritems()
if k.startswith('X-AppEngine')})
finally:
namespace_manager.set_namespace(previous_namespace)
@contextlib.contextmanager
def capture_stdout():
"""Capture stdout to a StringIO object."""
fake_stdout = cStringIO()
old_stdout = sys.stdout
try:
sys.stdout = fake_stdout
yield fake_stdout
finally:
sys.stdout = old_stdout
class Http2Mock(object):
"""Mock httplib2.Http"""
def __init__(self, responses):
self.responses = responses
def add_credentials(self, user, pwd):
self.credentials = (user, pwd)
def request(self, token_uri, method, body, headers=None, *args, **kwargs):
response = self.responses.pop(0)
self.status = response.get('status', 200)
self.body = response.get('body', '')
self.headers = response.get('headers', '')
return (self, self.body)
def __enter__(self):
self.httplib2_orig = httplib2.Http
httplib2.Http = self
return self
def __exit__(self, exc_type, exc_value, traceback):
httplib2.Http = self.httplib2_orig
def __call__(self, *args, **kwargs):
return self