Skip to content

Commit c4a717a

Browse files
authored
Merge pull request #195 from padovan/fix-hardware-list-api
Fix kci-dev results hardware list
2 parents 43bcbc4 + 94d7777 commit c4a717a

File tree

3 files changed

+96
-8
lines changed

3 files changed

+96
-8
lines changed

kcidev/libs/dashboard.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,8 @@ def dashboard_fetch_hardware_list(origin, use_json):
226226
last_week = now - timedelta(days=7)
227227
params = {
228228
"origin": origin,
229-
"endTimeStampInSeconds": int(now.timestamp()),
230-
"startTimestampInSeconds": int(last_week.timestamp()),
229+
"endTimestampInSeconds": str(int(now.timestamp())),
230+
"startTimestampInSeconds": str(int(last_week.timestamp())),
231231
}
232232
logging.info(f"Fetching hardware list for origin: {origin}")
233233
logging.debug(

kcidev/subcommands/results/parser.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -471,17 +471,19 @@ def cmd_hardware_list(data, use_json):
471471

472472
if use_json:
473473
hardware = [
474-
{"name": hardware["hardware_name"], "compatibles": hardware["platform"]}
474+
{
475+
"name": hardware.get("hardware") or hardware["platform"],
476+
"compatibles": hardware["platform"],
477+
}
475478
for hardware in data["hardware"]
476479
]
477-
kci_msg(hardware)
480+
kci_msg(json.dumps(hardware))
478481
else:
479482
for hardware in data["hardware"]:
480-
logging.debug(
481-
f"Hardware: {hardware['hardware_name']} - {hardware['platform']}"
482-
)
483+
hardware_name = hardware.get("hardware") or hardware["platform"]
484+
logging.debug(f"Hardware: {hardware_name} - {hardware['platform']}")
483485
kci_msg_nonl("- name: ")
484-
kci_msg_cyan(hardware["hardware_name"], nl=False)
486+
kci_msg_cyan(hardware_name, nl=False)
485487
kci_msg("")
486488

487489
kci_msg_nonl(" compatibles: ")

tests/test_kcidev.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,92 @@ def test_kcidev_results_summary_history_import():
210210
assert "/" in result # Should contain separators
211211

212212

213+
def test_kcidev_results_hardware_list():
214+
"""Test that hardware list command works and returns expected output format"""
215+
command = [
216+
"poetry",
217+
"run",
218+
"kci-dev",
219+
"results",
220+
"hardware",
221+
"list",
222+
"--origin",
223+
"maestro",
224+
]
225+
result = run(command, stdout=PIPE, stderr=PIPE, universal_newlines=True)
226+
227+
print("returncode: " + str(result.returncode))
228+
print("#### stdout ####")
229+
print(result.stdout)
230+
print("#### stderr ####")
231+
print(result.stderr)
232+
233+
# Test should succeed
234+
assert result.returncode == 0
235+
236+
# Should contain hardware entries with expected format
237+
output_lines = result.stdout.strip().split("\n")
238+
239+
# Should have at least some hardware entries
240+
assert len(output_lines) > 0
241+
242+
# Check format: lines should contain "- name:" and "compatibles:"
243+
name_lines = [line for line in output_lines if line.strip().startswith("- name:")]
244+
compatible_lines = [
245+
line for line in output_lines if line.strip().startswith("compatibles:")
246+
]
247+
248+
# Should have matching number of name and compatible lines
249+
assert len(name_lines) > 0
250+
assert len(compatible_lines) > 0
251+
assert len(name_lines) == len(compatible_lines)
252+
253+
254+
def test_kcidev_results_hardware_list_json():
255+
"""Test that hardware list command works with JSON output"""
256+
command = [
257+
"poetry",
258+
"run",
259+
"kci-dev",
260+
"results",
261+
"hardware",
262+
"list",
263+
"--origin",
264+
"maestro",
265+
"--json",
266+
]
267+
result = run(command, stdout=PIPE, stderr=PIPE, universal_newlines=True)
268+
269+
print("returncode: " + str(result.returncode))
270+
print("#### stdout ####")
271+
print(result.stdout[:500] + "..." if len(result.stdout) > 500 else result.stdout)
272+
print("#### stderr ####")
273+
print(result.stderr)
274+
275+
# Test should succeed
276+
assert result.returncode == 0
277+
278+
# Should be valid JSON
279+
import json
280+
281+
try:
282+
data = json.loads(result.stdout)
283+
assert isinstance(data, list)
284+
285+
# Should have at least some hardware entries
286+
assert len(data) > 0
287+
288+
# Each entry should have 'name' and 'compatibles' fields
289+
for entry in data[:5]: # Check first 5 entries
290+
assert "name" in entry
291+
assert "compatibles" in entry
292+
assert isinstance(entry["name"], str)
293+
assert isinstance(entry["compatibles"], str)
294+
295+
except json.JSONDecodeError:
296+
pytest.fail("Output is not valid JSON")
297+
298+
213299
def test_clean():
214300
# clean enviroment
215301
shutil.rmtree("my-new-repo/")

0 commit comments

Comments
 (0)