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 7 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
30 changes: 30 additions & 0 deletions .github/workflows/system_emulated.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: "Run systests on emulator"
on:
#pull_request
# branches:
# - main
push: # temporary
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid merging until this is switched to run on PRs, rather than pushes.


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.8'

- 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
10 changes: 9 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 @@ -122,6 +123,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 = "nonesuch-project-123"
tseaver marked this conversation as resolved.
Show resolved Hide resolved

super(BaseClient, self).__init__(
project=project,
credentials=credentials,
Expand All @@ -137,7 +146,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
40 changes: 40 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,45 @@ def unit(session):
default(session)


@nox.session(python="3.8")
tseaver marked this conversation as resolved.
Show resolved Hide resolved
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)
pass
tseaver marked this conversation as resolved.
Show resolved Hide resolved


@nox.session(python=SYSTEM_TEST_PYTHON_VERSIONS)
def system(session):
"""Run the system test suite."""
Expand Down
71 changes: 71 additions & 0 deletions owlbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,77 @@ 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="3.8")
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)
pass

"""

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
38 changes: 38 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,44 @@ 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 _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, "nonesuch-project-123")

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 _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, "nonesuch-project-123")

@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