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
|
###############################################################################
# Copyright (c) 2017 Koren Lev (Cisco Systems), Yaron Yogev (Cisco Systems) #
# and others #
# #
# All rights reserved. This program and the accompanying materials #
# are made available under the terms of the Apache License, Version 2.0 #
# which accompanies this distribution, and is available at #
# http://www.apache.org/licenses/LICENSE-2.0 #
###############################################################################
from discover.scanner import Scanner
from test.scan.test_scan import TestScan
from unittest.mock import MagicMock, patch
from discover.scan_metadata_parser import ScanMetadataParser
from test.scan.test_data.scanner import *
from monitoring.setup.monitoring_setup_manager import MonitoringSetupManager
class TestScanner(TestScan):
def setUp(self):
super().setUp()
ScanMetadataParser.parse_metadata_file = MagicMock(return_value=METADATA)
self.scanner = Scanner()
self.scanner.set_env(self.env)
MonitoringSetupManager.create_setup = MagicMock()
self.scanner.inv.monitoring_setup_manager = \
MonitoringSetupManager(self.env)
def test_check_type_env_without_environment_condition(self):
result = self.scanner.check_type_env(TYPE_TO_FETCH_WITHOUT_ENV_CON)
self.assertEqual(result, True,
"Can't get true when the type_to_fetch " +
"doesn't contain environment condition")
def test_check_type_with_error_value(self):
# store original method
original_get_env_config = self.scanner.config.get_env_config
# mock get_env_config method
self.scanner.config.get_env_config =\
MagicMock(return_value=CONFIGURATIONS)
result = self.scanner.check_type_env(TYPE_TO_FETCH_WITH_ERROR_VALUE)
# reset get_env_config method
self.scanner.config.get_env_config = original_get_env_config
self.assertEqual(result, False,
"Can't get false when the type_to_fetch " +
"contain error value")
def test_check_type_env_without_mechanism_drivers_in_env_config(self):
# store original method
original_get_env_config = self.scanner.config.get_env_config
# mock get_env_config_method
self.scanner.config.get_env_config =\
MagicMock(return_value=CONFIGURATIONS_WITHOUT_MECHANISM_DRIVERS)
result = self.scanner.check_type_env(TYPE_TO_FETCH)
# reset get_env_config method
self.scanner.check_type_env = original_get_env_config
self.assertEqual(result, False,
"Can't get false when configuration " +
"doesn't contain mechanism drivers")
def test_check_type_env_with_wrong_mech_drivers_in_env_condition(self):
# store original method
original_get_env_config = self.scanner.config.get_env_config
# mock get_env_config_method
self.scanner.config.get_env_config =\
MagicMock(return_value=CONFIGURATIONS)
result = self.scanner.\
check_type_env(TYPE_TO_FETCH_WITH_WRONG_ENVIRONMENT_CONDITION)
# reset get_env_config method
self.scanner.check_type_env = original_get_env_config
self.assertEqual(result, False, "Can't get false when the mechanism " +
"drivers in type_to_fetch " +
"don't exist in configurations")
def test_check_type_env(self):
# store original method
original_get_env_config = self.scanner.config.get_env_config
# mock method
self.scanner.config.get_env_config =\
MagicMock(return_value=CONFIGURATIONS)
result = self.scanner.check_type_env(TYPE_TO_FETCH)
# reset method
self.scanner.config.get_env_config = original_get_env_config
self.assertEqual(result, True,
"Can't get True when the type_to_fetch is correct")
def test_scan_error_type(self):
# store original method
original_check_type_env = self.scanner.check_type_env
# mock method
self.scanner.check_type_env = MagicMock(return_value=False)
result = self.scanner.scan_type(TYPE_TO_FETCH_FOR_ENVIRONMENT, PARENT,
ID_FIELD)
# reset method
self.scanner.check_type_env = original_check_type_env
self.assertEqual(result, [],
"Can't get [], when the type_to_fetch is wrong")
def test_scan_type_without_parent_id(self):
try:
self.scanner.scan_type(TYPE_TO_FETCH_FOR_ENVIRONMENT,
PARENT_WITHOUT_ID, ID_FIELD)
self.fail("Can't get error when the parent " +
"doesn't contain id attribute")
except:
pass
@patch("discover.fetchers.folder_fetcher.FolderFetcher.get")
def test_scan_type_with_get_exception(self, fetcher_get):
fetcher_get.side_effect = Exception("get exception")
try:
self.scanner.scan_type(TYPE_TO_FETCH_FOR_ENVIRONMENT,
PARENT, ID_FIELD)
self.fail("Can't get exception when fetcher.get throws an exception")
except:
pass
@patch("discover.fetchers.folder_fetcher.FolderFetcher.get")
def test_scan_type_without_master_parent(self, fetcher_get):
fetcher_get.return_value = DB_RESULTS_WITHOUT_MASTER_PARENT_IN_DB
# store original get_by_id
original_get_by_id = self.scanner.inv.get_by_id
original_set = self.scanner.inv.set
# mock methods
self.scanner.inv.get_by_id = MagicMock(return_value=[])
result = self.scanner.scan_type(TYPE_TO_FETCH_FOR_ENVIRONMENT, PARENT,
ID_FIELD)
# reset methods
self.scanner.inv.get_by_id = original_get_by_id
self.scanner.inv.set = original_set
self.assertEqual(result, [], "Can't get [], when the master parent " +
"doesn't exist in database")
@patch("discover.fetchers.folder_fetcher.FolderFetcher.get")
def test_scan_type_with_master_parent(self, fetcher_get):
fetcher_get.return_value = DB_RESULTS_WITH_MASTER_PARENT_IN_DB
# store original methods
original_get_by_id = self.scanner.inv.get_by_id
original_set = self.scanner.inv.set
# mock methods
self.scanner.inv.get_by_id = MagicMock(return_value=MASTER_PARENT)
self.scanner.inv.set = MagicMock()
self.scanner.scan_type(TYPE_TO_FETCH_FOR_ENVIRONMENT, PARENT, ID_FIELD)
self.assertEqual(self.scanner.inv.set.call_count, 2, "Can't create additional folder")
self.assertNotIn("master_parent_type", DB_RESULTS_WITH_MASTER_PARENT_IN_DB, "Can't delete the master_parent_type")
self.assertNotIn("master_parent_id", DB_RESULTS_WITH_MASTER_PARENT_IN_DB, "Can't delete the master_parent_id")
# reset methods
self.scanner.inv.get_by_id = original_get_by_id
self.scanner.inv.set = original_set
@patch("discover.fetchers.folder_fetcher.FolderFetcher.get")
def test_scan_type_with_in_project(self, fetcher_get):
fetcher_get.return_value = DB_RESULTS_WITH_PROJECT
# store original method
original_set = self.scanner.inv.set
# mock method
self.scanner.inv.set = MagicMock()
self.scanner.scan_type(TYPE_TO_FETCH_FOR_ENVIRONMENT, PARENT, ID_FIELD)
self.assertIn("projects", DB_RESULTS_WITH_PROJECT[0],
"Can't get the projects from DB result")
self.assertNotIn(PROJECT_KEY, DB_RESULTS_WITH_PROJECT[0],
"Can't delete the project key in the object")
self.scanner.inv.set = original_set
@patch("discover.fetchers.folder_fetcher.FolderFetcher.get")
def test_scan_type_without_create_object(self, fetcher_get):
fetcher_get.return_value = DB_RESULTS_WITHOUT_CREATE_OBJECT
original_set = self.scanner.inv.set
self.scanner.inv.set = MagicMock()
self.scanner.scan_type(TYPE_TO_FETCH_FOR_ENVIRONMENT, PARENT, ID_FIELD)
self.assertEqual(self.scanner.inv.set.call_count, 0,
"Set the object when the create object is false")
self.scanner.inv.set = original_set
@patch("discover.fetchers.folder_fetcher.FolderFetcher.get")
def test_scan_type_with_create_object(self, fetcher_get):
fetcher_get.return_value = DB_RESULTS_WITH_CREATE_OBJECT
original_set = self.scanner.inv.set
self.scanner.inv.set = MagicMock()
self.scanner.scan_type(TYPE_TO_FETCH_FOR_ENVIRONMENT, PARENT, ID_FIELD)
self.assertEqual(self.scanner.inv.set.call_count, 1,
"Set the object when the create object is false")
self.scanner.inv.set = original_set
@patch("discover.fetchers.folder_fetcher.FolderFetcher.get")
def test_scan_type_with_children_scanner(self, fetcher_get):
fetcher_get.return_value = DB_RESULTS_WITH_CREATE_OBJECT
original_set = self.scanner.inv.set
original_queue_for_scan = self.scanner.queue_for_scan
self.scanner.inv.set = MagicMock()
self.scanner.queue_for_scan = MagicMock()
self.scanner.scan_type(TYPE_TO_FETCH_FOR_ENVIRONMENT, PARENT, ID_FIELD)
self.assertEqual(self.scanner.queue_for_scan.call_count, 1,
"Can't put children scanner in the queue")
self.scanner.inv.set = original_set
self.scanner.queue_for_scan = original_queue_for_scan
@patch("discover.fetchers.folder_fetcher.FolderFetcher.get")
def test_scan_type_without_children_scanner(self, fetcher_get):
fetcher_get.return_value = DB_RESULTS_WITH_CREATE_OBJECT
original_set = self.scanner.inv.set
original_queue_for_scan = self.scanner.queue_for_scan
self.scanner.inv.set = MagicMock()
self.scanner.queue_for_scan = MagicMock()
self.scanner.scan_type(TYPE_TO_FETCH_FOR_ENV_WITHOUT_CHILDREN_FETCHER,
PARENT, ID_FIELD)
self.assertEqual(self.scanner.queue_for_scan.call_count, 0,
"Can't put children scanner in the queue")
self.scanner.inv.set = original_set
self.scanner.queue_for_scan = original_queue_for_scan
@patch("discover.fetchers.folder_fetcher.FolderFetcher.get")
def test_scan_type(self, fetcher_get):
fetcher_get.return_value = DB_RESULTS_WITH_CREATE_OBJECT
original_set = self.scanner.inv.set
original_queue_for_scan = self.scanner.queue_for_scan
self.scanner.inv.set = MagicMock()
self.scanner.queue_for_scan = MagicMock()
result = self.scanner.scan_type(TYPE_TO_FETCH_FOR_ENVIRONMENT, PARENT,
ID_FIELD)
self.assertNotEqual(result, [], "Can't get children form scan_type")
self.scanner.inv.set = original_set
self.scanner.queue_for_scan = original_queue_for_scan
def test_scan_with_limit_to_child_type(self):
original_scan_type = self.scanner.scan_type
original_get_scanner = self.scanner.get_scanner
self.scanner.scan_type = MagicMock(return_value=[])
self.scanner.get_scanner = MagicMock(return_value=TYPES_TO_FETCH)
limit_to_child_type = TYPES_TO_FETCH[0]['type']
self.scanner.scan(SCANNER_TYPE_FOR_ENV, PARENT, limit_to_child_type=limit_to_child_type)
# only scan the limit child type
self.scanner.scan_type.assert_called_with(TYPES_TO_FETCH[0], PARENT,
ID_FIELD)
self.scanner.scan_type = original_scan_type
self.scanner.get_scanner = original_get_scanner
def test_scan_with_limit_to_child_id(self):
original_scan_type = self.scanner.scan_type
original_get_scanner = self.scanner.get_scanner
self.scanner.get_scanner = MagicMock(return_value=TYPES_TO_FETCH)
limit_to_child_id = SCAN_TYPE_RESULTS[0][ID_FIELD]
self.scanner.scan_type = MagicMock(return_value=SCAN_TYPE_RESULTS)
children = self.scanner.scan(SCANNER_TYPE_FOR_ENV, PARENT, id_field=ID_FIELD,
limit_to_child_id=limit_to_child_id)
# only get the limit child
self.assertEqual(children, SCAN_TYPE_RESULTS[0])
self.scanner.scan_type = original_scan_type
self.scanner.get_scanner = original_get_scanner
def test_scan(self):
original_scan_type = self.scanner.scan_type
original_get_scanner = self.scanner.get_scanner
self.scanner.get_scanner = MagicMock(return_values=TYPES_TO_FETCH)
result = self.scanner.scan(SCANNER_TYPE_FOR_ENV, PARENT)
self.assertEqual(PARENT, result,
"Can't get the original parent after the scan")
self.scanner.get_scanner = original_get_scanner
self.scanner.scan_type = original_scan_type
def test_run_scan(self):
original_scan = self.scanner.scan
original_scan_from_queue = self.scanner.scan_from_queue
self.scanner.scan = MagicMock()
self.scanner.scan_from_queue = MagicMock()
self.scanner.run_scan(SCANNER_TYPE_FOR_ENV, PARENT, ID_FIELD, LIMIT_TO_CHILD_ID,
LIMIT_TO_CHILD_TYPE)
self.scanner.scan.assert_called_with(SCANNER_TYPE_FOR_ENV, PARENT, ID_FIELD,
LIMIT_TO_CHILD_ID,
LIMIT_TO_CHILD_TYPE)
self.scanner.scan_from_queue.assert_any_call()
self.scanner.scan = original_scan
self.scanner.scan_from_queue = original_scan_from_queue
@patch("discover.scanner.Scanner.scan")
def test_scan_from_queue(self, scan):
scan.return_value = []
Scanner.scan_queue = SCAN_QUEUE
self.scanner.scan_from_queue()
self.assertEqual(self.scanner.scan.call_count, QUEUE_SIZE,
"Can't scan all the objects in the queue")
|