1111# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212# See the License for the specific language governing permissions and
1313# limitations under the License.
14+ from __future__ import annotations
1415
1516from dataclasses import dataclass
1617from textwrap import dedent
2728 VenvPexProcess ,
2829 rules as pex_rules ,
2930)
31+ from pants .core .goals .test import TestExtraEnv
32+ from pants .engine .env_vars import EnvironmentVars
3033from pants .engine .fs import CreateDigest , Digest , FileContent
3134from pants .engine .rules import collect_rules , Get , MultiGet , rule
3235from pants .engine .process import FallibleProcessResult , ProcessCacheScope
@@ -55,21 +58,50 @@ class UsesMongoRequest:
5558 # for unit tests: st2tests/st2tests/config.py
5659 # for integration tests: conf/st2.tests*.conf st2tests/st2tests/fixtures/conf/st2.tests*.conf
5760 # (changed by setting ST2_CONFIG_PATH env var inside the tests)
58- # TODO: for unit tests: modify code to pull db connect settings from env vars
59- # TODO: for int tests: modify st2.tests*.conf on the fly to set the per-pantsd-slot db_name
60- # and either add env vars for db connect settings or modify conf files as well
61-
62- # with our version of oslo.config (newer are slower) we can't directly override opts w/ environment variables.
61+ # These can also be updated via the ST2_DATABASE__* env vars (which oslo_config reads).
62+ # Integration tests should pass these changes onto subprocesses via the same env vars.
6363
6464 db_host : str = "127.0.0.1" # localhost in test_db.DbConnectionTestCase
6565 db_port : int = 27017
6666 # db_name is "st2" in test_db.DbConnectionTestCase
6767 db_name : str = "st2-test{}" # {} will be replaced by test slot (a format string)
6868
69+ # username and password are not required to validate connectivity, so this doesn't have them.
70+
6971 db_connection_timeout : int = 3000
7072
7173 execution_slot_var : str = "ST2TESTS_PARALLEL_SLOT"
7274
75+ @classmethod
76+ def from_env (
77+ cls , execution_slot_var : str , env : EnvironmentVars
78+ ) -> UsesMongoRequest :
79+ default = cls ()
80+ host = env .get ("ST2_DATABASE__HOST" , default .db_host )
81+ port_raw = env .get ("ST2_DATABASE__PORT" , str (default .db_port ))
82+ db_name = default .db_name # not overridable via ST2_DATABASE__DB_NAME
83+ db_connection_timeout_raw = env .get (
84+ "ST2_DATABASE__CONNECTION_TIMEOUT" , str (default .db_connection_timeout )
85+ )
86+
87+ try :
88+ port = int (port_raw )
89+ except (TypeError , ValueError ):
90+ port = default .db_port
91+
92+ try :
93+ db_connection_timeout = int (db_connection_timeout_raw )
94+ except (TypeError , ValueError ):
95+ db_connection_timeout = default .db_connection_timeout
96+
97+ return cls (
98+ db_host = host ,
99+ db_port = port ,
100+ db_name = db_name ,
101+ db_connection_timeout = db_connection_timeout ,
102+ execution_slot_var = execution_slot_var ,
103+ )
104+
73105
74106@dataclass (frozen = True )
75107class MongoIsRunning :
@@ -90,7 +122,9 @@ def is_applicable(cls, target: Target) -> bool:
90122 level = LogLevel .DEBUG ,
91123)
92124async def mongo_is_running_for_pytest (
93- request : PytestUsesMongoRequest , pytest : PyTest
125+ request : PytestUsesMongoRequest ,
126+ pytest : PyTest ,
127+ test_extra_env : TestExtraEnv ,
94128) -> PytestPluginSetup :
95129 # TODO: delete these comments once the Makefile becomes irrelevant.
96130 # the comments explore how the Makefile prepares to run and runs tests
@@ -109,7 +143,9 @@ async def mongo_is_running_for_pytest(
109143 # this will raise an error if mongo is not running
110144 _ = await Get (
111145 MongoIsRunning ,
112- UsesMongoRequest (execution_slot_var = pytest .execution_slot_var or "" ),
146+ UsesMongoRequest .from_env (
147+ execution_slot_var = pytest .execution_slot_var or "" , env = test_extra_env .env
148+ ),
113149 )
114150
115151 return PytestPluginSetup ()
@@ -151,8 +187,8 @@ async def mongo_is_running(
151187 str (request .db_port ),
152188 request .db_name ,
153189 str (request .db_connection_timeout ),
154- request .execution_slot_var ,
155190 ),
191+ extra_env = {"PANTS_PYTEST_EXECUTION_SLOT_VAR" : request .execution_slot_var },
156192 input_digest = script_digest ,
157193 execution_slot_variable = request .execution_slot_var ,
158194 description = "Checking to see if Mongo is up and accessible." ,
@@ -200,6 +236,18 @@ async def mongo_is_running(
200236 """
201237 ),
202238 service_start_cmd_generic = "systemctl start mongod" ,
239+ env_vars_hint = dedent (
240+ """\
241+ You can also export the ST2_DATABASE__HOST and ST2_DATABASE__PORT
242+ env vars to automatically use any MongoDB host, local or remote,
243+ while running unit and integration tests. Note that you cannot
244+ override the db-name, which is st2-test suffixed by an integer
245+ to allow tests to safely run in parallel. If needed you can also
246+ override the default username, password, and connection timeout
247+ by exporting one or more of: ST2_DATABASE__USERNAME,
248+ ST2_DATABASE__PASSWORD, and ST2_DATABASE__CONNECTION_TIMEOUT.
249+ """
250+ ),
203251 ),
204252 )
205253
0 commit comments