Skip to content

Commit 149e688

Browse files
committed
refactor uses_services plugin + comments
Now the db connection settings come from the generic request object (which for now just defaults to what is hard-coded in the st2 tests). And the comments make a bit more sense in where they are.
1 parent 637590c commit 149e688

1 file changed

Lines changed: 42 additions & 30 deletions

File tree

pants-plugins/uses_services/mongo_rules.py

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,27 @@
4444

4545
@dataclass(frozen=True)
4646
class UsesMongoRequest:
47-
pass
47+
"""One or more targets need a running mongo service using these settings.
48+
49+
The db_* attributes represent the db connection settings from st2.conf.
50+
In st2 code, they come from:
51+
oslo_config.cfg.CONF.database.{host,port,db_name,connection_timeout}
52+
"""
53+
# These config opts currently hard-coded in:
54+
# for unit tests: st2tests/st2tests/config.py
55+
# for integration tests: conf/st2.tests*.conf st2tests/st2tests/fixtures/conf/st2.tests*.conf
56+
# (changed by setting ST2_CONFIG_PATH env var inside the tests)
57+
# TODO: for unit tests: modify code to pull db connect settings from env vars
58+
# TODO: for int tests: modify st2.tests*.conf on the fly to set the per-pantsd-slot db_name
59+
# and either add env vars for db connect settings or modify conf files as well
60+
61+
# with our version of oslo.config (newer are slower) we can't directly override opts w/ environment variables.
62+
63+
db_host: str = "127.0.0.1" # localhost in test_db.DbConnectionTestCase
64+
db_port: int = 27017
65+
# db_name is "st2" in test_db.DbConnectionTestCase
66+
db_name: str = f"st2-test{os.environ.get('ST2TESTS_PARALLEL_SLOT', '')}"
67+
db_connection_timeout: int = 3000
4868

4969

5070
@dataclass(frozen=True)
@@ -68,6 +88,20 @@ def is_applicable(cls, target: Target) -> bool:
6888
async def mongo_is_running_for_pytest(
6989
request: PytestUsesMongoRequest
7090
) -> PytestPluginSetup:
91+
# TODO: delete these comments once the Makefile becomes irrelevant.
92+
# the comments explore how the Makefile prepares to run and runs tests
93+
94+
# The st2-test database gets dropped between (in Makefile based testing):
95+
# - each component (st2*/ && various config/ dirs) in Makefile
96+
# - DbTestCase/CleanDbTestCase setUpClass
97+
98+
# Makefile
99+
# .run-unit-tests-coverage (<- .combine-unit-tests-coverage <- .coverage.unit <- .unit-tests-coverage-html <- ci-unit <- ci)
100+
# echo "----- Dropping st2-test db -----"
101+
# mongo st2-test --eval "db.dropDatabase();"
102+
# for component in $(COMPONENTS_TEST)
103+
# nosetests $(NOSE_OPTS) -s -v $(NOSE_COVERAGE_FLAGS) $(NOSE_COVERAGE_PACKAGES) $$component/tests/unit
104+
71105
# this will raise an error if mongo is not running
72106
_ = await Get(MongoIsRunning, UsesMongoRequest())
73107

@@ -81,34 +115,6 @@ async def mongo_is_running_for_pytest(
81115
async def mongo_is_running(
82116
request: UsesMongoRequest, platform: Platform
83117
) -> MongoIsRunning:
84-
# These config opts are used via oslo_config.cfg.CONF.database.{host,port,db_name,connection_timeout}
85-
# These config opts currently hard-coded in:
86-
# for unit tests: st2tests/st2tests/config.py
87-
# for integration tests: conf/st2.tests*.conf st2tests/st2tests/fixtures/conf/st2.tests*.conf
88-
# (changed by setting ST2_CONFIG_PATH env var inside the tests)
89-
# TODO: for unit tests: modify code to pull db connect settings from env vars
90-
# TODO: for int tests: modify st2.tests*.conf on the fly to set the per-pantsd-slot db_name
91-
# and either add env vars for db connect settings or modify conf files as well
92-
93-
db_host = "127.0.0.1" # localhost in test_db.DbConnectionTestCase
94-
db_port = 27017
95-
db_name = f"st2-test{os.environ.get('ST2TESTS_PARALLEL_SLOT', '')}"
96-
# db_name = "st2-test" # st2 in test_db.DbConnectionTestCase
97-
connection_timeout = 3000
98-
99-
# The st2-test database gets dropped between (in Makefile based testing):
100-
# - each component (st2*/ && various config/ dirs) in Makefile
101-
# - DbTestCase/CleanDbTestCase setUpClass
102-
103-
# with our version of oslo.config (newer are slower) we can't directly override opts w/ environment variables.
104-
105-
# Makefile
106-
# .run-unit-tests-coverage (<- .combine-unit-tests-coverage <- .coverage.unit <- .unit-tests-coverage-html <- ci-unit <- ci)
107-
# echo "----- Dropping st2-test db -----"
108-
# mongo st2-test --eval "db.dropDatabase();"
109-
# for component in $(COMPONENTS_TEST)
110-
# nosetests $(NOSE_OPTS) -s -v $(NOSE_COVERAGE_FLAGS) $(NOSE_COVERAGE_PACKAGES) $$component/tests/unit
111-
112118
script_path = "./is_mongo_running.py"
113119

114120
# pants is already watching this directory as it is under a source root.
@@ -132,7 +138,13 @@ async def mongo_is_running(
132138
FallibleProcessResult,
133139
VenvPexProcess(
134140
mongoengine_pex,
135-
argv=(script_path, db_host, str(db_port), db_name, str(connection_timeout)),
141+
argv=(
142+
script_path,
143+
request.db_host,
144+
str(request.db_port),
145+
request.db_name,
146+
str(request.db_connection_timeout),
147+
),
136148
input_digest=script_digest,
137149
description="Checking to see if Mongo is up and accessible.",
138150
# this can change from run to run, so don't cache results.

0 commit comments

Comments
 (0)