Skip to content

Commit

Permalink
Adapt tests for Netbox 4
Browse files Browse the repository at this point in the history
Change-Id: I357b1a90519599d694b31dc4066c4d55905da750
  • Loading branch information
XioNoX committed Jul 17, 2024
1 parent 83153f9 commit 994a671
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
3 changes: 2 additions & 1 deletion spicerack/tests/unit/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ def test_spicerack_netbox(mocked_pynetbox, mocked_remote_query, mocked_dns, read
assert spicerack.netbox_master_host.hosts == "netbox-server.example.com"

mocked_pynetbox.reset_mock()
mocked_pynetbox.return_value.dcim.devices.get.return_value.device_role.slug = "server"
del mocked_pynetbox.return_value.dcim.devices.get.return_value.device_role
mocked_pynetbox.return_value.dcim.devices.get.return_value.role.slug = "server"
assert isinstance(spicerack.netbox_server("host1"), NetboxServer)


Expand Down
18 changes: 9 additions & 9 deletions spicerack/tests/unit/test_netbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def _request_error():
return pynetbox.RequestError(fakestatus)


def _base_netbox_obj(name, role_key, additional_properties):
def _base_netbox_obj(name, additional_properties):
"""Return a simple object to represent a response from Netbox API."""
dict_obj = {
"name": name,
Expand All @@ -59,14 +59,14 @@ def _base_netbox_obj(name, role_key, additional_properties):
"address": "2620:0:861:103:10::1/64",
"dns_name": f"{name}.example.com",
},
role_key: {
"role": {
"id": 1,
"name": "Server",
"slug": "server",
},
}
dict_obj["primary_ip"] = dict_obj["primary_ip6"]
dict_obj["primary_ip"]["assigned_object"] = {"connected_endpoint": {"untagged_vlan": {"name": "test_vlan"}}}
dict_obj["primary_ip"]["assigned_object"] = {"connected_endpoints": [{"untagged_vlan": {"name": "test_vlan"}}]}
dict_obj.update(additional_properties)

def custom_hook(decoded_dict):
Expand All @@ -84,13 +84,13 @@ def custom_hook(decoded_dict):
@pytest.fixture(name="netbox_host")
def _netbox_host():
"""Return a mocked Netbox physical device."""
return _base_netbox_obj("physical", "device_role", {"rack": {"id": 1, "name": "rack1"}, "cluster": None})
return _base_netbox_obj("physical", {"rack": {"id": 1, "name": "rack1"}, "cluster": None})


@pytest.fixture(name="netbox_virtual_machine")
def _netbox_virtual_machine():
"""Return a mocked Netbox virtual machine."""
return _base_netbox_obj("virtual", "role", {"cluster": {"id": 1, "name": "testcluster"}})
return _base_netbox_obj("virtual", {"cluster": {"id": 1, "name": "testcluster"}})


class TestNetbox:
Expand Down Expand Up @@ -193,7 +193,7 @@ def setup_method_fixture(self, netbox_host, netbox_virtual_machine):

def test_init_not_a_server(self):
"""It should raise a NetboxError if the device doesn't have a server role."""
self.netbox_host.device_role.slug = "not-server"
self.netbox_host.role.slug = "not-server"
with pytest.raises(NetboxError, match="has invalid role not-server"):
NetboxServer(api=self.mocked_api, server=self.netbox_host)

Expand Down Expand Up @@ -366,13 +366,13 @@ def test_access_vlan_getter_primary_ip_not_assigned(self):

def test_access_vlan_getter_primary_interface_not_connected(self):
"""It should raise a NetboxError if the primary interface not connected."""
self.netbox_host.primary_ip.assigned_object.connected_endpoint = None
self.netbox_host.primary_ip.assigned_object.connected_endpoints = None
with pytest.raises(NetboxError, match="Primary interface not connected."):
self.physical_server.access_vlan # pylint: disable=pointless-statement

def test_access_vlan_getter_switch_interface_no_vlan(self):
"""It should return an empty string."""
self.netbox_host.primary_ip.assigned_object.connected_endpoint.untagged_vlan = None
self.netbox_host.primary_ip.assigned_object.connected_endpoints[0].untagged_vlan = None
assert self.physical_server.access_vlan == ""

def test_access_vlan_getter_virtual(self):
Expand All @@ -383,7 +383,7 @@ def test_access_vlan_getter_virtual(self):
def test_access_vlan_setter_ok(self):
"""It should set the access vlan."""
self.physical_server.access_vlan = "set_test_vlan"
self.netbox_host.primary_ip.assigned_object.connected_endpoint.save.assert_called_once_with()
self.netbox_host.primary_ip.assigned_object.connected_endpoints[0].save.assert_called_once_with()

def test_access_vlan_setter_not_found(self):
"""It should raise a NetboxError if trying to set an non active vlan."""
Expand Down

0 comments on commit 994a671

Please sign in to comment.