Skip to content

Commit ec9e3e4

Browse files
committed
add test for uses_services plugin mongo rule
1 parent 149e688 commit ec9e3e4

1 file changed

Lines changed: 193 additions & 0 deletions

File tree

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
# Copyright 2023 The StackStorm Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
from __future__ import annotations
15+
16+
import os
17+
18+
import pytest
19+
20+
from pants.backend.python import target_types_rules
21+
from pants.backend.python.target_types import PythonSourcesGeneratorTarget
22+
23+
from pants.core.util_rules.source_files import SourceFiles, SourceFilesRequest
24+
from pants.engine.addresses import Address
25+
from pants.engine.internals.scheduler import ExecutionError
26+
from pants.engine.fs import CreateDigest, Digest, FileContent, Snapshot
27+
from pants.engine.target import Target
28+
from pants.core.goals.fmt import FmtResult
29+
from pants.testutil.rule_runner import QueryRule, RuleRunner
30+
31+
from .exceptions import ServiceMissingError
32+
from .mongo_rules import (
33+
MongoIsRunning,
34+
PytestUsesMongoRequest,
35+
UsesMongoRequest,
36+
rules as mongo_rules
37+
)
38+
from .platform_rules import Platform
39+
from .target_types import UsesServicesField
40+
41+
42+
class MockTarget(Target):
43+
alias = "mock_target"
44+
core_fields = (UsesServicesField,)
45+
46+
47+
@pytest.fixture
48+
def rule_runner() -> RuleRunner:
49+
return RuleRunner(
50+
rules=[
51+
*mongo_rules(),
52+
# *target_types_rules.rules(),
53+
QueryRule(MongoIsRunning, (UsesMongoRequest, Platform)),
54+
# QueryRule(PytestPluginSetup, (PytestUsesMongoRequest)),
55+
],
56+
target_types=[
57+
# MockTarget,
58+
# PythonTestsGeneratorTarget,
59+
# PythonTestTarget,
60+
],
61+
)
62+
63+
64+
def run_mongo_is_running(
65+
rule_runner: RuleRunner,
66+
uses_mongo_request: UsesMongoRequest,
67+
mock_platform: Platform,
68+
*,
69+
extra_args: list[str] | None = None,
70+
) -> MongoIsRunning:
71+
rule_runner.set_options(
72+
[
73+
"--backend-packages=uses_services",
74+
*(extra_args or ()),
75+
],
76+
env_inherit={"PATH", "PYENV_ROOT", "HOME"},
77+
)
78+
result = rule_runner.request(
79+
MongoIsRunning,
80+
[uses_mongo_request, mock_platform],
81+
)
82+
return result
83+
84+
85+
def platform(
86+
arch="",
87+
os="",
88+
distro="",
89+
distro_name="",
90+
distro_codename="",
91+
distro_like="",
92+
distro_major_version="",
93+
distro_version="",
94+
mac_release="",
95+
win_release="",
96+
) -> Platform:
97+
"""Create a Platform with all values defaulted to the empty string."""
98+
return Platform(
99+
arch=arch,
100+
os=os,
101+
distro=distro,
102+
distro_name=distro_name,
103+
distro_codename=distro_codename,
104+
distro_like=distro_like,
105+
distro_major_version=distro_major_version,
106+
distro_version=distro_version,
107+
mac_release=mac_release,
108+
win_release=win_release,
109+
)
110+
111+
112+
# TODO: this requires that mongo be running...
113+
#def test_is_running(rule_runner: RuleRunner) -> None:
114+
# request = UsesMongoRequest()
115+
# mock_platform = platform()
116+
117+
# we are asserting that this does not raise an exception
118+
# is_running = run_mongo_is_running(rule_runner, request, mock_platform)
119+
# assert is_running
120+
121+
122+
@pytest.mark.parametrize(
123+
"mock_platform",
124+
(
125+
platform(), # empty
126+
# platform(
127+
# arch="x86_64",
128+
# os="Linux",
129+
# distro="",
130+
# distro_name="",
131+
# distro_codename="",
132+
# distro_like="",
133+
# distro_major_version="",
134+
# distro_version="",
135+
# ),
136+
platform(
137+
arch="x86_64",
138+
os="Linux",
139+
distro="centos",
140+
distro_name="Centos Linux",
141+
distro_codename="Core",
142+
distro_like="rhel fedora",
143+
distro_major_version="7",
144+
distro_version="7",
145+
),
146+
platform(
147+
arch="x86_64",
148+
os="Linux",
149+
distro="ubuntu",
150+
distro_name="Ubuntu",
151+
distro_codename="xenial",
152+
distro_like="debian",
153+
distro_major_version="16",
154+
distro_version="16.04",
155+
),
156+
platform(
157+
arch="x86_64",
158+
os="Linux",
159+
distro="gentoo",
160+
distro_name="Gentoo",
161+
distro_codename="n/a",
162+
distro_major_version="2",
163+
distro_version="2.7",
164+
),
165+
platform(
166+
arch="x86_64",
167+
os="Darwin",
168+
distro="darwin",
169+
distro_name="Darwin",
170+
distro_major_version="19",
171+
distro_version="19.6.0",
172+
mac_release="10.15.7",
173+
),
174+
),
175+
)
176+
def test_not_running(rule_runner: RuleRunner, mock_platform: Platform) -> None:
177+
request = UsesMongoRequest(
178+
db_host="127.100.20.7",
179+
db_port=10, # unassigned port, unlikely to be used
180+
)
181+
182+
with pytest.raises(ExecutionError) as exception_info:
183+
run_mongo_is_running(rule_runner, request, mock_platform)
184+
185+
execution_error = exception_info.value
186+
assert len(execution_error.wrapped_exceptions) == 1
187+
188+
exc = execution_error.wrapped_exceptions[0]
189+
assert isinstance(exc, ServiceMissingError)
190+
191+
assert exc.service == "mongo"
192+
assert "The mongo service does not seem to be running" in str(exc)
193+
assert exc.instructions != ""

0 commit comments

Comments
 (0)