Skip to content

Commit 0917d36

Browse files
committed
Start creating unit tests for inventory plugin netbox-community#56
I've specifically started with tests for netbox-community#140 new query_filters options and refresh_url
1 parent b57ce54 commit 0917d36

File tree

7 files changed

+345
-19
lines changed

7 files changed

+345
-19
lines changed

tests/test_data.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright: (c) 2019, Bruno Inec (@sweenu) <[email protected]>
3+
# Copyright: (c) 2019, Mikhail Yohman (@FragmentedPacket) <[email protected]>
4+
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5+
6+
import json
7+
8+
# Load test data from a json file, for a pytest parametrize
9+
def load_test_data(path, test_path):
10+
with open(f"{path}/test_data/{test_path}/data.json", "r") as f:
11+
data = json.loads(f.read())
12+
tests = []
13+
for test in data:
14+
tuple_data = tuple(test.values())
15+
tests.append(tuple_data)
16+
return tests
17+

tests/unit/inventory/__init__.py

Whitespace-only changes.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[
2+
{
3+
"parameters": [
4+
{"a": "value1"},
5+
{"b": "value2"}
6+
],
7+
"expected": [
8+
{"a": "value1"},
9+
{"b": "value2"}
10+
]
11+
},
12+
{
13+
"parameters": [
14+
{"z": "value1"},
15+
{"b": "value2"}
16+
],
17+
"expected": [
18+
{"b": "value2"}
19+
]
20+
},
21+
{
22+
"parameters": [
23+
{"x": "value1"},
24+
{"y": "value2"}
25+
],
26+
"expected": [
27+
]
28+
},
29+
{
30+
"parameters": {
31+
},
32+
"expected": [
33+
]
34+
}
35+
]
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
[
2+
{
3+
"options": {
4+
"query_filters": [
5+
],
6+
"device_query_filters": {
7+
},
8+
"vm_query_filters": {
9+
},
10+
"config_context": true
11+
},
12+
"expected": [
13+
"https://netbox.test.endpoint:1234/api/dcim/devices/?limit=0",
14+
"https://netbox.test.endpoint:1234/api/virtualization/virtual-machines/?limit=0"
15+
]
16+
},
17+
{
18+
"options": {
19+
"query_filters": 12345,
20+
"device_query_filters": 6543,
21+
"vm_query_filters": null,
22+
"config_context": true
23+
},
24+
"expected": [
25+
"https://netbox.test.endpoint:1234/api/dcim/devices/?limit=0",
26+
"https://netbox.test.endpoint:1234/api/virtualization/virtual-machines/?limit=0"
27+
]
28+
},
29+
{
30+
"options": {
31+
"query_filters": [
32+
],
33+
"device_query_filters": {
34+
},
35+
"vm_query_filters": {
36+
},
37+
"config_context": false
38+
},
39+
"expected": [
40+
"https://netbox.test.endpoint:1234/api/dcim/devices/?limit=0&exclude=config_context",
41+
"https://netbox.test.endpoint:1234/api/virtualization/virtual-machines/?limit=0&exclude=config_context"
42+
]
43+
},
44+
{
45+
"options": {
46+
"query_filters": [
47+
{"name": "name value"},
48+
{"region": "region value"}
49+
],
50+
"device_query_filters": {
51+
},
52+
"vm_query_filters": {
53+
},
54+
"config_context": true
55+
},
56+
"expected": [
57+
"https://netbox.test.endpoint:1234/api/dcim/devices/?limit=0&name=name+value&region=region+value",
58+
"https://netbox.test.endpoint:1234/api/virtualization/virtual-machines/?limit=0&name=name+value&region=region+value"
59+
]
60+
},
61+
{
62+
"options": {
63+
"query_filters": [
64+
{"name": "name value"}
65+
],
66+
"device_query_filters": [
67+
{"region": "device"}
68+
],
69+
"vm_query_filters": [
70+
{"region": "vm"}
71+
],
72+
"config_context": true
73+
},
74+
"expected": [
75+
"https://netbox.test.endpoint:1234/api/dcim/devices/?limit=0&name=name+value&region=device",
76+
"https://netbox.test.endpoint:1234/api/virtualization/virtual-machines/?limit=0&name=name+value&region=vm"
77+
]
78+
},
79+
{
80+
"options": {
81+
"query_filters": [
82+
{"name": "name value"},
83+
{"invalid query filter": "nope"}
84+
],
85+
"device_query_filters": [
86+
{"has_primary_ip": "true"},
87+
{"invalid device filter": "nope"}
88+
],
89+
"vm_query_filters": [
90+
{"disk": 42},
91+
{"invalid vm filter": "nope"}
92+
],
93+
"config_context": true
94+
},
95+
"expected": [
96+
"https://netbox.test.endpoint:1234/api/dcim/devices/?limit=0&name=name+value&has_primary_ip=true",
97+
"https://netbox.test.endpoint:1234/api/virtualization/virtual-machines/?limit=0&name=name+value&disk=42"
98+
]
99+
},
100+
{
101+
"options": {
102+
"query_filters": [
103+
{"disk": "3"}
104+
],
105+
"device_query_filters": [
106+
],
107+
"vm_query_filters": [
108+
],
109+
"config_context": true
110+
},
111+
"expected": [
112+
null,
113+
"https://netbox.test.endpoint:1234/api/virtualization/virtual-machines/?limit=0&disk=3"
114+
]
115+
},
116+
{
117+
"options": {
118+
"query_filters": [
119+
{"has_primary_ip": "true"}
120+
],
121+
"device_query_filters": [
122+
],
123+
"vm_query_filters": [
124+
],
125+
"config_context": true
126+
},
127+
"expected": [
128+
"https://netbox.test.endpoint:1234/api/dcim/devices/?limit=0&has_primary_ip=true",
129+
null
130+
]
131+
},
132+
{
133+
"options": {
134+
"query_filters": [
135+
{"disk": "3"}
136+
],
137+
"device_query_filters": [
138+
{"name": "name value"}
139+
],
140+
"vm_query_filters": [
141+
],
142+
"config_context": true
143+
},
144+
"expected": [
145+
"https://netbox.test.endpoint:1234/api/dcim/devices/?limit=0&name=name+value",
146+
"https://netbox.test.endpoint:1234/api/virtualization/virtual-machines/?limit=0&disk=3"
147+
]
148+
},
149+
{
150+
"options": {
151+
"query_filters": [
152+
{"has_primary_ip": "true"}
153+
],
154+
"device_query_filters": [
155+
],
156+
"vm_query_filters": [
157+
{"name": "name value"}
158+
],
159+
"config_context": true
160+
},
161+
"expected": [
162+
"https://netbox.test.endpoint:1234/api/dcim/devices/?limit=0&has_primary_ip=true",
163+
"https://netbox.test.endpoint:1234/api/virtualization/virtual-machines/?limit=0&name=name+value"
164+
]
165+
}
166+
167+
]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[
2+
{
3+
"parameter": "a",
4+
"expected": true
5+
},
6+
{
7+
"parameter": "b",
8+
"expected": true
9+
},
10+
{
11+
"parameter": "c",
12+
"expected": true
13+
},
14+
{
15+
"parameter": "x",
16+
"expected": false
17+
},
18+
{
19+
"parameter": "y",
20+
"expected": false
21+
},
22+
{
23+
"parameter": "z",
24+
"expected": false
25+
}
26+
]
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright: (c) 2020, Hillsong, Douglas Heriot (@DouglasHeriot) <[email protected]>
3+
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
4+
5+
import pytest
6+
import os
7+
from functools import partial
8+
from unittest.mock import patch, MagicMock, Mock
9+
10+
try:
11+
from ansible_collections.netbox.netbox.plugins.inventory.nb_inventory import (
12+
InventoryModule,
13+
)
14+
from ansible_collections.netbox.netbox.tests.test_data import load_test_data
15+
16+
except ImportError:
17+
import sys
18+
19+
# Not installed as a collection
20+
# Try importing relative to root directory of this ansible_modules project
21+
22+
sys.path.append("plugins/inventory")
23+
sys.path.append("tests")
24+
from nb_inventory import InventoryModule
25+
from test_data import load_test_data
26+
27+
load_relative_test_data = partial(load_test_data, os.path.dirname(os.path.abspath(__file__)))
28+
29+
30+
@pytest.fixture
31+
def inventory_fixture():
32+
# TODO: Mock _fetch_information() to return static HTTP responses
33+
inventory = InventoryModule()
34+
inventory.api_endpoint = "https://netbox.test.endpoint:1234"
35+
return inventory
36+
37+
38+
@pytest.fixture
39+
def allowed_query_parameters_fixture():
40+
return ["a", "b", "c"]
41+
42+
43+
@pytest.mark.parametrize("parameter, expected", load_relative_test_data("validate_query_parameter"))
44+
def test_validate_query_parameter(
45+
inventory_fixture,
46+
allowed_query_parameters_fixture,
47+
parameter, expected):
48+
49+
value = "some value, doesn't matter"
50+
result = inventory_fixture.validate_query_parameter({parameter: value}, allowed_query_parameters_fixture)
51+
assert (result == (parameter, value)) == expected
52+
53+
54+
@pytest.mark.parametrize("parameters, expected", load_relative_test_data("filter_query_parameters"))
55+
def test_filter_query_parameters(
56+
inventory_fixture,
57+
allowed_query_parameters_fixture,
58+
parameters, expected):
59+
60+
result = inventory_fixture.filter_query_parameters(parameters, allowed_query_parameters_fixture)
61+
62+
# Result is iterators of tuples
63+
# expected from json file is an array of dicts
64+
65+
# Convert result iterator to list so we can get the length, and iterate over with an index
66+
result_list = list(result)
67+
68+
assert len(result_list) == len(expected)
69+
70+
for i, parameter in enumerate(result_list):
71+
assert parameter[0] == list(expected[i].keys())[0]
72+
assert parameter[1] == list(expected[i].values())[0]
73+
74+
75+
@pytest.mark.parametrize("options, expected", load_relative_test_data("refresh_url"))
76+
def test_refresh_url(inventory_fixture, options, expected):
77+
78+
inventory_fixture.query_filters = options["query_filters"]
79+
inventory_fixture.device_query_filters = options["device_query_filters"]
80+
inventory_fixture.vm_query_filters = options["vm_query_filters"]
81+
inventory_fixture.config_context = options["config_context"]
82+
83+
result = inventory_fixture.refresh_url()
84+
85+
assert result == tuple(expected)

0 commit comments

Comments
 (0)