This is an automated email from the ASF dual-hosted git repository.
HackedRico pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/caldera.git
The following commit(s) were added to refs/heads/master by this push:
new b8d029d9 Test operations started by the scheduler and on resume (#3438)
b8d029d9 is described below
commit b8d029d9b01e9c14c01f6e1c3557ae81945a0294
Author: deacon-mp <[email protected]>
AuthorDate: Tue Dec 1 13:27:49 2026 -0500
Test operations started by the scheduler and on resume (#3438)
* test: cover operations started by the scheduler and on resume
Build AppService before the event loop that runs it, as server.py does,
and check that resumed and scheduled operations start on the running
loop and that a scheduled operation uses the stored adversary, planner
and source.
* server: correct the comment on the run_tasks() event loop
The comment said services do not cache the event loop at construction
time and resolve it lazily. Services are built in __main__ before
run_tasks() creates the loop that runs the server, so a loop captured
in __init__ is a different loop that never runs. Say so, and point to
asyncio.get_running_loop() for scheduling background work.
---------
Co-authored-by: Mark Perry <[email protected]>
Co-authored-by: Ricky Chen <[email protected]>
---
server.py | 13 ++++---
tests/services/test_app_svc.py | 88 ++++++++++++++++++++++++++++++++++++++++++
2 files changed, 95 insertions(+), 6 deletions(-)
diff --git a/server.py b/server.py
index 1097cdc2..fa97c20b 100644
--- a/server.py
+++ b/server.py
@@ -88,12 +88,13 @@ def run_tasks(services, run_vue_server=False):
signal.signal(signal.SIGTERM, _handle_sigterm)
loop = asyncio.new_event_loop()
- # The event loop is set here, before any async work begins. Services
- # (AppService, DataService, etc.) are instantiated in __main__ prior to
- # this call but they do not cache the loop at construction time — they
- # resolve it lazily via asyncio.get_event_loop(). Setting it explicitly
- # here ensures all subsequent loop.create_task() /
loop.run_until_complete()
- # calls operate on the same, controlled loop instance.
+ # This is the loop that runs the server; it is set as the current loop
+ # here, before any async work begins. Services (AppService, DataService,
+ # etc.) are instantiated in __main__ before this call, so a loop that a
+ # service captures at construction time (asyncio.get_event_loop() in
+ # __init__) is a different loop that never runs, and tasks created on it
+ # never start. Schedule background work on the running loop instead,
+ # e.g. asyncio.get_running_loop().create_task() inside a coroutine.
asyncio.set_event_loop(loop)
try:
loop.create_task(app_svc.validate_requirements())
diff --git a/tests/services/test_app_svc.py b/tests/services/test_app_svc.py
index 5cf2af06..6cbf21b7 100644
--- a/tests/services/test_app_svc.py
+++ b/tests/services/test_app_svc.py
@@ -1,5 +1,14 @@
+import asyncio
+from unittest import mock
+
import pytest
+from app.objects.c_adversary import Adversary
+from app.objects.c_operation import Operation
+from app.objects.c_planner import Planner
+from app.objects.c_schedule import Schedule
+from app.objects.c_source import Source
+from app.service.app_svc import AppService
from app.utility.base_service import BaseService
@@ -129,3 +138,82 @@ class TestAppService:
assert operation.state == operation.States.CLEANUP.value
assert test_agent in operation.agents
assert not operation.untrusted_agents
+
+
+class _StopScheduler(Exception):
+ pass
+
+
+def _build_app_svc_before_loop(running_loop):
+ """Construct AppService the way server.py does: before run_tasks() creates
the loop that runs it."""
+ construction_loop = asyncio.new_event_loop()
+ asyncio.set_event_loop(construction_loop)
+ try:
+ svc = AppService(None)
+ finally:
+ asyncio.set_event_loop(running_loop)
+ return svc, construction_loop
+
+
+class TestOperationStartup:
+ def test_resume_operations_starts_on_running_loop(self, event_loop,
data_svc):
+ svc, construction_loop = _build_app_svc_before_loop(event_loop)
+ operation = Operation(name='resume check', id='resume-check-op')
+ event_loop.run_until_complete(data_svc.store(operation))
+ started = []
+
+ async def fake_run(op, services):
+ started.append(op.id)
+
+ try:
+ with mock.patch.object(Operation, 'run', fake_run):
+ with mock.patch('app.service.app_svc.asyncio.sleep',
new=mock.AsyncMock()):
+ event_loop.run_until_complete(svc.resume_operations())
+ event_loop.run_until_complete(asyncio.sleep(0))
+ finally:
+ construction_loop.close()
+ event_loop.run_until_complete(data_svc.remove('operations',
dict(id=operation.id)))
+ assert operation.id in started
+
+ def test_scheduled_operation_uses_stored_objects_and_running_loop(self,
event_loop, data_svc):
+ svc, construction_loop = _build_app_svc_before_loop(event_loop)
+ adversary = Adversary(adversary_id='sched-adv', name='scheduled
adversary', atomic_ordering=['sched-ability'])
+ planner = Planner(planner_id='sched-planner', name='scheduled
planner', module='app.planners.atomic')
+ source = Source(id='sched-source', name='scheduled source', facts=[])
+ for obj in (adversary, planner, source):
+ event_loop.run_until_complete(data_svc.store(obj))
+ # Stand-ins that carry only the identifiers, so the assertions below
show that
+ # run_scheduler() replaced them with the objects stored in data_svc.
+ task = Operation(name='scheduled op',
adversary=Adversary(adversary_id='sched-adv', atomic_ordering=[]),
+ planner=Planner(planner_id='sched-planner'),
source=Source(id='sched-source'))
+ schedule = Schedule(schedule='* * * * *', task=task, id='sched-check')
+ event_loop.run_until_complete(data_svc.store(schedule))
+ started = []
+
+ async def fake_run(op, services):
+ started.append(op)
+
+ async def no_agents(op, services):
+ op.agents = []
+
+ try:
+ with mock.patch.object(Operation, 'run', fake_run), \
+ mock.patch.object(Operation, 'update_operation_agents',
no_agents):
+ with mock.patch('app.service.app_svc.asyncio.sleep',
new=mock.AsyncMock(side_effect=_StopScheduler)):
+ with pytest.raises(_StopScheduler):
+ event_loop.run_until_complete(svc.run_scheduler())
+ event_loop.run_until_complete(asyncio.sleep(0))
+ finally:
+ construction_loop.close()
+ event_loop.run_until_complete(data_svc.remove('schedules',
dict(id=schedule.id)))
+ for op in started:
+ event_loop.run_until_complete(data_svc.remove('operations',
dict(id=op.id)))
+ event_loop.run_until_complete(data_svc.remove('adversaries',
dict(adversary_id=adversary.adversary_id)))
+ event_loop.run_until_complete(data_svc.remove('planners',
dict(planner_id=planner.planner_id)))
+ event_loop.run_until_complete(data_svc.remove('sources',
dict(id=source.id)))
+ assert len(started) == 1
+ scheduled = started[0]
+ assert scheduled.name.startswith('scheduled op (')
+ assert scheduled.adversary.atomic_ordering == ['sched-ability']
+ assert scheduled.planner.module == 'app.planners.atomic'
+ assert scheduled.source.name == 'scheduled source'