Skip to content

Commit 2c2f575

Browse files
committed
Merge pull request #1396 from dhermes/pubsub-w-emulator
Adding support for pubsub emulator in system test.
2 parents 411fd38 + 6469d43 commit 2c2f575

File tree

7 files changed

+60
-26
lines changed

7 files changed

+60
-26
lines changed

CONTRIBUTING.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,28 @@ Running System Tests
248248

249249
.. _emulators: https://cloud.google.com/sdk/gcloud/reference/beta/emulators/
250250

251+
- To run the ``pubsub`` system tests with an emulator, first start the
252+
emulator and take note of the process ID::
253+
254+
$ gcloud beta emulators pubsub start &
255+
[1] 44444
256+
257+
then determine the environment variables needed to interact with
258+
the emulator::
259+
260+
$ gcloud beta emulators pubsub env-init
261+
export PUBSUB_EMULATOR_HOST=localhost:8897
262+
263+
using these environment variables run the emulator::
264+
265+
$ DATASTORE_HOST=http://localhost:8897 \
266+
> python system_tests/run_system_test.py \
267+
> --package=pubsub
268+
269+
and after completion stop the emulator::
270+
271+
$ kill 44444
272+
251273
Test Coverage
252274
-------------
253275

gcloud/connection.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ def _make_request(self, method, url, data=None, content_type=None,
225225
else:
226226
content_length = 0
227227

228-
headers['Content-Length'] = content_length
228+
# NOTE: str is intended, bytes are sufficient for headers.
229+
headers['Content-Length'] = str(content_length)
229230

230231
if content_type:
231232
headers['Content-Type'] = content_type

gcloud/storage/test_batch.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def test__make_request_GET_normal(self):
115115
self.assertEqual(http._requests, [])
116116
EXPECTED_HEADERS = [
117117
('Accept-Encoding', 'gzip'),
118-
('Content-Length', 0),
118+
('Content-Length', '0'),
119119
]
120120
solo_request, = batch._requests
121121
self.assertEqual(solo_request[0], 'GET')
@@ -140,7 +140,7 @@ def test__make_request_POST_normal(self):
140140
self.assertEqual(http._requests, [])
141141
EXPECTED_HEADERS = [
142142
('Accept-Encoding', 'gzip'),
143-
('Content-Length', 10),
143+
('Content-Length', '10'),
144144
]
145145
solo_request, = batch._requests
146146
self.assertEqual(solo_request[0], 'POST')
@@ -165,7 +165,7 @@ def test__make_request_PATCH_normal(self):
165165
self.assertEqual(http._requests, [])
166166
EXPECTED_HEADERS = [
167167
('Accept-Encoding', 'gzip'),
168-
('Content-Length', 10),
168+
('Content-Length', '10'),
169169
]
170170
solo_request, = batch._requests
171171
self.assertEqual(solo_request[0], 'PATCH')
@@ -190,7 +190,7 @@ def test__make_request_DELETE_normal(self):
190190
self.assertEqual(http._requests, [])
191191
EXPECTED_HEADERS = [
192192
('Accept-Encoding', 'gzip'),
193-
('Content-Length', 0),
193+
('Content-Length', '0'),
194194
]
195195
solo_request, = batch._requests
196196
self.assertEqual(solo_request[0], 'DELETE')

gcloud/test_connection.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def test__make_request_no_data_no_content_type_no_headers(self):
163163
self.assertEqual(http._called_with['body'], None)
164164
expected_headers = {
165165
'Accept-Encoding': 'gzip',
166-
'Content-Length': 0,
166+
'Content-Length': '0',
167167
'User-Agent': conn.USER_AGENT,
168168
}
169169
self.assertEqual(http._called_with['headers'], expected_headers)
@@ -181,7 +181,7 @@ def test__make_request_w_data_no_extra_headers(self):
181181
self.assertEqual(http._called_with['body'], {})
182182
expected_headers = {
183183
'Accept-Encoding': 'gzip',
184-
'Content-Length': 0,
184+
'Content-Length': '0',
185185
'Content-Type': 'application/json',
186186
'User-Agent': conn.USER_AGENT,
187187
}
@@ -200,7 +200,7 @@ def test__make_request_w_extra_headers(self):
200200
self.assertEqual(http._called_with['body'], None)
201201
expected_headers = {
202202
'Accept-Encoding': 'gzip',
203-
'Content-Length': 0,
203+
'Content-Length': '0',
204204
'X-Foo': 'foo',
205205
'User-Agent': conn.USER_AGENT,
206206
}
@@ -225,7 +225,7 @@ def test_api_request_defaults(self):
225225
self.assertEqual(http._called_with['body'], None)
226226
expected_headers = {
227227
'Accept-Encoding': 'gzip',
228-
'Content-Length': 0,
228+
'Content-Length': '0',
229229
'User-Agent': conn.USER_AGENT,
230230
}
231231
self.assertEqual(http._called_with['headers'], expected_headers)
@@ -274,7 +274,7 @@ def test_api_request_w_query_params(self):
274274
self.assertEqual(http._called_with['body'], None)
275275
expected_headers = {
276276
'Accept-Encoding': 'gzip',
277-
'Content-Length': 0,
277+
'Content-Length': '0',
278278
'User-Agent': conn.USER_AGENT,
279279
}
280280
self.assertEqual(http._called_with['headers'], expected_headers)
@@ -301,7 +301,7 @@ def test_api_request_w_data(self):
301301
self.assertEqual(http._called_with['body'], DATAJ)
302302
expected_headers = {
303303
'Accept-Encoding': 'gzip',
304-
'Content-Length': len(DATAJ),
304+
'Content-Length': str(len(DATAJ)),
305305
'Content-Type': 'application/json',
306306
'User-Agent': conn.USER_AGENT,
307307
}
@@ -345,7 +345,7 @@ def test_api_request_non_binary_response(self):
345345
self.assertEqual(http._called_with['body'], None)
346346
expected_headers = {
347347
'Accept-Encoding': 'gzip',
348-
'Content-Length': 0,
348+
'Content-Length': '0',
349349
'User-Agent': conn.USER_AGENT,
350350
}
351351
self.assertEqual(http._called_with['headers'], expected_headers)

system_tests/datastore.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
# repository root is the current directory.
3030
from system_tests import clear_datastore
3131
from system_tests import populate_datastore
32+
from system_tests.system_test_utils import EmulatorCreds
3233

3334

3435
# Isolated namespace so concurrent test runs don't collide.
@@ -45,18 +46,6 @@ class Config(object):
4546
CLIENT = None
4647

4748

48-
class _EmulatorCreds(object):
49-
"""A mock credential object.
50-
51-
Used to avoid unnecessary token refreshing or reliance on the network
52-
while an emulator is running.
53-
"""
54-
55-
@staticmethod
56-
def create_scoped_required():
57-
return False
58-
59-
6049
def clone_client(client):
6150
# Fool the Client constructor to avoid creating a new connection.
6251
cloned_client = datastore.Client(project=client.project,
@@ -71,7 +60,7 @@ def setUpModule():
7160
client_mod.DATASET = TESTS_DATASET
7261
Config.CLIENT = datastore.Client(namespace=TEST_NAMESPACE)
7362
else:
74-
credentials = _EmulatorCreds()
63+
credentials = EmulatorCreds()
7564
http = httplib2.Http() # Un-authorized.
7665
Config.CLIENT = datastore.Client(project=EMULATOR_DATASET,
7766
namespace=TEST_NAMESPACE,

system_tests/pubsub.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import os
1516
import time
1617

18+
import httplib2
1719
import unittest2
1820

1921
from gcloud import _helpers
22+
from gcloud.environment_vars import PUBSUB_EMULATOR
2023
from gcloud.environment_vars import TESTS_PROJECT
2124
from gcloud import pubsub
25+
from system_tests.system_test_utils import EmulatorCreds
2226

2327

2428
DEFAULT_TOPIC_NAME = 'subscribe-me%d' % (1000 * time.time(),)
@@ -35,7 +39,13 @@ class Config(object):
3539

3640
def setUpModule():
3741
_helpers.PROJECT = TESTS_PROJECT
38-
Config.CLIENT = pubsub.Client()
42+
if os.getenv(PUBSUB_EMULATOR) is None:
43+
Config.CLIENT = pubsub.Client()
44+
else:
45+
credentials = EmulatorCreds()
46+
http = httplib2.Http() # Un-authorized.
47+
Config.CLIENT = pubsub.Client(credentials=credentials,
48+
http=http)
3949

4050

4151
class TestPubsub(unittest2.TestCase):

system_tests/system_test_utils.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@
3434
"""
3535

3636

37+
class EmulatorCreds(object):
38+
"""A mock credential object.
39+
40+
Used to avoid unnecessary token refreshing or reliance on the network
41+
while an emulator is running.
42+
"""
43+
44+
@staticmethod
45+
def create_scoped_required():
46+
return False
47+
48+
3749
def check_environ(*requirements):
3850

3951
missing = []

0 commit comments

Comments
 (0)