Skip to content

Commit 637590c

Browse files
committed
separate pytest-specific rule from generic mongo_is_running rule
1 parent 50dfcd8 commit 637590c

1 file changed

Lines changed: 31 additions & 6 deletions

File tree

pants-plugins/uses_services/mongo_rules.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414
import os
1515

16+
from dataclasses import dataclass
1617
from textwrap import dedent
1718

1819
from pants.backend.python.goals.pytest_runner import (
@@ -41,7 +42,17 @@
4142
from uses_services.target_types import UsesServicesField
4243

4344

44-
class UsesMongoRequest(PytestPluginSetupRequest):
45+
@dataclass(frozen=True)
46+
class UsesMongoRequest:
47+
pass
48+
49+
50+
@dataclass(frozen=True)
51+
class MongoIsRunning:
52+
pass
53+
54+
55+
class PytestUsesMongoRequest(PytestPluginSetupRequest):
4556
@classmethod
4657
def is_applicable(cls, target: Target) -> bool:
4758
if not target.has_field(UsesServicesField):
@@ -51,13 +62,25 @@ def is_applicable(cls, target: Target) -> bool:
5162

5263

5364
@rule(
54-
desc="Test to see if mongodb is running and accessible for tests.",
65+
desc="Ensure mongodb is running and accessible before running tests.",
5566
level=LogLevel.DEBUG,
5667
)
57-
async def mongo_is_running(
58-
request: UsesMongoRequest, platform: Platform
68+
async def mongo_is_running_for_pytest(
69+
request: PytestUsesMongoRequest
5970
) -> PytestPluginSetup:
71+
# this will raise an error if mongo is not running
72+
_ = await Get(MongoIsRunning, UsesMongoRequest())
73+
74+
return PytestPluginSetup()
6075

76+
77+
@rule(
78+
desc="Test to see if mongodb is running and accessible.",
79+
level=LogLevel.DEBUG,
80+
)
81+
async def mongo_is_running(
82+
request: UsesMongoRequest, platform: Platform
83+
) -> MongoIsRunning:
6184
# These config opts are used via oslo_config.cfg.CONF.database.{host,port,db_name,connection_timeout}
6285
# These config opts currently hard-coded in:
6386
# for unit tests: st2tests/st2tests/config.py
@@ -120,7 +143,9 @@ async def mongo_is_running(
120143
is_running = result.exit_code == 0
121144

122145
if is_running:
123-
return PytestPluginSetup()
146+
return MongoIsRunning()
147+
148+
# mongo is not running, so raise an error with instructions.
124149

125150
if platform.distro in ["centos", "rhel"] or "rhel" in platform.distro_like:
126151
instructions = dedent(
@@ -240,6 +265,6 @@ async def mongo_is_running(
240265
def rules():
241266
return [
242267
*collect_rules(),
243-
UnionRule(PytestPluginSetupRequest, UsesMongoRequest),
268+
UnionRule(PytestPluginSetupRequest, PytestUsesMongoRequest),
244269
*pex_rules(),
245270
]

0 commit comments

Comments
 (0)