Initial commit
This commit is contained in:
commit
1b51716d1b
72 changed files with 8204 additions and 0 deletions
255
tests/cli/test_power.py
Normal file
255
tests/cli/test_power.py
Normal file
|
|
@ -0,0 +1,255 @@
|
|||
"""Test for the cli componenets"""
|
||||
# pylint: disable=redefined-outer-name, no-name-in-module, unused-import
|
||||
|
||||
import json
|
||||
|
||||
import pytest
|
||||
import requests
|
||||
|
||||
import redfish_cli.cli
|
||||
import redfish_cli.cli.power
|
||||
from redfish_cli import api
|
||||
|
||||
from ..utils import get_test_json, MockResponse, MockRedfishResponse
|
||||
|
||||
|
||||
def test_set_power_state(capsys, monkeypatch):
|
||||
"""Test the set-power-state cli command"""
|
||||
|
||||
monkeypatch.setattr(
|
||||
requests,
|
||||
"post",
|
||||
lambda *args, **kwargs: MockResponse(content="{}", status_code=200),
|
||||
)
|
||||
|
||||
args = redfish_cli.cli.parse_args(
|
||||
[
|
||||
"-s",
|
||||
"172.1.0.1",
|
||||
"-f",
|
||||
"json",
|
||||
"power",
|
||||
"-u",
|
||||
"root",
|
||||
"-p",
|
||||
"password123",
|
||||
"set",
|
||||
"On",
|
||||
]
|
||||
)
|
||||
redfish_cli.cli.power.set_power_state(args=args)
|
||||
out = capsys.readouterr().out
|
||||
result = out
|
||||
assert result == '"Powerstate set to On"\n'
|
||||
|
||||
monkeypatch.setattr(
|
||||
requests, "post", lambda *args, **kwargs: MockResponse(ok=False)
|
||||
)
|
||||
try:
|
||||
redfish_cli.cli.power.set_power_state(args)
|
||||
except SystemExit:
|
||||
result = capsys.readouterr().err
|
||||
assert result == "Failed to set powerstate to On\n"
|
||||
|
||||
|
||||
def test_power_state(capsys, monkeypatch):
|
||||
"""Test the get power state cli command"""
|
||||
_json = {"PowerState": "Off"}
|
||||
|
||||
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",
|
||||
"power",
|
||||
"-u",
|
||||
"root",
|
||||
"-p",
|
||||
"password123",
|
||||
"get",
|
||||
]
|
||||
)
|
||||
redfish_cli.cli.power.power_state(args=args)
|
||||
out = capsys.readouterr().out
|
||||
result = json.loads(out)
|
||||
assert result == "Off"
|
||||
|
||||
|
||||
def test_list_power_states(capsys, monkeypatch):
|
||||
"""Test the product cli command"""
|
||||
# pylint: disable=duplicate-code
|
||||
|
||||
# This code is similar to the direct api test. Disable the pylint warning.
|
||||
|
||||
_json = get_test_json("power_states_allowed.json")
|
||||
|
||||
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",
|
||||
"power",
|
||||
"-u",
|
||||
"root",
|
||||
"-p",
|
||||
"password123",
|
||||
"list",
|
||||
]
|
||||
)
|
||||
redfish_cli.cli.power.power_states(args)
|
||||
result = json.loads(capsys.readouterr().out)
|
||||
assert result == [
|
||||
"On",
|
||||
"ForceOff",
|
||||
"ForceRestart",
|
||||
"GracefulShutdown",
|
||||
"PushPowerButton",
|
||||
"Nmi",
|
||||
]
|
||||
|
||||
|
||||
def test_system_reset(capsys, monkeypatch):
|
||||
"""Test resetting a system"""
|
||||
monkeypatch.setattr(
|
||||
requests,
|
||||
"post",
|
||||
lambda args, auth=None, verify=None, data=None, headers=None, timeout=None: MockResponse(
|
||||
content="{}"
|
||||
),
|
||||
)
|
||||
|
||||
args = redfish_cli.cli.parse_args(
|
||||
[
|
||||
"-s",
|
||||
"172.1.0.1",
|
||||
"-f",
|
||||
"json",
|
||||
"power",
|
||||
"-u",
|
||||
"root",
|
||||
"-p",
|
||||
"password123",
|
||||
"reset",
|
||||
]
|
||||
)
|
||||
redfish_cli.cli.power.reset_system(args=args)
|
||||
out = capsys.readouterr().out
|
||||
result = json.loads(out)
|
||||
assert result == "System reset"
|
||||
|
||||
monkeypatch.setattr(
|
||||
requests,
|
||||
"post",
|
||||
lambda *args, **kwargs: MockResponse(
|
||||
ok=False, content="Failed to set power state"
|
||||
),
|
||||
)
|
||||
try:
|
||||
redfish_cli.cli.power.reset_system(args)
|
||||
except SystemExit:
|
||||
result = capsys.readouterr().err
|
||||
assert result == "Failed to set power state\n"
|
||||
|
||||
|
||||
def test_power_on(capsys, monkeypatch):
|
||||
"""Test resetting a system"""
|
||||
monkeypatch.setattr(
|
||||
requests,
|
||||
"post",
|
||||
lambda args, auth=None, verify=None, data=None, headers=None, timeout=None: MockResponse(
|
||||
content="{}"
|
||||
),
|
||||
)
|
||||
|
||||
args = redfish_cli.cli.parse_args(
|
||||
[
|
||||
"-s",
|
||||
"172.1.0.1",
|
||||
"-f",
|
||||
"json",
|
||||
"power",
|
||||
"-u",
|
||||
"root",
|
||||
"-p",
|
||||
"password123",
|
||||
"on",
|
||||
]
|
||||
)
|
||||
redfish_cli.cli.power.power_on(args=args)
|
||||
out = capsys.readouterr().out
|
||||
result = json.loads(out)
|
||||
assert result == "System.Embedded.1 powered on"
|
||||
|
||||
monkeypatch.setattr(
|
||||
requests,
|
||||
"post",
|
||||
lambda *args, **kwargs: MockResponse(
|
||||
ok=False, content="Failed to power on"
|
||||
),
|
||||
)
|
||||
try:
|
||||
redfish_cli.cli.power.power_on(args)
|
||||
except SystemExit:
|
||||
result = capsys.readouterr().err
|
||||
assert result == "Failed to power on\n"
|
||||
|
||||
|
||||
def test_power_off(capsys, monkeypatch):
|
||||
"""Test resetting a system"""
|
||||
monkeypatch.setattr(
|
||||
requests,
|
||||
"post",
|
||||
lambda args, auth=None, verify=None, data=None, headers=None, timeout=None: MockResponse(
|
||||
content="{}"
|
||||
),
|
||||
)
|
||||
|
||||
args = redfish_cli.cli.parse_args(
|
||||
[
|
||||
"-s",
|
||||
"172.1.0.1",
|
||||
"-f",
|
||||
"json",
|
||||
"power",
|
||||
"-u",
|
||||
"root",
|
||||
"-p",
|
||||
"password123",
|
||||
"off",
|
||||
]
|
||||
)
|
||||
redfish_cli.cli.power.power_off(args=args)
|
||||
out = capsys.readouterr().out
|
||||
result = json.loads(out)
|
||||
assert result == "System.Embedded.1 powered off"
|
||||
|
||||
monkeypatch.setattr(
|
||||
requests,
|
||||
"post",
|
||||
lambda *args, **kwargs: MockResponse(
|
||||
ok=False, content="Failed to power off"
|
||||
),
|
||||
)
|
||||
try:
|
||||
redfish_cli.cli.power.power_off(args)
|
||||
except SystemExit:
|
||||
result = capsys.readouterr().err
|
||||
assert result == "Failed to power off\n"
|
||||
Loading…
Add table
Add a link
Reference in a new issue