Skip to content

Commit d985b0d

Browse files
authored
Make unit tests use redis (#6245)
2 parents aa01b88 + 774f45b commit d985b0d

File tree

9 files changed

+181
-92
lines changed

9 files changed

+181
-92
lines changed

.github/workflows/ci.yaml

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,18 @@ jobs:
133133
- 5672:5672/tcp # AMQP standard port
134134
- 15672:15672/tcp # Management: HTTP, CLI
135135

136+
redis:
137+
# Docker Hub image
138+
image: redis
139+
# Set health checks to wait until redis has started
140+
options: >-
141+
--name "redis"
142+
--health-cmd "redis-cli ping"
143+
--health-interval 10s
144+
--health-timeout 5s
145+
--health-retries 5
146+
ports:
147+
- 6379:6379/tcp
136148
env:
137149
# CI st2.conf (with ST2_CI_USER user instead of stanley)
138150
ST2_CONF: 'conf/st2.ci.conf'
@@ -163,11 +175,6 @@ jobs:
163175
- name: Install requirements
164176
run: |
165177
./scripts/ci/install-requirements.sh
166-
- name: Run Redis Service Container
167-
timeout-minutes: 2
168-
run: |
169-
docker run --rm --detach -p 127.0.0.1:6379:6379/tcp --name redis redis:latest
170-
until [ "$(docker inspect -f {{.State.Running}} redis)" == "true" ]; do sleep 0.1; done
171178
- name: Setup Tests
172179
run: |
173180
# prep a ci-specific dev conf file that uses runner instead of stanley
@@ -234,9 +241,6 @@ jobs:
234241
name: logs-py${{ matrix.python-version }}
235242
path: logs.tar.gz
236243
retention-days: 7
237-
- name: Stop Redis Service Container
238-
if: "${{ always() }}"
239-
run: docker rm --force redis || true
240244

241245
unit-tests:
242246
needs: pre_job
@@ -287,6 +291,19 @@ jobs:
287291
image: mongo:4.4
288292
ports:
289293
- 27017:27017
294+
redis:
295+
# Docker Hub image
296+
image: redis
297+
# Set health checks to wait until redis has started
298+
options: >-
299+
--name "redis"
300+
--health-cmd "redis-cli ping"
301+
--health-interval 10s
302+
--health-timeout 5s
303+
--health-retries 5
304+
ports:
305+
- 6379:6379/tcp
306+
290307

291308
rabbitmq:
292309
image: rabbitmq:3.8-management
@@ -471,21 +488,18 @@ jobs:
471488
#- 4369:4369/tcp # epmd
472489
#
473490

474-
# Used for the coordination backend for integration tests
475-
# NOTE: To speed things up, we only start redis for integration tests
476-
# where it's needed
477-
# redis:
478-
# # Docker Hub image
479-
# image: redis
480-
# # Set health checks to wait until redis has started
481-
# options: >-
482-
# --name "redis"
483-
# --health-cmd "redis-cli ping"
484-
# --health-interval 10s
485-
# --health-timeout 5s
486-
# --health-retries 5
487-
# ports:
488-
# - 6379:6379/tcp
491+
redis:
492+
# Docker Hub image
493+
image: redis
494+
# Set health checks to wait until redis has started
495+
options: >-
496+
--name "redis"
497+
--health-cmd "redis-cli ping"
498+
--health-interval 10s
499+
--health-timeout 5s
500+
--health-retries 5
501+
ports:
502+
- 6379:6379/tcp
489503

490504
env:
491505
TASK: '${{ matrix.task }}'
@@ -538,11 +552,6 @@ jobs:
538552
cp conf/st2.dev.conf "${ST2_CONF}" ; sed -i -e "s/stanley/${ST2_CI_USER}/" "${ST2_CONF}"
539553
540554
sudo -E ./scripts/ci/add-itest-user-key.sh
541-
- name: Run Redis Service Container
542-
timeout-minutes: 2
543-
run: |
544-
docker run --rm --detach -p 127.0.0.1:6379:6379/tcp --name redis redis:latest
545-
until [ "$(docker inspect -f {{.State.Running}} redis)" == "true" ]; do sleep 0.1; done
546555
- name: Permissions Workaround
547556
run: |
548557
echo "$ST2_CI_REPO_PATH"
@@ -585,9 +594,6 @@ jobs:
585594
name: logs-py${{ matrix.python-version }}-nose-${{ matrix.nosetests_node_index }}
586595
path: logs.tar.gz
587596
retention-days: 7
588-
- name: Stop Redis Service Container
589-
if: "${{ always() }}"
590-
run: docker rm --force redis || true
591597

592598
slack-notification:
593599
name: Slack notification for failed master builds

CHANGELOG.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ Changed
2828
* Update st2client deps: editor and prompt-toolkit. #6189 (by @nzlosh)
2929
* Updated dependency oslo.config to prepare for python 3.10 support. #6193 (by @nzlosh)
3030

31+
* Updated unit tests to use redis for coordination instead of the NoOp driver. This will hopefully make CI more stable. #6245
32+
Contributed by @FileMagic, @guzzijones, and @cognifloyd
33+
3134
Added
3235
~~~~~
3336
* Continue introducing `pants <https://www.pantsbuild.org/docs>`_ to improve DX (Developer Experience)

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ COVERAGE_GLOBS_QUOTED := $(foreach glob,$(COVERAGE_GLOBS),'$(glob)')
5353

5454
REQUIREMENTS := test-requirements.txt requirements.txt
5555

56+
# Redis config for testing
57+
ST2TESTS_REDIS_HOST := 127.0.0.1
58+
ST2TESTS_REDIS_PORT := 6379
59+
5660
# Pin common pip version here across all the targets
5761
# Note! Periodic maintenance pip upgrades are required to be up-to-date with the latest pip security fixes and updates
5862
PIP_VERSION ?= 24.2
@@ -824,6 +828,8 @@ unit-tests: requirements .unit-tests
824828
echo "Running tests in" $$component; \
825829
echo "-----------------------------------------------------------"; \
826830
. $(VIRTUALENV_DIR)/bin/activate; \
831+
ST2TESTS_REDIS_HOST=$(ST2TESTS_REDIS_HOST) \
832+
ST2TESTS_REDIS_PORT=$(ST2TESTS_REDIS_PORT) \
827833
nosetests $(NOSE_OPTS) -s -v \
828834
$$component/tests/unit || ((failed+=1)); \
829835
echo "-----------------------------------------------------------"; \
@@ -848,6 +854,8 @@ endif
848854
echo "Running tests in" $$component; \
849855
echo "-----------------------------------------------------------"; \
850856
. $(VIRTUALENV_DIR)/bin/activate; \
857+
ST2TESTS_REDIS_HOST=$(ST2TESTS_REDIS_HOST) \
858+
ST2TESTS_REDIS_PORT=$(ST2TESTS_REDIS_PORT) \
851859
COVERAGE_FILE=.coverage.unit.$$(echo $$component | tr '/' '.') \
852860
nosetests $(NOSE_OPTS) -s -v $(NOSE_COVERAGE_FLAGS) \
853861
$(NOSE_COVERAGE_PACKAGES) \

st2actions/tests/unit/test_policies.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from st2common.transport.publishers import CUDPublisher
2929
from st2common.bootstrap import runnersregistrar as runners_registrar
3030
from st2tests import ExecutionDbTestCase
31+
from st2tests import config as tests_config
3132
from st2tests.fixtures.generic.fixture import PACK_NAME as PACK
3233
from st2tests.fixturesloader import FixturesLoader
3334
from st2tests.mocks.runners import runner
@@ -36,6 +37,8 @@
3637
from st2tests.policies.concurrency import FakeConcurrencyApplicator
3738
from st2tests.policies.mock_exception import RaiseExceptionApplicator
3839

40+
# This needs to run before creating FakeConcurrencyApplicator below.
41+
tests_config.parse_args()
3942

4043
TEST_FIXTURES = {
4144
"actions": ["action1.yaml"],

st2actions/tests/unit/test_worker.py

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
import mock
2121
import os
2222
from oslo_config import cfg
23+
from tooz.drivers.redis import RedisDriver
2324
import tempfile
2425

2526
# This import must be early for import-time side-effects.
2627
from st2tests.base import DbTestCase
2728

2829
import st2actions.worker as actions_worker
30+
import st2tests.config as tests_config
2931
from st2common.constants import action as action_constants
3032
from st2common.models.db.liveaction import LiveActionDB
3133
from st2common.models.system.common import ResourceReference
@@ -66,6 +68,35 @@ def setUpClass(cls):
6668
)
6769
WorkerTestCase.local_action_db = models["actions"]["local.yaml"]
6870

71+
@staticmethod
72+
def reset_config(
73+
graceful_shutdown=True, # default is True (st2common.config)
74+
exit_still_active_check=None, # default is 300 (st2common.config)
75+
still_active_check_interval=None, # default is 2 (st2common.config)
76+
service_registry=None, # default is False (st2common.config)
77+
):
78+
tests_config.reset()
79+
tests_config.parse_args()
80+
cfg.CONF.set_override(
81+
name="graceful_shutdown", override=graceful_shutdown, group="actionrunner"
82+
)
83+
if exit_still_active_check is not None:
84+
cfg.CONF.set_override(
85+
name="exit_still_active_check",
86+
override=exit_still_active_check,
87+
group="actionrunner",
88+
)
89+
if still_active_check_interval is not None:
90+
cfg.CONF.set_override(
91+
name="still_active_check_interval",
92+
override=still_active_check_interval,
93+
group="actionrunner",
94+
)
95+
if service_registry is not None:
96+
cfg.CONF.set_override(
97+
name="service_registry", override=service_registry, group="coordination"
98+
)
99+
69100
def _get_liveaction_model(self, action_db, params):
70101
status = action_constants.LIVEACTION_STATUS_REQUESTED
71102
start_timestamp = date_utils.get_datetime_utc_now()
@@ -116,9 +147,8 @@ def test_non_utf8_action_result_string(self):
116147
)
117148

