Skip to content

Commit b03451a

Browse files
committed
Add st2tests.config.db_opts_as_env_vars for itests
Integration tests need to start subprocesses that use the same database as the test. This matters when running under pantsbuild, because pants runs several instances of pytest in parallel. We configured pants to pass an env var to disambiguate parallel test runs: ST2TESTS_PARALLEL_SLOT. Then, the db name gets suffixed with this slot number at runtime. But, when an integration test runs production code in a subprocess, the production code does not use-- and should not use--any ST2TESTS_* vars, meaning the subprocess ends up using what is configured in the conf file instead of the parallel-safe test db. Thanks to a new-ish oslo_config feature, we can now update config via env variables. So, make use of that in integration tests to override the conf-file provided values with test-provided values.
1 parent d103fbf commit b03451a

File tree

6 files changed

+45
-17
lines changed

6 files changed

+45
-17
lines changed

st2api/tests/integration/test_gunicorn_configs.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import eventlet
2323
from eventlet.green import subprocess
2424

25+
import st2tests.config
2526
from st2common.models.utils import profiling
2627
from st2common.util.shell import kill_process
2728
from st2tests.base import IntegrationTestCase
@@ -41,6 +42,7 @@ def test_st2api_wsgi_entry_point(self):
4142
)
4243
env = os.environ.copy()
4344
env["ST2_CONFIG_PATH"] = ST2_CONFIG_PATH
45+
env.update(st2tests.config.db_opts_as_env_vars())
4446
process = subprocess.Popen(cmd, env=env, shell=True, preexec_fn=os.setsid)
4547
try:
4648
self.add_process(process=process)
@@ -60,6 +62,7 @@ def test_st2auth(self):
6062
)
6163
env = os.environ.copy()
6264
env["ST2_CONFIG_PATH"] = ST2_CONFIG_PATH
65+
env.update(st2tests.config.db_opts_as_env_vars())
6366
process = subprocess.Popen(cmd, env=env, shell=True, preexec_fn=os.setsid)
6467
try:
6568
self.add_process(process=process)

st2common/tests/integration/test_register_content_script.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import sys
2020
import glob
2121

22+
import st2tests.config
2223
from st2tests.base import IntegrationTestCase
2324
from st2common.util.shell import run_command
2425
from st2tests import config as test_config
@@ -56,7 +57,7 @@ def test_register_from_pack_success(self):
5657
"--register-runner-dir=%s" % (runner_dirs),
5758
]
5859
cmd = BASE_REGISTER_ACTIONS_CMD_ARGS + opts
59-
exit_code, _, stderr = run_command(cmd=cmd)
60+
exit_code, _, stderr = self._run_command(cmd=cmd)
6061
self.assertIn("Registered 3 actions.", stderr)
6162
self.assertEqual(exit_code, 0)
6263

@@ -71,7 +72,7 @@ def test_register_from_pack_fail_on_failure_pack_dir_doesnt_exist(self):
7172
"--register-no-fail-on-failure",
7273
]
7374
cmd = BASE_REGISTER_ACTIONS_CMD_ARGS + opts
74-
exit_code, _, _ = run_command(cmd=cmd)
75+
exit_code, _, _ = self._run_command(cmd=cmd)
7576
self.assertEqual(exit_code, 0)
7677

7778
# Fail on failure, should fail
@@ -81,7 +82,7 @@ def test_register_from_pack_fail_on_failure_pack_dir_doesnt_exist(self):
8182
"--register-fail-on-failure",
8283
]
8384
cmd = BASE_REGISTER_ACTIONS_CMD_ARGS + opts
84-
exit_code, _, stderr = run_command(cmd=cmd)
85+
exit_code, _, stderr = self._run_command(cmd=cmd)
8586
self.assertIn('Directory "doesntexistblah" doesn\'t exist', stderr)
8687
self.assertEqual(exit_code, 1)
8788

@@ -97,7 +98,7 @@ def test_register_from_pack_action_metadata_fails_validation(self):
9798
]
9899

