redfish-cli/tests/cli/test_cli.py
2023-09-11 20:22:22 +10:00

307 lines
7.9 KiB
Python

"""Test for the cli componenets"""
import argparse
import json
import os.path
import pytest
import requests
import redfish_cli.cli
import redfish_cli.api
from redfish_cli.cli.parsers.utils import EnvDefault
from ..utils import get_test_json, MockResponse, MockRedfishResponse
# pylint: disable=redefined-outer-name
################################################################################
### Look at moving this into a shared file so we don't also define it in api
@pytest.fixture
def secrets():
"""
Secrets for use as a pytest fiixture. There aren't actually any secrets
here because everything is mocked now, but we still need some defaults when
testing the cli.
"""
with open(
os.path.join(os.path.dirname(__file__), "../test_secrets.json"),
encoding="utf-8",
) as secrets_file:
secrets = json.loads(" ".join(secrets_file.readlines()))
return secrets
################################################################################
def test_base_command(capsys, monkeypatch, secrets):
"""Test the base cli command"""
def mock_get(*args, **kwargs):
# pylint: disable=unused-argument
mock_redfish = MockRedfishResponse()
mock_redfish.json_dict = get_test_json("cli_base_command.json")
return MockResponse(status_code=200, content=mock_redfish.text)
monkeypatch.setattr(requests, "get", mock_get)
args = redfish_cli.cli.parse_args(
[
"-s",
secrets["server"],
"-f",
"json",
]
)
redfish_cli.cli.redfish(args=args)
captured = capsys.readouterr()
result = json.loads(captured.out)
assert result["Name"] == "Root Service"
args = redfish_cli.cli.parse_args(
["-s", secrets["server"], "-f", "json", "version"]
)
redfish_cli.cli.redfish(args=args)
captured = capsys.readouterr()
result = json.loads(captured.out)
assert result == "1.4.0"
def test_get(capsys, monkeypatch, secrets):
"""Test the cli get command"""
def mock_get(*args, **kwargs):
# pylint: disable=unused-argument
mock_redfish = MockRedfishResponse()
mock_redfish.json_dict = get_test_json("cli_get.json")
return MockResponse(status_code=200, content=mock_redfish.text)
monkeypatch.setattr(requests, "get", mock_get)
args = redfish_cli.cli.parse_args(
[
"-s",
secrets["server"],
"-f",
"json",
"--debug",
"get",
"-p",
"password",
"-u",
"username",
]
)
redfish_cli.cli.redfish(args=args)
captured = capsys.readouterr()
result = json.loads(captured.out)
assert result["Name"] == "Root Service"
monkeypatch.setattr(
requests, "get", lambda *args, **kwargs: MockResponse(status_code=401)
)
args = redfish_cli.cli.parse_args(
[
"-s",
secrets["server"],
"-f",
"json",
"get",
"-p",
"password",
"-u",
"username",
"--expand",
]
)
try:
redfish_cli.cli.redfish(args=args)
except SystemExit:
captured = capsys.readouterr()
result = captured.err
assert result == "This url requires authentication\n\n"
monkeypatch.setattr(
requests,
"get",
lambda *args, **kwargs: MockResponse(
ok=False, content='"Invalid url"'
),
)
try:
redfish_cli.cli.redfish(args=args)
except SystemExit:
result = capsys.readouterr().err
assert result == '"Invalid url"\n'
def test_error():
"""Test the cli error function"""
with pytest.raises(SystemExit):
redfish_cli.cli.utils.error("This is an error")
with pytest.raises(SystemExit):
redfish_cli.cli.utils.error("This is an error", output_format="json")
def test_parser():
"""Test the cli parse_args function"""
args = redfish_cli.cli.parse_args(["-s", "xxxx", "version"])
assert args.server == "xxxx"
assert args.format == "json"
assert args.func.__name__ == "version"
def test_add_subparser():
"""Test a subparser with no args"""
parser = argparse.ArgumentParser(description="Test parser")
subparsers = parser.add_subparsers()
redfish_cli.cli.parsers.utils.add_subparser(
subparsers, "Dummy", arguments=None
)
def test_product(capsys, monkeypatch):
"""Test the product cli command"""
_json = {"Product": "Integrated Dell Remote Access Controller"}
monkeypatch.setattr(
requests,
"get",
lambda *args, **kwargs: MockResponse(
content=json.dumps(_json), status_code=200
),
)
args = redfish_cli.cli.parse_args(
[
"-s",
"172.1.0.1",
"-f",
"json",
"product",
]
)
redfish_cli.cli.redfish(args=args)
result = json.loads(capsys.readouterr().out)
assert result == "Integrated Dell Remote Access Controller"
def test_redfish():
"""Test the base redfish command"""
with pytest.raises(SystemExit):
redfish_cli.cli.redfish()
def test_service_tag(capsys, monkeypatch, secrets):
"""Test the service-tag cli command"""
_json = get_test_json("cli_service_tag.json")
def mock_get(*args, **kwargs):
# pylint: disable=unused-argument
mock_redfish = MockRedfishResponse()
mock_redfish.json_dict = _json
return MockResponse(status_code=200, content=mock_redfish.text)
monkeypatch.setattr(requests, "get", mock_get)
args = redfish_cli.cli.parse_args(
[
"-s",
secrets["server"],
"service-tag",
]
)
redfish_cli.cli.service_tag(args)
captured = capsys.readouterr()
result = captured.out
assert result == '"JYYZY42"\n'
_json["Oem"]["NotDell"] = _json["Oem"]["Dell"]
del _json["Oem"]["Dell"]
monkeypatch.setattr(
requests,
"get",
lambda *args, **kwargs: MockResponse(
content=json.dumps(_json), status_code=200
),
)
args = redfish_cli.cli.parse_args(
[
"-s",
secrets["server"],
"service-tag",
]
)
with pytest.raises(SystemExit):
redfish_cli.cli.service_tag(args)
captured = capsys.readouterr()
result = captured.err
assert result == "Service tags are only available for Dell iDRACs\n"
def test_version(capsys, monkeypatch, secrets):
"""Test the version command"""
def mock_get(*args, **kwargs):
# pylint: disable=unused-argument
mock_redfish = MockRedfishResponse()
mock_redfish.json_dict = get_test_json("cli_version.json")
return MockResponse(status_code=200, content=mock_redfish.text)
monkeypatch.setattr(requests, "get", mock_get)
args = redfish_cli.cli.parse_args(
[
"-s",
secrets["server"],
"version",
]
)
redfish_cli.cli.version(args)
captured = capsys.readouterr()
result = captured.out
assert result == '"1.4.0"\n'
def test_write():
"""Test the cli write function"""
redfish_cli.cli.utils.write_output("This is not an error")
redfish_cli.cli.utils.write_output(
"This is not an error", output_format="json"
)
def test_env_var():
"""Test the envvar action parser action"""
parser = argparse.ArgumentParser(
description="Interact with servers via RedFish. Only Dells supported at the moment"
)
parser.add_argument(
"-s",
"--server",
required=True,
dest="server",
action=EnvDefault,
default=os.environ.get("RF_USERNAME"),
envvar="USER",
)
parser.add_argument(
"-u",
"--user",
required=True,
dest="server",
help="Help",
action=EnvDefault,
envvar="USER",
)