-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add neutron understack mechanism tests
- Loading branch information
Milan Fencik
committed
Nov 25, 2024
1 parent
60c927a
commit b08c7c1
Showing
2 changed files
with
122 additions
and
46 deletions.
There are no files selected for viewing
50 changes: 31 additions & 19 deletions
50
.../neutron-understack/neutron_understack/tests/fixtures/neutron_update_port_postcommit.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,47 @@ | ||
{ | ||
"admin_state_up": true, | ||
"allowed_address_pairs": [], | ||
"binding:host_id": "", | ||
"binding:profile": {}, | ||
"binding:vif_details": {}, | ||
"binding:vif_type": "unbound", | ||
"binding:vnic_type": "normal", | ||
"created_at": "2024-07-24T13:42:24Z", | ||
"binding:host_id": "4c2e4c6e-bdbe-4d93-857f-a6003884e8cc", | ||
"binding:profile": { | ||
"local_link_information": [ | ||
{ | ||
"port_id": "03921f8d-b4de-412e-a733-f8eade4c6268", | ||
"switch_id": "11:22:33:44:55:66" | ||
} | ||
] | ||
}, | ||
"binding:vif_details": { | ||
"bound_drivers": { | ||
"0": "understack" | ||
} | ||
}, | ||
"binding:vif_type": "other", | ||
"binding:vnic_type": "baremetal", | ||
"created_at": "2024-11-13T10:40:50Z", | ||
"description": "", | ||
"device_id": "41d18c6a-5548-4ee9-926f-4e3ebf43153f", | ||
"device_id": "e5e28c6a-abea-4974-9f25-748ee149ab38", | ||
"device_owner": "compute:nova", | ||
"extra_dhcp_opts": [], | ||
"fixed_ips": [ | ||
{ | ||
"ip_address": "192.168.17.17", | ||
"subnet_id": "7f2f436c-fd65-44b0-9265-6b642cecbec5" | ||
"ip_address": "172.16.0.197", | ||
"subnet_id": "304bd384-338a-4365-9394-0c356ec698ed" | ||
} | ||
], | ||
"id": "e5d5cd73-ca9a-4b74-9d52-43188d0cdcaa", | ||
"mac_address": "fa:16:3e:35:1c:3d", | ||
"id": "9238973b-57d7-43f3-b425-8114e225913c", | ||
"ip_allocation": "immediate", | ||
"mac_address": "d4:04:e6:4f:87:84", | ||
"name": "", | ||
"network_id": "c2702769-5592-4555-8ae6-e670db82c31e", | ||
"network_id": "0c9f8f79-66d3-41c8-99c2-83bc0354e02b", | ||
"port_security_enabled": true, | ||
"project_id": "ebc5b22e420d4dfc9e385a63b4583623", | ||
"revision_number": 2, | ||
"project_id": "d3c2c85bdbf24ff5843f323524b63768", | ||
"revision_number": 6, | ||
"security_groups": [ | ||
"684dcc25-f419-44a5-b4e7-fdcad4335ecd" | ||
"73d7e451-fa23-4a98-8897-c9ef1b1d531b" | ||
], | ||
"standard_attr_id": 56, | ||
"status": "DOWN", | ||
"standard_attr_id": 508, | ||
"status": "ACTIVE", | ||
"tags": [], | ||
"tenant_id": "ebc5b22e420d4dfc9e385a63b4583623", | ||
"updated_at": "2024-07-24T13:42:25Z" | ||
"tenant_id": "d3c2c85bdbf24ff5843f323524b63768", | ||
"updated_at": "2024-11-13T10:40:58Z" | ||
} |
118 changes: 91 additions & 27 deletions
118
python/neutron-understack/neutron_understack/tests/test_neutron_understack_mech.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,100 @@ | ||
from unittest.mock import MagicMock | ||
import json | ||
from unittest.mock import MagicMock, patch | ||
|
||
import pytest | ||
|
||
from neutron_understack.argo.workflows import ArgoClient | ||
from neutron_understack.nautobot import Nautobot | ||
from neutron_understack.neutron_understack_mech import UnderstackDriver | ||
from neutron_understack.undersync import Undersync | ||
|
||
|
||
@pytest.fixture | ||
def argo_client() -> ArgoClient: | ||
return MagicMock(spec_set=ArgoClient) | ||
|
||
|
||
def test_move_to_network__provisioning(argo_client, device_id, network_id, mac_address): | ||
driver = UnderstackDriver() | ||
driver._move_to_network( | ||
vif_type="other", | ||
mac_address=mac_address, | ||
device_uuid=str(device_id), | ||
network_id=str(network_id), | ||
argo_client=argo_client, | ||
) | ||
def current_context() -> dict: | ||
file_path = "neutron_understack/tests/fixtures/neutron_update_port_postcommit.json" | ||
with open(file_path) as context_file: | ||
return json.load(context_file) | ||
|
||
|
||
@pytest.fixture | ||
def context(current_context) -> MagicMock: | ||
return MagicMock(current=current_context) | ||
|
||
|
||
@pytest.fixture | ||
def nautobot_client() -> Nautobot: | ||
return MagicMock(spec_set=Nautobot) | ||
|
||
|
||
@pytest.fixture | ||
def undersync_client() -> Undersync: | ||
return MagicMock(spec_set=Undersync) | ||
|
||
|
||
driver = UnderstackDriver() | ||
|
||
|
||
def test_fetch_connected_interface_uuid(context): | ||
result = driver.fetch_connected_interface_uuid(context) | ||
assert result == "03921f8d-b4de-412e-a733-f8eade4c6268" | ||
|
||
argo_client.submit.assert_called_once_with( | ||
template_name="undersync-device", | ||
entrypoint="trigger-undersync", | ||
parameters={ | ||
"interface_mac": mac_address, | ||
"device_uuid": str(device_id), | ||
"network_name": "tenant", | ||
"network_id": str(network_id), | ||
"dry_run": True, | ||
"force": False, | ||
}, | ||
service_account="workflow", | ||
|
||
def test_fail_fetch_connected_interface_uuid(context): | ||
context.current["binding:profile"]["local_link_information"][0]["port_id"] = 11 | ||
with pytest.raises(ValueError): | ||
driver.fetch_connected_interface_uuid(context) | ||
|
||
|
||
def test_update_nautobot_for_tenant_network(nautobot_client): | ||
driver.nb = nautobot_client | ||
attrs = { | ||
"prep_switch_interface.return_value": "304bd384-338a-4365-9394-0c356ec698ed" | ||
} | ||
nautobot_client.configure_mock(**attrs) | ||
driver.update_nautobot("111", "222") | ||
|
||
nautobot_client.prep_switch_interface.assert_called_once_with("222", "111") | ||
|
||
|
||
def test_update_nautobot_for_provisioning_network(nautobot_client): | ||
attrs = { | ||
"configure_port_status.return_value": {"device": {"id": "444"}}, | ||
"fetch_vlan_group_uuid.return_value": "304bd384-338a-4365-9394-0c356ec698ed", | ||
} | ||
nautobot_client.configure_mock(**attrs) | ||
driver.nb = nautobot_client | ||
driver.update_nautobot("change_me", "333") | ||
|
||
nautobot_client.configure_port_status.assert_called_once_with( | ||
"333", "Provisioning-Interface" | ||
) | ||
nautobot_client.fetch_vlan_group_uuid.assert_called_once_with("444") | ||
|
||
|
||
@patch("neutron_understack.neutron_understack_mech.UnderstackDriver.update_nautobot") | ||
@patch( | ||
"neutron_understack.neutron_understack_mech.UnderstackDriver.fetch_connected_interface_uuid" | ||
) | ||
def test_success_update_port_post_commit( | ||
mocked_update_nautobot, | ||
mocked_fetch_connected_interface_uuid, | ||
context, | ||
undersync_client, | ||
): | ||
driver.undersync = undersync_client | ||
driver.update_port_postcommit(context) | ||
|
||
mocked_fetch_connected_interface_uuid.assert_called_once() | ||
mocked_update_nautobot.assert_called_once() | ||
undersync_client.sync_devices.assert_called_once() | ||
|
||
|
||
@patch( | ||
"neutron_understack.neutron_understack_mech.UnderstackDriver.fetch_connected_interface_uuid" | ||
) | ||
def test_wrong_vif_type_update_port_post_commit( | ||
mocked_fetch_connected_interface_uuid, context | ||
): | ||
context.current["binding:vif_type"] = "unbound" | ||
driver.update_port_postcommit(context) | ||
|
||
mocked_fetch_connected_interface_uuid.assert_not_called() |