-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathconftest.py
More file actions
488 lines (343 loc) · 15.6 KB
/
conftest.py
File metadata and controls
488 lines (343 loc) · 15.6 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
"""Used to setup fixtures to be used through tests.
Copyright (c) 2020 Network To Code, LLC <info@networktocode.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from typing import ClassVar, Dict, List, Optional, Tuple
import pytest
from diffsync import Adapter, DiffSyncModel
from diffsync.diff import Diff, DiffElement
from diffsync.exceptions import ObjectNotCreated, ObjectNotDeleted, ObjectNotUpdated
@pytest.fixture
def generic_diffsync_model():
"""Provide a generic DiffSyncModel instance."""
return DiffSyncModel()
class ErrorProneModelMixin:
"""Test class that sometimes throws exceptions when creating/updating/deleting instances."""
_counter: ClassVar[int] = 0
@classmethod
def create(cls, adapter: Adapter, ids: Dict, attrs: Dict):
"""As DiffSyncModel.create(), but periodically throw exceptions."""
cls._counter += 1
if not cls._counter % 5:
raise ObjectNotCreated("Random creation error!")
if not cls._counter % 4:
return None # non-fatal error
return super().create(adapter, ids, attrs) # type: ignore
def update(self, attrs: Dict):
"""As DiffSyncModel.update(), but periodically throw exceptions."""
# pylint: disable=protected-access
self.__class__._counter += 1
if not self.__class__._counter % 5:
raise ObjectNotUpdated("Random update error!")
if not self.__class__._counter % 4:
return None # non-fatal error
return super().update(attrs) # type: ignore
def delete(self):
"""As DiffSyncModel.delete(), but periodically throw exceptions."""
# pylint: disable=protected-access
self.__class__._counter += 1
if not self.__class__._counter % 5:
raise ObjectNotDeleted("Random deletion error!")
if not self.__class__._counter % 4:
return None # non-fatal error
return super().delete() # type: ignore
class ExceptionModelMixin:
"""Test class that always throws exceptions when creating/updating/deleting instances."""
@classmethod
def create(cls, adapter: Adapter, ids: Dict, attrs: Dict):
"""As DiffSyncModel.create(), but always throw exceptions."""
raise NotImplementedError
def update(self, attrs: Dict):
"""As DiffSyncModel.update(), but always throw exceptions."""
raise NotImplementedError
def delete(self):
"""As DiffSyncModel.delete(), but always throw exceptions."""
raise NotImplementedError
class Site(DiffSyncModel):
"""Concrete DiffSyncModel subclass representing a site or location that contains devices."""
_modelname = "site"
_identifiers = ("name",)
_children = {"device": "devices"}
name: str
devices: List = []
@pytest.fixture
def make_site():
"""Factory for Site instances."""
def site(name="site1", devices=None, **kwargs):
"""Provide an instance of a Site model."""
if not devices:
devices = []
return Site(name=name, devices=devices, **kwargs)
return site
class Device(DiffSyncModel):
"""Concrete DiffSyncModel subclass representing a device."""
_modelname = "device"
_identifiers = ("name",)
_attributes: ClassVar[Tuple[str, ...]] = ("role",)
_children = {"interface": "interfaces"}
name: str
site_name: Optional[str] = None # note this is not included in _attributes
role: str
interfaces: List = []
@pytest.fixture
def make_device():
"""Factory for Device instances."""
def device(name="device1", site_name="site1", role="default", **kwargs):
"""Provide an instance of a Device model."""
return Device(name=name, site_name=site_name, role=role, **kwargs)
return device
class Interface(DiffSyncModel):
"""Concrete DiffSyncModel subclass representing an interface."""
_modelname = "interface"
_identifiers = ("device_name", "name")
_shortname = ("name",)
_attributes = ("interface_type", "description")
device_name: str
name: str
interface_type: str = "ethernet"
description: Optional[str] = None
@pytest.fixture
def make_interface():
"""Factory for Interface instances."""
def interface(device_name="device1", name="eth0", **kwargs):
"""Provide an instance of an Interface model."""
return Interface(device_name=device_name, name=name, **kwargs)
return interface
@pytest.fixture
def generic_adapter():
"""Provide a generic Adapter instance."""
return Adapter()
class UnusedModel(DiffSyncModel):
"""Concrete DiffSyncModel subclass that can be referenced as a class attribute but never has any data."""
_modelname = "unused"
_identifiers = ("name",)
name: str
class GenericBackend(Adapter):
"""An example semi-abstract subclass of Adapter."""
site = Site # to be overridden by subclasses
device = Device
interface = Interface
unused = UnusedModel
top_level = ["site", "unused"]
DATA: dict = {}
def load(self):
"""Initialize the Backend object by loading some site, device and interfaces from DATA."""
for site_name, site_data in self.DATA.items():
site = self.site(name=site_name)
self.add(site)
for device_name, device_data in site_data.items():
device = self.device(name=device_name, role=device_data["role"], site_name=site_name)
self.add(device)
site.add_child(device)
for intf_name, desc in device_data["interfaces"].items():
intf = self.interface(name=intf_name, device_name=device_name, description=desc)
self.add(intf)
device.add_child(intf)
class SiteA(Site):
"""Extend Site with a `people` list."""
_children = {"device": "devices", "person": "people"}
people: List = []
class DeviceA(Device):
"""Extend Device with additional data fields."""
_attributes = ("role", "tag")
tag: str = ""
class PersonA(DiffSyncModel):
"""Concrete DiffSyncModel subclass representing a person; only used by BackendA."""
_modelname = "person"
_identifiers = ("name",)
name: str
class BackendA(GenericBackend):
"""An example concrete subclass of DiffSync."""
site = SiteA
device = DeviceA
person = PersonA
DATA = {
"nyc": {
"nyc-spine1": {"role": "spine", "interfaces": {"eth0": "Interface 0", "eth1": "Interface 1"}},
"nyc-spine2": {"role": "spine", "interfaces": {"eth0": "Interface 0", "eth1": "Interface 1"}},
},
"sfo": {
"sfo-spine1": {"role": "spine", "interfaces": {"eth0": "Interface 0", "eth1": "Interface 1"}},
"sfo-spine2": {"role": "spine", "interfaces": {"eth0": "TBD", "eth1": "ddd", "eth2": "Interface 2"}},
},
"rdu": {
"rdu-spine1": {"role": "spine", "interfaces": {"eth0": "Interface 0", "eth1": "Interface 1"}},
"rdu-spine2": {"role": "spine", "interfaces": {"eth0": "Interface 0", "eth1": "Interface 1"}},
},
}
def load(self):
"""Extend the base load() implementation with subclass-specific logic."""
super().load()
person = self.person(name="Glenn Matthews")
self.add(person)
self.get("site", "rdu").add_child(person)
@pytest.fixture
def backend_a():
"""Provide an instance of BackendA subclass of DiffSync."""
diffsync = BackendA()
diffsync.load()
return diffsync
@pytest.fixture
def backend_a_with_extra_models():
"""Provide an instance of BackendA subclass of DiffSync with some extra sites and devices."""
extra_models = BackendA()
extra_models.load()
extra_site = extra_models.site(name="lax")
extra_models.add(extra_site)
extra_device = extra_models.device(name="nyc-spine3", site_name="nyc", role="spine")
extra_models.get(extra_models.site, "nyc").add_child(extra_device)
extra_models.add(extra_device)
return extra_models
@pytest.fixture
def backend_a_minus_some_models():
"""Provide an instance of BackendA subclass of DiffSync with fewer models than the default."""
missing_models = BackendA()
missing_models.load()
missing_models.remove(missing_models.get(missing_models.site, "rdu"))
missing_device = missing_models.get(missing_models.device, "sfo-spine2")
missing_models.get(missing_models.site, "sfo").remove_child(missing_device)
missing_models.remove(missing_device)
return missing_models
class ErrorProneSiteA(ErrorProneModelMixin, SiteA):
"""A Site that sometimes throws exceptions."""
class ErrorProneDeviceA(ErrorProneModelMixin, DeviceA):
"""A Device that sometimes throws exceptions."""
class ErrorProneInterface(ErrorProneModelMixin, Interface):
"""An Interface that sometimes throws exceptions."""
class ErrorProneBackendA(BackendA):
"""A variant of BackendA that sometimes fails to create/update/delete objects."""
site = ErrorProneSiteA
device = ErrorProneDeviceA
interface = ErrorProneInterface
@pytest.fixture
def error_prone_backend_a():
"""Provide an instance of ErrorProneBackendA subclass of DiffSync."""
diffsync = ErrorProneBackendA()
diffsync.load()
return diffsync
class ExceptionSiteA(ExceptionModelMixin, SiteA): # pylint: disable=abstract-method
"""A Site that always throws exceptions."""
class ExceptionDeviceA(ExceptionModelMixin, DeviceA): # pylint: disable=abstract-method
"""A Device that always throws exceptions."""
class ExceptionInterface(ExceptionModelMixin, Interface): # pylint: disable=abstract-method
"""An Interface that always throws exceptions."""
class ExceptionDeviceBackendA(BackendA):
"""A variant of BackendA that always fails to create/update/delete Device objects."""
device = ExceptionDeviceA
@pytest.fixture
def exception_backend_a():
"""Provide an instance of ExceptionBackendA subclass of DiffSync."""
diffsync = ExceptionDeviceBackendA()
diffsync.load()
return diffsync
class SiteB(Site):
"""Extend Site with a `places` list."""
_children = {"device": "devices", "place": "places"}
places: List = []
class DeviceB(Device):
"""Extend Device with a `vlans` list."""
_attributes = ("role", "vlans")
vlans: List = []
class PlaceB(DiffSyncModel):
"""Concrete DiffSyncModel subclass representing a place; only used by BackendB."""
_modelname = "place"
_identifiers = ("name",)
name: str
class BackendB(GenericBackend):
"""Another DiffSync concrete subclass with different data from BackendA."""
site = SiteB
device = DeviceB
place = PlaceB
type = "Backend_B"
DATA = {
"nyc": {
"nyc-spine1": {"role": "spine", "interfaces": {"eth0": "Interface 0/0", "eth1": "Interface 1"}},
"nyc-spine2": {"role": "spine", "interfaces": {"eth0": "Interface 0", "eth1": "Interface 1"}},
},
"sfo": {
"sfo-spine1": {"role": "leaf", "interfaces": {"eth0": "Interface 0", "eth1": "Interface 1"}},
"sfo-spine2": {"role": "spine", "interfaces": {"eth0": "TBD", "eth1": "ddd", "eth3": "Interface 3"}},
},
"atl": {
"atl-spine1": {"role": "spine", "interfaces": {"eth0": "Interface 0", "eth1": "Interface 1"}},
"atl-spine2": {"role": "spine", "interfaces": {"eth0": "Interface 0", "eth1": "Interface 1"}},
},
}
def load(self):
"""Extend the base load() implementation with subclass-specific logic."""
super().load()
place = self.place(name="Statue of Liberty")
self.add(place)
self.get("site", "nyc").add_child(place)
@pytest.fixture
def backend_b():
"""Provide an instance of BackendB subclass of DiffSync."""
diffsync = BackendB(name="backend-b")
diffsync.load()
return diffsync
class TrackedDiff(Diff):
"""Subclass of Diff that knows when it's completed."""
is_complete: bool = False
def complete(self):
"""Function called when the Diff has been fully constructed and populated with data."""
self.is_complete = True
@pytest.fixture
def diff_with_children():
"""Provide a Diff which has multiple children, some of which have children of their own."""
diff = Diff()
# person_element_1 only exists in the source
person_element_1 = DiffElement("person", "Jimbo", {"name": "Jimbo"})
person_element_1.add_attrs(source={})
diff.add(person_element_1)
# person_element_2 only exists in the dest
person_element_2 = DiffElement("person", "Sully", {"name": "Sully"})
person_element_2.add_attrs(dest={})
diff.add(person_element_2)
# device_element has no diffs of its own, but has a child intf_element
device_element = DiffElement("device", "device1", {"name": "device1"})
diff.add(device_element)
# intf_element exists in both source and dest as a child of device_element, and has differing attrs
intf_element = DiffElement("interface", "eth0", {"device_name": "device1", "name": "eth0"})
source_attrs = {"interface_type": "ethernet", "description": "my interface"}
dest_attrs = {"description": "your interface"}
intf_element.add_attrs(source=source_attrs, dest=dest_attrs)
device_element.add_child(intf_element)
# address_element exists in both source and dest but has no diffs
address_element = DiffElement("address", "RTP", {"name": "RTP"})
address_element.add_attrs(source={"state": "NC"}, dest={"state": "NC"})
diff.add(address_element)
diff.models_processed = 8
return diff
@pytest.fixture()
def diff_element_with_children():
"""Construct a DiffElement that has some diffs of its own as well as a child diff with additional diffs."""
# parent_element has differing "role" attribute, while "location" does not differ
parent_element = DiffElement("device", "device1", {"name": "device1"})
parent_element.add_attrs(source={"role": "switch", "location": "RTP"}, dest={"role": "router", "location": "RTP"})
# child_element_1 has differing "description" attribute, while "interface_type" is only present on one side
child_element_1 = DiffElement("interface", "eth0", {"device_name": "device1", "name": "eth0"})
source_attrs = {"interface_type": "ethernet", "description": "my interface"}
dest_attrs = {"description": "your interface"}
child_element_1.add_attrs(source=source_attrs, dest=dest_attrs)
# child_element_2 only exists on source, and has no attributes
child_element_2 = DiffElement("interface", "lo0", {"device_name": "device1", "name": "lo0"})
child_element_2.add_attrs(source={})
# child_element_3 only exists on dest, and has some attributes
child_element_3 = DiffElement("interface", "lo1", {"device_name": "device1", "name": "lo1"})
child_element_3.add_attrs(dest={"description": "Loopback 1"})
# child_element_4 is identical between source and dest
child_element_4 = DiffElement("interface", "lo100", {"device_name": "device1", "name": "lo100"})
child_element_4.add_attrs(source={"description": "Loopback 100"}, dest={"description": "Loopback 100"})
parent_element.add_child(child_element_1)
parent_element.add_child(child_element_2)
parent_element.add_child(child_element_3)
parent_element.add_child(child_element_4)
return parent_element