diff --git a/python/understack-workflows/tests/conftest.py b/python/understack-workflows/tests/conftest.py index 0dd9d3913..4907c656f 100644 --- a/python/understack-workflows/tests/conftest.py +++ b/python/understack-workflows/tests/conftest.py @@ -23,6 +23,16 @@ def project_id() -> uuid.UUID: return uuid.uuid4() +@pytest.fixture +def bmc_username() -> str: + return 'root' + + +@pytest.fixture +def bmc_password() -> str: + return 'password' + + @pytest.fixture def project_data(domain_id: uuid.UUID, project_id: uuid.UUID): return { diff --git a/python/understack-workflows/tests/test_models.py b/python/understack-workflows/tests/test_models.py index 102d2eb2f..423de2355 100644 --- a/python/understack-workflows/tests/test_models.py +++ b/python/understack-workflows/tests/test_models.py @@ -1,4 +1,5 @@ from understack_workflows.models import NIC +from understack_workflows.models import Systeminfo def test_nic(): @@ -8,3 +9,12 @@ def test_nic(): assert a.name == value assert a.location == value assert a.model == value + + +def test_system_info(): + value = "test" + sys_info = Systeminfo(asset_tag=value, serial_number=value, platform=value) + + assert sys_info.asset_tag == value + assert sys_info.serial_number == value + assert sys_info.platform == value diff --git a/python/understack-workflows/tests/test_provision_state_mapper.py b/python/understack-workflows/tests/test_provision_state_mapper.py index 277fd7095..b51b9f6cc 100644 --- a/python/understack-workflows/tests/test_provision_state_mapper.py +++ b/python/understack-workflows/tests/test_provision_state_mapper.py @@ -15,6 +15,7 @@ def test_translate(ironic_state, nautobot_state): result = ProvisionStateMapper.translate_to_nautobot(ironic_state) assert result == nautobot_state + def test_raises_on_unknown(): with pytest.raises(ValueError): ProvisionStateMapper.translate_to_nautobot("blahblah") diff --git a/python/understack-workflows/tests/test_sync_bmc_creds.py b/python/understack-workflows/tests/test_sync_bmc_creds.py new file mode 100644 index 000000000..49753457f --- /dev/null +++ b/python/understack-workflows/tests/test_sync_bmc_creds.py @@ -0,0 +1,57 @@ +import sys +import pytest +import pathlib +import json + +from understack_workflows.main.sync_bmc_creds import get_args +from understack_workflows.node_configuration import IronicNodeConfiguration + + +def read_json_samples(file_path): + here = pathlib.Path(__file__).parent + ref = here.joinpath(file_path) + with ref.open("r") as f: + return f.read() + + +@pytest.fixture(autouse=True) +def mock_args(monkeypatch): + monkeypatch.setattr(sys, "argv", ["pytest", + read_json_samples("json_samples/event-interface-update.json")]) + + +@pytest.fixture +def fake_ironic_client(mocker): + return mocker.patch("understack_workflows.ironic.client.IronicClient") + + +def get_ironic_node_state(fake_ironic_client, node_data): + node = IronicNodeConfiguration.from_event(json.loads(read_json_samples("json_samples/event-interface-update.json"))) + + ironic_node = fake_ironic_client.get_node(node.uuid) + ironic_node.return_value = node_data + + return ironic_node.return_value['provision_state'] + + +def test_args(): + var = get_args() + assert var['data']['ip_addresses'][0]['host'] == "10.46.96.156" + + +def test_ironic_non_allowing_states(fake_ironic_client): + ironic_node_state = get_ironic_node_state(fake_ironic_client, + json.loads(read_json_samples( + "json_samples/ironic-active-node-data.json"))) + with pytest.raises(SystemExit) as sys_exit: + if ironic_node_state not in ["enroll", "manageable"]: + print('checking') + sys.exit(0) + assert sys_exit.value.code == 0 + + +def test_ironic_node_allowing_states(fake_ironic_client): + ironic_node_state = get_ironic_node_state(fake_ironic_client, + json.loads(read_json_samples( + "json_samples/ironic-enroll-node-data.json"))) + assert ironic_node_state in ["enroll", "manageable"] diff --git a/python/understack-workflows/tests/test_sync_nautobot_system_info.py b/python/understack-workflows/tests/test_sync_nautobot_system_info.py new file mode 100644 index 000000000..47ba46ff0 --- /dev/null +++ b/python/understack-workflows/tests/test_sync_nautobot_system_info.py @@ -0,0 +1,28 @@ +import pytest + +from understack_workflows.main.sync_nautobot_system_info import argument_parser, do_sync +from understack_workflows.models import Systeminfo + + +@pytest.fixture +def fakebot(mocker): + return mocker.patch("understack_workflows.nautobot.Nautobot", autospec=True) + + +def test_parse_device_name(): + parser = argument_parser(__name__) + with pytest.raises(SystemExit): + parser.parse_args(["--device-id", "FOO", "--bmc_username", "root", "--bmc_password", "password"]) + + +def test_parse_device_id(device_id, bmc_username, bmc_password): + parser = argument_parser(__name__) + args = parser.parse_args(["--device-id", str(device_id), "--bmc_username", bmc_username, + "--bmc_password", bmc_password]) + + assert args.device_id == device_id + assert args.bmc_username == bmc_username + assert args.bmc_password == bmc_password + + + diff --git a/python/understack-workflows/understack_workflows/main/sync_bmc_creds.py b/python/understack-workflows/understack_workflows/main/sync_bmc_creds.py index 1592ba235..2659fd40b 100644 --- a/python/understack-workflows/understack_workflows/main/sync_bmc_creds.py +++ b/python/understack-workflows/understack_workflows/main/sync_bmc_creds.py @@ -12,18 +12,22 @@ logger = setup_logger(__name__) -def main(): +def get_args(): if len(sys.argv) < 1: raise ValueError( "Please provide node configuration in JSON format as first argument." ) + return json.loads(sys.argv[1]) + + +def main(): + interface_update_event = get_args() + logger.debug(f"Received: {json.dumps(interface_update_event, indent=2)}") + logger.info("Pushing device new node to Ironic.") client = IronicClient() - interface_update_event = json.loads(sys.argv[1]) - logger.debug(f"Received: {interface_update_event}") - node = IronicNodeConfiguration.from_event(interface_update_event) logger.debug(f"Checking if node with UUID {node.uuid} exists in Ironic.")