99100
cmd = BASE_REGISTER_ACTIONS_CMD_ARGS + opts
100-
exit_code, _, stderr = run_command(cmd=cmd)
101+
exit_code, _, stderr = self._run_command(cmd=cmd)
101102
self.assertIn("Registered 0 actions.", stderr)
102103
self.assertEqual(exit_code, 0)
103104

@@ -109,7 +110,7 @@ def test_register_from_pack_action_metadata_fails_validation(self):
109110
"--register-runner-dir=%s" % (runner_dirs),
110111
]
111112
cmd = BASE_REGISTER_ACTIONS_CMD_ARGS + opts
112-
exit_code, _, stderr = run_command(cmd=cmd)
113+
exit_code, _, stderr = self._run_command(cmd=cmd)
113114
self.assertIn("object has no attribute 'get'", stderr)
114115
self.assertEqual(exit_code, 1)
115116

@@ -127,7 +128,7 @@ def test_register_from_packs_doesnt_throw_on_missing_pack_resource_folder(self):
127128
"-v",
128129
"--register-sensors",
129130
]
130-
exit_code, _, stderr = run_command(cmd=cmd)
131+
exit_code, _, stderr = self._run_command(cmd=cmd)
131132
self.assertIn("Registered 0 sensors.", stderr, "Actual stderr: %s" % (stderr))
132133
self.assertEqual(exit_code, 0)
133134

@@ -139,7 +140,7 @@ def test_register_from_packs_doesnt_throw_on_missing_pack_resource_folder(self):
139140
"--register-all",
140141
"--register-no-fail-on-failure",
141142
]
142-
exit_code, _, stderr = run_command(cmd=cmd)
143+
exit_code, _, stderr = self._run_command(cmd=cmd)
143144
self.assertIn("Registered 0 actions.", stderr)
144145
self.assertIn("Registered 0 sensors.", stderr)
145146
self.assertIn("Registered 0 rules.", stderr)
@@ -155,7 +156,7 @@ def test_register_all_and_register_setup_virtualenvs(self):
155156
"--register-setup-virtualenvs",
156157
"--register-no-fail-on-failure",
157158
]
158-
exit_code, stdout, stderr = run_command(cmd=cmd)
159+
exit_code, stdout, stderr = self._run_command(cmd=cmd)
159160
self.assertIn("Registering actions", stderr, "Actual stderr: %s" % (stderr))
160161
self.assertIn("Registering rules", stderr)
161162
self.assertIn("Setup virtualenv for %s pack(s)" % ("1"), stderr)
@@ -170,7 +171,7 @@ def test_register_setup_virtualenvs(self):
170171
"--register-setup-virtualenvs",
171172
"--register-no-fail-on-failure",
172173
]
173-
exit_code, stdout, stderr = run_command(cmd=cmd)
174+
exit_code, stdout, stderr = self._run_command(cmd=cmd)
174175

175176
self.assertIn('Setting up virtualenv for pack "dummy_pack_1"', stderr)
176177
self.assertIn("Setup virtualenv for 1 pack(s)", stderr)
@@ -186,7 +187,7 @@ def test_register_recreate_virtualenvs(self):
186187
"--register-setup-virtualenvs",
187188
"--register-no-fail-on-failure",
188189
]
189-
exit_code, stdout, stderr = run_command(cmd=cmd)
190+
exit_code, stdout, stderr = self._run_command(cmd=cmd)
190191

191192
self.assertIn('Setting up virtualenv for pack "dummy_pack_1"', stderr)
192193
self.assertIn("Setup virtualenv for 1 pack(s)", stderr)
@@ -200,9 +201,15 @@ def test_register_recreate_virtualenvs(self):
200201
"--register-recreate-virtualenvs",
201202
"--register-no-fail-on-failure",
202203
]
203-
exit_code, stdout, stderr = run_command(cmd=cmd)
204+
exit_code, stdout, stderr = self._run_command(cmd=cmd)
204205

205206
self.assertIn('Setting up virtualenv for pack "dummy_pack_1"', stderr)
206207
self.assertIn("Virtualenv successfully removed.", stderr)
207208
self.assertIn("Setup virtualenv for 1 pack(s)", stderr)
208209
self.assertEqual(exit_code, 0)
210+
211+
@staticmethod
212+
def _run_command(cmd):
213+
env = os.environ.copy()
214+
env.update(st2tests.config.db_opts_as_env_vars())
215+
return run_command(cmd=cmd, env=env)