118149
def test_worker_shutdown(self):
119-
cfg.CONF.set_override(
120-
name="graceful_shutdown", override=False, group="actionrunner"
121-
)
150+
self.reset_config(graceful_shutdown=False)
151+
122152
action_worker = actions_worker.get_worker()
123153
temp_file = None
124154

@@ -169,14 +199,19 @@ def test_worker_shutdown(self):
169199
runner_thread.wait()
170200

171201
@mock.patch.object(
172-
coordination.NoOpDriver,
202+
RedisDriver,
173203
"get_members",
174-
mock.MagicMock(return_value=coordination.NoOpAsyncResult("member-1")),
204+
mock.MagicMock(
205+
return_value=coordination.NoOpAsyncResult(("member-1", "member-2"))
206+
),
175207
)
176208
def test_worker_graceful_shutdown_with_multiple_runners(self):
177-
cfg.CONF.set_override(
178-
name="graceful_shutdown", override=True, group="actionrunner"
209+
self.reset_config(
210+
exit_still_active_check=10,
211+
still_active_check_interval=1,
212+
service_registry=True,
179213
)
214+
180215
action_worker = actions_worker.get_worker()
181216
temp_file = None
182217

@@ -234,9 +269,12 @@ def test_worker_graceful_shutdown_with_multiple_runners(self):
234269
shutdown_thread.kill()
235270

236271
def test_worker_graceful_shutdown_with_single_runner(self):
237-
cfg.CONF.set_override(
238-
name="graceful_shutdown", override=True, group="actionrunner"
272+
self.reset_config(
273+
exit_still_active_check=10,
274+
still_active_check_interval=1,
275+
service_registry=True,
239276
)
277+
240278
action_worker = actions_worker.get_worker()
241279
temp_file = None
242280

@@ -296,17 +334,13 @@ def test_worker_graceful_shutdown_with_single_runner(self):
296334
shutdown_thread.kill()
297335

298336
@mock.patch.object(
299-
coordination.NoOpDriver,
337+
RedisDriver,
300338
"get_members",
301-
mock.MagicMock(return_value=coordination.NoOpAsyncResult("member-1")),
339+
mock.MagicMock(return_value=coordination.NoOpAsyncResult(("member-1",))),
302340
)
303341
def test_worker_graceful_shutdown_exit_timeout(self):
304-
cfg.CONF.set_override(
305-
name="graceful_shutdown", override=True, group="actionrunner"
306-
)
307-
cfg.CONF.set_override(
308-
name="exit_still_active_check", override=5, group="actionrunner"
309-
)
342+
self.reset_config(exit_still_active_check=5)
343+
310344
action_worker = actions_worker.get_worker()
311345
temp_file = None
312346

0 commit comments

Comments
 (0)