def test_poll_thermal_zones(self, iglob_mock): """Tests sensor files handling""" ## BEGIN PRELUDE ## # We'll store there filenames of some temp files mocking those under # `/sys/class/thermal/thermal_zone*/temp` tmp_files = [] for temperature in ( # Fake temperatures b'50000', b'0', b'40000', b'50000'): with tempfile.NamedTemporaryFile(delete=False) as temp_file: temp_file.write(temperature) temp_file.seek(0) tmp_files.append(temp_file) ## END PRELUDE ## iglob_mock.return_value = iter([file.name for file in tmp_files]) # pylint: disable=protected-access Temperature._poll_thermal_zones(self.temperature_mock) self.assertListEqual(self.temperature_mock._temps, [50.0, 40.0, 50.0]) # pylint: enable=protected-access ## BEGIN POSTLUDE ## for tmp_file in tmp_files: tmp_file.close() os.remove(tmp_file.name)
def test_run_vcgencmd(self, _): """Test for `vcgencmd` output only""" # pylint: disable=protected-access Temperature._run_vcgencmd(self.temperature_mock) self.assertListEmpty(self.temperature_mock._temps) Temperature._run_vcgencmd(self.temperature_mock) self.assertListEqual(self.temperature_mock._temps, [42.8])
def test_run_sensors_ko_exec(self, _): """Test `sensors` (hard) failure handling""" # pylint: disable=protected-access Temperature._run_sensors(self.temperature_mock) self.assertListEmpty(self.temperature_mock._temps) Temperature._run_sensors(self.temperature_mock) self.assertListEmpty(self.temperature_mock._temps)
def test_run_sensors_ko_output(self, run_mock): """Test `sensors` (soft) failure handling""" # JSON decoding from `sensors` will fail... run_mock.return_value.stdout = """\ { "Is this JSON valid ?": [ "You", "should", "look", "twice.", ] } """ # pylint: disable=protected-access Temperature._run_sensors(self.temperature_mock) self.assertListEmpty(self.temperature_mock._temps)
def test_files_only_plus_fahrenheit(self, _, glob_mock, __): """Test sensor files only, Fahrenheit (naive) conversion and special degree character""" glob_mock.return_value = [file.name for file in self.tempfiles] self.assertRegex( Temperature().value, r'116\.0@F \(Max\. 122\.0@F\)' # 46.6 converted into Fahrenheit )
def test_files_only_in_fahrenheit(self, _, glob_mock, __): """Test sensor files only, Fahrenheit (naive) conversion and special degree character""" glob_mock.return_value = [file.name for file in self.tempfiles] self.assertEqual( Temperature().value, '116.0@F (Max. 122.0@F)' # 46.7 and 50.0 converted into Fahrenheit. )
def test_convert_to_fahrenheit(self): """Simple tests for the `_convert_to_fahrenheit` static method""" test_conversion_cases = ((-273.15, -459.67), (0.0, 32.0), (21.0, 69.8), (37.0, 98.6), (100.0, 212.0)) for celsius_value, expected_fahrenheit_value in test_conversion_cases: self.assertAlmostEqual( Temperature._convert_to_fahrenheit(celsius_value), # pylint: disable=protected-access expected_fahrenheit_value)
def test_run_sensors_ok_multiple_chipsets(self, run_mock): """Test `sensors` when multiple chipsets names have been passed""" run_mock.side_effect = [ Mock(stdout="""\ { "who-cares-about":{ "temp1":{ "temp1_input": 45.000, "temp1_crit": 128.000 }, "temp2":{ "temp2_input": 0.000, "temp2_crit": 128.000 }, "temp3":{ "temp3_input": 38.000, "temp3_crit": 128.000 } } } """, stderr=None), Mock(stdout="""\ { "the-chipsets-names":{ "what-are":{ "temp1_input": 45.000, "temp1_max": 100.000, "temp1_crit": 100.000, "temp1_crit_alarm": 0.000 } } } """, stderr=None) ] sensors_chipsets = ['who-cares-about', 'the-chipsets-names'] # pylint: disable=protected-access Temperature._run_sensors(self.temperature_mock, sensors_chipsets) self.assertListEqual(self.temperature_mock._temps, [45.0, 38.0, 45.0]) # pylint: enable=protected-access # Check that our `run` mock has been called up to the number of passed chipsets. self.assertEqual(run_mock.call_count, len(sensors_chipsets))
def test_vcgencmd_only_no_max(self, _, __): """ Test for `vcgencmd` output only (no sensor files). Only one value is retrieved, so no maximum should be displayed (see #39). """ temperature = Temperature() output_mock = MagicMock() temperature.output(output_mock) self.assertDictEqual( temperature.value, { 'temperature': 42.8, 'max_temperature': 42.8, 'char_before_unit': ' ', 'unit': 'C' }) self.assertEqual(output_mock.append.call_args[0][1], '42.8 C')
def test_sensors_only_in_fahrenheit(self, _): """Test computations around `sensors` output and Fahrenheit (naive) conversion""" self.assertDictEqual( Temperature().value, { 'temperature': 126.6, # (52.6 C in F) 'max_temperature': 237.2, # (114.0 C in F) 'char_before_unit': ' ', 'unit': 'F' })
def test_sensors_error_1(self, iglob_mock, _): """Test `sensors` (hard) failure handling and polling from files in Celsius""" iglob_mock.return_value = iter( [file.name for file in self._temp_files]) temperature = Temperature() output_mock = MagicMock() temperature.output(output_mock) self.assertDictEqual( temperature.value, { 'temperature': 46.7, 'max_temperature': 50.0, 'char_before_unit': 'o', 'unit': 'C' }) self.assertEqual(output_mock.append.call_args[0][1], '46.7oC (Max. 50.0oC)')
def test_sensors_error_2(self, iglob_mock, _): """Test `sensors` (hard) failure handling and polling from files in Celsius""" iglob_mock.return_value = iter( [file.name for file in self._temp_files]) self.assertDictEqual( Temperature().value, { 'temperature': 46.7, 'max_temperature': 50.0, 'char_before_unit': 'o', 'unit': 'C' })
def test_vcgencmd_and_files(self, iglob_mock, _): """Tests `vcgencmd` output AND sensor files""" iglob_mock.return_value = iter( [file.name for file in self._temp_files]) self.assertDictEqual( Temperature().value, { 'temperature': 45.0, 'max_temperature': 50.0, 'char_before_unit': ' ', 'unit': 'C' })
def test_files_only_in_fahrenheit(self, iglob_mock, _): """Test sensor files only, Fahrenheit (naive) conversion and special degree character""" iglob_mock.return_value = iter( [file.name for file in self._temp_files]) self.assertDictEqual( Temperature().value, { 'temperature': 116.0, # 46.7 degrees C in Fahrenheit. 'max_temperature': 122.0, # 50 degrees C in Fahrenheit 'char_before_unit': '@', 'unit': 'F' })
def test_output(self): """Test `output` method""" output_mock = MagicMock() # No value --> not detected. Temperature.output(self.temperature_mock, output_mock) self.assertEqual(output_mock.append.call_args[0][1], DEFAULT_CONFIG['default_strings']['not_detected']) output_mock.reset_mock() # Values --> normal behavior. self.temperature_mock._temps = [ # pylint: disable=protected-access 50.0, 40.0, 50.0 ] self.temperature_mock.value = { 'temperature': 46.7, 'max_temperature': 50.0, 'char_before_unit': 'o', 'unit': 'C' } Temperature.output(self.temperature_mock, output_mock) self.assertEqual(output_mock.append.call_args[0][1], '46.7oC (Max. 50.0oC)') # Only one value --> no maximum. self.temperature_mock._temps = [42.8] # pylint: disable=protected-access self.temperature_mock.value = { 'temperature': 42.8, 'max_temperature': 42.8, 'char_before_unit': ' ', 'unit': 'C' } Temperature.output(self.temperature_mock, output_mock) self.assertEqual(output_mock.append.call_args[0][1], '42.8 C')
def test_run_sensors_ok_excluded_subfeatures(self, run_mock): """Test `sensors` when chipset subfeatures have been excluded""" run_mock.side_effect = [ Mock(stdout="""\ { "k10temp-pci-00c3":{ "Tctl":{ "temp1_input": 42.000 }, "Tdie":{ "temp2_input": 32.000 } } } """, stderr=None) ] # pylint: disable=protected-access Temperature._run_sensors(self.temperature_mock, excluded_subfeatures=["Tctl"]) self.assertListEqual(self.temperature_mock._temps, [32.0])
def test_vcgencmd_and_files(self, iglob_mock, _): """Tests `vcgencmd` output AND sensor files""" iglob_mock.return_value = iter( [file.name for file in self._temp_files]) self.assertDictEqual( Temperature( options={ 'sensors_chipsets': [], 'use_fahrenheit': False, 'char_before_unit': ' ' }).value, { 'temperature': 45.0, 'max_temperature': 50.0, 'char_before_unit': ' ', 'unit': 'C' })
def test_celsius_to_fahrenheit_conversion(self, _, __, ___): """Simple tests for the `_convert_to_fahrenheit` static method""" temperature = Temperature() # pylint: disable=protected-access self.assertAlmostEqual(temperature._convert_to_fahrenheit(-273.15), -459.67) self.assertAlmostEqual(temperature._convert_to_fahrenheit(0.0), 32.0) self.assertAlmostEqual(temperature._convert_to_fahrenheit(21.0), 69.8) self.assertAlmostEqual(temperature._convert_to_fahrenheit(37.0), 98.6) self.assertAlmostEqual(temperature._convert_to_fahrenheit(100.0), 212.0)
def test_run_sysctl_dev_cpu(self, _, __): """Test for `sysctl` output only""" # pylint: disable=protected-access # First case. Temperature._run_sysctl_dev_cpu(self.temperature_mock) self.assertListEmpty(self.temperature_mock._temps) # Second case. Temperature._run_sysctl_dev_cpu(self.temperature_mock) self.assertListEmpty(self.temperature_mock._temps) # Third case. Temperature._run_sysctl_dev_cpu(self.temperature_mock) self.assertListEqual(self.temperature_mock._temps, [42.0, 42.0, 45.0, 45.0]) # Reset internal object here. self.temperature_mock._temps = [] # Fourth case. Temperature._run_sysctl_dev_cpu(self.temperature_mock) self.assertListEqual(self.temperature_mock._temps, [42.0, 40.0, 38.0])
def test_run_istats_or_osxcputemp(self, _): """Test for `iStats` and `OSX CPU Temp` third-party programs outputs""" # pylint: disable=protected-access # First case. Temperature._run_istats_or_osxcputemp(self.temperature_mock) self.assertListEmpty(self.temperature_mock._temps) # Second case. Temperature._run_istats_or_osxcputemp(self.temperature_mock) self.assertListEqual(self.temperature_mock._temps, [41.125]) # Reset internal object here. self.temperature_mock._temps = [] # Third case. Temperature._run_istats_or_osxcputemp(self.temperature_mock) self.assertListEqual(self.temperature_mock._temps, [61.8]) # Reset internal object here. self.temperature_mock._temps = [] # Fourth case. Temperature._run_istats_or_osxcputemp(self.temperature_mock) self.assertListEmpty(self.temperature_mock._temps)
def test_vcgencmd_and_files(self, _, glob_mock, __): """Tests `vcgencmd` output AND sensor files""" glob_mock.return_value = [file.name for file in self.tempfiles] self.assertEqual(Temperature().value, '45.0 C (Max. 50.0 C)')
def test_vcgencmd_and_files(self, _, glob_mock, __): """Tests `vcgencmd` output AND sensor files""" glob_mock.return_value = [file.name for file in self.tempfiles] self.assertRegex(Temperature().value, r'45\.0.?.? \(Max\. 50\.0.?.?\)')
def test_no_output(self, _, __): """Test when no value could be retrieved (anyhow)""" self.assertIsNone(Temperature().value)
def test_vcgencmd_only_no_max(self, _, __, ___): """ Test for `vcgencmd` output only (no sensor files). Only one value is retrieved, so no maximum is displayed (see #39). """ self.assertEqual(Temperature().value, '42.8 C')
def test_sensors_error_2(self, _, glob_mock, ___): """Test `sensors` (hard) failure handling and polling from files in Celsius""" glob_mock.return_value = [file.name for file in self.tempfiles] self.assertEqual(Temperature().value, '46.7oC (Max. 50.0oC)')
def test_sensors_only_in_fahrenheit(self, _, __): """Test computations around `sensors` output and Fahrenheit (naive) conversion""" self.assertEqual( Temperature().value, '126.6 F (Max. 237.2 F)' # 52.6 and 114.0 converted into Fahrenheit. )
def test_no_output(self, _, __, ___): """Test when no value could be retrieved (anyhow)""" self.assertEqual(Temperature().value, 'Not detected')
def test_run_sensors_ok(self, run_mock): """Test computations around `sensors` output""" run_mock.return_value.stdout = """\ { "who-cares-about":{ "temp1":{ "temp1_input": 45.000, "temp1_crit": 128.000 }, "temp2":{ "temp2_input": 0.000, "temp2_crit": 128.000 }, "temp3":{ "temp3_input": 38.000, "temp3_crit": 128.000 }, "temp4":{ "temp4_input": 39.000, "temp4_crit": 128.000 }, "temp5":{ "temp5_input": 0.000, "temp5_crit": 128.000 }, "temp6":{ "temp6_input": 114.000, "temp6_crit": 128.000 } }, "the-chipsets-names":{ "what-are":{ "temp1_input": 45.000, "temp1_max": 100.000, "temp1_crit": 100.000, "temp1_crit_alarm": 0.000 }, "those":{ "temp2_input": 43.000, "temp2_max": 100.000, "temp2_crit": 100.000, "temp2_crit_alarm": 0.000 }, "identifiers":{ "temp3_input": 44.000, "temp3_max": 100.000, "temp3_crit": 100.000, "temp3_crit_alarm": 0.000 } }, "crap-a-fan-chip":{ "fan1":{ "fan1_input": 3386.000 } } } """ # pylint: disable=protected-access Temperature._run_sensors(self.temperature_mock) self.assertListEqual(self.temperature_mock._temps, [45.0, 38.0, 39.0, 114.0, 45.0, 43.0, 44.0])
def test_no_output(self, _, __): """Test when no value could be retrieved (anyhow)""" self.assertIsNone(Temperature(options={'sensors_chipsets': []}).value)