Skip to content

Commit

Permalink
Merge pull request #1396 from dhermes/pubsub-w-emulator
Browse files Browse the repository at this point in the history
Adding support for pubsub emulator in system test.
  • Loading branch information
dhermes committed Jan 20, 2016
2 parents 411fd38 + 6469d43 commit 2c2f575
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 26 deletions.
22 changes: 22 additions & 0 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,28 @@ Running System Tests

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

- To run the ``pubsub`` system tests with an emulator, first start the
emulator and take note of the process ID::

$ gcloud beta emulators pubsub start &
[1] 44444

then determine the environment variables needed to interact with
the emulator::

$ gcloud beta emulators pubsub env-init
export PUBSUB_EMULATOR_HOST=localhost:8897

using these environment variables run the emulator::

$ DATASTORE_HOST=http://localhost:8897 \
> python system_tests/run_system_test.py \
> --package=pubsub

and after completion stop the emulator::

$ kill 44444

Test Coverage
-------------

Expand Down
3 changes: 2 additions & 1 deletion gcloud/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ def _make_request(self, method, url, data=None, content_type=None,
else:
content_length = 0

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

if content_type:
headers['Content-Type'] = content_type
Expand Down
8 changes: 4 additions & 4 deletions gcloud/storage/test_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def test__make_request_GET_normal(self):
self.assertEqual(http._requests, [])
EXPECTED_HEADERS = [
('Accept-Encoding', 'gzip'),
('Content-Length', 0),
('Content-Length', '0'),
]
solo_request, = batch._requests
self.assertEqual(solo_request[0], 'GET')
Expand All @@ -140,7 +140,7 @@ def test__make_request_POST_normal(self):
self.assertEqual(http._requests, [])
EXPECTED_HEADERS = [
('Accept-Encoding', 'gzip'),
('Content-Length', 10),
('Content-Length', '10'),
]
solo_request, = batch._requests
self.assertEqual(solo_request[0], 'POST')
Expand All @@ -165,7 +165,7 @@ def test__make_request_PATCH_normal(self):
self.assertEqual(http._requests, [])
EXPECTED_HEADERS = [
('Accept-Encoding', 'gzip'),
('Content-Length', 10),
('Content-Length', '10'),
]
solo_request, = batch._requests
self.assertEqual(solo_request[0], 'PATCH')
Expand All @@ -190,7 +190,7 @@ def test__make_request_DELETE_normal(self):
self.assertEqual(http._requests, [])
EXPECTED_HEADERS = [
('Accept-Encoding', 'gzip'),
('Content-Length', 0),
('Content-Length', '0'),
]
solo_request, = batch._requests
self.assertEqual(solo_request[0], 'DELETE')
Expand Down
14 changes: 7 additions & 7 deletions gcloud/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def test__make_request_no_data_no_content_type_no_headers(self):
self.assertEqual(http._called_with['body'], None)
expected_headers = {
'Accept-Encoding': 'gzip',
'Content-Length': 0,
'Content-Length': '0',
'User-Agent': conn.USER_AGENT,
}
self.assertEqual(http._called_with['headers'], expected_headers)
Expand All @@ -181,7 +181,7 @@ def test__make_request_w_data_no_extra_headers(self):
self.assertEqual(http._called_with['body'], {})
expected_headers = {
'Accept-Encoding': 'gzip',
'Content-Length': 0,
'Content-Length': '0',
'Content-Type': 'application/json',
'User-Agent': conn.USER_AGENT,
}
Expand All @@ -200,7 +200,7 @@ def test__make_request_w_extra_headers(self):
self.assertEqual(http._called_with['body'], None)
expected_headers = {
'Accept-Encoding': 'gzip',
'Content-Length': 0,
'Content-Length': '0',
'X-Foo': 'foo',
'User-Agent': conn.USER_AGENT,
}
Expand All @@ -225,7 +225,7 @@ def test_api_request_defaults(self):
self.assertEqual(http._called_with['body'], None)
expected_headers = {
'Accept-Encoding': 'gzip',
'Content-Length': 0,
'Content-Length': '0',
'User-Agent': conn.USER_AGENT,
}
self.assertEqual(http._called_with['headers'], expected_headers)
Expand Down Expand Up @@ -274,7 +274,7 @@ def test_api_request_w_query_params(self):
self.assertEqual(http._called_with['body'], None)
expected_headers = {
'Accept-Encoding': 'gzip',
'Content-Length': 0,
'Content-Length': '0',
'User-Agent': conn.USER_AGENT,
}
self.assertEqual(http._called_with['headers'], expected_headers)
Expand All @@ -301,7 +301,7 @@ def test_api_request_w_data(self):
self.assertEqual(http._called_with['body'], DATAJ)
expected_headers = {
'Accept-Encoding': 'gzip',
'Content-Length': len(DATAJ),
'Content-Length': str(len(DATAJ)),
'Content-Type': 'application/json',
'User-Agent': conn.USER_AGENT,
}
Expand Down Expand Up @@ -345,7 +345,7 @@ def test_api_request_non_binary_response(self):
self.assertEqual(http._called_with['body'], None)
expected_headers = {
'Accept-Encoding': 'gzip',
'Content-Length': 0,
'Content-Length': '0',
'User-Agent': conn.USER_AGENT,
}
self.assertEqual(http._called_with['headers'], expected_headers)
Expand Down
15 changes: 2 additions & 13 deletions system_tests/datastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
# repository root is the current directory.
from system_tests import clear_datastore
from system_tests import populate_datastore
from system_tests.system_test_utils import EmulatorCreds


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


class _EmulatorCreds(object):
"""A mock credential object.
Used to avoid unnecessary token refreshing or reliance on the network
while an emulator is running.
"""

@staticmethod
def create_scoped_required():
return False


def clone_client(client):
# Fool the Client constructor to avoid creating a new connection.
cloned_client = datastore.Client(project=client.project,
Expand All @@ -71,7 +60,7 @@ def setUpModule():
client_mod.DATASET = TESTS_DATASET
Config.CLIENT = datastore.Client(namespace=TEST_NAMESPACE)
else:
credentials = _EmulatorCreds()
credentials = EmulatorCreds()
http = httplib2.Http() # Un-authorized.
Config.CLIENT = datastore.Client(project=EMULATOR_DATASET,
namespace=TEST_NAMESPACE,
Expand Down
12 changes: 11 additions & 1 deletion system_tests/pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import time

import httplib2
import unittest2

from gcloud import _helpers
from gcloud.environment_vars import PUBSUB_EMULATOR
from gcloud.environment_vars import TESTS_PROJECT
from gcloud import pubsub
from system_tests.system_test_utils import EmulatorCreds


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

def setUpModule():
_helpers.PROJECT = TESTS_PROJECT
Config.CLIENT = pubsub.Client()
if os.getenv(PUBSUB_EMULATOR) is None:
Config.CLIENT = pubsub.Client()
else:
credentials = EmulatorCreds()
http = httplib2.Http() # Un-authorized.
Config.CLIENT = pubsub.Client(credentials=credentials,
http=http)


class TestPubsub(unittest2.TestCase):
Expand Down
12 changes: 12 additions & 0 deletions system_tests/system_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@
"""


class EmulatorCreds(object):
"""A mock credential object.
Used to avoid unnecessary token refreshing or reliance on the network
while an emulator is running.
"""

@staticmethod
def create_scoped_required():
return False


def check_environ(*requirements):

missing = []
Expand Down

0 comments on commit 2c2f575

Please sign in to comment.