-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest_node.py
More file actions
352 lines (299 loc) · 13.3 KB
/
test_node.py
File metadata and controls
352 lines (299 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import pytest
from infrahub_sdk import InfrahubClient
from infrahub_sdk.exceptions import NodeNotFoundError
from infrahub_sdk.node import InfrahubNode
from infrahub_sdk.schema import NodeSchema, NodeSchemaAPI, SchemaRoot
from infrahub_sdk.testing.docker import TestInfrahubDockerClient
from infrahub_sdk.testing.schemas.car_person import TESTING_CAR, TESTING_MANUFACTURER, SchemaCarPerson
class TestInfrahubNode(TestInfrahubDockerClient, SchemaCarPerson):
@pytest.fixture(scope="class")
async def initial_schema(self, default_branch: str, client: InfrahubClient, schema_base: SchemaRoot) -> None:
await client.schema.wait_until_converged(branch=default_branch)
resp = await client.schema.load(
schemas=[schema_base.to_schema_dict()], branch=default_branch, wait_until_converged=True
)
assert resp.errors == {}
async def test_node_create(
self, client: InfrahubClient, initial_schema: None, schema_manufacturer_base: NodeSchema
) -> None:
schema_manufacturer = NodeSchemaAPI(**schema_manufacturer_base.model_dump(exclude_unset=True))
data = {
"name": "Fiat",
"description": "An italian brand",
}
node = InfrahubNode(client=client, schema=schema_manufacturer, data=data)
await node.save()
assert node.id is not None
async def test_node_delete(
self,
client: InfrahubClient,
initial_schema: None,
) -> None:
obj = await client.create(kind=TESTING_MANUFACTURER, name="Dacia")
await obj.save()
await client.get(kind=TESTING_MANUFACTURER, id=obj.id)
await obj.delete()
with pytest.raises(NodeNotFoundError):
await client.get(kind=TESTING_MANUFACTURER, id=obj.id)
async def test_node_create_with_relationships(
self,
default_branch: str,
client: InfrahubClient,
initial_schema: None,
manufacturer_mercedes,
person_joe,
) -> None:
node = await client.create(
kind=TESTING_CAR, name="Tiguan", color="Black", manufacturer=manufacturer_mercedes.id, owner=person_joe.id
)
await node.save()
assert node.id is not None
node_after = await client.get(kind=TESTING_CAR, id=node.id, prefetch_relationships=True)
assert node_after.name.value == node.name.value
assert node_after.manufacturer.peer.id == manufacturer_mercedes.id
async def test_node_create_with_relationships_using_related_node(
self,
default_branch: str,
client: InfrahubClient,
initial_schema: None,
manufacturer_mercedes,
car_golf,
person_joe,
) -> None:
related_node = car_golf.owner
node = await client.create(
kind=TESTING_CAR, name="Tiguan", color="Black", manufacturer=manufacturer_mercedes, owner=related_node
)
await node.save(allow_upsert=True)
assert node.id is not None
node_after = await client.get(kind=TESTING_CAR, id=node.id, prefetch_relationships=True)
assert node_after.name.value == node.name.value
assert node_after.manufacturer.peer.id == manufacturer_mercedes.id
assert node_after.owner.peer.id == person_joe.id
assert node_after.owner.peer.typename == "TestingPerson"
async def test_node_update_with_original_data(
self,
default_branch: str,
client: InfrahubClient,
initial_schema: None,
) -> None:
person_marina = await client.create(kind="TestingPerson", name="marina", age=20)
await person_marina.save()
person_marina = await client.get(kind="TestingPerson", id=person_marina.id)
person_marina.age.value = 30
await person_marina.save()
person_marina.age.value = 20
await person_marina.save()
node = await client.get(kind="TestingPerson", id=person_marina.id)
assert node.age.value == 20, node.age.value
# async def test_node_update_payload_with_relationships(
# self,
# db: InfrahubDatabase,
# client: InfrahubClient,
# init_db_base,
# load_builtin_schema,
# tag_blue: Node,
# tag_red: Node,
# repo01: Node,
# gqlquery01: Node,
# ):
# data = {
# "name": "rfile10",
# "template_path": "mytemplate.j2",
# "query": gqlquery01.id,
# "repository": repo01.id,
# "tags": [tag_blue.id, tag_red.id],
# }
# schema = await client.schema.get(kind="CoreTransformJinja2", branch="main")
# create_payload = client.schema.generate_payload_create(
# schema=schema, data=data, source=repo01.id, is_protected=True
# )
# obj = await client.create(kind="CoreTransformJinja2", branch="main", **create_payload)
# await obj.save()
# assert obj.id is not None
# nodedb = await client.get(kind="CoreTransformJinja2", id=str(obj.id))
# input_data = nodedb._generate_input_data()["data"]["data"]
# assert input_data["name"]["value"] == "rfile10"
# # Validate that the source isn't a dictionary bit a reference to the repo
# assert input_data["name"]["source"] == repo01.id
# async def test_node_create_with_properties(
# self,
# db: InfrahubDatabase,
# client: InfrahubClient,
# init_db_base,
# load_builtin_schema,
# tag_blue: Node,
# tag_red: Node,
# repo01: Node,
# gqlquery01: Node,
# first_account: Node,
# ):
# data = {
# "name": {
# "value": "rfile02",
# "is_protected": True,
# "source": first_account.id,
# "owner": first_account.id,
# },
# "template_path": {"value": "mytemplate.j2"},
# "query": {"id": gqlquery01.id}, # "source": first_account.id, "owner": first_account.id},
# "repository": {"id": repo01.id}, # "source": first_account.id, "owner": first_account.id},
# "tags": [tag_blue.id, tag_red.id],
# }
# node = await client.create(kind="CoreTransformJinja2", data=data)
# await node.save()
# assert node.id is not None
# nodedb = await NodeManager.get_one(id=node.id, db=db, include_owner=True, include_source=True)
# assert nodedb.name.value == node.name.value
# assert nodedb.name.is_protected is True
async def test_node_update(
self,
default_branch: str,
client: InfrahubClient,
initial_schema: None,
manufacturer_mercedes,
person_joe,
person_jane,
car_golf,
tag_blue,
tag_red,
tag_green,
) -> None:
car_golf.color.value = "White"
await car_golf.tags.fetch()
car_golf.tags.add(tag_blue.id)
car_golf.tags.add(tag_red.id)
await car_golf.save()
car2 = await client.get(kind=TESTING_CAR, id=car_golf.id)
assert car2.color.value == "White"
await car2.tags.fetch()
assert len(car2.tags.peers) == 2
car2.owner = person_jane.id
car2.tags.add(tag_green.id)
car2.tags.remove(tag_red.id)
await car2.save()
car3 = await client.get(kind=TESTING_CAR, id=car_golf.id)
await car3.tags.fetch()
assert sorted([tag.id for tag in car3.tags.peers]) == sorted([tag_green.id, tag_blue.id])
# async def test_node_update_3_idempotency(
# self,
# db: InfrahubDatabase,
# client: InfrahubClient,
# init_db_base,
# load_builtin_schema,
# tag_green: Node,
# tag_red: Node,
# tag_blue: Node,
# gqlquery03: Node,
# repo99: Node,
# ):
# node = await client.get(kind="CoreGraphQLQuery", name__value="query03")
# assert node.id is not None
# updated_query = f"\n\n{node.query.value}"
# node.name.value = "query031"
# node.query.value = updated_query
# first_update = node._generate_input_data(exclude_unmodified=True)
# await node.save()
# nodedb = await NodeManager.get_one(id=node.id, db=db, include_owner=True, include_source=True)
# node = await client.get(kind="CoreGraphQLQuery", name__value="query031")
# node.name.value = "query031"
# node.query.value = updated_query
# second_update = node._generate_input_data(exclude_unmodified=True)
# assert nodedb.query.value == updated_query
# assert "query" in first_update["data"]["data"]
# assert "value" in first_update["data"]["data"]["query"]
# assert first_update["variables"]
# assert "query" not in second_update["data"]["data"]
# assert not second_update["variables"]
# async def test_relationship_manager_errors_without_fetch(self, client: InfrahubClient, load_builtin_schema):
# organization = await client.create("TestOrganization", name="organization-1")
# await organization.save()
# tag = await client.create("BuiltinTag", name="blurple")
# await tag.save()
# with pytest.raises(UninitializedError, match=r"Must call fetch"):
# organization.tags.add(tag)
# await organization.tags.fetch()
# organization.tags.add(tag)
# await organization.save()
# organization = await client.get("TestOrganization", name__value="organization-1")
# assert [t.id for t in organization.tags.peers] == [tag.id]
# async def test_relationships_not_overwritten(
# self, client: InfrahubClient, load_builtin_schema, schema_extension_01
# ):
# await client.schema.load(schemas=[schema_extension_01])
# rack = await client.create("InfraRack", name="rack-1")
# await rack.save()
# tag = await client.create("BuiltinTag", name="blizzow")
# # TODO: is it a bug that we need to save the object and fetch the tags before adding to a RelationshipManager now?
# await tag.save()
# await tag.racks.fetch()
# tag.racks.add(rack)
# await tag.save()
# tag_2 = await client.create("BuiltinTag", name="blizzow2")
# await tag_2.save()
# # the "rack" object has no link to the "tag" object here
# # rack.tags.peers is empty
# rack.name.value = "New Rack Name"
# await rack.save()
# # assert that the above rack.save() did not overwrite the existing Rack-Tag relationship
# refreshed_rack = await client.get("InfraRack", id=rack.id)
# await refreshed_rack.tags.fetch()
# assert [t.id for t in refreshed_rack.tags.peers] == [tag.id]
# # check that we can purposefully remove a tag
# refreshed_rack.tags.remove(tag.id)
# await refreshed_rack.save()
# rack_without_tag = await client.get("InfraRack", id=rack.id)
# await rack_without_tag.tags.fetch()
# assert rack_without_tag.tags.peers == []
# # check that we can purposefully add a tag
# rack_without_tag.tags.add(tag_2)
# await rack_without_tag.save()
# refreshed_rack_with_tag = await client.get("InfraRack", id=rack.id)
# await refreshed_rack_with_tag.tags.fetch()
# assert [t.id for t in refreshed_rack_with_tag.tags.peers] == [tag_2.id]
# async def test_node_create_from_pool(
# self, db: InfrahubDatabase, client: InfrahubClient, init_db_base, default_ipam_namespace, load_ipam_schema
# ):
# ip_prefix = await client.create(kind="IpamIPPrefix", prefix="192.0.2.0/24")
# await ip_prefix.save()
# ip_pool = await client.create(
# kind="CoreIPAddressPool",
# name="Core loopbacks 1",
# default_address_type="IpamIPAddress",
# default_prefix_length=32,
# ip_namespace=default_ipam_namespace,
# resources=[ip_prefix],
# )
# await ip_pool.save()
# devices = []
# for i in range(1, 5):
# d = await client.create(kind="InfraDevice", name=f"core0{i}", primary_address=ip_pool)
# await d.save()
# devices.append(d)
# assert [str(device.primary_address.peer.address.value) for device in devices] == [
# "192.0.2.1/32",
# "192.0.2.2/32",
# "192.0.2.3/32",
# "192.0.2.4/32",
# ]
# async def test_node_update_from_pool(
# self, db: InfrahubDatabase, client: InfrahubClient, init_db_base, default_ipam_namespace, load_ipam_schema
# ):
# starter_ip_address = await client.create(kind="IpamIPAddress", address="10.0.0.1/32")
# await starter_ip_address.save()
# ip_prefix = await client.create(kind="IpamIPPrefix", prefix="192.168.0.0/24")
# await ip_prefix.save()
# ip_pool = await client.create(
# kind="CoreIPAddressPool",
# name="Core loopbacks 2",
# default_address_type="IpamIPAddress",
# default_prefix_length=32,
# ip_namespace=default_ipam_namespace,
# resources=[ip_prefix],
# )
# await ip_pool.save()
# device = await client.create(kind="InfraDevice", name="core05", primary_address=starter_ip_address)
# await device.save()
# device.primary_address = ip_pool
# await device.save()
# assert str(device.primary_address.peer.address.value) == "192.168.0.1/32"