Skip to content

Commit

Permalink
Merge pull request #336 from rackerlabs/PUC-539
Browse files Browse the repository at this point in the history
chore: create additional test case for testing workflows
  • Loading branch information
cardoe authored Sep 24, 2024
2 parents a9c0ce8 + d239cd6 commit 898fdba
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 4 deletions.
10 changes: 10 additions & 0 deletions python/understack-workflows/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions python/understack-workflows/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from understack_workflows.models import NIC
from understack_workflows.models import Systeminfo


def test_nic():
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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")
57 changes: 57 additions & 0 deletions python/understack-workflows/tests/test_sync_bmc_creds.py
Original file line number Diff line number Diff line change
@@ -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"]
Original file line number Diff line number Diff line change
@@ -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



Original file line number Diff line number Diff line change
Expand Up @@ -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.")

Expand Down

0 comments on commit 898fdba

Please sign in to comment.