Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
S2-group
GitHub Repository: S2-group/android-runner
Path: blob/master/tests/unit/test_profilers.py
908 views
1
import os
2
from shutil import copyfile
3
4
import pytest
5
from mock import MagicMock, Mock, patch, call
6
7
import paths
8
from AndroidRunner.PluginHandler import PluginHandler
9
from AndroidRunner.Profilers import Profilers
10
from AndroidRunner.util import load_json, makedirs
11
12
13
class TestProfilers(object):
14
15
@pytest.fixture()
16
@patch('AndroidRunner.PluginHandler.PluginHandler.__init__')
17
def profilers(self, mock):
18
mock.return_value = None
19
params = {"test1": "waarde1", "test2": "waarde2"}
20
config = {"testPlugin": params}
21
return Profilers(config)
22
23
@patch('AndroidRunner.PluginHandler.PluginHandler.__init__')
24
def test_init(self, mock):
25
mock.return_value = None
26
params = {"test1": "waarde1", "test2": "waarde2"}
27
config = {"testPlugin": params}
28
current_profiler = Profilers(config)
29
mock.assert_called_once_with('testPlugin', params)
30
assert len(current_profiler.profilers) == 1
31
assert type(current_profiler.profilers[0]) == PluginHandler
32
33
@patch('AndroidRunner.PluginHandler.PluginHandler.__init__')
34
def test_init_error(self, mock):
35
mock.side_effect = ImportError
36
params = {"test1": "waarde1", "test2": "waarde2"}
37
config = {"testPlugin": params}
38
with pytest.raises(ImportError):
39
Profilers(config)
40
41
def test_dependencies(self, profilers):
42
profiler1 = Mock()
43
profiler1.dependencies.return_value = ["dependencie1"]
44
profiler2 = Mock()
45
profiler2.dependencies.return_value = ["dependencie2"]
46
m = MagicMock()
47
m.__iter__.return_value = [profiler1, profiler2]
48
profilers.profilers = m
49
dependencies = profilers.dependencies()
50
assert len(dependencies) == 2
51
assert "dependencie1" in dependencies
52
assert "dependencie2" in dependencies
53
54
def test_load_empty(self, profilers):
55
fake_device = Mock
56
fake_device.name = "fake_device"
57
profiler1 = Mock()
58
profiler2 = Mock()
59
m = MagicMock()
60
m.__iter__.return_value = [profiler1, profiler2]
61
profilers.profilers = m
62
63
assert len(profilers.loaded_devices) == 0
64
profilers.load(fake_device)
65
profiler1.load.assert_called_once_with(fake_device)
66
profiler2.load.assert_called_once_with(fake_device)
67
assert len(profilers.loaded_devices) == 1
68
assert profilers.loaded_devices[0] == "fake_device"
69
70
def test_load_non_empty(self, profilers):
71
fake_device = Mock
72
fake_device.name = "fake_device"
73
profiler1 = Mock()
74
profiler2 = Mock()
75
m = MagicMock()
76
m.__iter__.return_value = [profiler1, profiler2]
77
profilers.profilers = m
78
profilers.loaded_devices.append("fake_device")
79
80
assert len(profilers.loaded_devices) == 1
81
profilers.load(fake_device)
82
assert profiler1.load.call_count == 0
83
assert profiler2.load.call_count == 0
84
assert len(profilers.loaded_devices) == 1
85
86
def test_start_profiling(self, profilers):
87
fake_device = Mock()
88
profiler1 = Mock()
89
profiler2 = Mock()
90
m = MagicMock()
91
m.__iter__.return_value = [profiler1, profiler2]
92
profilers.profilers = m
93
profilers.start_profiling(fake_device, arg1="arg1", arg2="arg2")
94
profiler1.start_profiling.assert_called_once_with(fake_device, arg1="arg1", arg2="arg2")
95
profiler2.start_profiling.assert_called_once_with(fake_device, arg1="arg1", arg2="arg2")
96
97
def test_stop_profiling(self, profilers):
98
fake_device = Mock()
99
profiler1 = Mock()
100
profiler2 = Mock()
101
m = MagicMock()
102
m.__iter__.return_value = [profiler1, profiler2]
103
profilers.profilers = m
104
profilers.stop_profiling(fake_device, arg1="arg1", arg2="arg2")
105
profiler1.stop_profiling.assert_called_once_with(fake_device, arg1="arg1", arg2="arg2")
106
profiler2.stop_profiling.assert_called_once_with(fake_device, arg1="arg1", arg2="arg2")
107
108
def test_collect_results(self, profilers):
109
fake_device = Mock()
110
profiler1 = Mock()
111
profiler2 = Mock()
112
m = MagicMock()
113
m.__iter__.return_value = [profiler1, profiler2]
114
profilers.profilers = m
115
profilers.collect_results(fake_device)
116
profiler1.collect_results.assert_called_once_with(fake_device)
117
profiler2.collect_results.assert_called_once_with(fake_device)
118
119
def test_unload(self, profilers):
120
fake_device = Mock()
121
profiler1 = Mock()
122
profiler2 = Mock()
123
m = MagicMock()
124
m.__iter__.return_value = [profiler1, profiler2]
125
profilers.profilers = m
126
profilers.unload(fake_device)
127
profiler1.unload.assert_called_once_with(fake_device)
128
profiler2.unload.assert_called_once_with(fake_device)
129
130
def test_set_output(self, profilers):
131
profiler1 = Mock()
132
profiler2 = Mock()
133
m = MagicMock()
134
m.__iter__.return_value = [profiler1, profiler2]
135
profilers.profilers = m
136
profilers.set_output()
137
profiler1.set_output.assert_called_once_with()
138
profiler2.set_output.assert_called_once_with()
139
140
def test_aggregate_subject(self, profilers):
141
profiler1 = Mock()
142
profiler2 = Mock()
143
m = MagicMock()
144
m.__iter__.return_value = [profiler1, profiler2]
145
profilers.profilers = m
146
profilers.aggregate_subject()
147
profiler1.aggregate_subject.assert_called_once()
148
profiler2.aggregate_subject.assert_called_once()
149
150
def test_aggregate_end(self, profilers):
151
profiler1 = Mock()
152
profiler2 = Mock()
153
m = MagicMock()
154
m.__iter__.return_value = [profiler1, profiler2]
155
profilers.profilers = m
156
profilers.aggregate_end("fake/dir/path")
157
profiler1.aggregate_data_end.assert_called_once_with("fake/dir/path")
158
profiler2.aggregate_data_end.assert_called_once_with("fake/dir/path")
159
160
161
class TestPluginHandler(object):
162
@pytest.fixture()
163
def fixture_dir(self):
164
fixture_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "fixtures")
165
return fixture_dir
166
167
@pytest.fixture()
168
def android_test_plugin_handler(self, tmpdir, fixture_dir):
169
android_config = load_json(os.path.join(fixture_dir, 'test_config.json'))['profilers']['android']
170
tmpdir = str(tmpdir)
171
paths.CONFIG_DIR = tmpdir
172
os.makedirs(os.path.join(tmpdir, 'Plugins'))
173
copyfile(os.path.join(fixture_dir, 'Android1.py'), os.path.join(tmpdir, 'Plugins', 'Android1.py'))
174
plugin_handler = PluginHandler('Android1', android_config)
175
return plugin_handler
176
177
@patch("pluginbase.PluginBase.make_plugin_source")
178
@patch("pluginbase.PluginBase.__init__")
179
def test_handler_init_default(self, plugin_base_init_mock, make_plugin_source_mock, fixture_dir):
180
plugin_base_init_mock.return_value = None
181
mock_loaded_plugin = Mock()
182
mock_plugin_source = Mock()
183
mock_plugin_source.load_plugin.return_value = mock_loaded_plugin
184
make_plugin_source_mock.return_value = mock_plugin_source
185
186
android_config = load_json(os.path.join(fixture_dir, 'test_config.json'))['profilers']['android']
187
PluginHandler('Android', android_config)
188
plugin_base_init_mock.assert_called_once()
189
make_plugin_source_mock.assert_called_once_with(
190
searchpath=[os.path.join(paths.ROOT_DIR, 'AndroidRunner', 'Plugins', 'android')])
191
mock_plugin_source.load_plugin.assert_called_once_with('Android')
192
193
def test_handler_init_plugin(self, tmpdir, fixture_dir):
194
android_config = load_json(os.path.join(fixture_dir, 'test_config.json'))['profilers']['android']
195
tmpdir = str(tmpdir)
196
paths.CONFIG_DIR = tmpdir
197
os.makedirs(os.path.join(tmpdir, 'Plugins'))
198
copyfile(os.path.join(fixture_dir, 'Android1.py'), os.path.join(tmpdir, 'Plugins', 'Android1.py'))
199
plugin_handler = PluginHandler('Android1', android_config)
200
assert plugin_handler.currentProfiler.__class__.__name__ == 'Android1'
201
assert plugin_handler.currentProfiler.data_points == ['cpu', 'mem']
202
203
def test_handler_init_error(self, tmpdir):
204
paths.CONFIG_DIR = str(tmpdir)
205
with pytest.raises(ImportError):
206
PluginHandler('android2', dict())
207
208
def test_dependencies_empty(self, android_test_plugin_handler):
209
mock_profiler = Mock()
210
mock_profiler.dependencies.return_value = []
211
android_test_plugin_handler.currentProfiler = mock_profiler
212
assert android_test_plugin_handler.dependencies() == []
213
214
def test_dependencies_non_empty(self, android_test_plugin_handler):
215
mock_profiler = Mock()
216
mock_profiler.dependencies.return_value = ['test.dependencie']
217
android_test_plugin_handler.currentProfiler = mock_profiler
218
assert android_test_plugin_handler.dependencies() == ['test.dependencie']
219
220
def test_load(self, android_test_plugin_handler):
221
fake_device = Mock()
222
mock_profiler = Mock()
223
android_test_plugin_handler.currentProfiler = mock_profiler
224
225
android_test_plugin_handler.load(fake_device)
226
227
mock_profiler.load.assert_called_once_with(fake_device)
228
229
def test_start_profiling(self, android_test_plugin_handler):
230
fake_device = Mock()
231
mock_profiler = Mock()
232
android_test_plugin_handler.currentProfiler = mock_profiler
233
234
android_test_plugin_handler.start_profiling(fake_device, arg1="arg1", arg2="arg2")
235
236
mock_profiler.start_profiling.assert_called_once_with(fake_device, arg1="arg1", arg2="arg2")
237
238
def test_stop_profiling(self, android_test_plugin_handler):
239
fake_device = Mock()
240
mock_profiler = Mock()
241
android_test_plugin_handler.currentProfiler = mock_profiler
242
243
android_test_plugin_handler.stop_profiling(fake_device, arg1="arg1", arg2="arg2")
244
245
mock_profiler.stop_profiling.assert_called_once_with(fake_device, arg1="arg1", arg2="arg2")
246
247
def test_unload(self, android_test_plugin_handler):
248
fake_device = Mock()
249
mock_profiler = Mock()
250
android_test_plugin_handler.currentProfiler = mock_profiler
251
252
android_test_plugin_handler.unload(fake_device)
253
254
mock_profiler.unload.assert_called_once_with(fake_device)
255
256
def test_set_output(self, android_test_plugin_handler, tmpdir):
257
tmpdir = str(tmpdir)
258
paths.OUTPUT_DIR = tmpdir
259
mock_profiler = Mock()
260
android_test_plugin_handler.currentProfiler = mock_profiler
261
262
android_test_plugin_handler.set_output()
263
264
mock_profiler.set_output.assert_called_once_with(os.path.join(tmpdir, 'Android1'))
265
assert os.path.isdir(os.path.join(tmpdir, 'Android1'))
266
267
def test_list_dir_empty(self, tmpdir, android_test_plugin_handler):
268
tmpdir = os.path.join(str(tmpdir), 'test')
269
makedirs(tmpdir)
270
assert android_test_plugin_handler.list_subdir(tmpdir) == []
271
272
def test_list_dir_non_empty(self, tmpdir, android_test_plugin_handler):
273
tmpdir = os.path.join(str(tmpdir), 'test')
274
makedirs(tmpdir)
275
os.makedirs(os.path.join(tmpdir, '1'))
276
os.makedirs(os.path.join(tmpdir, '2'))
277
sub_dirs = android_test_plugin_handler.list_subdir(tmpdir)
278
assert len(sub_dirs) == 2
279
assert '1' in sub_dirs
280
assert '2' in sub_dirs
281
282
def test_collect_result(self, android_test_plugin_handler):
283
fake_device = Mock()
284
mock_profiler = Mock()
285
android_test_plugin_handler.currentProfiler = mock_profiler
286
287
android_test_plugin_handler.collect_results(fake_device)
288
289
mock_profiler.collect_results.assert_called_once_with(fake_device)
290
291
@patch('AndroidRunner.Python3.Python3.__init__')
292
def test_aggregate_subject_no_selection(self, python, android_test_plugin_handler):
293
mock_profiler = Mock()
294
android_test_plugin_handler.currentProfiler = mock_profiler
295
android_test_plugin_handler.pluginParams = {}
296
297
android_test_plugin_handler.aggregate_subject()
298
299
mock_profiler.aggregate_subject.assert_called_once()
300
assert python.call_count == 0
301
assert android_test_plugin_handler.subject_aggregated
302
assert android_test_plugin_handler.subject_aggregated_default
303
304
@patch('AndroidRunner.Python3.Python3.__init__')
305
def test_aggregate_subject_default(self, python, android_test_plugin_handler):
306
mock_profiler = Mock()
307
android_test_plugin_handler.currentProfiler = mock_profiler
308
android_test_plugin_handler.pluginParams = {'subject_aggregation': 'default'}
309
android_test_plugin_handler.aggregate_subject()
310
311
mock_profiler.aggregate_subject.assert_called_once()
312
assert python.call_count == 0
313
assert android_test_plugin_handler.subject_aggregated
314
assert android_test_plugin_handler.subject_aggregated_default
315
316
@patch('AndroidRunner.Python3.Python3.__init__')
317
def test_aggregate_subject_none(self, python, android_test_plugin_handler):
318
mock_profiler = Mock()
319
android_test_plugin_handler.currentProfiler = mock_profiler
320
android_test_plugin_handler.pluginParams = {'subject_aggregation': 'none'}
321
322
android_test_plugin_handler.aggregate_subject()
323
324
assert mock_profiler.aggregate_subject.call_count == 0
325
assert python.call_count == 0
326
assert not android_test_plugin_handler.subject_aggregated
327
assert not android_test_plugin_handler.subject_aggregated_default
328
329
@patch('AndroidRunner.Python3.Python3.__init__')
330
@patch('AndroidRunner.Python3.Python3.run')
331
def test_aggregate_subject_user_script(self, python_run, python_init, android_test_plugin_handler):
332
python_init.return_value = None
333
mock_profiler = Mock()
334
android_test_plugin_handler.currentProfiler = mock_profiler
335
android_test_plugin_handler.pluginParams = {'subject_aggregation': 'user_script'}
336
337
android_test_plugin_handler.aggregate_subject()
338
339
assert mock_profiler.aggregate_subject.call_count == 0
340
python_init.assert_called_once_with(os.path.join(paths.CONFIG_DIR, 'user_script'))
341
python_run.assert_called_once_with(None, paths.OUTPUT_DIR)
342
assert android_test_plugin_handler.subject_aggregated
343
assert not android_test_plugin_handler.subject_aggregated_default
344
345
@patch('AndroidRunner.PluginHandler.PluginHandler.aggregate_subjects_default')
346
@patch('AndroidRunner.Python3.Python3.__init__')
347
def test_aggregate_data_end_none(self, python, aggregate_subjects, android_test_plugin_handler):
348
android_test_plugin_handler.pluginParams = {'experiment_aggregation': 'none'}
349
mock_profiler = Mock()
350
android_test_plugin_handler.currentProfiler = mock_profiler
351
android_test_plugin_handler.aggregate_data_end('fake/dir/')
352
assert mock_profiler.aggregate_end.call_count == 0
353
assert python.call_count == 0
354
assert aggregate_subjects.call_count == 0
355
356
@patch('AndroidRunner.PluginHandler.PluginHandler.aggregate_subjects_default')
357
@patch('AndroidRunner.Python3.Python3.__init__')
358
def test_aggregate_data_end_default_subject_default(self, python, aggregate_subjects, android_test_plugin_handler):
359
android_test_plugin_handler.pluginParams = {'experiment_aggregation': 'default'}
360
mock_profiler = Mock()
361
android_test_plugin_handler.subject_aggregated_default = True
362
android_test_plugin_handler.subject_aggregated = True
363
android_test_plugin_handler.currentProfiler = mock_profiler
364
android_test_plugin_handler.aggregate_data_end('fake/dir/')
365
366
mock_profiler.aggregate_end.assert_called_once_with('fake/dir/data', 'fake/dir/Aggregated_Results_Android1.csv')
367
assert python.call_count == 0
368
assert aggregate_subjects.call_count == 0
369
370
@patch('AndroidRunner.PluginHandler.PluginHandler.aggregate_subjects_default')
371
@patch('AndroidRunner.Python3.Python3.__init__')
372
def test_aggregate_data_end_default_no_subject(self, python, aggregate_subjects, android_test_plugin_handler):
373
android_test_plugin_handler.pluginParams = {'experiment_aggregation': 'default'}
374
mock_profiler = Mock()
375
mock_manager = Mock()
376
mock_manager.attach_mock(mock_profiler.aggregate_end, 'end')
377
mock_manager.attach_mock(aggregate_subjects, 'subjects')
378
android_test_plugin_handler.subject_aggregated_default = False
379
android_test_plugin_handler.subject_aggregated = False
380
android_test_plugin_handler.currentProfiler = mock_profiler
381
android_test_plugin_handler.aggregate_data_end('fake/dir/')
382
383
expected_call_order = "[call.subjects('fake/dir/data'),\n call.end('fake/dir/data', " \
384
"'fake/dir/Aggregated_Results_Android1.csv')]"
385
assert expected_call_order == str(mock_manager.mock_calls)
386
mock_profiler.aggregate_end.assert_called_once_with('fake/dir/data', 'fake/dir/Aggregated_Results_Android1.csv')
387
assert python.call_count == 0
388
aggregate_subjects.assert_called_once()
389
390
@patch('AndroidRunner.PluginHandler.PluginHandler.aggregate_subjects_default')
391
@patch('AndroidRunner.Python3.Python3.__init__')
392
def test_aggregate_data_end_default_user_aggregated(self, python, aggregate_subjects, android_test_plugin_handler):
393
android_test_plugin_handler.pluginParams = {'experiment_aggregation': 'default'}
394
mock_profiler = Mock()
395
android_test_plugin_handler.subject_aggregated_default = False
396
android_test_plugin_handler.subject_aggregated = True
397
android_test_plugin_handler.currentProfiler = mock_profiler
398
android_test_plugin_handler.aggregate_data_end('fake/dir/')
399
400
assert mock_profiler.aggregate_end.call_count == 0
401
assert python.call_count == 0
402
assert aggregate_subjects.call_count == 0
403
404
@patch('AndroidRunner.PluginHandler.PluginHandler.aggregate_subjects_default')
405
@patch('AndroidRunner.Python3.Python3.run')
406
@patch('AndroidRunner.Python3.Python3.__init__')
407
def test_aggregate_data_end_user_script(self, python_init, python_run, aggregate_subjects,
408
android_test_plugin_handler):
409
python_init.return_value = None
410
android_test_plugin_handler.pluginParams = {'experiment_aggregation': 'user_script'}
411
mock_profiler = Mock()
412
android_test_plugin_handler.currentProfiler = mock_profiler
413
android_test_plugin_handler.aggregate_data_end('fake/dir/')
414
415
assert mock_profiler.aggregate_end.call_count == 0
416
python_init.assert_called_once_with(os.path.join(paths.CONFIG_DIR, 'user_script'))
417
python_run.assert_called_once_with(None, 'fake/dir/data', 'fake/dir/Aggregated_Results_Android1.csv')
418
assert aggregate_subjects.call_count == 0
419
420
def test_aggregate_subjects_default_no_data(self, android_test_plugin_handler, tmpdir):
421
tmpdir = str(tmpdir)
422
android_test_plugin_handler.aggregate_subjects_default(tmpdir)
423
424
@staticmethod
425
def make_paths(tmp_dir, paths_end):
426
complete_paths = []
427
for path_end in paths_end:
428
complete_path = os.path.join(tmp_dir, path_end)
429
makedirs(complete_path)
430
complete_paths.append(complete_path)
431
return complete_paths
432
433
def test_aggregate_subject_default_native_experiment(self, android_test_plugin_handler, tmpdir):
434
tmpdir = str(tmpdir)
435
paths_ends = ['device1/native1/Android1', 'device1/native2/Android1', 'device1/native3/Android1']
436
created_paths = self.make_paths(tmpdir, paths_ends)
437
mock_profiler = Mock()
438
android_test_plugin_handler.currentProfiler = mock_profiler
439
440
android_test_plugin_handler.aggregate_subjects_default(tmpdir)
441
442
assert mock_profiler.set_output.call_count == 3
443
mock_profiler.set_output.assert_has_calls([call(created_paths[2]), call(created_paths[1]), call(created_paths[0])])
444
assert mock_profiler.aggregate_subject.call_count == 3
445
446
def test_aggregate_subject_default_web_experiment(self, android_test_plugin_handler, tmpdir):
447
tmpdir = str(tmpdir)
448
paths_ends = ['device1/subject1/browser1/Android1', 'device1/subject2/browser1/Android1',
449
'device1/subject3/browser1/Android1']
450
created_paths = self.make_paths(tmpdir, paths_ends)
451
452
mock_profiler = Mock()
453
android_test_plugin_handler.currentProfiler = mock_profiler
454
455
android_test_plugin_handler.aggregate_subjects_default(tmpdir)
456
457
assert mock_profiler.set_output.call_count == 3
458
mock_profiler.set_output.assert_has_calls([call(created_paths[2]), call(created_paths[1]), call(created_paths[0])])
459
assert mock_profiler.aggregate_subject.call_count == 3
460
461