Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
S2-group
GitHub Repository: S2-group/android-runner
Path: blob/master/tests/PluginTests.py
629 views
1
import inspect
2
import logging
3
import os
4
import time
5
import traceback
6
7
from AndroidRunner.Devices import Devices
8
from AndroidRunner.PluginHandler import PluginHandler
9
from AndroidRunner.util import makedirs
10
import paths
11
12
13
# noinspection PyUnusedLocal
14
class PluginTests(object):
15
16
def __init__(self, config):
17
self.logger = logging.getLogger(self.__class__.__name__)
18
self.errors = []
19
self.config = config
20
adb_path = config.get('adb_path', 'adb')
21
self.devices = Devices(config['devices'], adb_path=adb_path)
22
self.profilers = None
23
self.output_root = paths.OUTPUT_DIR
24
self.result_file = os.path.join(self.output_root, 'Test_results.txt')
25
self.dirs = {}
26
27
@staticmethod
28
def get_progress_xml_file():
29
return "Testing, no progress file has been made"
30
31
def check_profilers(self):
32
self.check_init_profilers()
33
default_profilers = ['android', 'trepn', 'monsoon']
34
for profiler in self.profilers:
35
if profiler.name.lower() not in default_profilers:
36
self.check_profiler(profiler.currentProfiler, profiler.name)
37
38
def check_init_profilers(self):
39
self.profilers = []
40
for name, params in list(self.config.get('profilers', {}).items()):
41
try:
42
self.profilers.append(PluginHandler(name, params))
43
except Exception:
44
self.errors.append('Profiler {}: Initializing profiler resulted in the following error:\n{}'.
45
format(name, traceback.format_exc()))
46
47
def check_profiler(self, profiler, profiler_name):
48
profiler_parent = inspect.getmro(type(profiler))[1]
49
profiler_parent_module = profiler_parent.__module__
50
profiler_parent_name = profiler_parent.__name__
51
if '.Profiler' in profiler_parent_module and profiler_parent_name == 'Profiler':
52
self.check_profiler_methods(profiler, profiler_name)
53
else:
54
self.errors.append('Profiler {}: doesn\'t have the \'Profiler\' '
55
'as parent class, plugin can\'t be tested'.format(profiler_name))
56
57
def check_profiler_methods(self, profiler, profiler_name):
58
device = self.check_profiler_dependencies(profiler, profiler_name)
59
if device is not None:
60
self.set_dirs(device, profiler_name)
61
methods = ['load', 'set_output', 'start_profiling', 'stop_profiling', 'collect_results',
62
'aggregate_subject', 'unload', 'aggregate_end']
63
64
for current_method in methods:
65
self.check_profiler_method(device, profiler, current_method, profiler_name)
66
67
def set_dirs(self, device, profiler_name):
68
self.dirs['subject'] = os.path.join(self.output_root, 'data', device.name, 'test_dir_1', 'test_dir_2',
69
profiler_name)
70
self.dirs['aggregated'] = os.path.join(paths.OUTPUT_DIR, '{}_aggregated.csv'.format(profiler_name))
71
self.dirs['data_folder'] = os.path.join(paths.OUTPUT_DIR, 'data')
72
makedirs(self.dirs['subject'])
73
74
def check_profiler_method(self, device, profiler, current_method, profiler_name):
75
try:
76
if current_method == 'set_output':
77
method_result = getattr(profiler, current_method)(self.dirs['subject'])
78
elif current_method == 'stop_profiling':
79
time.sleep(5)
80
method_result = getattr(profiler, current_method)(device)
81
time.sleep(5)
82
elif current_method == 'aggregate_subject':
83
method_result = getattr(profiler, current_method)()
84
elif current_method == 'aggregate_end':
85
method_result = getattr(profiler, current_method)(self.dirs['data_folder'], self.dirs['aggregated'])
86
else:
87
method_result = getattr(profiler, current_method)(device)
88
89
if method_result is not None:
90
self.errors.append("Profiler {}: Method {} gives non expected return value.".
91
format(profiler_name, current_method))
92
except NotImplementedError:
93
self.errors.append('Profiler {}: Method {} not implemented.'.format(profiler_name, current_method))
94
except Exception:
95
self.errors.append('Profiler {}: Method {} gave the following error: \n{}'
96
.format(profiler_name, current_method, traceback.format_exc()))
97
98
def check_profiler_dependencies(self, profiler, profiler_name):
99
method = 'dependencies()'
100
try:
101
method_result = profiler.dependencies()
102
self.check_dependencies(method_result, profiler_name)
103
except NotImplementedError:
104
self.errors.append('Profiler {}: Method {} not implemented.'.format(profiler_name, method))
105
except Exception:
106
self.errors.append('Profiler {}: Method {} gave the following error: \n{}'
107
.format(profiler_name, method, traceback.format_exc()))
108
109
device = None
110
try:
111
for current_device in self.devices:
112
installed_apps = current_device.is_installed(profiler.dependencies())
113
not_installed_apps = [name for name, installed in list(installed_apps.items()) if not installed]
114
if len(not_installed_apps) == 0:
115
device = current_device
116
break
117
finally:
118
if device is None:
119
self.errors.append('Profiler {}: plugin not further tested, '
120
'no device available that meets the dependencies. '
121
'Check devices and dependencies'.format(profiler_name))
122
123
def check_dependencies(self, dependencies, profiler_name):
124
if isinstance(dependencies, list):
125
for dependency in dependencies:
126
if isinstance(dependency, str):
127
if len(dependency.split(".")) == 3:
128
continue
129
else:
130
self.errors.append('Profiler {}: dependency \'{}\' has an invalid format'
131
.format(profiler_name, dependency))
132
else:
133
self.errors.append('Profiler {}: invalid object in dependency list'
134
.format(profiler_name))
135
else:
136
self.errors.append('Profiler {}: return value of dependencies() not a list object'.format(profiler_name))
137
138
def format_errors(self):
139
result_string = ''
140
if len(self.errors) > 0:
141
result_string += '{} Errors found during testing: \n'.format(len(self.errors))
142
for error in self.errors:
143
result_string += '\n{}'.format(error)
144
else:
145
result_string += "No errors found during testing"
146
return result_string
147
148
def write_to_file(self, formatted_errors):
149
with open(self.result_file, 'w') as f:
150
f.write(formatted_errors)
151
152
def start(self):
153
self.check_profilers()
154
formated_errors = self.format_errors()
155
self.write_to_file(formated_errors)
156
print('\n{}'.format(formated_errors))
157
print('\nTest results saved to file: {}'.format(self.result_file))
158
159