forked from openmsa/python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_msa_api.py
More file actions
276 lines (201 loc) · 7.45 KB
/
test_msa_api.py
File metadata and controls
276 lines (201 loc) · 7.45 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
"""
Test MSA API
"""
import datetime
import json
import os
import re
from unittest.mock import patch
import pytest
from msa_sdk.msa_api import MSA_API
@pytest.fixture
def api_fixture(tmpdir):
"""
API Fixtures
"""
f_name = tmpdir.mkdir('test').join('vars_ctx_file')
api_info = 'OTHER_VALUE-1=foobar-1\n'
api_info += 'UBI_WILDFLY_JNDI_ADDRESS=test_hostname\n'
api_info += 'UBI_WILDFLY_JNDI_PORT=1111\n'
api_info += 'OTHER_VALUE-2=foobar-2\n'
api_info += 'OTHER_VALUE-4=foobar-3\n'
api_info += 'OTHER_VALUE-5=foobar-4\n'
api_info += 'OTHER_VALUE-6=foobar-5\n'
with open(f_name, 'w+') as t_file:
t_file.write(api_info)
with patch('requests.post') as mock_post:
mock_post.return_value.json.return_value = {'token': '12345qwert'}
with patch('msa_sdk.constants.VARS_CTX_FILE', f_name):
api = MSA_API()
return api
@pytest.fixture
def environment_variable_fixture():
"""
Fixture to test environment variables for API endpoint
"""
os.environ['MSA_SDK_API_HOSTNAME'] = "environ_hostname"
os.environ['MSA_SDK_API_PORT'] = "2222"
with patch('requests.post') as mock_post:
mock_post.return_value.json.return_value = {'token': '12345qwert'}
api = MSA_API()
return api
@pytest.fixture
def default_hostname_fixture():
"""
Fixure to use default hostname and port for API endpoint
"""
with patch('requests.post') as mock_post:
mock_post.return_value.json.return_value = {'token': '12345qwert'}
api = MSA_API()
return api
# pylint: disable=redefined-outer-name
def test_default_hostname_fixture(default_hostname_fixture):
"""
Test read hostname from json
"""
api = default_hostname_fixture
assert api.url == 'http://localhost:8480/ubi-api-rest'
# pylint: disable=redefined-outer-name
def test_get_token(api_fixture):
"""
Test Get Token
"""
api = api_fixture
assert api.token == '12345qwert'
# pylint: disable=redefined-outer-name
def test_content_no_log(api_fixture):
"""
Test content with no log
"""
api = api_fixture
response = {
"wo_status": 'ENDED',
"wo_comment": 'Task OK',
"wo_newparams": {"SERVICEINSTANCEID": "1234", "Other": "Value"}
}
assert api.process_content(
'ENDED', 'Task OK', {
"SERVICEINSTANCEID": "1234",
"Other": "Value"}) == json.dumps(response)
# pylint: disable=redefined-outer-name
def test_content_with_log(api_fixture, tmpdir):
"""
Test content with log
"""
temp_dir = tmpdir.mkdir('with_log')
with patch('msa_sdk.constants.PROCESS_LOGS_DIRECTORY', temp_dir):
api = api_fixture
params = {
"SERVICEINSTANCEID": 1234, "Other": "Value",
"PROCESSINSTANCEID": 2345
}
response = {
"wo_status": 'ENDED',
"wo_comment": 'Task OK',
"wo_newparams": params
}
assert api.process_content('ENDED', 'Task OK', params,
True) == json.dumps(response)
log_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
log_msg = '\n=== {} ===|{}|\n{}\n=== {} ===|{}--|'.format(
log_time, params['PROCESSINSTANCEID'], json.dumps(
params, indent=4), log_time, params['PROCESSINSTANCEID'])
assert log_msg == open(
'{}/{}'.format(temp_dir, 'process-1234.log'), 'r').read()
# pylint: disable=redefined-outer-name
def test_content_with_log_more_lines(api_fixture, tmpdir):
"""
Test content with log with more lines
"""
temp_dir = tmpdir.mkdir('with_log_more_lines')
with patch('msa_sdk.constants.PROCESS_LOGS_DIRECTORY', temp_dir):
api = api_fixture
params1 = {
"SERVICEINSTANCEID": 1234,
"Other": "Value1",
"PROCESSINSTANCEID": 2345}
params2 = {
"SERVICEINSTANCEID": 1234,
"Other": "Value2",
"PROCESSINSTANCEID": 3456}
response = {
"wo_status": 'ENDED',
"wo_comment": 'Task OK',
"wo_newparams": params1
}
assert api.process_content('ENDED', 'Task OK', params1,
True) == json.dumps(response)
log_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
log_msg_1 = '\n=== {} ===|{}|\n{}\n=== {} ===|{}--|'.format(
log_time, params1['PROCESSINSTANCEID'], json.dumps(
params1, indent=4), log_time, params1['PROCESSINSTANCEID'])
assert log_msg_1 == open(
'{}/{}'.format(temp_dir, 'process-1234.log'), 'r').read()
api.process_content('ENDED', 'Task OK', params2, True)
log_msg_2 = '{}\n=== {} ===|{}|\n{}\n=== {} ===|{}--|'.format(
log_msg_1, log_time, params2['PROCESSINSTANCEID'], json.dumps(
params2, indent=4), log_time, params2['PROCESSINSTANCEID'])
assert log_msg_2 == open(
'{}/{}'.format(temp_dir, 'process-1234.log'), 'r').read()
def test_log_to_process_file_success(api_fixture, tmpdir):
"""
Test if log to process file is success
"""
temp_dir = tmpdir.mkdir('log_to_process_file_success')
with patch('msa_sdk.constants.PROCESS_LOGS_DIRECTORY', temp_dir):
api = api_fixture
params = {"SERVICEINSTANCEID": 1234, "Other": "Value"}
log_message = 'Lorem ipsum dolor sit amet'
assert api.log_to_process_file(
params['SERVICEINSTANCEID'], log_message)
check_pattern = f'^.+?:DEBUG:{log_message}$'
with open(f'{temp_dir}/process-1234.log', 'r') as log_file:
assert re.match(check_pattern, log_file.read())
def test_log_to_process_file_fail(api_fixture, tmpdir):
"""
Test if log to process file is fail due IOError
"""
with patch('msa_sdk.constants.PROCESS_LOGS_DIRECTORY', '/loprem/'):
api = api_fixture
params = {"SERVICEINSTANCEID": 1234, "Other": "Value"}
log_message = 'Lorem ipsum dolor sit amet'
assert not api.log_to_process_file(
params['SERVICEINSTANCEID'],
log_message)
# pylint: disable=redefined-outer-name
def test_constants(api_fixture):
"""
Test Constants
"""
msa = api_fixture
assert msa.ENDED == 'ENDED'
assert msa.FAILED == 'FAIL'
assert msa.RUNNING == 'RUNNING'
assert msa.WARNING == 'WARNING'
assert msa.PAUSED == 'PAUSE'
def test_task_error(api_fixture, capsys):
msa = api_fixture
with pytest.raises(SystemExit) as sys_exit:
msa.task_error("Task error", {'SERVICEINSTANCEID': 1}, False)
captured = capsys.readouterr()
output = (
'{"wo_status": "FAIL", "wo_comment": "Task error",'
' "wo_newparams": {"SERVICEINSTANCEID": 1}}\n'
)
assert captured.out == output
assert json.loads(captured.out)['wo_comment'] == 'Task error'
assert sys_exit.type == SystemExit
assert sys_exit.value.code == 1
def test_task_success(api_fixture, capsys):
msa = api_fixture
with pytest.raises(SystemExit) as sys_exit:
msa.task_success("Task success", {'SERVICEINSTANCEID': 1}, False)
captured = capsys.readouterr()
output = (
'{"wo_status": "ENDED", "wo_comment": "Task success",'
' "wo_newparams": {"SERVICEINSTANCEID": 1}}\n'
)
assert captured.out == output
assert json.loads(captured.out)['wo_comment'] == 'Task success'
assert sys_exit.type == SystemExit
assert sys_exit.value.code == 0