Skip to content

Add unit tests for osism/utils task output and helpers#2298

Merged
berendt merged 2 commits into
mainfrom
gh2229
Jun 1, 2026
Merged

Add unit tests for osism/utils task output and helpers#2298
berendt merged 2 commits into
mainfrom
gh2229

Conversation

@berendt
Copy link
Copy Markdown
Member

@berendt berendt commented May 20, 2026

Covers fetch_task_output / push_task_output / finish_task_output, revoke_task, get_ansible_vault_password, check_ansible_facts and the first iterator helper from osism/utils/init.py. Companion to the connection-init suite added in

Closes #2229.

AI-assisted: Claude Code

Covers fetch_task_output / push_task_output / finish_task_output, revoke_task,
get_ansible_vault_password, check_ansible_facts and the first iterator helper
from osism/utils/__init__.py. Companion to the connection-init suite added in

Closes #2229.

AI-assisted: Claude Code
Signed-off-by: Christian Berendt <berendt@osism.tech>
Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@berendt berendt requested a review from ideaship May 26, 2026 14:01
mocker.patch("osism.utils.time.time", side_effect=lambda: next(time_values))

with pytest.raises(TimeoutError):
utils_pkg.fetch_task_output("task-1", timeout=10)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Design question: the normal exit path calls r.close() before returning; this path raises TimeoutError without closing r. Is that intentional? If closing on timeout is correct, this test should add mock_r.close.assert_called_once() to guard against it being dropped.

mock_r.xadd.assert_called_once_with("task-1", {"type": "action", "content": "quit"})


def test_finish_task_output_rc_zero_publishes_only_quit(mocker):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

`if rc:` silently drops `rc=0`, making a successful subprocess exit indistinguishable from "no rc message received" — `fetch_task_output` defaults `rc = 0` and returns it either way. The pre-refactor code (commit `7257aa4`) published the rc message unconditionally; the `if rc:` guard was introduced in that refactor without explanation, suggesting an oversight rather than intent. Consider `if rc is not None:`.

The docstring here ("intentionally truthy") appears to be a post-hoc interpretation of the existing code rather than documented design intent, and would block a correct fix.

Copy link
Copy Markdown
Contributor

@ideaship ideaship left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug in production code not covered by new tests — osism/utils/__init__.py:372

timeout=os.environ.get("OSISM_TASK_TIMEOUT", 300) is evaluated at import time. When OSISM_TASK_TIMEOUT is set in the environment the default becomes a str, and time.time() + timeout crashes with TypeError. The call site in wait.py passes no explicit timeout, so this is reachable in production.

Fix: timeout=int(os.environ.get("OSISM_TASK_TIMEOUT", 300)).

Note: all new tests pass an explicit timeout= kwarg, which is why this went undetected. No test exercises the default-parameter path.

…ask output

- fetch_task_output: cast OSISM_TASK_TIMEOUT to int; the env-var read at
  import time produced a str, crashing time.time() + timeout
- fetch_task_output: close redis before raising TimeoutError, matching the
  quit path
- finish_task_output: use "if rc is not None" so rc=0 publishes an rc
  message, restoring pre-refactor behavior (commit 7257aa4)
- tests: cover the env-var default path, assert close() on timeout, and
  flip the rc=0 expectation

AI-assisted: Claude Code

Signed-off-by: Christian Berendt <berendt@osism.tech>
@berendt berendt requested a review from ideaship May 27, 2026 21:03
@berendt berendt merged commit 58ac59e into main Jun 1, 2026
3 checks passed
@berendt berendt deleted the gh2229 branch June 1, 2026 08:14
@github-project-automation github-project-automation Bot moved this from Ready to Done in Human Board Jun 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Unit tests for osism/utils/__init__.py — task output, revoke, ansible helpers

3 participants