|
| 1 | +import pytest |
1 | 2 | from graphql import graphql |
2 | 3 |
|
3 | 4 | from infrahub.core.branch import Branch |
4 | 5 | from infrahub.core.node import Node |
5 | 6 | from infrahub.database import InfrahubDatabase |
6 | 7 | from infrahub.graphql.initialization import prepare_graphql_params |
| 8 | +from infrahub.graphql.queries.search import _collapse_ipv6 |
7 | 9 |
|
8 | 10 | SEARCH_QUERY = """ |
9 | 11 | query ($search: String!) { |
@@ -93,3 +95,190 @@ async def test_search_anywhere_by_string( |
93 | 95 |
|
94 | 96 | assert sorted(node_ids) == sorted([person_john_main.id, person_jane_main.id]) |
95 | 97 | assert sorted(node_kinds) == sorted([person_john_main.get_kind(), person_jane_main.get_kind()]) |
| 98 | + |
| 99 | + |
| 100 | +async def test_search_ipv6_address_extended_format( |
| 101 | + db: InfrahubDatabase, |
| 102 | + ip_dataset_01, |
| 103 | + branch: Branch, |
| 104 | +): |
| 105 | + gql_params = prepare_graphql_params(db=db, include_subscription=False, branch=branch) |
| 106 | + |
| 107 | + res_collapsed = await graphql( |
| 108 | + schema=gql_params.schema, |
| 109 | + source=SEARCH_QUERY, |
| 110 | + context_value=gql_params.context, |
| 111 | + root_value=None, |
| 112 | + variable_values={"search": "2001:db8::"}, |
| 113 | + ) |
| 114 | + |
| 115 | + res_extended = await graphql( |
| 116 | + schema=gql_params.schema, |
| 117 | + source=SEARCH_QUERY, |
| 118 | + context_value=gql_params.context, |
| 119 | + root_value=None, |
| 120 | + variable_values={"search": "2001:0db8:0000:0000:0000:0000:0000:0000"}, |
| 121 | + ) |
| 122 | + |
| 123 | + assert ( |
| 124 | + res_extended.data["InfrahubSearchAnywhere"]["count"] |
| 125 | + == res_collapsed.data["InfrahubSearchAnywhere"]["count"] |
| 126 | + == 2 |
| 127 | + ) |
| 128 | + |
| 129 | + assert ( |
| 130 | + res_extended.data["InfrahubSearchAnywhere"]["edges"][0]["node"]["id"] |
| 131 | + == res_collapsed.data["InfrahubSearchAnywhere"]["edges"][0]["node"]["id"] |
| 132 | + ) |
| 133 | + |
| 134 | + assert ( |
| 135 | + res_extended.data["InfrahubSearchAnywhere"]["edges"][1]["node"]["id"] |
| 136 | + == res_collapsed.data["InfrahubSearchAnywhere"]["edges"][1]["node"]["id"] |
| 137 | + ) |
| 138 | + |
| 139 | + |
| 140 | +async def test_search_ipv6_network_extended_format( |
| 141 | + db: InfrahubDatabase, |
| 142 | + ip_dataset_01, |
| 143 | + branch: Branch, |
| 144 | +): |
| 145 | + gql_params = prepare_graphql_params(db=db, include_subscription=False, branch=branch) |
| 146 | + |
| 147 | + res_collapsed = await graphql( |
| 148 | + schema=gql_params.schema, |
| 149 | + source=SEARCH_QUERY, |
| 150 | + context_value=gql_params.context, |
| 151 | + root_value=None, |
| 152 | + variable_values={"search": "2001:db8::/48"}, |
| 153 | + ) |
| 154 | + |
| 155 | + res_extended = await graphql( |
| 156 | + schema=gql_params.schema, |
| 157 | + source=SEARCH_QUERY, |
| 158 | + context_value=gql_params.context, |
| 159 | + root_value=None, |
| 160 | + variable_values={"search": "2001:0db8:0000:0000:0000:0000:0000:0000/48"}, |
| 161 | + ) |
| 162 | + |
| 163 | + assert ( |
| 164 | + res_extended.data["InfrahubSearchAnywhere"]["count"] |
| 165 | + == res_collapsed.data["InfrahubSearchAnywhere"]["count"] |
| 166 | + == 1 |
| 167 | + ) |
| 168 | + |
| 169 | + assert ( |
| 170 | + res_extended.data["InfrahubSearchAnywhere"]["edges"][0]["node"]["id"] |
| 171 | + == res_collapsed.data["InfrahubSearchAnywhere"]["edges"][0]["node"]["id"] |
| 172 | + ) |
| 173 | + |
| 174 | + |
| 175 | +async def test_search_ipv6_partial_address( |
| 176 | + db: InfrahubDatabase, |
| 177 | + ip_dataset_01, |
| 178 | + branch: Branch, |
| 179 | +): |
| 180 | + gql_params = prepare_graphql_params(db=db, include_subscription=False, branch=branch) |
| 181 | + |
| 182 | + res_two_segments = await graphql( |
| 183 | + schema=gql_params.schema, |
| 184 | + source=SEARCH_QUERY, |
| 185 | + context_value=gql_params.context, |
| 186 | + root_value=None, |
| 187 | + variable_values={"search": "2001:0db8"}, |
| 188 | + ) |
| 189 | + |
| 190 | + res_partial_segment_1 = await graphql( |
| 191 | + schema=gql_params.schema, |
| 192 | + source=SEARCH_QUERY, |
| 193 | + context_value=gql_params.context, |
| 194 | + root_value=None, |
| 195 | + variable_values={"search": "2001:0db8:0"}, |
| 196 | + ) |
| 197 | + |
| 198 | + res_partial_segment_2 = await graphql( |
| 199 | + schema=gql_params.schema, |
| 200 | + source=SEARCH_QUERY, |
| 201 | + context_value=gql_params.context, |
| 202 | + root_value=None, |
| 203 | + variable_values={"search": "2001:0db8:0000:0"}, |
| 204 | + ) |
| 205 | + |
| 206 | + assert ( |
| 207 | + res_two_segments.data["InfrahubSearchAnywhere"]["count"] |
| 208 | + == res_partial_segment_1.data["InfrahubSearchAnywhere"]["count"] |
| 209 | + == res_partial_segment_2.data["InfrahubSearchAnywhere"]["count"] |
| 210 | + == 2 |
| 211 | + ) |
| 212 | + |
| 213 | + assert ( |
| 214 | + res_two_segments.data["InfrahubSearchAnywhere"]["edges"][0]["node"]["id"] |
| 215 | + == res_partial_segment_1.data["InfrahubSearchAnywhere"]["edges"][0]["node"]["id"] |
| 216 | + == res_partial_segment_2.data["InfrahubSearchAnywhere"]["edges"][0]["node"]["id"] |
| 217 | + ) |
| 218 | + |
| 219 | + |
| 220 | +async def test_search_ipv4( |
| 221 | + db: InfrahubDatabase, |
| 222 | + ip_dataset_01, |
| 223 | + branch: Branch, |
| 224 | +): |
| 225 | + """ |
| 226 | + This only tests that ipv6 search specific behavior does not break ipv4 search. |
| 227 | + """ |
| 228 | + |
| 229 | + gql_params = prepare_graphql_params(db=db, include_subscription=False, branch=branch) |
| 230 | + |
| 231 | + result_address = await graphql( |
| 232 | + schema=gql_params.schema, |
| 233 | + source=SEARCH_QUERY, |
| 234 | + context_value=gql_params.context, |
| 235 | + root_value=None, |
| 236 | + variable_values={"search": "10.0.0.0"}, |
| 237 | + ) |
| 238 | + |
| 239 | + result_network = await graphql( |
| 240 | + schema=gql_params.schema, |
| 241 | + source=SEARCH_QUERY, |
| 242 | + context_value=gql_params.context, |
| 243 | + root_value=None, |
| 244 | + variable_values={"search": "10.0.0.0/8"}, |
| 245 | + ) |
| 246 | + |
| 247 | + assert ( |
| 248 | + result_address.data["InfrahubSearchAnywhere"]["count"] |
| 249 | + == result_network.data["InfrahubSearchAnywhere"]["count"] |
| 250 | + == 1 |
| 251 | + ) |
| 252 | + |
| 253 | + assert ( |
| 254 | + result_address.data["InfrahubSearchAnywhere"]["edges"][0]["node"]["id"] |
| 255 | + == result_network.data["InfrahubSearchAnywhere"]["edges"][0]["node"]["id"] |
| 256 | + ) |
| 257 | + |
| 258 | + |
| 259 | +@pytest.mark.parametrize( |
| 260 | + "query,expected", |
| 261 | + [ |
| 262 | + ("2001:0db8:0000:0000:0000:0000:0000:0000/48", "2001:db8::/48"), |
| 263 | + ("2001:0db8:0000:0000:0000:0000:0000:0000", "2001:db8::"), |
| 264 | + ("2001:0db8", "2001:db8"), |
| 265 | + ("2001:0db8:0", "2001:db8"), |
| 266 | + ("2001:0db8:0000", "2001:db8"), |
| 267 | + ("2001:0db8:0000:0", "2001:db8"), |
| 268 | + ("2001:0db8:0000:0000:00", "2001:db8"), |
| 269 | + ("2001:0db8:0000:0001:00", "2001:db8:0:1"), |
| 270 | + ("2001:0db8:0001:0002:00", "2001:db8:1:2"), |
| 271 | + ("2001:0db8:0001:0000:0002:0000:0003", "2001:db8:1:0:2:0:3"), |
| 272 | + ], |
| 273 | +) |
| 274 | +def test_collapse_ipv6_address_or_network(query, expected): |
| 275 | + assert _collapse_ipv6(query) == expected |
| 276 | + |
| 277 | + |
| 278 | +@pytest.mark.parametrize( |
| 279 | + "query", |
| 280 | + ["invalid", "invalid:case", "2001:invalid", "2001:0db81:0000", "10.0.0.0", "2001:db8:1"], |
| 281 | +) |
| 282 | +def test_collapse_ipv6_address_or_network_invalid_cases(query): |
| 283 | + with pytest.raises(ValueError): |
| 284 | + _collapse_ipv6(query) |
0 commit comments