st2common/tests/integration/test_service_setup_log_level_filtering.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import eventlet
2222
from eventlet.green import subprocess
2323

24+
import st2tests.config
2425
from st2tests.base import IntegrationTestCase
2526
from st2tests.fixtures.conf.fixture import FIXTURE_PATH as CONF_FIXTURES_PATH
2627

@@ -223,13 +224,16 @@ def test_kombu_heartbeat_tick_log_messages_are_excluded(self):
223224
stdout = "\n".join(process.stdout.read().decode("utf-8").split("\n"))
224225
self.assertNotIn("heartbeat_tick", stdout)
225226

226-
def _start_process(self, config_path, env=None):
227+
@staticmethod
228+
def _start_process(config_path, env=None):
227229
cmd = CMD + [config_path]
228230
cwd = os.path.abspath(os.path.join(BASE_DIR, "../../../"))
229231
cwd = os.path.abspath(cwd)
232+
env = env or os.environ.copy()
233+
env.update(st2tests.config.db_opts_as_env_vars())
230234
process = subprocess.Popen(
231235
cmd,
232-
env=env or os.environ.copy(),
236+
env=env,
233237
cwd=cwd,
234238
stdout=subprocess.PIPE,
235239
stderr=subprocess.PIPE,

st2reactor/tests/integration/test_garbage_collector.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
import signal
2121
import datetime
2222

23-
from oslo_config import cfg
24-
23+
import st2tests.config
2524
from st2common.util import concurrency
2625
from st2common.constants import action as action_constants
2726
from st2common.util import date as date_utils
@@ -277,7 +276,7 @@ def _create_inquiry(self, ttl, timestamp):
277276
def _start_garbage_collector(self):
278277
subprocess = concurrency.get_subprocess_module()
279278
env = os.environ.copy()
280-
env["ST2_DATABASE__DB_NAME"] = cfg.CONF.database.db_name
279+
env.update(st2tests.config.db_opts_as_env_vars())
281280
process = subprocess.Popen(
282281
CMD_INQUIRY,
283282
stdout=subprocess.PIPE,

st2reactor/tests/integration/test_sensor_container.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def test_single_sensor_mode(self):
245245
def _start_sensor_container(self, cmd=DEFAULT_CMD):
246246
subprocess = concurrency.get_subprocess_module()
247247
env = os.environ.copy()
248-
env["ST2_DATABASE__DB_NAME"] = cfg.CONF.database.db_name
248+
env.update(st2tests.config.db_opts_as_env_vars())
249249
print("Using command: %s" % (" ".join(cmd)))
250250
process = subprocess.Popen(
251251
cmd,

st2tests/st2tests/config.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from __future__ import absolute_import
1717

1818
import os
19+
from typing import Dict
1920

2021
from oslo_config import cfg, types
2122

@@ -92,6 +93,20 @@ def _override_db_opts():
9293
CONF.set_override(name="host", override="127.0.0.1", group="database")
9394

9495

96+
def db_opts_as_env_vars() -> Dict[str, str]:
97+
env = {
98+
"ST2_DATABASE__HOST": CONF.database.host,
99+
"ST2_DATABASE__PORT": str(CONF.database.port),
100+
"ST2_DATABASE__DB_NAME": CONF.database.db_name,
101+
"ST2_DATABASE__CONNECTION_TIMEOUT": CONF.database.connection_timeout,
102+
}
103+
if hasattr(CONF.database, "username"):
104+
env["ST2_DATABASE__USERNAME"] = CONF.database.username
105+
if hasattr(CONF.database, "password"):
106+
env["ST2_DATABASE__PASSWORD"] = CONF.database.password
107+
return env
108+
109+
95110
def _override_common_opts():
96111
packs_base_path = get_fixtures_packs_base_path()
97112
CONF.set_override(name="base_path", override=packs_base_path, group="system")

0 commit comments

Comments
 (0)