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

tests: ensure systests pass under emulator in a clean environment #464

Merged
merged 13 commits into from
Sep 30, 2021
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
29 changes: 29 additions & 0 deletions .github/workflows/system_emulated.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: "Run systests on emulator"
on:
pull_request:
branches:
- main

jobs:

run-systests:
runs-on: ubuntu-20.04

steps:

- name: Checkout
uses: actions/checkout@v2

- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: '3.7'

- name: Setup GCloud SDK
uses: google-github-actions/setup-gcloud@v0.2.1

- name: Install / run Nox
run: |
python -m pip install --upgrade setuptools pip
python -m pip install nox
nox -s system_emulated
11 changes: 10 additions & 1 deletion google/cloud/firestore_v1/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import os
import grpc # type: ignore

from google.auth.credentials import AnonymousCredentials
import google.api_core.client_options # type: ignore
import google.api_core.path_template # type: ignore
from google.api_core import retry as retries # type: ignore
Expand Down Expand Up @@ -61,6 +62,7 @@

DEFAULT_DATABASE = "(default)"
"""str: The default database used in a :class:`~google.cloud.firestore_v1.client.Client`."""
_DEFAULT_EMULATOR_PROJECT = "google-cloud-firestore-emulator"
_BAD_OPTION_ERR = (
"Exactly one of ``last_update_time`` or ``exists`` " "must be provided."
)
Expand Down Expand Up @@ -122,6 +124,14 @@ def __init__(
# NOTE: This API has no use for the _http argument, but sending it
# will have no impact since the _http() @property only lazily
# creates a working HTTP object.
self._emulator_host = os.getenv(_FIRESTORE_EMULATOR_HOST)

if self._emulator_host is not None:
if credentials is None:
credentials = AnonymousCredentials()
if project is None:
project = _DEFAULT_EMULATOR_PROJECT

super(BaseClient, self).__init__(
project=project,
credentials=credentials,
Expand All @@ -137,7 +147,6 @@ def __init__(
self._client_options = client_options

self._database = database
self._emulator_host = os.getenv(_FIRESTORE_EMULATOR_HOST)
tseaver marked this conversation as resolved.
Show resolved Hide resolved

def _firestore_api_helper(self, transport, client_class, client_module) -> Any:
"""Lazy-loading getter GAPIC Firestore API.
Expand Down
39 changes: 39 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
# 'docfx' is excluded since it only needs to run in 'docs-presubmit'
nox.options.sessions = [
"unit",
"system_emulated",
"system",
"cover",
"lint",
Expand Down Expand Up @@ -128,6 +129,44 @@ def unit(session):
default(session)


@nox.session(python=SYSTEM_TEST_PYTHON_VERSIONS)
def system_emulated(session):
import subprocess
import signal

try:
subprocess.call(["gcloud", "--version"])
except OSError:
session.skip("gcloud not found but required for emulator support")

# Currently, CI/CD doesn't have beta component of gcloud.
subprocess.call(
["gcloud", "components", "install", "beta", "cloud-firestore-emulator",]
)

hostport = "localhost:8789"
session.env["FIRESTORE_EMULATOR_HOST"] = hostport

p = subprocess.Popen(
[
"gcloud",
"--quiet",
"beta",
"emulators",
"firestore",
"start",
"--host-port",
hostport,
]
)

try:
system(session)
finally:
# Stop Emulator
os.killpg(os.getpgid(p.pid), signal.SIGKILL)


@nox.session(python=SYSTEM_TEST_PYTHON_VERSIONS)
def system(session):
"""Run the system test suite."""
Expand Down
70 changes: 70 additions & 0 deletions owlbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,76 @@ def update_fixup_scripts(library):

s.move(templated_files)

# ----------------------------------------------------------------------------
# Customize noxfile.py
# ----------------------------------------------------------------------------

def place_before(path, text, *before_text, escape=None):
replacement = "\n".join(before_text) + "\n" + text
if escape:
for c in escape:
text = text.replace(c, '\\' + c)
s.replace([path], text, replacement)

system_emulated_session = """
@nox.session(python=SYSTEM_TEST_PYTHON_VERSIONS)
def system_emulated(session):
import subprocess
import signal

try:
subprocess.call(["gcloud", "--version"])
except OSError:
session.skip("gcloud not found but required for emulator support")

# Currently, CI/CD doesn't have beta component of gcloud.
subprocess.call(
["gcloud", "components", "install", "beta", "cloud-firestore-emulator",]
)

hostport = "localhost:8789"
session.env["FIRESTORE_EMULATOR_HOST"] = hostport

p = subprocess.Popen(
[
"gcloud",
"--quiet",
"beta",
"emulators",
"firestore",
"start",
"--host-port",
hostport,
]
)

try:
system(session)
finally:
# Stop Emulator
os.killpg(os.getpgid(p.pid), signal.SIGKILL)

"""

place_before(
"noxfile.py",
"@nox.session(python=SYSTEM_TEST_PYTHON_VERSIONS)\n"
"def system(session):",
system_emulated_session,
escape="()"
)

# add system_emulated nox session
s.replace("noxfile.py",
"""nox.options.sessions = \[
"unit",
"system",""",
"""nox.options.sessions = [
"unit",
"system_emulated",
"system",""",
)

s.replace(
"noxfile.py",
"""\"--quiet\",
Expand Down
12 changes: 0 additions & 12 deletions tests/unit/v1/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,6 @@ def test_constructor(self):
self.assertEqual(client._credentials, credentials)
self.assertEqual(client._database, DEFAULT_DATABASE)
self.assertIs(client._client_info, _CLIENT_INFO)
self.assertIsNone(client._emulator_host)

def test_constructor_with_emulator_host(self):
from google.cloud.firestore_v1.base_client import _FIRESTORE_EMULATOR_HOST

credentials = _make_credentials()
emulator_host = "localhost:8081"
with mock.patch("os.getenv") as getenv:
getenv.return_value = emulator_host
client = self._make_one(project=self.PROJECT, credentials=credentials)
self.assertEqual(client._emulator_host, emulator_host)
getenv.assert_called_once_with(_FIRESTORE_EMULATOR_HOST)
tseaver marked this conversation as resolved.
Show resolved Hide resolved

def test_constructor_explicit(self):
from google.api_core.client_options import ClientOptions
Expand Down
40 changes: 40 additions & 0 deletions tests/unit/v1/test_base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,46 @@ def _make_default_one(self):
credentials = _make_credentials()
return self._make_one(project=self.PROJECT, credentials=credentials)

def test_constructor_with_emulator_host_defaults(self):
from google.auth.credentials import AnonymousCredentials
from google.cloud.firestore_v1.base_client import _DEFAULT_EMULATOR_PROJECT
from google.cloud.firestore_v1.base_client import _FIRESTORE_EMULATOR_HOST

emulator_host = "localhost:8081"

with mock.patch("os.environ", {_FIRESTORE_EMULATOR_HOST: emulator_host}):
client = self._make_one()

self.assertEqual(client._emulator_host, emulator_host)
self.assertIsInstance(client._credentials, AnonymousCredentials)
self.assertEqual(client.project, _DEFAULT_EMULATOR_PROJECT)

def test_constructor_with_emulator_host_w_project(self):
from google.auth.credentials import AnonymousCredentials
from google.cloud.firestore_v1.base_client import _FIRESTORE_EMULATOR_HOST

emulator_host = "localhost:8081"

with mock.patch("os.environ", {_FIRESTORE_EMULATOR_HOST: emulator_host}):
client = self._make_one(project=self.PROJECT)

self.assertEqual(client._emulator_host, emulator_host)
self.assertIsInstance(client._credentials, AnonymousCredentials)

def test_constructor_with_emulator_host_w_creds(self):
from google.cloud.firestore_v1.base_client import _DEFAULT_EMULATOR_PROJECT
from google.cloud.firestore_v1.base_client import _FIRESTORE_EMULATOR_HOST

credentials = _make_credentials()
emulator_host = "localhost:8081"

with mock.patch("os.environ", {_FIRESTORE_EMULATOR_HOST: emulator_host}):
client = self._make_one(credentials=credentials)

self.assertEqual(client._emulator_host, emulator_host)
self.assertIs(client._credentials, credentials)
self.assertEqual(client.project, _DEFAULT_EMULATOR_PROJECT)

@mock.patch(
"google.cloud.firestore_v1.services.firestore.client.FirestoreClient",
autospec=True,
Expand Down
12 changes: 0 additions & 12 deletions tests/unit/v1/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,6 @@ def test_constructor(self):
self.assertEqual(client._credentials, credentials)
self.assertEqual(client._database, DEFAULT_DATABASE)
self.assertIs(client._client_info, _CLIENT_INFO)
self.assertIsNone(client._emulator_host)

def test_constructor_with_emulator_host(self):
from google.cloud.firestore_v1.base_client import _FIRESTORE_EMULATOR_HOST

credentials = _make_credentials()
emulator_host = "localhost:8081"
with mock.patch("os.getenv") as getenv:
getenv.return_value = emulator_host
client = self._make_one(project=self.PROJECT, credentials=credentials)
self.assertEqual(client._emulator_host, emulator_host)
getenv.assert_called_once_with(_FIRESTORE_EMULATOR_HOST)
tseaver marked this conversation as resolved.
Show resolved Hide resolved

def test_constructor_explicit(self):
from google.api_core.client_options import ClientOptions
Expand Down