Repository navigation
sdk: Add REQUIRE_COUCHDB_INTEGRATION_TESTS to fail CouchDB tests
#653
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
base: develop
Are you sure you want to change the base?
Changes from all commits
3850a54
dfe1177
f00ae8f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,10 +7,12 @@ | |
|
|
||
| import base64 | ||
| import configparser | ||
| import http.client | ||
| import os | ||
| import os.path | ||
| import urllib.error | ||
| import unittest | ||
| import urllib.request | ||
| from typing import Final, Optional | ||
|
|
||
| TEST_CONFIG = configparser.ConfigParser() | ||
| TEST_CONFIG.read( | ||
|
|
@@ -21,13 +23,25 @@ | |
| ) | ||
|
|
||
|
|
||
| def env_flag(name: str) -> bool: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice helper.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It already accepts the same values. It can't import env_flag because the server tests don't depend on sdk/test, so I'd leave it. |
||
| """Return whether the environment variable ``name`` is set to "1", "true" or "yes" (case-insensitive).""" | ||
| return os.environ.get(name, "false").lower() in {"1", "true", "yes"} | ||
|
|
||
|
|
||
| # By default, the schema validation tests are skipped when the AAS schema files are missing, so that a plain local | ||
| # `python -m unittest` run doesn't require downloading them first. Set this environment variable to "1"/"true" | ||
| # (e.g. in CI) to instead make those tests fail if the files are missing, so a broken download can't silently cause | ||
| # the tests to be skipped without anyone noticing. | ||
| REQUIRE_SCHEMAS = os.environ.get("REQUIRE_SCHEMA_TESTS", "false").lower() in {"1", "true", "yes"} | ||
| REQUIRE_SCHEMAS: Final[bool] = env_flag("REQUIRE_SCHEMA_TESTS") | ||
|
|
||
| # Likewise, the CouchDB tests are skipped when no CouchDB is reachable. Set this environment variable to fail them | ||
| # instead (see require_couchdb()). | ||
| REQUIRE_COUCHDB_INTEGRATION_TESTS: Final[bool] = env_flag("REQUIRE_COUCHDB_INTEGRATION_TESTS") | ||
|
|
||
| # Check if CouchDB database is available. Otherwise, skip tests. | ||
| # Check if the CouchDB test database is reachable. The timeout keeps an unresponsive server from stalling the test run. | ||
| # OSError covers HTTP errors (urllib's URLError) as well as refused connections and timeouts. HTTPException covers | ||
| # malformed responses, e.g. from a service on the port that does not speak HTTP. | ||
| COUCHDB_ERROR: Optional[Exception] = None | ||
| try: | ||
| request = urllib.request.Request( | ||
| "{}/{}".format( | ||
|
|
@@ -47,9 +61,22 @@ | |
| }, | ||
| method="HEAD", | ||
| ) | ||
| urllib.request.urlopen(request) | ||
| COUCHDB_OKAY = True | ||
| COUCHDB_ERROR = None | ||
| except urllib.error.URLError as e: | ||
| COUCHDB_OKAY = False | ||
| urllib.request.urlopen(request, timeout=5).close() | ||
| except (OSError, http.client.HTTPException) as e: | ||
| COUCHDB_ERROR = e | ||
|
|
||
|
|
||
| def require_couchdb() -> None: | ||
| """ | ||
| Skip the calling test if no CouchDB is reachable, or fail it if ``REQUIRE_COUCHDB_INTEGRATION_TESTS`` is set. | ||
|
|
||
| Call it in ``setUpClass()`` of a test class or at the beginning of a test method. | ||
| """ | ||
| if COUCHDB_ERROR is None: | ||
| return | ||
| location = "{}/{}".format(TEST_CONFIG["couchdb"]["url"], TEST_CONFIG["couchdb"]["database"]) | ||
| if REQUIRE_COUCHDB_INTEGRATION_TESTS: | ||
| raise RuntimeError( | ||
| f"REQUIRE_COUCHDB_INTEGRATION_TESTS is set, but no CouchDB is reachable at {location}: {COUCHDB_ERROR}" | ||
| ) | ||
| raise unittest.SkipTest(f"No CouchDB is reachable at {location}: {COUCHDB_ERROR}, skipping test") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,7 +17,7 @@ | |
|
|
||
| from basyx.aas import model | ||
|
|
||
| from .._helper.test_helpers import COUCHDB_ERROR, COUCHDB_OKAY, TEST_CONFIG | ||
| from .._helper.test_helpers import require_couchdb | ||
|
|
||
|
|
||
| class TutorialTest(unittest.TestCase): | ||
|
|
@@ -35,17 +35,10 @@ def test_tutorial_storage(self): | |
| pass | ||
| # The tutorial already includes assert statements for the relevant points. So no further checks are required. | ||
|
|
||
| @unittest.skipUnless( | ||
| COUCHDB_OKAY, | ||
| "No CouchDB is reachable at {}/{}: {}".format( | ||
| TEST_CONFIG["couchdb"]["url"], | ||
| TEST_CONFIG["couchdb"]["database"], | ||
| COUCHDB_ERROR, | ||
| ), | ||
| ) | ||
| def test_tutorial_backend_couchdb(self): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. The other empty tutorial tests are tracked in #634, so I kept them out of this PR. |
||
| pass | ||
| # The tutorial already includes assert statements for the relevant points. So no further checks are required. | ||
| require_couchdb() | ||
| # Importing the tutorial runs it. It raises an error if storing or deleting its objects in the CouchDB fails. | ||
| from basyx.aas.examples import tutorial_backend_couchdb # noqa: F401 | ||
|
|
||
| def test_tutorial_serialization_deserialization_json(self): | ||
| with temporary_workingdirectory(): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The docstring presents
--failsafeas safe for idempotent re-runs, but it has two side effects that should be mentioned:_securityentry. If an earlier run failed after creating the DB, re-running with--failsafeleaves the user missing, the probe gets a 401, and the tests skip or fail.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Note that deleting only the database isn't enough, the script then fails with 409 because the user exists, so the docstring says to delete both.