|
| 1 | +# Copyright (c) 2026 Agentspan |
| 2 | +# Licensed under the MIT License. See LICENSE file in the project root for details. |
| 3 | + |
| 4 | +"""Regeneration guards for the HAND-FIXes in scheduler_resource_api.py, plus the |
| 5 | +OrkesSchedulerClient contract they enable. |
| 6 | +
|
| 7 | +The scheduler API spec used for code generation is out of date; these tests pin the |
| 8 | +hand-applied fixes so a future regeneration that reverts them fails loudly: |
| 9 | +
|
| 10 | +- per-schedule pause/resume send PUT (OSS Conductor dialect; Orkes servers get a |
| 11 | + GET fallback on 405 — see the verb-split analysis in the SDK docs) |
| 12 | +- pause accepts an optional ``reason`` query param |
| 13 | +- ``get_schedule`` deserializes to ``WorkflowSchedule`` (not a raw camelCase dict) |
| 14 | +
|
| 15 | +No network calls. |
| 16 | +""" |
| 17 | + |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | +from unittest.mock import MagicMock |
| 21 | + |
| 22 | +import pytest |
| 23 | + |
| 24 | +from conductor.client.configuration.configuration import Configuration |
| 25 | +from conductor.client.http.api.scheduler_resource_api import SchedulerResourceApi |
| 26 | +from conductor.client.http.models.workflow_schedule import WorkflowSchedule |
| 27 | +from conductor.client.http.rest import ApiException |
| 28 | +from conductor.client.orkes.orkes_scheduler_client import OrkesSchedulerClient |
| 29 | + |
| 30 | + |
| 31 | +def _client_with_mocks() -> OrkesSchedulerClient: |
| 32 | + client = OrkesSchedulerClient(Configuration(server_api_url="http://localhost:8080/api")) |
| 33 | + client.schedulerResourceApi = MagicMock() |
| 34 | + client.api_client = MagicMock() |
| 35 | + client.api_client.select_header_accept.return_value = "application/json" |
| 36 | + return client |
| 37 | + |
| 38 | + |
| 39 | +# ── HAND-FIX guards on the generated resource API ─────────────────── |
| 40 | + |
| 41 | + |
| 42 | +class TestSchedulerResourceHandFixes: |
| 43 | + def setup_method(self): |
| 44 | + self.api_client = MagicMock() |
| 45 | + self.api = SchedulerResourceApi(api_client=self.api_client) |
| 46 | + |
| 47 | + def _call_args(self): |
| 48 | + assert self.api_client.call_api.call_count == 1 |
| 49 | + return self.api_client.call_api.call_args |
| 50 | + |
| 51 | + def test_pause_sends_put(self): |
| 52 | + self.api.pause_schedule("sched-1") |
| 53 | + args, kwargs = self._call_args() |
| 54 | + assert args[0] == "/scheduler/schedules/{name}/pause" |
| 55 | + assert args[1] == "PUT" |
| 56 | + assert args[3] == [] # no reason -> no query params |
| 57 | + |
| 58 | + def test_pause_forwards_reason_query_param(self): |
| 59 | + self.api.pause_schedule("sched-1", reason="maintenance window") |
| 60 | + args, _ = self._call_args() |
| 61 | + assert args[1] == "PUT" |
| 62 | + assert ("reason", "maintenance window") in args[3] |
| 63 | + |
| 64 | + def test_resume_sends_put(self): |
| 65 | + self.api.resume_schedule("sched-1") |
| 66 | + args, _ = self._call_args() |
| 67 | + assert args[0] == "/scheduler/schedules/{name}/resume" |
| 68 | + assert args[1] == "PUT" |
| 69 | + |
| 70 | + def test_get_schedule_deserializes_to_workflow_schedule(self): |
| 71 | + self.api.get_schedule("sched-1") |
| 72 | + args, kwargs = self._call_args() |
| 73 | + assert args[0] == "/scheduler/schedules/{name}" |
| 74 | + assert args[1] == "GET" |
| 75 | + assert kwargs["response_type"] == "WorkflowSchedule" |
| 76 | + |
| 77 | + |
| 78 | +# ── OrkesSchedulerClient contract over the fixed transport ───────── |
| 79 | + |
| 80 | + |
| 81 | +class TestOrkesSchedulerClientContract: |
| 82 | + def test_get_schedule_returns_model_when_name_present(self): |
| 83 | + client = _client_with_mocks() |
| 84 | + ws = WorkflowSchedule(name="sched-1", cron_expression="0 9 * * *") |
| 85 | + client.schedulerResourceApi.get_schedule.return_value = ws |
| 86 | + assert client.get_schedule("sched-1") is ws |
| 87 | + |
| 88 | + def test_get_schedule_normalizes_empty_model_to_none(self): |
| 89 | + client = _client_with_mocks() |
| 90 | + client.schedulerResourceApi.get_schedule.return_value = WorkflowSchedule() |
| 91 | + assert client.get_schedule("missing") is None |
| 92 | + |
| 93 | + def test_get_schedule_normalizes_falsy_to_none(self): |
| 94 | + client = _client_with_mocks() |
| 95 | + client.schedulerResourceApi.get_schedule.return_value = None |
| 96 | + assert client.get_schedule("missing") is None |
| 97 | + |
| 98 | + def test_pause_bare_call_omits_reason(self): |
| 99 | + client = _client_with_mocks() |
| 100 | + client.pause_schedule("sched-1") |
| 101 | + client.schedulerResourceApi.pause_schedule.assert_called_once_with("sched-1") |
| 102 | + |
| 103 | + def test_pause_forwards_reason(self): |
| 104 | + client = _client_with_mocks() |
| 105 | + client.pause_schedule("sched-1", reason="maintenance") |
| 106 | + client.schedulerResourceApi.pause_schedule.assert_called_once_with( |
| 107 | + "sched-1", reason="maintenance" |
| 108 | + ) |
| 109 | + |
| 110 | + |
| 111 | +# ── Verb fallback for Orkes servers (GET-only dialect) ───────────── |
| 112 | + |
| 113 | + |
| 114 | +class TestVerbFallback: |
| 115 | + def test_405_falls_back_to_get(self): |
| 116 | + client = _client_with_mocks() |
| 117 | + client.schedulerResourceApi.pause_schedule.side_effect = ApiException( |
| 118 | + status=405, reason="Method Not Allowed" |
| 119 | + ) |
| 120 | + client.pause_schedule("sched-1", reason="maintenance") |
| 121 | + args, _ = client.api_client.call_api.call_args |
| 122 | + assert args[0] == "/scheduler/schedules/{name}/pause" |
| 123 | + assert args[1] == "GET" |
| 124 | + assert args[2] == {"name": "sched-1"} |
| 125 | + assert ("reason", "maintenance") in args[3] |
| 126 | + |
| 127 | + def test_405_result_is_cached_for_subsequent_calls(self): |
| 128 | + client = _client_with_mocks() |
| 129 | + client.schedulerResourceApi.pause_schedule.side_effect = ApiException( |
| 130 | + status=405, reason="Method Not Allowed" |
| 131 | + ) |
| 132 | + client.pause_schedule("sched-1") |
| 133 | + client.pause_schedule("sched-2") |
| 134 | + client.resume_schedule("sched-1") |
| 135 | + # PUT attempted exactly once; everything after the 405 goes straight to GET. |
| 136 | + assert client.schedulerResourceApi.pause_schedule.call_count == 1 |
| 137 | + client.schedulerResourceApi.resume_schedule.assert_not_called() |
| 138 | + assert client.api_client.call_api.call_count == 3 |
| 139 | + |
| 140 | + def test_404_propagates_without_fallback(self): |
| 141 | + client = _client_with_mocks() |
| 142 | + client.schedulerResourceApi.pause_schedule.side_effect = ApiException( |
| 143 | + status=404, reason="Not Found" |
| 144 | + ) |
| 145 | + with pytest.raises(ApiException): |
| 146 | + client.pause_schedule("missing") |
| 147 | + client.api_client.call_api.assert_not_called() |
| 148 | + assert client._legacy_scheduler_verbs is False |
| 149 | + |
| 150 | + def test_resume_405_falls_back_to_get(self): |
| 151 | + client = _client_with_mocks() |
| 152 | + client.schedulerResourceApi.resume_schedule.side_effect = ApiException( |
| 153 | + status=405, reason="Method Not Allowed" |
| 154 | + ) |
| 155 | + client.resume_schedule("sched-1") |
| 156 | + args, _ = client.api_client.call_api.call_args |
| 157 | + assert args[0] == "/scheduler/schedules/{name}/resume" |
| 158 | + assert args[1] == "GET" |
| 159 | + assert args[3] == [] |
0 commit comments