-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathtest_diffsync_model_flags.py
228 lines (172 loc) · 9.19 KB
/
test_diffsync_model_flags.py
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
"""Unit tests for the DiffSyncModel flags.
Copyright (c) 2020-2021 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 List
import pytest
from diffsync import Adapter, DiffSyncModel
from diffsync.enum import DiffSyncModelFlags
from diffsync.exceptions import ObjectNotFound
def test_diffsync_diff_with_skip_unmatched_src_flag_on_models(backend_a, backend_a_with_extra_models):
# Validate that there are 2 extras objects out of the box
diff = backend_a.diff_from(backend_a_with_extra_models)
assert diff.summary() == {"create": 2, "update": 0, "delete": 0, "no-change": 23, "skip": 0}
# Check that only 1 object is affected by the flag
backend_a_with_extra_models.get(
backend_a_with_extra_models.site, "lax"
).model_flags |= DiffSyncModelFlags.SKIP_UNMATCHED_SRC
diff = backend_a.diff_from(backend_a_with_extra_models)
assert diff.summary() == {"create": 1, "update": 0, "delete": 0, "no-change": 23, "skip": 1}
backend_a_with_extra_models.get(
backend_a_with_extra_models.device, "nyc-spine3"
).model_flags |= DiffSyncModelFlags.SKIP_UNMATCHED_SRC
diff = backend_a.diff_from(backend_a_with_extra_models)
assert diff.summary() == {"create": 0, "update": 0, "delete": 0, "no-change": 23, "skip": 2}
def test_diffsync_sync_with_skip_unmatched_src_flag_on_models(backend_a, backend_a_with_extra_models):
backend_a_with_extra_models.get(
backend_a_with_extra_models.site, "lax"
).model_flags |= DiffSyncModelFlags.SKIP_UNMATCHED_SRC
backend_a_with_extra_models.get(
backend_a_with_extra_models.device, "nyc-spine3"
).model_flags |= DiffSyncModelFlags.SKIP_UNMATCHED_SRC
backend_a.sync_from(backend_a_with_extra_models)
# New objects should not have been created
with pytest.raises(ObjectNotFound):
backend_a.get(backend_a.site, "lax")
with pytest.raises(ObjectNotFound):
backend_a.get(backend_a.device, "nyc-spine3")
assert "nyc-spine3" not in backend_a.get(backend_a.site, "nyc").devices
diff = backend_a.diff_from(backend_a_with_extra_models)
assert diff.summary() == {"create": 0, "update": 0, "delete": 0, "no-change": 23, "skip": 2}
def test_diffsync_diff_with_skip_unmatched_dst_flag_on_models(backend_a, backend_a_minus_some_models):
# Validate that there are 3 extras objects out of the box
diff = backend_a.diff_from(backend_a_minus_some_models)
assert diff.summary() == {"create": 0, "update": 0, "delete": 12, "no-change": 11, "skip": 0}
# Check that only the device "rdu-spine1" and its 2 interfaces are affected by the flag
backend_a.get(backend_a.device, "rdu-spine1").model_flags |= DiffSyncModelFlags.SKIP_UNMATCHED_DST
diff = backend_a.diff_from(backend_a_minus_some_models)
assert diff.summary() == {"create": 0, "update": 0, "delete": 9, "no-change": 11, "skip": 1}
# Check that only one additional device "sfo-spine2" and its 3 interfaces are affected by the flag
backend_a.get(backend_a.device, "sfo-spine2").model_flags |= DiffSyncModelFlags.SKIP_UNMATCHED_DST
diff = backend_a.diff_from(backend_a_minus_some_models)
assert diff.summary() == {"create": 0, "update": 0, "delete": 5, "no-change": 11, "skip": 2}
def test_diffsync_sync_with_skip_unmatched_dst_flag_on_models(backend_a, backend_a_minus_some_models):
backend_a.get(backend_a.site, "rdu").model_flags |= DiffSyncModelFlags.SKIP_UNMATCHED_DST
backend_a.get(backend_a.device, "sfo-spine2").model_flags |= DiffSyncModelFlags.SKIP_UNMATCHED_DST
backend_a.sync_from(backend_a_minus_some_models)
# Objects should not have been deleted
# rdu-spine1 hasn't been deleted because its parent hasn't been deleted
assert backend_a.get(backend_a.site, "rdu") is not None
assert backend_a.get(backend_a.device, "rdu-spine1") is not None
assert backend_a.get(backend_a.device, "sfo-spine2") is not None
assert backend_a.get(backend_a.interface, "sfo-spine2__eth0") is not None
assert "sfo-spine2" in backend_a.get(backend_a.site, "sfo").devices
def test_diffsync_diff_with_ignore_flag_on_source_models(backend_a, backend_a_with_extra_models):
# Directly ignore the extra source site
backend_a_with_extra_models.get(backend_a_with_extra_models.site, "lax").model_flags |= DiffSyncModelFlags.IGNORE
# Ignore any diffs on source site NYC, which should extend to its child nyc-spine3 device
backend_a_with_extra_models.get(backend_a_with_extra_models.site, "nyc").model_flags |= DiffSyncModelFlags.IGNORE
diff = backend_a.diff_from(backend_a_with_extra_models)
print(diff.str()) # for debugging of any failure
assert not diff.has_diffs()
def test_diffsync_diff_with_ignore_flag_on_target_models(backend_a, backend_a_minus_some_models):
# Directly ignore the extra target site
backend_a.get(backend_a.site, "rdu").model_flags |= DiffSyncModelFlags.IGNORE
# Ignore any diffs on target site SFO, which should extend to its child sfo-spine2 device
backend_a.get(backend_a.site, "sfo").model_flags |= DiffSyncModelFlags.IGNORE
diff = backend_a.diff_from(backend_a_minus_some_models)
print(diff.str()) # for debugging of any failure
assert not diff.has_diffs()
def test_diffsync_diff_with_natural_deletion_order():
# This list will contain the order in which the delete methods were called
call_order = []
class TestModelChild(DiffSyncModel): # pylint: disable=missing-class-docstring
_modelname = "child"
_identifiers = ("name",)
name: str
def delete(self):
call_order.append(self.name)
return super().delete()
class TestModelParent(DiffSyncModel): # pylint: disable=missing-class-docstring
_modelname = "parent"
_identifiers = ("name",)
_children = {"child": "children"}
name: str
children: List[TestModelChild] = []
def delete(self):
call_order.append(self.name)
return super().delete()
class TestBackend(Adapter): # pylint: disable=missing-class-docstring
top_level = ["parent"]
parent = TestModelParent
child = TestModelChild
def load(self):
parent = self.parent(name="Test-Parent")
parent.model_flags |= DiffSyncModelFlags.NATURAL_DELETION_ORDER
self.add(parent)
child = self.child(name="Test-Child")
parent.add_child(child)
self.add(child)
source = TestBackend()
source.load()
destination = TestBackend()
destination.load()
source.remove(source.get("parent", {"name": "Test-Parent"}), remove_children=True)
source.sync_to(destination)
assert call_order == ["Test-Child", "Test-Parent"]
def test_natural_deletion_order_with_noop_parent():
"""Test whether children are recursed through when natural deletion order is set and the parent has no changes."""
call_order = []
class ChildModel(DiffSyncModel):
"""Test child model that reports when its update method is called."""
_modelname = "child"
_identifiers = ("name",)
_attributes = ("attribute",)
name: str
attribute: str
def update(self, attrs):
call_order.append("Update on child")
return super().update(attrs)
class ParentModel(DiffSyncModel):
"""Test parent model."""
_modelname = "parent"
_identifiers = ("name",)
_attributes = ("attribute",)
_children = {"child": "children"}
name: str
attribute: str
children: List[ChildModel] = []
class TestAdapter(Adapter):
"""Test adapter."""
top_level = ["parent"]
parent = ParentModel
child = ChildModel
def load(self, is_source=False) -> None:
"""Test load method. Generate a difference with the is_source parameter."""
parent = self.parent(name="Test Parent", attribute="This doesn't change")
parent.model_flags |= DiffSyncModelFlags.NATURAL_DELETION_ORDER
self.add(parent)
if is_source:
child = self.child(name="Test Child", attribute="Attribute from source")
child.model_flags |= DiffSyncModelFlags.NATURAL_DELETION_ORDER
parent.add_child(child)
self.add(child)
else:
child = self.child(name="Test Child", attribute="Attribute from destination")
child.model_flags |= DiffSyncModelFlags.NATURAL_DELETION_ORDER
parent.add_child(child)
self.add(child)
source_adapter = TestAdapter()
source_adapter.load(is_source=True)
destination_adapter = TestAdapter()
destination_adapter.load()
source_adapter.sync_to(destination_adapter)
assert "Update on child" in call_order