204 lines
4.9 KiB
Python
204 lines
4.9 KiB
Python
"""Test for the cli componenets"""
|
|
# pylint: disable=redefined-outer-name, no-name-in-module, unused-import
|
|
|
|
import json
|
|
from unittest import mock
|
|
|
|
import requests
|
|
|
|
from redfish_cli.cli import parse_args
|
|
from redfish_cli.cli.jobs import jobs_list, job_details, watch_job
|
|
|
|
from ..utils import get_test_content, MockResponse, MockWatchedJobResponse
|
|
|
|
|
|
def test_list_jobs(capsys, monkeypatch):
|
|
"""Test the list jobs command"""
|
|
# pylint: disable=fixme, unused-argument, unused-variable
|
|
|
|
args = parse_args(
|
|
[
|
|
"-s",
|
|
"172.1.0.1",
|
|
"-f",
|
|
"json",
|
|
"jobs",
|
|
"-u",
|
|
"root",
|
|
"-p",
|
|
"password123",
|
|
"list",
|
|
"--all",
|
|
]
|
|
)
|
|
monkeypatch.setattr(
|
|
requests,
|
|
"get",
|
|
lambda args, auth=None, verify=None, data=None, headers=None, timeout=None: MockResponse(
|
|
content=get_test_content("jobs_list.json")
|
|
),
|
|
)
|
|
|
|
jobs_list(args=args)
|
|
captured = capsys.readouterr()
|
|
result = captured.out
|
|
assert 5 == json.loads(result)["Members@odata.count"]
|
|
|
|
args = parse_args(
|
|
[
|
|
"-s",
|
|
"172.1.0.1",
|
|
"-f",
|
|
"json",
|
|
"jobs",
|
|
"-u",
|
|
"root",
|
|
"-p",
|
|
"password123",
|
|
"list",
|
|
]
|
|
)
|
|
jobs_list(args=args)
|
|
captured = capsys.readouterr()
|
|
result = captured.out
|
|
assert 0 == json.loads(result)["Members@odata.count"]
|
|
|
|
args = parse_args(
|
|
[
|
|
"-s",
|
|
"172.1.0.1",
|
|
"-f",
|
|
"text",
|
|
"jobs",
|
|
"-u",
|
|
"root",
|
|
"-p",
|
|
"password123",
|
|
"list",
|
|
"--all",
|
|
]
|
|
)
|
|
jobs_list(args=args)
|
|
captured = capsys.readouterr()
|
|
result = captured.out
|
|
assert (
|
|
"JID_878659984891 RAIDConfiguration 100 Config:RAID:RAID.Slot.6-1 Completed\n"
|
|
"JID_878682850779 RAIDConfiguration 100 Config:RAID:RAID.Slot.6-1 Completed\n"
|
|
"JID_920326518992 RAIDConfiguration 100 Config:RAID:RAID.Slot.6-1 Completed\n"
|
|
"JID_923469653542 RAIDConfiguration 100 Config:RAID:RAID.Slot.6-1 Completed\n"
|
|
"JID_924369311959 RAIDConfiguration 100 Config:RAID:RAID.Slot.6-1 Completed\n"
|
|
) == result
|
|
|
|
args = parse_args(
|
|
[
|
|
"-s",
|
|
"172.1.0.1",
|
|
"-f",
|
|
"table",
|
|
"jobs",
|
|
"-u",
|
|
"root",
|
|
"-p",
|
|
"password123",
|
|
"list",
|
|
"--all",
|
|
]
|
|
)
|
|
jobs_list(args=args)
|
|
captured = capsys.readouterr()
|
|
result = captured.out
|
|
expected = get_test_content("job_list_table.txt")
|
|
assert expected == result
|
|
|
|
|
|
def test_job_details(capsys, monkeypatch):
|
|
"""Test the list jobs command"""
|
|
# pylint: disable=fixme, unused-argument, unused-variable
|
|
|
|
args = parse_args(
|
|
[
|
|
"-s",
|
|
"172.1.0.1",
|
|
"-f",
|
|
"json",
|
|
"jobs",
|
|
"-u",
|
|
"root",
|
|
"-p",
|
|
"password123",
|
|
"show",
|
|
"-j",
|
|
"JID_924369311959",
|
|
]
|
|
)
|
|
monkeypatch.setattr(
|
|
requests,
|
|
"get",
|
|
lambda args, auth=None, verify=None, data=None, headers=None, timeout=None: MockResponse(
|
|
content=get_test_content("api_job_details.json")
|
|
),
|
|
)
|
|
|
|
job_details(args=args)
|
|
captured = capsys.readouterr()
|
|
result = captured.out
|
|
assert "2023-08-20T05:59:24" == json.loads(result)["CompletionTime"]
|
|
|
|
|
|
def test_watch_jobs(capsys, monkeypatch):
|
|
"""Test the list jobs command"""
|
|
# pylint: disable=fixme, unused-argument, unused-variable
|
|
|
|
response = MockWatchedJobResponse()
|
|
args = parse_args(
|
|
[
|
|
"-s",
|
|
"172.1.0.1",
|
|
"-f",
|
|
"json",
|
|
"jobs",
|
|
"-u",
|
|
"root",
|
|
"-p",
|
|
"password123",
|
|
"show",
|
|
"-j",
|
|
"JID_924369311959",
|
|
]
|
|
)
|
|
monkeypatch.setattr(
|
|
requests,
|
|
"get",
|
|
lambda args, auth=None, verify=None, data=None, headers=None, timeout=None: response,
|
|
)
|
|
|
|
watch_job(args=args)
|
|
captured = capsys.readouterr()
|
|
result = captured.out
|
|
assert '"PercentComplete": 100,' in result
|
|
|
|
response.accessed = 0
|
|
args = parse_args(
|
|
[
|
|
"-s",
|
|
"172.1.0.1",
|
|
"-f",
|
|
"text",
|
|
"jobs",
|
|
"-u",
|
|
"root",
|
|
"-p",
|
|
"password123",
|
|
"show",
|
|
"-j",
|
|
"JID_924369311959",
|
|
]
|
|
)
|
|
watch_job(args=args)
|
|
captured = capsys.readouterr()
|
|
result = captured.out
|
|
assert "Completed 100\n" in result
|
|
|
|
# Test that watch jobs catches a KeyboardInterrup and exits cleanly
|
|
with mock.patch("requests.get", side_effect=KeyboardInterrupt):
|
|
watch_job(args=args)
|