Skip to content

Commit

Permalink
Speed up functional tests with ops_test.fast_forward() (#65)
Browse files Browse the repository at this point in the history
ops_test.fast_forward() temporarily speeds up the update-status firing
rate. This is effectively equal to setting and resetting the
"update-status-hook-interval" model config before and after the relevant
code.

Previously, we would have had to spend 5m waiting for the unit
status change to be detected after any state change to the
hardware-exporter systemd service.

Refer: https://github.com/charmed-kubernetes/pytest-operator/blob/main/docs/reference.md#async-def-fast_forwardself-fast_interval-str--10s-slow_interval-optionalstr--none
  • Loading branch information
dashmage authored Sep 13, 2023
1 parent 58746fa commit 53ccbd9
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions tests/functional/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,34 +162,38 @@ async def test_10_start_and_stop_exporter(self, app, unit, sync_helper, ops_test
"""Test starting and stopping the exporter results in correct charm status."""
# Stop the exporter
stop_cmd = "systemctl stop hardware-exporter"
await asyncio.gather(
unit.run(stop_cmd),
ops_test.model.wait_for_idle(apps=[APP_NAME], status="blocked", timeout=TIMEOUT),
)
assert unit.workload_status_message == AppStatus.NOT_RUNNING
async with ops_test.fast_forward():
await asyncio.gather(
unit.run(stop_cmd),
ops_test.model.wait_for_idle(apps=[APP_NAME], status="blocked", timeout=TIMEOUT),
)
assert unit.workload_status_message == AppStatus.NOT_RUNNING

# Start the exporter
start_cmd = "systemctl start hardware-exporter"
await asyncio.gather(
unit.run(start_cmd),
ops_test.model.wait_for_idle(apps=[APP_NAME], status="active", timeout=TIMEOUT),
)
assert unit.workload_status_message == AppStatus.READY
async with ops_test.fast_forward():
await asyncio.gather(
unit.run(start_cmd),
ops_test.model.wait_for_idle(apps=[APP_NAME], status="active", timeout=TIMEOUT),
)
assert unit.workload_status_message == AppStatus.READY

async def test_11_exporter_failed(self, app, unit, sync_helper, ops_test):
"""Test failure in the exporter results in correct charm status."""
# Setting incorrect log level will crash the exporter
await asyncio.gather(
app.set_config({"exporter-log-level": "RANDOM_LEVEL"}),
ops_test.model.wait_for_idle(apps=[APP_NAME], status="blocked", timeout=TIMEOUT),
)
assert unit.workload_status_message == AppStatus.UNHEALTHY
async with ops_test.fast_forward():
await asyncio.gather(
app.set_config({"exporter-log-level": "RANDOM_LEVEL"}),
ops_test.model.wait_for_idle(apps=[APP_NAME], status="blocked", timeout=TIMEOUT),
)
assert unit.workload_status_message == AppStatus.UNHEALTHY

await asyncio.gather(
app.reset_config(["exporter-log-level"]),
ops_test.model.wait_for_idle(apps=[APP_NAME], status="active", timeout=TIMEOUT),
)
assert unit.workload_status_message == AppStatus.READY
async with ops_test.fast_forward():
await asyncio.gather(
app.reset_config(["exporter-log-level"]),
ops_test.model.wait_for_idle(apps=[APP_NAME], status="active", timeout=TIMEOUT),
)
assert unit.workload_status_message == AppStatus.READY

async def test_20_on_remove_event(self, app, sync_helper, ops_test):
"""Test _on_remove event cleans up the service on the host machine."""
Expand Down

0 comments on commit 53ccbd9

Please sign in to comment.