def build_preferences(self, params): """Build the XML files to setup Trepn and the data points""" current_dir = op.dirname(op.realpath(__file__)) # lxml is not the most secure parser, it is up to the user for valid configurations # https://docs.python.org/2/library/xml.html#xml-vulnerabilities self.pref_dir = op.join(self.paths['OUTPUT_DIR'], 'trepn.pref/') util.makedirs(self.pref_dir) preferences_file = et.parse(op.join(current_dir, 'preferences.xml')) preferences_file = self.override_preferences(params, preferences_file) preferences_file.write(op.join(self.pref_dir, 'com.quicinc.trepn_preferences.xml'), encoding='utf-8', xml_declaration=True, standalone=True) datapoints_file = et.parse(op.join(current_dir, 'data_points.xml')) dp_root = datapoints_file.getroot() data_points_dict = util.load_json( op.join(current_dir, 'data_points.json')) for dp in params['data_points']: dp = str(data_points_dict[dp]) self.data_points.append(dp) dp_root.append(et.Element('int', {'name': dp, 'value': dp})) datapoints_file.write(op.join( self.pref_dir, 'com.quicinc.preferences.saved_data_points.xml'), encoding='utf-8', xml_declaration=True, standalone=True)
def test_load_json_succes(self, tmp_file): fixtures = op.join(op.dirname(op.realpath(__file__)), "fixtures") config = util.load_json(op.join(fixtures, 'test_config.json')) assert config['type'] == 'web' assert config['devices'] == ['nexus6p'] assert config['randomization'] == False assert config['repetitions'] == 3
def __init__(self, config, paths): super(Batterystats, self).__init__(config, paths) self.output_dir = '' self.paths = paths self.profile = False self.cleanup = config.get('cleanup') self.enable_systrace_parsing = config.get('enable_systrace_parsing', True) self.python2_path = config.get('python2_path', 'python2') # "config" only passes the fields under "profilers", so config.json is loaded again for the fields below # FIX config_f = util.load_json( op.join(self.paths["CONFIG_DIR"], self.paths['ORIGINAL_CONFIG_DIR'])) self.type = config_f['type'] self.systrace = config_f.get('systrace_path', 'systrace') self.powerprofile = config_f['powerprofile_path'] self.duration = Tests.is_integer(config_f.get('duration', 0)) / 1000 if self.type == 'web': self.browsers = [ BrowserFactory.get_browser(b)(config_f) for b in config_f.get('browsers', ['chrome']) ] if os.path.exists( self.systrace ): # If it does not exist, then there might be a prefix already added to the path self.systrace = ' '.join([self.python2_path, self.systrace]) else: print("Did not prefix python2 path to systrace path due to the systrace path not existing. " + \ "This is fine if you added a prefix path yourself, if not, double check the systrace_path inside of your config and make sure it exists.")
def test_progress_init(self, build_progress_mock, write_to_file_mock, tmp_path, test_config, test_progress): with open(test_progress, 'r') as f: expected_xml = f.read() paths.OUTPUT_DIR = tmp_path.as_posix() build_progress_mock.return_value = et.fromstring(expected_xml) mock_manager = Mock() mock_manager.attach_mock(build_progress_mock, 'managed_build_progress') mock_manager.attach_mock(write_to_file_mock, 'managed_write_to_file') progress = Progress(config_file=test_config, config=load_json(test_config)) expected_calls = [call.managed_build_progress(load_json(test_config), test_config), call.managed_write_to_file()] assert mock_manager.mock_calls == expected_calls expected_lxml = et.fromstring(expected_xml) current_lxml = progress.progress_xml_content assert self.elements_equal(current_lxml, expected_lxml)
def android_test_plugin_handler(self, tmpdir, fixture_dir): android_config = load_json(os.path.join(fixture_dir, 'test_config.json'))['profilers']['android'] tmpdir = str(tmpdir) paths.CONFIG_DIR = tmpdir os.makedirs(os.path.join(tmpdir, 'Plugins')) copyfile(os.path.join(fixture_dir, 'Android1.py'), os.path.join(tmpdir, 'Plugins', 'Android1.py')) plugin_handler = PluginHandler('Android1', android_config) return plugin_handler
def test_handler_init_plugin(self, tmpdir, fixture_dir): android_config = load_json(os.path.join(fixture_dir, 'test_config.json'))['profilers']['android'] tmpdir = str(tmpdir) paths.CONFIG_DIR = tmpdir os.makedirs(os.path.join(tmpdir, 'Plugins')) copyfile(os.path.join(fixture_dir, 'Android1.py'), os.path.join(tmpdir, 'Plugins', 'Android1.py')) plugin_handler = PluginHandler('Android1', android_config) assert plugin_handler.currentProfiler.__class__.__name__ == 'Android1' assert plugin_handler.currentProfiler.data_points == ['cpu', 'mem']
def test_handler_init_default(self, plugin_base_init_mock, make_plugin_source_mock, fixture_dir): plugin_base_init_mock.return_value = None mock_loaded_plugin = Mock() mock_plugin_source = Mock() mock_plugin_source.load_plugin.return_value = mock_loaded_plugin make_plugin_source_mock.return_value = mock_plugin_source android_config = load_json(os.path.join(fixture_dir, 'test_config.json'))['profilers']['android'] PluginHandler('Android', android_config) plugin_base_init_mock.assert_called_once() make_plugin_source_mock.assert_called_once_with( searchpath=[os.path.join(paths.ROOT_DIR, 'AndroidRunner', 'Plugins', 'android')]) mock_plugin_source.load_plugin.assert_called_once_with('Android')
def __init__(self, config, paths): """ Inits the Perfetto class with config and paths params. Parameters ---------- config : collections.OrderedDict OrderedDictionary that contains the Perfetto plugin settings as provided by the given config.json file. - paths : dict Dictionary that contains the ROOT_DIR path, CONFIG_DIR path, OUTPUT_DIR path, ORIGINAL_CONFIG_DIR path and BASE_OUTPUT_DIR path. """ super(Perfetto, self).__init__(config, paths) self.paths = paths self.perfetto_trace_file_device_path = "" self.perfetto_config_file_local_path = config["config_file"] self.perfetto_config_file_format = config.get("config_file_format", "text") self.perfetto_config_file_device_path = "" self.adb_path = util.load_json(op.join(self.paths["CONFIG_DIR"], self.paths['ORIGINAL_CONFIG_DIR'])).get("adb_path", "adb")
def build_preferences(self, params): """Build the XML files to setup Trepn and the data points""" current_dir = op.dirname(op.realpath(__file__)) # lxml is not the most secure parser, it is up to the user for valid configurations # https://docs.python.org/2/library/xml.html#xml-vulnerabilities self.pref_dir = op.join(self.paths['OUTPUT_DIR'], 'trepn.pref/') util.makedirs(self.pref_dir) preferences_file = et.parse(op.join(current_dir, 'preferences.xml')) if 'preferences' in params: for i in preferences_file.getroot().iter(): if 'sample_interval' in params['preferences'] and \ i.get('name') == 'com.quicinc.preferences.general.profiling_interval': i.set('value', str(params['preferences']['sample_interval'])) if 'battery_power_source_selection' in params['preferences'] and \ i.get('name') == 'com.quicinc.preferences.battery_power_source_selection': i.text = params['preferences'][ 'battery_power_source_selection'] preferences_file.write(op.join(self.pref_dir, 'com.quicinc.trepn_preferences.xml'), encoding='utf-8', xml_declaration=True, standalone=True) datapoints_file = et.parse(op.join(current_dir, 'data_points.xml')) dp_root = datapoints_file.getroot() data_points_dict = util.load_json( op.join(current_dir, 'data_points.json')) for dp in params['data_points']: dp = str(data_points_dict[dp]) self.data_points.append(dp) dp_root.append(et.Element('int', {'name': dp, 'value': dp})) datapoints_file.write(op.join( self.pref_dir, 'com.quicinc.preferences.saved_data_points.xml'), encoding='utf-8', xml_declaration=True, standalone=True)
def test_load_json_file_permission_denied(self, tmp_file): os.chmod(tmp_file, 0o222) with pytest.raises(IOError) as except_result: util.load_json(tmp_file) assert "Permission denied" in str(except_result.value)
def test_load_json_file_file_not_found(self, tmp_file): fixtures = op.join(op.dirname(op.realpath(__file__)), "fixtures") with pytest.raises(util.FileNotFoundError) as except_result: util.load_json(op.join(fixtures, 'fake_file.json')) assert "FileNotFoundError" in str(except_result.typename)
def test_load_json_file_format_error(self, tmp_file): fixtures = op.join(op.dirname(op.realpath(__file__)), "fixtures") with pytest.raises(util.FileFormatError) as except_result: util.load_json(op.join(fixtures, 'test_progress.xml')) assert op.join(fixtures, 'test_progress.xml') in str(except_result.value)