Skip to content

Commit

Permalink
Adding support for pubsub emulator in system test.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes committed Jan 16, 2016
1 parent c2999f3 commit 6469d43
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 14 deletions.
22 changes: 22 additions & 0 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,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
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


class Config(object):
Expand All @@ -32,7 +36,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 6469d43

Please sign in to comment.