Repository navigation
feat: add wait support for job start #304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| kind: added | ||
| body: Add --wait (-w), --timeout, and --polling_interval to job start to wait for completion like job run. | ||
| time: 2026-10-26T19:11:06.155500618Z | ||
| custom: | ||
| Author: tarostok_microsoft | ||
| AuthorLink: https://github.com/tarostok |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,201 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|
|
||
| """Unit tests for job start waiting and job run compatibility.""" | ||
|
|
||
| import inspect | ||
| import json | ||
| from argparse import Namespace | ||
| from unittest.mock import Mock | ||
|
|
||
| import pytest | ||
|
|
||
| from fabric_cli.commands.jobs import fab_jobs | ||
| from fabric_cli.core import fab_constant | ||
| from fabric_cli.core.fab_exceptions import FabricCLIError | ||
| from fabric_cli.core.fab_parser_setup import CustomArgumentParser | ||
| from fabric_cli.core.hiearchy.fab_item import Item | ||
| from fabric_cli.parsers import fab_jobs_parser | ||
|
|
||
|
|
||
| def _parse_job_args(command: str, flags: list[str]) -> Namespace: | ||
| parser = CustomArgumentParser() | ||
| fab_jobs_parser.register_parser(parser.add_subparsers(dest="command")) | ||
| args = parser.parse_args(["job", command, "ws.Workspace/nb.Notebook", *flags]) | ||
| args.item = "nb.Notebook" | ||
| args.output_format = "json" | ||
| args.command_path = f"job {command}" | ||
| return args | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def job_mocks(monkeypatch: pytest.MonkeyPatch) -> Namespace: | ||
| """Isolate context resolution, HTTP requests, output, and polling delays.""" | ||
| item = Mock(spec=Item) | ||
| monkeypatch.setattr( | ||
| fab_jobs.handle_context, "get_command_context", Mock(return_value=item) | ||
| ) | ||
| monkeypatch.setattr(fab_jobs.utils_job, "add_item_props_to_args", Mock()) | ||
| monkeypatch.setattr(fab_jobs.utils_job, "build_config_from_args", Mock()) | ||
|
|
||
| response = Mock(status_code=202, headers={}) | ||
| start = Mock(return_value=(response, "test-job-id")) | ||
| poll = Mock( | ||
| side_effect=[ | ||
| Mock(status_code=200, headers={}, text=json.dumps({"status": status})) | ||
| for status in ("InProgress", "Completed") | ||
| ] | ||
| ) | ||
| sleep = Mock() | ||
| output = Mock() | ||
| monkeypatch.setattr(fab_jobs.jobs_run.jobs_api, "run_on_demand_item_job", start) | ||
| monkeypatch.setattr(fab_jobs.utils_job.jobs_api, "get_item_job_instance", poll) | ||
| monkeypatch.setattr(fab_jobs.utils_job.time, "sleep", sleep) | ||
| monkeypatch.setattr(fab_jobs.fab_ui, "print_grey", Mock()) | ||
| monkeypatch.setattr(fab_jobs.fab_ui, "print_progress", Mock()) | ||
| monkeypatch.setattr(fab_jobs.fab_ui, "print_output_format", output) | ||
| monkeypatch.setattr(fab_jobs.fab_ui, "print_entries_unix_style", Mock()) | ||
| return Namespace(start=start, poll=poll, sleep=sleep, output=output) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("command", "flags", "wait"), | ||
| [ | ||
| ("start", [], False), | ||
| ("start", ["--wait"], True), | ||
| ("start", ["-w"], True), | ||
| ("run", [], True), | ||
| ], | ||
| ) | ||
| def test_job_wait_behavior( | ||
| command: str, flags: list[str], wait: bool, job_mocks: Namespace | ||
| ) -> None: | ||
| args = _parse_job_args(command, flags) | ||
|
|
||
| inspect.unwrap(fab_jobs.run_command)(args) | ||
|
|
||
| assert args.wait is wait | ||
| job_mocks.start.assert_called_once_with(args, None) | ||
| assert job_mocks.poll.call_count == (2 if wait else 0) | ||
| assert job_mocks.sleep.call_count == (2 if wait else 0) | ||
| job_mocks.output.assert_called_once() | ||
| assert job_mocks.output.call_args.kwargs["data"] == {"id": "test-job-id"} | ||
| assert job_mocks.output.call_args.kwargs["message"] == ( | ||
| f"Job instance 'test-job-id' {'completed' if wait else 'created'}" | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("command", ["start", "run"]) | ||
| def test_job_wait_reports_failure(command: str, job_mocks: Namespace) -> None: | ||
| args = _parse_job_args(command, ["--wait"] if command == "start" else []) | ||
| job_mocks.poll.side_effect = None | ||
| job_mocks.poll.return_value = Mock( | ||
| status_code=200, headers={}, text=json.dumps({"status": "Failed"}) | ||
| ) | ||
|
|
||
| with pytest.raises(FabricCLIError) as exc_info: | ||
| inspect.unwrap(fab_jobs.run_command)(args) | ||
|
|
||
| assert exc_info.value.status_code == fab_constant.ERROR_JOB_FAILED | ||
| job_mocks.output.assert_not_called() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("flags", "expected"), | ||
| [ | ||
| (["--timeout", "60"], "--timeout can only be used with --wait"), | ||
| ( | ||
| ["--polling_interval", "10"], | ||
| "--polling_interval can only be used with --wait", | ||
| ), | ||
| ( | ||
| ["--timeout", "60", "--polling_interval", "10"], | ||
| "--timeout, --polling_interval can only be used with --wait", | ||
| ), | ||
| ], | ||
| ) | ||
| def test_job_start_wait_options_without_wait_failure( | ||
| flags: list[str], expected: str, job_mocks: Namespace | ||
| ) -> None: | ||
| args = _parse_job_args("start", flags) | ||
|
|
||
| with pytest.raises(FabricCLIError) as exc_info: | ||
| inspect.unwrap(fab_jobs.run_command)(args) | ||
|
|
||
| assert exc_info.value.status_code == fab_constant.ERROR_INVALID_INPUT | ||
| assert exc_info.value.message == expected | ||
| job_mocks.start.assert_not_called() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("command", ["start", "run"]) | ||
| def test_job_negative_timeout_failure(command: str, job_mocks: Namespace) -> None: | ||
| flags = ["--wait"] if command == "start" else [] | ||
| args = _parse_job_args(command, [*flags, "--timeout", "-1"]) | ||
|
|
||
| with pytest.raises(FabricCLIError) as exc_info: | ||
| inspect.unwrap(fab_jobs.run_command)(args) | ||
|
|
||
| assert exc_info.value.status_code == fab_constant.ERROR_INVALID_INPUT | ||
| job_mocks.start.assert_not_called() | ||
| job_mocks.poll.assert_not_called() | ||
| job_mocks.sleep.assert_not_called() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("command", ["start", "run"]) | ||
| def test_job_zero_timeout_success( | ||
| command: str, job_mocks: Namespace, monkeypatch: pytest.MonkeyPatch | ||
| ) -> None: | ||
| flags = ["--wait"] if command == "start" else [] | ||
| args = _parse_job_args(command, [*flags, "--timeout", "0"]) | ||
| monkeypatch.setattr( | ||
| fab_jobs.jobs_run.config, "get_config", Mock(return_value="false") | ||
| ) | ||
| warning = Mock() | ||
| monkeypatch.setattr(fab_jobs.fab_ui, "print_warning", warning) | ||
|
|
||
| inspect.unwrap(fab_jobs.run_command)(args) | ||
|
|
||
| job_mocks.start.assert_called_once_with(args, None) | ||
| job_mocks.sleep.assert_called_once_with(0) | ||
| job_mocks.poll.assert_not_called() | ||
| warning.assert_called_once_with( | ||
| "Job instance 'test-job-id' timed out after 0 seconds" | ||
| ) | ||
|
|
||
|
|
||
| def test_job_start_wait_timeout_success( | ||
| job_mocks: Namespace, monkeypatch: pytest.MonkeyPatch | ||
| ) -> None: | ||
| args = _parse_job_args( | ||
| "start", ["--wait", "--timeout", "60", "--polling_interval", "10"] | ||
| ) | ||
| wait = Mock() | ||
| monkeypatch.setattr(fab_jobs.jobs_run.utils_job, "wait_for_job_completion", wait) | ||
|
|
||
| inspect.unwrap(fab_jobs.run_command)(args) | ||
|
|
||
| wait.assert_called_once() | ||
| assert wait.call_args.kwargs["timeout"] == 60 | ||
| assert wait.call_args.kwargs["custom_polling_interval"] == 10 | ||
|
|
||
|
|
||
| def test_job_start_wait_timeout_cancels_job( | ||
| job_mocks: Namespace, | ||
| monkeypatch: pytest.MonkeyPatch, | ||
| mock_fab_set_state_config, | ||
| ) -> None: | ||
| args = _parse_job_args("start", ["--wait", "--timeout", "5"]) | ||
| mock_fab_set_state_config(fab_constant.FAB_JOB_CANCEL_ONTIMEOUT, "true") | ||
| monkeypatch.setattr( | ||
| fab_jobs.jobs_run.utils_job, | ||
| "wait_for_job_completion", | ||
| Mock(side_effect=TimeoutError("timed out")), | ||
| ) | ||
| monkeypatch.setattr(fab_jobs.fab_ui, "print_warning", Mock()) | ||
| cancel = Mock(return_value=Mock(status_code=202)) | ||
| monkeypatch.setattr(fab_jobs.jobs_run.jobs_api, "cancel_item_job_instance", cancel) | ||
|
|
||
| inspect.unwrap(fab_jobs.run_command)(args) | ||
|
|
||
| cancel.assert_called_once_with(args) | ||
| assert args.instance_id == "test-job-id" |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.