Skip to content

Commit 898fdba

Browse files
authored
Merge pull request #336 from rackerlabs/PUC-539
chore: create additional test case for testing workflows
2 parents a9c0ce8 + d239cd6 commit 898fdba

File tree

6 files changed

+114
-4
lines changed

6 files changed

+114
-4
lines changed

python/understack-workflows/tests/conftest.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ def project_id() -> uuid.UUID:
2323
return uuid.uuid4()
2424

2525

26+
@pytest.fixture
27+
def bmc_username() -> str:
28+
return 'root'
29+
30+
31+
@pytest.fixture
32+
def bmc_password() -> str:
33+
return 'password'
34+
35+
2636
@pytest.fixture
2737
def project_data(domain_id: uuid.UUID, project_id: uuid.UUID):
2838
return {

python/understack-workflows/tests/test_models.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from understack_workflows.models import NIC
2+
from understack_workflows.models import Systeminfo
23

34

45
def test_nic():
@@ -8,3 +9,12 @@ def test_nic():
89
assert a.name == value
910
assert a.location == value
1011
assert a.model == value
12+
13+
14+
def test_system_info():
15+
value = "test"
16+
sys_info = Systeminfo(asset_tag=value, serial_number=value, platform=value)
17+
18+
assert sys_info.asset_tag == value
19+
assert sys_info.serial_number == value
20+
assert sys_info.platform == value

python/understack-workflows/tests/test_provision_state_mapper.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def test_translate(ironic_state, nautobot_state):
1515
result = ProvisionStateMapper.translate_to_nautobot(ironic_state)
1616
assert result == nautobot_state
1717

18+
1819
def test_raises_on_unknown():
1920
with pytest.raises(ValueError):
2021
ProvisionStateMapper.translate_to_nautobot("blahblah")
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import sys
2+
import pytest
3+
import pathlib
4+
import json
5+
6+
from understack_workflows.main.sync_bmc_creds import get_args
7+
from understack_workflows.node_configuration import IronicNodeConfiguration
8+
9+
10+
def read_json_samples(file_path):
11+
here = pathlib.Path(__file__).parent
12+
ref = here.joinpath(file_path)
13+
with ref.open("r") as f:
14+
return f.read()
15+
16+
17+
@pytest.fixture(autouse=True)
18+
def mock_args(monkeypatch):
19+
monkeypatch.setattr(sys, "argv", ["pytest",
20+
read_json_samples("json_samples/event-interface-update.json")])
21+
22+
23+
@pytest.fixture
24+
def fake_ironic_client(mocker):
25+
return mocker.patch("understack_workflows.ironic.client.IronicClient")
26+
27+
28+
def get_ironic_node_state(fake_ironic_client, node_data):
29+
node = IronicNodeConfiguration.from_event(json.loads(read_json_samples("json_samples/event-interface-update.json")))
30+
31+
ironic_node = fake_ironic_client.get_node(node.uuid)
32+
ironic_node.return_value = node_data
33+
34+
return ironic_node.return_value['provision_state']
35+
36+
37+
def test_args():
38+
var = get_args()
39+
assert var['data']['ip_addresses'][0]['host'] == "10.46.96.156"
40+
41+
42+
def test_ironic_non_allowing_states(fake_ironic_client):
43+
ironic_node_state = get_ironic_node_state(fake_ironic_client,
44+
json.loads(read_json_samples(
45+
"json_samples/ironic-active-node-data.json")))
46+
with pytest.raises(SystemExit) as sys_exit:
47+
if ironic_node_state not in ["enroll", "manageable"]:
48+
print('checking')
49+
sys.exit(0)
50+
assert sys_exit.value.code == 0
51+
52+
53+
def test_ironic_node_allowing_states(fake_ironic_client):
54+
ironic_node_state = get_ironic_node_state(fake_ironic_client,
55+
json.loads(read_json_samples(
56+
"json_samples/ironic-enroll-node-data.json")))
57+
assert ironic_node_state in ["enroll", "manageable"]
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import pytest
2+
3+
from understack_workflows.main.sync_nautobot_system_info import argument_parser, do_sync
4+
from understack_workflows.models import Systeminfo
5+
6+
7+
@pytest.fixture
8+
def fakebot(mocker):
9+
return mocker.patch("understack_workflows.nautobot.Nautobot", autospec=True)
10+
11+
12+
def test_parse_device_name():
13+
parser = argument_parser(__name__)
14+
with pytest.raises(SystemExit):
15+
parser.parse_args(["--device-id", "FOO", "--bmc_username", "root", "--bmc_password", "password"])
16+
17+
18+
def test_parse_device_id(device_id, bmc_username, bmc_password):
19+
parser = argument_parser(__name__)
20+
args = parser.parse_args(["--device-id", str(device_id), "--bmc_username", bmc_username,
21+
"--bmc_password", bmc_password])
22+
23+
assert args.device_id == device_id
24+
assert args.bmc_username == bmc_username
25+
assert args.bmc_password == bmc_password
26+
27+
28+

python/understack-workflows/understack_workflows/main/sync_bmc_creds.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,22 @@
1212
logger = setup_logger(__name__)
1313

1414

15-
def main():
15+
def get_args():
1616
if len(sys.argv) < 1:
1717
raise ValueError(
1818
"Please provide node configuration in JSON format as first argument."
1919
)
2020

21+
return json.loads(sys.argv[1])
22+
23+
24+
def main():
25+
interface_update_event = get_args()
26+
logger.debug(f"Received: {json.dumps(interface_update_event, indent=2)}")
27+
2128
logger.info("Pushing device new node to Ironic.")
2229
client = IronicClient()
2330

24-
interface_update_event = json.loads(sys.argv[1])
25-
logger.debug(f"Received: {interface_update_event}")
26-
2731
node = IronicNodeConfiguration.from_event(interface_update_event)
2832
logger.debug(f"Checking if node with UUID {node.uuid} exists in Ironic.")
2933

0 commit comments

Comments
 (0)