|
12 | 12 | # implementation. |
13 | 13 | # pylint: disable=protected-access |
14 | 14 |
|
| 15 | +import types |
| 16 | + |
15 | 17 | import kernelci.config |
16 | 18 | import kernelci.runtime |
17 | 19 |
|
18 | 20 |
|
| 21 | +class _FakeResponse: |
| 22 | + def __init__(self, payload, status_code=200, text=""): |
| 23 | + """Initialize a fake response with payload and status.""" |
| 24 | + self._payload = payload |
| 25 | + self.status_code = status_code |
| 26 | + self.text = text |
| 27 | + |
| 28 | + def raise_for_status(self): |
| 29 | + """Raise an error when the response indicates failure.""" |
| 30 | + if self.status_code >= 400: |
| 31 | + raise RuntimeError(f"HTTP {self.status_code}") |
| 32 | + |
| 33 | + def json(self): |
| 34 | + """Return the preloaded JSON payload.""" |
| 35 | + return self._payload |
| 36 | + |
| 37 | + |
| 38 | +class _FakeSession: |
| 39 | + def __init__(self, get_handler=None, post_handler=None): |
| 40 | + """Initialize a fake session with optional handlers.""" |
| 41 | + self._get_handler = get_handler |
| 42 | + self._post_handler = post_handler |
| 43 | + self.calls = [] |
| 44 | + |
| 45 | + def get(self, url, params=None, timeout=30): # pylint: disable=unused-argument |
| 46 | + """Invoke the GET handler and return a fake response.""" |
| 47 | + if not self._get_handler: |
| 48 | + raise AssertionError("GET handler not set") |
| 49 | + self.calls.append((url, params)) |
| 50 | + return _FakeResponse(self._get_handler(url, params)) |
| 51 | + |
| 52 | + def post( # pylint: disable=unused-argument |
| 53 | + self, url, json=None, allow_redirects=False, timeout=30 |
| 54 | + ): |
| 55 | + """Invoke the POST handler and return its response.""" |
| 56 | + if not self._post_handler: |
| 57 | + raise AssertionError("POST handler not set") |
| 58 | + self.calls.append((url, json)) |
| 59 | + return self._post_handler(url, json) |
| 60 | + |
| 61 | + |
19 | 62 | def test_runtimes_init(): |
20 | 63 | """Test that all the runtimes can be initialised (offline)""" |
21 | 64 | config = kernelci.config.load('tests/configs/runtimes.yaml') |
@@ -66,3 +109,94 @@ def test_lava_priority_scale(): |
66 | 109 | spec_priority = int(priority) |
67 | 110 | print(f"* {plan_name:12s} {lab_priority:3d} {spec_priority:3d}") |
68 | 111 | assert lab_priority == spec_priority |
| 112 | + |
| 113 | + |
| 114 | +def test_lava_get_devicetype_job_count(): |
| 115 | + """Test queued job count aggregation via scheduler.jobs.queue.""" |
| 116 | + config = kernelci.config.load('tests/configs/lava-runtimes.yaml') |
| 117 | + runtime_config = config['runtimes']['lab-min-12-max-40-new-runtime'] |
| 118 | + lab = kernelci.runtime.get_runtime(runtime_config) |
| 119 | + |
| 120 | + def handler(url, params): |
| 121 | + assert url.endswith('scheduler/jobs/queue/') |
| 122 | + assert params.get('device_types') == ['type-a', 'type-b'] |
| 123 | + assert params.get('limit') == 100 |
| 124 | + if params.get('start') == 0: |
| 125 | + jobs = ( |
| 126 | + [{'requested_device_type': 'type-a'} for _ in range(60)] + |
| 127 | + [{'requested_device_type': 'type-b'} for _ in range(40)] |
| 128 | + ) |
| 129 | + return jobs |
| 130 | + if params.get('start') == 100: |
| 131 | + return [{'requested_device_type': 'type-a'}] |
| 132 | + raise AssertionError(f"Unexpected request: {url} {params}") |
| 133 | + |
| 134 | + lab._server = types.SimpleNamespace( |
| 135 | + url='http://lava/api/v0.2/', |
| 136 | + session=_FakeSession(get_handler=handler), |
| 137 | + ) |
| 138 | + |
| 139 | + counts = lab.get_devicetype_job_count(['type-a', 'type-b']) |
| 140 | + assert counts == {'type-a': 61, 'type-b': 40} |
| 141 | + |
| 142 | + |
| 143 | +def test_lava_get_device_names_by_type(): |
| 144 | + """Test device name lookups with type filtering and health checks.""" |
| 145 | + config = kernelci.config.load('tests/configs/lava-runtimes.yaml') |
| 146 | + runtime_config = config['runtimes']['lab-min-12-max-40-new-runtime'] |
| 147 | + lab = kernelci.runtime.get_runtime(runtime_config) |
| 148 | + |
| 149 | + def handler(url, params): |
| 150 | + if url.endswith('devices/'): |
| 151 | + dev_type = params.get('device_type') |
| 152 | + if dev_type == 'type-a': |
| 153 | + return { |
| 154 | + 'results': [ |
| 155 | + {'device_type': 'type-a', 'hostname': 'dev-1', 'health': 'Good'}, |
| 156 | + {'device_type': 'type-a', 'hostname': 'dev-2', 'health': 'Bad'}, |
| 157 | + {'device_type': 'type-b', 'hostname': 'dev-x', 'health': 'Good'}, |
| 158 | + ], |
| 159 | + 'next': None, |
| 160 | + } |
| 161 | + if dev_type == 'type-b': |
| 162 | + return { |
| 163 | + 'results': [ |
| 164 | + {'device_type': 'type-b', 'hostname': 'dev-3', 'health': 'Good'}, |
| 165 | + ], |
| 166 | + 'next': None, |
| 167 | + } |
| 168 | + raise AssertionError(f"Unexpected request: {url} {params}") |
| 169 | + |
| 170 | + lab._server = types.SimpleNamespace( |
| 171 | + url='http://lava/api/v0.2/', |
| 172 | + session=_FakeSession(get_handler=handler), |
| 173 | + ) |
| 174 | + |
| 175 | + names = lab.get_device_names_by_type('type-a', online_only=True) |
| 176 | + assert names == ['dev-1'] |
| 177 | + |
| 178 | + names_by_type = lab.get_device_names_by_type(['type-a', 'type-b']) |
| 179 | + assert names_by_type == {'type-a': ['dev-1', 'dev-2'], 'type-b': ['dev-3']} |
| 180 | + |
| 181 | + |
| 182 | +def test_lava_submit_rest(): |
| 183 | + """Test LAVA REST submission builds a job with expected payload.""" |
| 184 | + config = kernelci.config.load('tests/configs/lava-runtimes.yaml') |
| 185 | + runtime_config = config['runtimes']['lab-min-12-max-40-new-runtime'] |
| 186 | + lab = kernelci.runtime.get_runtime(runtime_config) |
| 187 | + |
| 188 | + captured = {} |
| 189 | + |
| 190 | + def post_handler(url, payload): |
| 191 | + assert url.endswith('jobs/') |
| 192 | + captured['json'] = payload |
| 193 | + return _FakeResponse({'job_ids': [123]}) |
| 194 | + |
| 195 | + lab._server = types.SimpleNamespace( |
| 196 | + url='http://lava/api/v0.2/', |
| 197 | + session=_FakeSession(post_handler=post_handler), |
| 198 | + ) |
| 199 | + |
| 200 | + job_id = lab._submit("jobdef") |
| 201 | + assert job_id == 123 |
| 202 | + assert captured['json']['definition'] == "jobdef" |
0 commit comments