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
|
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# Copyright (c) 2017 Orange 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
"""Unitary test for energy module."""
# pylint: disable=unused-argument
import logging
import unittest
import mock
from functest.energy.energy import EnergyRecorder
import functest.energy.energy as energy
CASE_NAME = "UNIT_TEST_CASE"
STEP_NAME = "UNIT_TEST_STEP"
PREVIOUS_SCENARIO = "previous_scenario"
PREVIOUS_STEP = "previous_step"
class MockHttpResponse(object): # pylint: disable=too-few-public-methods
"""Mock response for Energy recorder API."""
def __init__(self, text, status_code):
"""Create an instance of MockHttpResponse."""
self.text = text
self.status_code = status_code
RECORDER_OK = MockHttpResponse(
'{"environment": "UNIT_TEST",'
' "step": "string",'
' "scenario": "' + CASE_NAME + '"}',
200
)
RECORDER_KO = MockHttpResponse(
'{"message": "An unhandled API exception occurred (MOCK)"}',
500
)
def config_loader_mock(config_key):
"""Return mocked config values."""
if config_key == "energy_recorder.api_url":
return "http://pod-uri:8888"
elif config_key == "energy_recorder.api_user":
return "user"
elif config_key == "energy_recorder.api_password":
return "password"
else:
raise Exception("Config not mocked")
def config_loader_mock_no_creds(config_key):
"""Return mocked config values."""
if config_key == "energy_recorder.api_url":
return "http://pod-uri:8888"
elif config_key == "energy_recorder.api_user":
return ""
elif config_key == "energy_recorder.api_password":
return ""
else:
raise Exception("Config not mocked:" + config_key)
class EnergyRecorderTest(unittest.TestCase):
"""Energy module unitary test suite."""
case_name = CASE_NAME
request_headers = {'content-type': 'application/json'}
returned_value_to_preserve = "value"
exception_message_to_preserve = "exception_message"
@mock.patch('functest.energy.energy.requests.post',
return_value=RECORDER_OK)
def test_start(self, post_mock=None):
"""EnergyRecorder.start method (regular case)."""
self.test_load_config()
self.assertTrue(EnergyRecorder.start(self.case_name))
post_mock.assert_called_once_with(
EnergyRecorder.energy_recorder_api["uri"],
auth=EnergyRecorder.energy_recorder_api["auth"],
data=mock.ANY,
headers=self.request_headers
)
@mock.patch('functest.energy.energy.requests.post',
side_effect=Exception("Internal execution error (MOCK)"))
def test_start_error(self, post_mock=None):
"""EnergyRecorder.start method (error in method)."""
self.test_load_config()
self.assertFalse(EnergyRecorder.start(self.case_name))
post_mock.assert_called_once_with(
EnergyRecorder.energy_recorder_api["uri"],
auth=EnergyRecorder.energy_recorder_api["auth"],
data=mock.ANY,
headers=self.request_headers
)
@mock.patch('functest.energy.energy.requests.post',
return_value=RECORDER_KO)
def test_start_api_error(self, post_mock=None):
"""EnergyRecorder.start method (API error)."""
self.test_load_config()
self.assertFalse(EnergyRecorder.start(self.case_name))
post_mock.assert_called_once_with(
EnergyRecorder.energy_recorder_api["uri"],
auth=EnergyRecorder.energy_recorder_api["auth"],
data=mock.ANY,
headers=self.request_headers
)
@mock.patch('functest.energy.energy.requests.post',
return_value=RECORDER_OK)
def test_set_step(self, post_mock=None):
"""EnergyRecorder.set_step method (regular case)."""
self.test_load_config()
self.assertTrue(EnergyRecorder.set_step(STEP_NAME))
post_mock.assert_called_once_with(
EnergyRecorder.energy_recorder_api["uri"] + "/step",
auth=EnergyRecorder.energy_recorder_api["auth"],
data=mock.ANY,
headers=self.request_headers
)
@mock.patch('functest.energy.energy.requests.post',
return_value=RECORDER_KO)
def test_set_step_api_error(self, post_mock=None):
"""EnergyRecorder.set_step method (API error)."""
self.test_load_config()
self.assertFalse(EnergyRecorder.set_step(STEP_NAME))
post_mock.assert_called_once_with(
EnergyRecorder.energy_recorder_api["uri"] + "/step",
auth=EnergyRecorder.energy_recorder_api["auth"],
data=mock.ANY,
headers=self.request_headers
)
@mock.patch('functest.energy.energy.requests.post',
side_effect=Exception("Internal execution error (MOCK)"))
def test_set_step_error(self, post_mock=None):
"""EnergyRecorder.set_step method (method error)."""
self.test_load_config()
self.assertFalse(EnergyRecorder.set_step(STEP_NAME))
post_mock.assert_called_once_with(
EnergyRecorder.energy_recorder_api["uri"] + "/step",
auth=EnergyRecorder.energy_recorder_api["auth"],
data=mock.ANY,
headers=self.request_headers
)
@mock.patch('functest.energy.energy.requests.delete',
return_value=RECORDER_OK)
def test_stop(self, delete_mock=None):
"""EnergyRecorder.stop method (regular case)."""
self.test_load_config()
self.assertTrue(EnergyRecorder.stop())
delete_mock.assert_called_once_with(
EnergyRecorder.energy_recorder_api["uri"],
auth=EnergyRecorder.energy_recorder_api["auth"],
headers=self.request_headers
)
@mock.patch('functest.energy.energy.requests.delete',
return_value=RECORDER_KO)
def test_stop_api_error(self, delete_mock=None):
"""EnergyRecorder.stop method (API Error)."""
self.test_load_config()
self.assertFalse(EnergyRecorder.stop())
delete_mock.assert_called_once_with(
EnergyRecorder.energy_recorder_api["uri"],
auth=EnergyRecorder.energy_recorder_api["auth"],
headers=self.request_headers
)
@mock.patch('functest.energy.energy.requests.delete',
side_effect=Exception("Internal execution error (MOCK)"))
def test_stop_error(self, delete_mock=None):
"""EnergyRecorder.stop method (method error)."""
self.test_load_config()
self.assertFalse(EnergyRecorder.stop())
delete_mock.assert_called_once_with(
EnergyRecorder.energy_recorder_api["uri"],
auth=EnergyRecorder.energy_recorder_api["auth"],
headers=self.request_headers
)
@energy.enable_recording
def __decorated_method(self):
"""Call with to energy recorder decorators."""
return self.returned_value_to_preserve
@energy.enable_recording
def __decorated_method_with_ex(self):
"""Call with to energy recorder decorators."""
raise Exception(self.exception_message_to_preserve)
@mock.patch("functest.energy.energy.EnergyRecorder.get_current_scenario",
return_value=None)
@mock.patch("functest.energy.energy.EnergyRecorder")
@mock.patch("functest.utils.functest_utils.get_pod_name",
return_value="MOCK_POD")
@mock.patch("functest.utils.functest_utils.get_functest_config",
side_effect=config_loader_mock)
def test_decorators(self,
loader_mock=None,
pod_mock=None,
recorder_mock=None,
cur_scenario_mock=None):
"""Test energy module decorators."""
self.__decorated_method()
calls = [mock.call.start(self.case_name),
mock.call.stop()]
recorder_mock.assert_has_calls(calls)
@mock.patch("functest.energy.energy.EnergyRecorder.get_current_scenario",
return_value={"scenario": PREVIOUS_SCENARIO,
"step": PREVIOUS_STEP})
@mock.patch("functest.energy.energy.EnergyRecorder")
@mock.patch("functest.utils.functest_utils.get_pod_name",
return_value="MOCK_POD")
@mock.patch("functest.utils.functest_utils.get_functest_config",
side_effect=config_loader_mock)
def test_decorators_with_previous(self,
loader_mock=None,
pod_mock=None,
recorder_mock=None,
cur_scenario_mock=None):
"""Test energy module decorators."""
self.__decorated_method()
calls = [mock.call.start(self.case_name),
mock.call.submit_scenario(PREVIOUS_SCENARIO,
PREVIOUS_STEP)]
recorder_mock.assert_has_calls(calls)
def test_decorator_preserve_return(self):
"""Test that decorator preserve method returned value."""
self.test_load_config()
self.assertTrue(
self.__decorated_method() == self.returned_value_to_preserve
)
def test_decorator_preserve_ex(self):
"""Test that decorator preserve method exceptions."""
self.test_load_config()
with self.assertRaises(Exception) as context:
self.__decorated_method_with_ex()
self.assertTrue(
self.exception_message_to_preserve in context.exception
)
@mock.patch("functest.utils.functest_utils.get_functest_config",
side_effect=config_loader_mock)
@mock.patch("functest.utils.functest_utils.get_pod_name",
return_value="MOCK_POD")
def test_load_config(self, loader_mock=None, pod_mock=None):
"""Test load config."""
EnergyRecorder.energy_recorder_api = None
EnergyRecorder.load_config()
self.assertEquals(
EnergyRecorder.energy_recorder_api["auth"],
("user", "password")
)
self.assertEquals(
EnergyRecorder.energy_recorder_api["uri"],
"http://pod-uri:8888/recorders/environment/MOCK_POD"
)
@mock.patch("functest.utils.functest_utils.get_functest_config",
side_effect=config_loader_mock_no_creds)
@mock.patch("functest.utils.functest_utils.get_pod_name",
return_value="MOCK_POD")
def test_load_config_no_creds(self, loader_mock=None, pod_mock=None):
"""Test load config without creds."""
EnergyRecorder.energy_recorder_api = None
EnergyRecorder.load_config()
self.assertEquals(EnergyRecorder.energy_recorder_api["auth"], None)
self.assertEquals(
EnergyRecorder.energy_recorder_api["uri"],
"http://pod-uri:8888/recorders/environment/MOCK_POD"
)
@mock.patch("functest.utils.functest_utils.get_functest_config",
return_value=None)
@mock.patch("functest.utils.functest_utils.get_pod_name",
return_value="MOCK_POD")
def test_load_config_ex(self, loader_mock=None, pod_mock=None):
"""Test load config with exception."""
with self.assertRaises(AssertionError):
EnergyRecorder.energy_recorder_api = None
EnergyRecorder.load_config()
self.assertEquals(EnergyRecorder.energy_recorder_api, None)
@mock.patch("functest.utils.functest_utils.get_functest_config",
return_value=None)
@mock.patch("functest.utils.functest_utils.get_pod_name",
return_value="MOCK_POD")
@mock.patch('functest.energy.energy.requests.get',
return_value=RECORDER_OK)
def test_get_current_scenario(self, loader_mock=None,
pod_mock=None, get_mock=None):
"""Test get_current_scenario."""
self.test_load_config()
scenario = EnergyRecorder.get_current_scenario()
self.assertTrue(scenario is not None)
if __name__ == "__main__":
logging.disable(logging.CRITICAL)
unittest.main(verbosity=2)
|