-
Notifications
You must be signed in to change notification settings - Fork 532
/
test_subscribers.py
381 lines (342 loc) · 13.1 KB
/
test_subscribers.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
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
from django.contrib.contenttypes.models import ContentType
from django.test import TestCase
from django.test.utils import override_settings
from ralph.accounts.tests.factories import TeamFactory
from ralph.assets.models import (
AssetModel,
ConfigurationClass,
ConfigurationModule
)
from ralph.assets.tests.factories import (
ConfigurationClassFactory,
ConfigurationModuleFactory,
EnvironmentFactory,
ServiceEnvironmentFactory,
ServiceFactory
)
from ralph.data_center.tests.factories import DataCenterAssetFactory
from ralph.data_importer.models import (
ImportedObjectDoesNotExist,
ImportedObjects
)
from ralph.lib.custom_fields.models import CustomField, CustomFieldTypes
from ralph.networks.tests.factories import IPAddressFactory
from ralph.ralph2_sync.subscribers import (
ralph2_sync_ack,
sync_custom_fields_to_ralph3,
sync_device_to_ralph3,
sync_venture_role_to_ralph3,
sync_venture_to_ralph3,
sync_virtual_server_to_ralph3
)
from ralph.virtual.models import VirtualServer, VirtualServerType
from ralph.virtual.tests.factories import VirtualServerFactory
def _create_imported_object(factory, old_id, factory_kwargs=None):
if factory_kwargs is None:
factory_kwargs = {}
obj = factory(**factory_kwargs)
ImportedObjects.create(obj, old_id)
return obj
class Ralph2SyncACKTestCase(TestCase):
def setUp(self):
self.ct = ContentType.objects.get_for_model(AssetModel)
def test_ack_when_entry_does_not_exist_before(self):
old_count = ImportedObjects.objects.count()
ralph2_sync_ack(
data={'model': 'AssetModel', 'id': 123, 'ralph3_id': 321}
)
self.assertEqual(ImportedObjects.objects.count(), old_count + 1)
io = ImportedObjects.objects.get(content_type=self.ct, object_pk='321')
self.assertEqual(io.old_object_pk, '123')
def test_ack_when_entry_exists_before(self):
ImportedObjects.objects.create(
content_type=self.ct,
object_pk='321',
old_object_pk='123',
)
old_count = ImportedObjects.objects.count()
ralph2_sync_ack(
data={'model': 'AssetModel', 'id': 123, 'ralph3_id': 321}
)
self.assertEqual(ImportedObjects.objects.count(), old_count)
ImportedObjects.objects.get(
content_type=ContentType.objects.get_for_model(AssetModel),
object_pk='321'
)
class Ralph2DataCenterAssetTestCase(TestCase):
def test_sync_device_custom_fields(self):
old_id = 1
field_name = 'test_field'
dca = DataCenterAssetFactory()
CustomField.objects.create(name=field_name)
ImportedObjects.create(obj=dca, old_pk=old_id)
custom_fields = {field_name: 'test_value'}
data = {
'id': old_id,
'custom_fields': custom_fields
}
sync_device_to_ralph3(data)
self.assertEqual(dca.custom_fields_as_dict, custom_fields)
def test_sync_device_new_management_ip(self):
old_id = 1
dca = DataCenterAssetFactory()
ImportedObjects.create(obj=dca, old_pk=old_id)
data = {
'id': old_id,
'management_ip': '10.20.30.40',
}
sync_device_to_ralph3(data)
self.assertEqual(dca.management_ip, '10.20.30.40')
def test_sync_device_existing_management_ip(self):
old_id = 1
ip = IPAddressFactory(address='10.20.30.40', is_management=True)
dca = DataCenterAssetFactory()
ImportedObjects.create(obj=dca, old_pk=old_id)
data = {
'id': old_id,
'management_ip': '10.20.30.40',
}
sync_device_to_ralph3(data)
self.assertEqual(dca.management_ip, '10.20.30.40')
ip.refresh_from_db()
ip.ethernet.refresh_from_db()
self.assertEqual(ip.ethernet.base_object.pk, dca.pk)
class Ralph2CustomFieldsTestCase(TestCase):
def test_sync_custom_fields_to_ralph3_with_choices(self):
data = {
'symbol': 'test_name',
'default': '1',
'choices': ['1', '2', '3']
}
sync_custom_fields_to_ralph3(data=data)
cf = CustomField.objects.get(name=data['symbol'])
self.assertEqual(cf._get_choices(), data['choices'])
self.assertEqual(cf.default_value, data['default'])
self.assertEqual(cf.type, CustomFieldTypes.CHOICE)
def test_sync_custom_fields_to_ralph3_without_choices(self):
data = {
'symbol': 'test_name',
'default': '1',
'choices': []
}
sync_custom_fields_to_ralph3(data=data)
cf = CustomField.objects.get(name=data['symbol'])
self.assertEqual(cf.default_value, data['default'])
self.assertEqual(cf.type, CustomFieldTypes.STRING)
class Ralph2SyncVentureTestCase(TestCase):
def setUp(self):
self.conf_module = ConfigurationModuleFactory()
ImportedObjects.create(self.conf_module, 11)
self.team = TeamFactory(name='TEAM1')
def test_new_venture_with_valid_team_without_parent(self):
data = {
'id': 1122,
'symbol': 'v1',
'department': 'TEAM1',
'parent': None,
}
sync_venture_to_ralph3(data)
conf_module = ImportedObjects.get_object_from_old_pk(
ConfigurationModule, 1122
)
self.assertEqual(conf_module.name, 'v1')
self.assertEqual(conf_module.support_team, self.team)
self.assertIsNone(conf_module.parent)
def test_new_venture_with_valid_team_with_parent(self):
data = {
'id': 1122,
'symbol': 'v1',
'department': None,
'parent': 11,
}
sync_venture_to_ralph3(data)
conf_module = ImportedObjects.get_object_from_old_pk(
ConfigurationModule, 1122
)
self.assertEqual(conf_module.name, 'v1')
self.assertIsNone(conf_module.support_team)
self.assertEqual(conf_module.parent, self.conf_module)
def test_update_existing_venture(self):
data = {
'id': 11,
'symbol': 'v22',
'department': 'TEAM1',
'parent': None,
}
sync_venture_to_ralph3(data)
self.conf_module.refresh_from_db()
self.assertEqual(self.conf_module.name, 'v22')
self.assertEqual(self.conf_module.support_team, self.team)
self.assertIsNone(self.conf_module.parent)
def test_venture_with_non_existing_parent(self):
data = {
'id': 33,
'symbol': 'v22',
'department': 'TEAM1',
'parent': -1,
}
conf_modules_count = ConfigurationModule.objects.count()
sync_venture_to_ralph3(data)
# new conf module should not be added
self.assertEqual(
ConfigurationModule.objects.count(), conf_modules_count
)
with self.assertRaises(ImportedObjectDoesNotExist):
ImportedObjects.get_object_from_old_pk(
ConfigurationModule, 33
)
class Ralph2SyncVentureRoleTestCase(TestCase):
def setUp(self):
self.conf_module = ConfigurationModuleFactory()
ImportedObjects.create(self.conf_module, 11)
self.conf_class = ConfigurationClassFactory()
ImportedObjects.create(self.conf_class, 12)
def test_new_venture_role(self):
data = {
'id': 1122,
'name': 'v1',
'venture': 11,
}
sync_venture_role_to_ralph3(data)
conf_class = ImportedObjects.get_object_from_old_pk(
ConfigurationClass, 1122
)
self.assertEqual(conf_class.class_name, 'v1')
self.assertEqual(conf_class.module, self.conf_module)
def test_update_existing_venture_role(self):
data = {
'id': 12,
'name': 'v1',
'venture': 11,
}
conf_classes_count = ConfigurationClass.objects.count()
sync_venture_role_to_ralph3(data)
self.assertEqual(
ConfigurationClass.objects.count(), conf_classes_count
)
self.conf_class.refresh_from_db()
self.assertEqual(self.conf_class.class_name, 'v1')
self.assertEqual(self.conf_class.module, self.conf_module)
def test_venture_role_with_non_existing_parent(self):
data = {
'id': 33,
'name': 'v22',
'venture': -1,
}
conf_classes_count = ConfigurationClass.objects.count()
sync_venture_role_to_ralph3(data)
self.assertEqual(
ConfigurationClass.objects.count(), conf_classes_count
)
with self.assertRaises(ImportedObjectDoesNotExist):
ImportedObjects.get_object_from_old_pk(
ConfigurationModule, 33
)
class Ralph2SyncVirtualServerTestCase(TestCase):
def setUp(self):
self.data = {
'id': 1,
'type': 'unknown',
'hostname': None,
'sn': None,
'service': None,
'environment': None,
'venture_role': None,
'parent_id': None,
'custom_fields': {}
}
def sync(self):
obj = self._create_imported_virtual_server()
sync_virtual_server_to_ralph3(self.data)
obj.refresh_from_db()
return obj
def _create_imported_virtual_server(self, old_id=None):
return _create_imported_object(
factory=VirtualServerFactory,
old_id=old_id if old_id else self.data['id']
)
def test_new_virtual_server_should_create_imported_object(self):
self.assertEqual(VirtualServer.objects.count(), 0)
sync_virtual_server_to_ralph3(self.data)
self.assertEqual(VirtualServer.objects.count(), 1)
vs = ImportedObjects.get_object_from_old_pk(
VirtualServer, self.data['id']
)
self.assertEqual(vs.hostname, self.data['hostname'])
def test_existing_virtual_server_should_updated(self):
vs = self._create_imported_virtual_server()
sync_virtual_server_to_ralph3(self.data)
self.assertEqual(VirtualServer.objects.count(), 1)
vs = ImportedObjects.get_object_from_old_pk(
VirtualServer, self.data['id']
)
self.assertEqual(vs.hostname, self.data['hostname'])
def test_sync_should_change_hostname(self):
vs = self._create_imported_virtual_server()
self.data['hostname'] = 'new.hostname.dc.net'
self.assertNotEqual(self.data['hostname'], vs.hostname)
sync_virtual_server_to_ralph3(self.data)
vs.refresh_from_db()
self.assertEqual(vs.hostname, self.data['hostname'])
def test_sync_should_change_sn(self):
vs = self._create_imported_virtual_server()
self.data['sn'] = 'sn-123'
self.assertNotEqual(self.data['sn'], vs.sn)
sync_virtual_server_to_ralph3(self.data)
vs.refresh_from_db()
self.assertEqual(vs.sn, self.data['sn'])
def test_sync_should_create_type_if_not_exist(self):
self.data['type'] = 'new unique type'
type_exists = VirtualServerType.objects.filter(name=self.data['type']).exists # noqa
self.assertFalse(type_exists())
sync_virtual_server_to_ralph3(self.data)
self.assertTrue(type_exists())
@override_settings(
RALPH2_RALPH3_VIRTUAL_SERVER_TYPE_MAPPING={
'type in R2': 'type in R3'
},
)
def test_sync_should_create_type_and_mapping(self):
self.data['type'] = 'type in R2'
type_exists = VirtualServerType.objects.filter(name='type in R3').exists # noqa
self.assertFalse(type_exists())
sync_virtual_server_to_ralph3(self.data)
self.assertTrue(type_exists())
def test_sync_should_change_service_env(self):
self.data['service'] = '123'
service = _create_imported_object(
ServiceFactory, self.data['service'], factory_kwargs={
'uid': self.data['service']
}
)
self.data['environment'] = '124'
env = _create_imported_object(
EnvironmentFactory, self.data['environment']
)
se = ServiceEnvironmentFactory(service=service, environment=env)
vs = self.sync()
self.assertEqual(vs.service_env, se)
def test_sync_should_change_configuration_path(self):
self.data['venture_role'] = 'venture_role_123'
conf_class = _create_imported_object(
ConfigurationClassFactory, self.data['venture_role']
)
vs = self.sync()
self.assertEqual(vs.configuration_path, conf_class)
def test_sync_should_change_parent(self):
self.data['parent_id'] = '123333'
parent = _create_imported_object(
DataCenterAssetFactory, self.data['parent_id']
)
vs = self.sync()
self.assertEqual(vs.parent.id, parent.id)
def test_sync_should_change_custom_fields(self):
cf = CustomField.objects.create(
name='test_str', type=CustomFieldTypes.STRING,
)
custom_fields = {
cf.name: 'test'
}
self.data['custom_fields'] = custom_fields
vs = self.sync()
self.assertEqual(vs.custom_fields_as_dict, custom_fields)