def test_mozpower_processor_info_missing_error(): """Tests that the error MissingProcessorInfoError is raised when failures occur during processor information parsing. """ # The builtins module name differs between python 2 and 3 builtins_name = "__builtin__" if sys.version_info[0] == 3: builtins_name = "builtins" def os_side_effect_true(*args, **kwargs): """Used as a passing side effect for os.path.exists calls.""" return True def os_side_effect_false(*args, **kwargs): """Used as a failing side effect for os.path.exists calls.""" return False def subprocess_side_effect_fail(*args, **kwargs): """Used to mock a failure in subprocess.check_output calls.""" raise subprocess.CalledProcessError(1, "Testing failure") # Test failures in macos processor information parsing with mock.patch.object(MozPower, "_get_os", return_value="Darwin") as _: with mock.patch("os.path.exists") as os_mock: os_mock.side_effect = os_side_effect_false # Check that we fail properly if the processor # information file doesn't exist. with pytest.raises(MissingProcessorInfoError): MozPower() # Check that we fail properly when an error occurs # in the subprocess call. os_mock.side_effect = os_side_effect_true with mock.patch("subprocess.check_output") as subprocess_mock: subprocess_mock.side_effect = subprocess_side_effect_fail with pytest.raises(MissingProcessorInfoError): MozPower() # Test failures in linux processor information parsing with mock.patch.object(MozPower, "_get_os", return_value="Linux") as _: with mock.patch("os.path.exists") as os_mock: os_mock.side_effect = os_side_effect_false # Check that we fail properly if the processor # information file doesn't exist. with pytest.raises(MissingProcessorInfoError): MozPower() # Check that we fail properly when the model cannot be found # with by searching for 'model name'. os_mock.side_effect = os_side_effect_true with mock.patch("%s.open" % builtins_name, mock.mock_open(read_data="")) as _: with pytest.raises(MissingProcessorInfoError): MozPower()
def test_mozpower_oscpu_combo_missing_error(): """Tests that the error OsCpuComboMissingError is raised when we can't find a OS, and CPU combination (and, therefore, cannot find a power measurer). """ with mock.patch.object(MozPower, '_get_os', return_value='Not-An-OS') as _, \ mock.patch.object( MozPower, '_get_processor_info', return_value='Not-A-Processor' ) as _: with pytest.raises(OsCpuComboMissingError): MozPower()
def run_test(self, test, timeout): # tests will be run warm (i.e. NO browser restart between page-cycles) # unless otheriwse specified in the test INI by using 'cold = true' mozpower_measurer = None if self.config.get("power_test", False): powertest_name = test["name"].replace("/", "-").replace("\\", "-") output_dir = os.path.join(self.artifact_dir, "power-measurements-%s" % powertest_name) test_dir = os.path.join(output_dir, powertest_name) try: if not os.path.exists(output_dir): os.mkdir(output_dir) if not os.path.exists(test_dir): os.mkdir(test_dir) except Exception: LOG.critical( "Could not create directories to store power testing data." ) raise # Start power measurements with IPG creating a power usage log # every 30 seconds with 1 data point per second (or a 1000 milli- # second sampling rate). mozpower_measurer = MozPower( ipg_measure_duration=30, sampling_rate=1000, output_file_path=os.path.join(test_dir, "power-usage"), ) mozpower_measurer.initialize_power_measurements() if self.config.get("cold") or test.get("cold"): self.__run_test_cold(test, timeout) else: self.__run_test_warm(test, timeout) if mozpower_measurer: mozpower_measurer.finalize_power_measurements( test_name=test["name"]) perfherder_data = mozpower_measurer.get_perfherder_data() if not self.config.get("run_local", False): # when not running locally, zip the data and delete the folder which # was placed in the zip powertest_name = test["name"].replace("/", "-").replace("\\", "-") power_data_path = os.path.join( self.artifact_dir, "power-measurements-%s" % powertest_name) shutil.make_archive(power_data_path, "zip", power_data_path) shutil.rmtree(power_data_path) for data_type in perfherder_data: self.control_server.submit_supporting_data( perfherder_data[data_type])
def mozpower_obj(): """Returns a MozPower object with subprocess.check_output and os.path.exists calls patched with side effects. """ with mock.patch.object(MozPower, '_get_os', return_value='Darwin') as _, \ mock.patch.object( MozPower, '_get_processor_info', return_value='GenuineIntel' ) as _, \ mock.patch.object( MacIntelPower, 'get_ipg_path', return_value='/' ) as _, \ mock.patch('subprocess.check_output') as subprocess_mock, \ mock.patch('os.path.exists') as os_mock: subprocess_mock.side_effect = subprocess_side_effect os_mock.side_effect = os_side_effect yield MozPower(ipg_measure_duration=2)
def mozpower_obj(): """Returns a MozPower object with subprocess.check_output and os.path.exists calls patched with side effects. """ with mock.patch.object( MozPower, "_get_os", return_value="Darwin") as _, mock.patch.object( MozPower, "_get_processor_info", return_value="GenuineIntel") as _, mock.patch.object( MacIntelPower, "get_ipg_path", return_value="/" ) as _, mock.patch( "subprocess.check_output") as subprocess_mock, mock.patch( "os.path.exists") as os_mock: subprocess_mock.side_effect = subprocess_side_effect os_mock.side_effect = os_side_effect yield MozPower(ipg_measure_duration=2)
def test_mozpower_android_init_failure(): """Tests that the MozPower object fails when the android flag is set. Remove this test once android is implemented. """ with pytest.raises(NotImplementedError): MozPower(android=True)