forked from openmsa/python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_repository.py
More file actions
366 lines (295 loc) · 16.3 KB
/
test_repository.py
File metadata and controls
366 lines (295 loc) · 16.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
353
354
355
356
357
358
359
360
361
362
363
364
365
"""
Test Repository
"""
from unittest.mock import patch
from urllib.parse import urlencode
from util import _is_valid_json
from util import microservice_info # pylint: disable=unused-import
from util import repository_fixture
# pylint: disable=redefined-outer-name
def test_file_update_comment(repository_fixture):
"""
Test file update comment
"""
with patch('msa_sdk.msa_api.MSA_API._call_post') as mock_call_post:
repository = repository_fixture
repository.file_update_comment(
'Configuration/ABR/ABRA1570/FORTINET/timezone',
'Comment value')
data = urlencode({'uri': 'Configuration/ABR/ABRA1570/FORTINET/timezone',
'comment': 'Comment value'})
assert repository.path == '/repository/comment?{}'.format(data)
mock_call_post.assert_called_once_with()
def test_get_microservice_variables(repository_fixture):
"""
Test get microservice variables.
"""
response = ('{"variables" : {"variable" : [ {'
'"defaultValue" : "1774", "displayName" : "Ansible server ME",'
'"name" : "params.ansible_device_id"}]}}')
with patch('msa_sdk.msa_api.MSA_API._call_get') as mock_call_get:
repository = repository_fixture
repository._content = response
repository.get_microservice_variables(
'CommandDefinition/Reference/AWS/Generic/EC2/instances.xml'
)
data = urlencode(
{'uri': 'CommandDefinition/Reference/AWS/Generic/EC2/instances.xml'})
assert repository.path == "/repository/v2/resource/variables?{}".format(
data)
mock_call_get.assert_called_once_with()
def test_post_repository_variables(repository_fixture):
"""
Test post repository variables
"""
with patch('msa_sdk.msa_api.MSA_API._call_post') as mock_call_post:
repository = repository_fixture
repository.post_repository_variables(
'fmc_repository'
)
data = urlencode({'repository': 'fmc_repository'})
assert repository.path == "/repository/v2/variables?{}".format(data)
mock_call_post.assert_called_once_with()
def test_get_microservice_details(repository_fixture):
"""
Test get microservice details.
"""
response = ('{"variables" : {"variable" : [ {'
'"defaultValue" : "None", "displayName" : "Ansible server ME",'
'"name" : "params.ansible_device_id"}]}}')
with patch('msa_sdk.msa_api.MSA_API._call_get') as mock_call_get:
repository = repository_fixture
repository._content = response
repository.get_microservice_details(
'CommandDefinition/Reference/AWS/Generic/EC2/instances.xml'
)
data = urlencode(
{'uri': 'CommandDefinition/Reference/AWS/Generic/EC2/instances.xml'})
assert repository.path == "/repository/v2/resource/microservice?{}".format(
data)
mock_call_get.assert_called_once_with()
def test_put_microservice_details(repository_fixture):
"""
Test put microservice details.
"""
with patch('msa_sdk.msa_api.MSA_API._call_put') as mock_call_put:
repository = repository_fixture
ms_details = microservice_info()
repository.put_microservice_details(ms_details)
assert repository.path == "/repository/v2/resource/microservice"
mock_call_put.assert_called_once()
def test_create_microservice(repository_fixture):
"""
Test put microservice details.
"""
with patch('msa_sdk.msa_api.MSA_API._call_post') as mock_call_post:
repository = repository_fixture
ms_details = microservice_info()
repository.create_microservice(ms_details)
assert repository.path == "/repository/v2/resource/microservice"
mock_call_post.assert_called_once()
def test_delete_repository_resource(repository_fixture):
"""
Delete repository resource.
"""
with patch('msa_sdk.msa_api.MSA_API._call_delete') as mock_call_delete:
repository = repository_fixture
repository.delete_repository_resource(
'CommandDefinition/Reference/AWS/Generic/EC2/instances.xml'
)
data = urlencode(
{'uri': 'CommandDefinition/Reference/AWS/Generic/EC2/instances.xml'})
assert repository.path == "/repository/v2/resource?{}".format(data)
mock_call_delete.assert_called_once
def test_get_microservice_path_by_name(repository_fixture):
"""
Test get microservice file path by microservice name
and deployment settings ID.
"""
response = (
'{"microserviceUris": {"CommandDefinition/ANSIBLE/Retrieve_playbook_files_list.xml":'
' {"name": "Retrieve playbook files list","groups": ["Default"]},'
'"CommandDefinition/ANSIBLE/Read_playbook_file.xml": {"name": "Read playbook file",'
'"groups": ["Default"]},"CommandDefinition/ANSIBLE/Read_hosts_file.xml": {"name": '
'"Read hosts file","groups": ["Default"]}}}')
with patch('msa_sdk.msa_api.MSA_API._call_get') as mock_call_get:
repository = repository_fixture
repository._content = response
assert repository.get_microservice_path_by_name(
'Read_playbook_file.xml',
'276') == 'CommandDefinition/ANSIBLE/Read_playbook_file.xml'
assert repository.get_microservice_path_by_name('test', '276') is None
def test_get_microservice_variables_default_value(repository_fixture):
"""
Test get default values for microservice variables.
"""
response = ('{"variable" : [ {'
'"defaultValue" : "1774", "displayName" : "Ansible server ME",'
'"name" : "params.ansible_device_id"}]}')
with patch('msa_sdk.msa_api.MSA_API._call_get') as mock_call_get:
repository = repository_fixture
repository._content = response
assert repository.get_microservice_variables_default_value(
'CommandDefinition/ANSIBLE/Read_playbook_file.xml') == {"ansible_device_id": "1774"}
def test_detach_microserviceis_from_configuration_profile(repository_fixture):
"""
Test detach microservice from configuration profile.
"""
with patch('msa_sdk.msa_api.MSA_API._call_put') as mock_call_put:
repository = repository_fixture
repository.detach_microserviceis_from_configuration_profile(
'276', ['CommandDefinition/Reference/AWS/Generic/EC2/instances.xml'], )
data = urlencode(
{'uri': 'CommandDefinition/Reference/AWS/Generic/EC2/instances.xml'})
assert repository.path == "/conf-profile/v2/detach/276/repository/files"
def test_get_workflow_definition(repository_fixture):
"""
Test Get workflow definition.
"""
#response = '{"variables": {"variable": [{"defaultValue": "name1", "displayName": "var_name", "name": "params.var_name", "startIncrement": 0, "type": "String", "mandatoryArray": False, "visible": True, "description": "", "groupSeparator": "", "groupDisplayName": "", "displayOrder": 0, "increment": 0, "refServiceURI": "", "refDeviceIdVar": None, "keepOnImport": False, "editable": False, "onlyDetailView": False, "localVarNameMatch": "", "remoteVarNameMatch": "", "arrayCanAdd": True, "arrayCanRemove": True, "arrayCanMove": True, "arrayCanEdit": True, "mandatory": False, "userLocked": False, "grouped": False, "searchable": False, "uniqueGlobal": False, "selector": None, "defaultValueLong": None, "displayNameHeader": "", "fullDisplayName": "", "behaviour": None, "indexVariables": None, "sections": None, "sdTypes": None, "class": None, "values": None}, {"defaultValue": "24", "displayName": "integer", "name": "params.integer", "startIncrement": 0, "type": "Integer", "mandatoryArray": False, "visible": True, "description": "", "groupSeparator": "", "groupDisplayName": "", "displayOrder": 0, "increment": 0, "refServiceURI": "", "refDeviceIdVar": None, "keepOnImport": False, "editable": False, "onlyDetailView": False, "localVarNameMatch": "", "remoteVarNameMatch": "", "arrayCanAdd": True, "arrayCanRemove": True, "arrayCanMove": True, "arrayCanEdit": True, "mandatory": False, "userLocked": False, "grouped": False, "searchable": False, "uniqueGlobal": False, "selector": None, "defaultValueLong": None, "displayNameHeader": "", "fullDisplayName": "", "behaviour": None, "indexVariables": None, "sections": None, "sdTypes": None, "class": None, "values": None}, {"defaultValue": None, "displayName": "array_array", "name": "params.array_array", "startIncrement": 0, "type": "String", "mandatoryArray": False, "visible": True, "description": "", "groupSeparator": "", "groupDisplayName": "", "displayOrder": 0, "increment": 0, "refServiceURI": "", "refDeviceIdVar": None, "keepOnImport": False, "editable": False, "onlyDetailView": False, "localVarNameMatch": "", "remoteVarNameMatch": "", "arrayCanAdd": True, "arrayCanRemove": True, "arrayCanMove": True, "arrayCanEdit": True, "mandatory": False, "userLocked": False, "grouped": False, "searchable": False, "uniqueGlobal": False, "selector": None, "defaultValueLong": None, "displayNameHeader": "", "fullDisplayName": "", "behaviour": None, "indexVariables": None, "sections": None, "sdTypes": None, "class": None, "values": None}], "frozen": 0}, "example": None, "process": [{"name": "Process/workflows/TEST_WF1/Process_instanciate", "displayName": "instanciate", "type": "CREATE", "visibility": 5, "allowSchedule": None, "tasks": [{"displayName": "instanciate", "fileName": "Task_instanciate.py", "fileUri": "/opt/fmc_repository/Process/workflows/TEST_WF1/Process_instanciate/Tasks"}]}, {"name": "Process/workflows/TEST_WF1/Process_update_array", "displayName": "update_array", "type": "UPDATE", "visibility": 5, "allowSchedule": None, "tasks": [{"displayName": "update_array", "fileName": "Task_update_array.py", "fileUri": "/opt/fmc_repository/Process/workflows/TEST_WF1/Process_update_array/Tasks"}]}], "information": {"displayName": "TEST_WF1", "icon": None, "description": "TEST_WF1", "category": None, "displayField": "service_id", "serviceTaskType": "python", "order": 10000, "visibility": 5}, "metaInformationList": [{"type": "FILE", "name": "TEST_WF1.xml", "displayName": "TEST_WF1.xml", "repositoryName": "Process", "parentURI": "Process/workflows/TEST_WF1", "fileType": "text", "tag": None, "comment": None, "modelId": 0, "vendorId": 0, "uri": "Process/workflows/TEST_WF1/TEST_WF1.xml", "file": True}]}'
response = (
'{"information" : {"displayName" : "Execute Get workflow definition"}}')
with patch('msa_sdk.msa_api.MSA_API._call_get') as mock_call_get:
repository = repository_fixture
repository._content = response
assert repository.get_workflow_definition('Process/workflows/TEST_WF1/TEST_WF1.xml')[
'information']['displayName'] == 'Execute Get workflow definition'
def test_change_workflow_definition(repository_fixture):
"""
Test change workflow definition.
"""
with patch('msa_sdk.msa_api.MSA_API._call_put') as mock_call_put:
repository = repository_fixture
assert repository.change_workflow_definition('Execute_Ansible_based_microservice.xml', {
"example": "", "process": [{"tasks": ""}]}) is None
def test_create_workflow_definition(repository_fixture):
"""
Test create workflow definition.
"""
import json
workflow_definition = ''' {
"example": {
"content": "string"
},
"metaInformationList": [
{
"type": "FILE",
"name": "TEST_NEW_WF.xml",
"displayName": "TEST_NEW_WF",
"repositoryName": "Process",
"parentURI": "Process/workflows/TEST_NEW_WF",
"fileType": "text",
"tag": "string",
"comment": "string",
"modelId": 0,
"vendorId": 0,
"uri": "Process/workflows/TEST_NEW_WF/TEST_NEW_WF.xml",
"file": "true"
}
],
"information": {
"displayName": "TEST_NEW_WF",
"icon": "string",
"description": "TEST_NEW_WF",
"category": "string",
"displayField": "string",
"serviceTaskType": "string",
"order": 0,
"visibility": "5"
},
"process": [
{
"name": "Process/workflows/TEST_NEW_WF/create",
"displayName": "instanciate",
"type": "CREATE",
"visibility": "5",
"allowSchedule": false,
"tasks": [
{
"fileName": "Task1.py",
"fileUri": "/opt/fmc_repository/Process/workflows/TEST_NEW_WF/Process_instanciate/Tasks",
"displayName": "Task1"
}
]
}
]
}
'''
workflow_definition_dict = json.loads(workflow_definition) #convert string into dict
with patch('msa_sdk.msa_api.MSA_API._call_post') as mock_call_post:
repository = repository_fixture
assert repository.create_workflow_definition(workflow_definition_dict) is None
def test_create_workflow_definition2(repository_fixture):
"""
Test create workflow definition with empty workflow_definition_dict['process']['tasks'].
"""
import json
workflow_definition = ''' {
"example": {
"content": "string"
},
"metaInformationList": [
{
"type": "FILE",
"name": "TEST_NEW_WF.xml",
"displayName": "TEST_NEW_WF",
"repositoryName": "Process",
"parentURI": "Process/workflows/TEST_NEW_WF",
"fileType": "text",
"tag": "string",
"comment": "string",
"modelId": 0,
"vendorId": 0,
"uri": "Process/workflows/TEST_NEW_WF/TEST_NEW_WF.xml",
"file": "true"
}
],
"information": {
"displayName": "TEST_NEW_WF",
"icon": "string",
"description": "TEST_NEW_WF",
"category": "string",
"displayField": "string",
"serviceTaskType": "string",
"order": 0,
"visibility": "5"
},
"process": [
{
"name": "Process/workflows/TEST_NEW_WF/create",
"displayName": "instanciate",
"type": "CREATE",
"visibility": "5",
"allowSchedule": false,
"tasks": []
}
]
}
'''
workflow_definition_dict = json.loads(workflow_definition) #convert string into dict
with patch('msa_sdk.msa_api.MSA_API._call_post') as mock_call_post:
repository = repository_fixture
assert repository.create_workflow_definition(workflow_definition_dict) is None
def test_delete_workflow_definition(repository_fixture):
"""
Test delete workflow definition.
"""
response = ('{}')
with patch('msa_sdk.msa_api.MSA_API._call_delete') as mock_call_delete:
repository = repository_fixture
repository._content = response
assert repository.delete_workflow_definition('Process/workflows/TEST_WF1/TEST_WF1.xml') is None
def test_get_file(repository_fixture):
"""
Test get file.
"""
response = ('{"content" : "<?php\\n\\n////test;\\n?>" }')
with patch('msa_sdk.msa_api.MSA_API._call_get') as mock_call_get:
repository = repository_fixture
repository._content = response
repository.get_file('Datafiles/test.php')
data = urlencode(
{'uri': 'Datafiles/test.php'})
assert repository.path == "/repository/file?{}".format(
data)
mock_call_get.assert_called_once_with()