def read_control_file(control_file=None): """ Read the NUC LED control file hex bytes string. Args: control_file: Sets the control file to use if provided, otherwise `nuc_wmi.CONTROL_FILE` is used. Exceptions: Raises normal `IOError`/`OSError` on failure to read the control file, or `ValueError` for hex conversion error. Returns: Tuple of ints representing the hex numbers read in from the control file. """ with open(control_file or CONTROL_FILE, 'r') as fin: raw_hex_byte_string = fin.read() # Remove the new line and null char the driver leaves raw_hex_byte_string = raw_hex_byte_string.rstrip("\x00").rstrip("\n") byte_list = [ int(hex_byte_str, 16) for hex_byte_str in raw_hex_byte_string.split(' ') ] for hex_byte in byte_list: if hex_byte < 0 or hex_byte > 255: raise NucWmiError( 'NUC WMI returned hex byte outside of 0-255 range') if len(byte_list) != 4: raise NucWmiError( 'NUC WMI control file did not return an expected 4 bytes') return tuple(byte_list)
def wmi_interface_spec_compliance_version(control_file=None): """ Returns the version for the WMI interface spec compliance. Args: control_file: Sets the control file to use if provided, otherwise `nuc_wmi.CONTROL_FILE` is used. Exceptions: Raises `nuc_wmi.NucWmiError` exception if kernel module returns an error code, or if `read_control_file` or `write_control_file` raise an exception. Returns: Tuple of two bytes representing the version number. """ wmi_version_byte_list = [ METHOD_ID, VERSION_TYPE.index('wmi_interface_spec_compliance_version') ] write_control_file(wmi_version_byte_list, control_file=control_file) (error_code, version_byte_1, version_byte_2, reserved_byte) = read_control_file(control_file=control_file) if error_code > 0: raise NucWmiError( RETURN_ERROR.get(error_code, 'Error (Unknown NUC WMI error code)')) return tuple([version_byte_2, version_byte_1])
def query_leds(control_file=None): """ List all LED types supported. Args: control_file: Sets the control file to use if provided, otherwise `nuc_wmi.CONTROL_FILE` is used. Exceptions: Raises `nuc_wmi.NucWmiError` exception if kernel module returns an error code, or if `read_control_file` or `write_control_file` raise an exception. Returns: List of available `nuc_wmi.LED_TYPE` indexes. """ query_leds_byte_list = [METHOD_ID, QUERY_TYPE.index('query_leds')] write_control_file(query_leds_byte_list, control_file=control_file) # Bitmap [0:7], [8:15], [16:23] (error_code, led_type_bitmap_1, led_type_bitmap_2, led_type_bitmap_3) = read_control_file(control_file=control_file) if error_code > 0: raise NucWmiError( RETURN_ERROR.get(error_code, 'Error (Unknown NUC WMI error code)')) led_type_bitmaps = [ led_type_bitmap_3, led_type_bitmap_2, led_type_bitmap_1 ] led_type_bitmap = byte_list_to_bitmap(led_type_bitmaps)[::-1] return [ index for index, bit in enumerate(led_type_bitmap) if int(bit) and index < len(LED_TYPE['new']) ]
def set_led_indicator_option(led_type, led_indicator_option, control_file=None): """ Set the LED indicator option for the specified LED type, Args: control_file: Sets the control file to use if provided, otherwise `nuc_wmi.CONTROL_FILE` is used. led_indicator_option: The LED indicator option to set for the LED type. led_type: The LED type for which to set the LED indicator option. Exceptions: Raises `nuc_wmi.NucWmiError` exception if kernel module returns an error code, or if `read_control_file` or `write_control_file` raise an exception. """ set_led_indicator_option_byte_list = [METHOD_ID, led_type, led_indicator_option] write_control_file(set_led_indicator_option_byte_list, control_file=control_file) ( error_code, reserved_byte_1, reserved_byte_2, reserved_byte_3 ) = read_control_file(control_file=control_file) if error_code > 0: raise NucWmiError(RETURN_ERROR.get(error_code, 'Error (Unknown NUC WMI error code)'))
def get_led(led, control_file=None): """ Get legacy LED state with regard to brightness, frequency, and color. Args: control_file: Sets the control file to use if provided, otherwise `nuc_wmi.CONTROL_FILE` is used. led: Selects the legacy LED to get the state for. Exceptions: Raises `nuc_wmi.NucWmiError` exception if kernel module returns an error code, or if `read_control_file` or `write_control_file` raise an exception. Returns: Tuple of legacy brightness, frequency, and color of the select LED. """ get_led_byte_list = [METHOD_ID, led] write_control_file(get_led_byte_list, control_file=control_file) ( error_code, brightness, frequency, color ) = read_control_file(control_file=control_file) if error_code > 0: raise NucWmiError(RETURN_ERROR.get(error_code, 'Error (Unknown NUC WMI error code)')) return tuple([brightness, frequency, color])
def get_led_indicator_option(led_type, control_file=None): """ Get the current indicator option for the LED type. Args: control_file: Sets the control file to use if provided, otherwise `nuc_wmi.CONTROL_FILE` is used. led_type: The LED type for which to retrieve the current indicator option. Exceptions: Raises `nuc_wmi.NucWmiError` exception if kernel module returns an error code, or if `read_control_file` or `write_control_file` raise an exception. Returns: `nuc_wmi.LED_INDICATOR_OPTION` index of the current LED indicator option. """ get_led_indicator_option_byte_list = [ METHOD_ID, GET_LED_TYPE.index('get_led_indicator_option'), led_type ] write_control_file(get_led_indicator_option_byte_list, control_file=control_file) ( error_code, led_indicator_option, reserved_byte_1, reserved_byte_2 ) = read_control_file(control_file=control_file) if error_code > 0: raise NucWmiError(RETURN_ERROR[error_code]) return led_indicator_option
def test_query_led_indicator_options_cli( self, nuc_wmi_query_led_indicator_options, nuc_wmi_sys_exit, nuc_wmi_print): """ Tests that `query_led_indicator_options_cli` returns the expected exceptions, return values, or outputs. """ self.assertTrue(nuc_wmi.cli.query_led.query_led_indicator_options is nuc_wmi_query_led_indicator_options) self.assertTrue(nuc_wmi.cli.query_led.exit is nuc_wmi_sys_exit) self.assertTrue(nuc_wmi.cli.query_led.print is nuc_wmi_print) # Branch 1: Test that query_led_indicator_options_cli returns the proper JSON response and exit # code for valid cli args # Return HDD LED indicator options of HDD Activity Indicator and Software Indicator expected_indicator_options = [0x01, 0x04] nuc_wmi_query_led_indicator_options.return_value = expected_indicator_options returned_query_led_indicator_options_cli = query_led_indicator_options_cli( [LED_TYPE['new'][1]]) nuc_wmi_query_led_indicator_options.assert_called_with( LED_TYPE['new'].index('HDD LED'), control_file=None) nuc_wmi_print.assert_called() self.assertEqual( json.loads(nuc_wmi_print.call_args.args[0]), { 'led': { 'type': LED_TYPE['new'][1], 'indicator_options': [ LED_INDICATOR_OPTION[indicator] for indicator in expected_indicator_options ] } }) self.assertEqual(returned_query_led_indicator_options_cli, None) # Reset nuc_wmi_query_led_indicator_options.reset_mock() nuc_wmi_sys_exit.reset_mock() nuc_wmi_print.reset_mock() # Branch 2: Test that query_led_indicator_options_cli captures raised errors and returns # the proper JSON error response and exit code. nuc_wmi_query_led_indicator_options.side_effect = NucWmiError( 'Error (Function not supported)') returned_query_led_indicator_options_cli = query_led_indicator_options_cli( [LED_TYPE['new'][1]]) nuc_wmi_query_led_indicator_options.assert_called_with( LED_TYPE['new'].index('HDD LED'), control_file=None) nuc_wmi_print.assert_called_with( '{"error": "Error (Function not supported)"}') nuc_wmi_sys_exit.assert_called_with(1) self.assertEqual(returned_query_led_indicator_options_cli, None)
def test_wmi_interface_spec_compliance_version_cli( self, nuc_wmi_cli_wmi_interface_spec_compliance_version, nuc_wmi_sys_exit, nuc_wmi_print): """ Tests that `wmi_interface_spec_compliance_version_cli` returns the expected exceptions, return values, or outputs. """ self.assertTrue(nuc_wmi.cli.version.wmi_interface_spec_compliance_version is \ nuc_wmi_cli_wmi_interface_spec_compliance_version) self.assertTrue(nuc_wmi.cli.version.exit is nuc_wmi_sys_exit) self.assertTrue(nuc_wmi.cli.version.print is nuc_wmi_print) # Branch 1: Test that wmi_interface_spec_compliance_version_cli returns the proper JSON response and exit # code for valid cli args wmi_interface_spec_compliance_version = (0x01, 0x36) nuc_wmi_cli_wmi_interface_spec_compliance_version.return_value = wmi_interface_spec_compliance_version returned_wmi_interface_spec_compliance_version_cli = wmi_interface_spec_compliance_version_cli( []) nuc_wmi_cli_wmi_interface_spec_compliance_version.assert_called_with( control_file=None) nuc_wmi_print.assert_called() self.assertEqual(json.loads(nuc_wmi_print.call_args.args[0]), { "version": { "semver": "1.54", "type": "wmi_interface_spec_compliance" } }) self.assertEqual(returned_wmi_interface_spec_compliance_version_cli, None) # Reset nuc_wmi_cli_wmi_interface_spec_compliance_version.reset_mock() nuc_wmi_sys_exit.reset_mock() nuc_wmi_print.reset_mock() # Branch 2: Test that wmi_interface_spec_compliance_version_cli captures raised errors and returns # the proper JSON error response and exit code. nuc_wmi_cli_wmi_interface_spec_compliance_version.side_effect = NucWmiError( 'Error (Function not supported)') returned_wmi_interface_spec_compliance_version_cli = wmi_interface_spec_compliance_version_cli( []) nuc_wmi_cli_wmi_interface_spec_compliance_version.assert_called_with( control_file=None) nuc_wmi_print.assert_called_with( '{"error": "Error (Function not supported)"}') nuc_wmi_sys_exit.assert_called_with(1) self.assertEqual(returned_wmi_interface_spec_compliance_version_cli, None)
def test_query_led_color_type_cli(self, nuc_wmi_query_led_color_type, nuc_wmi_sys_exit, nuc_wmi_print): """ Tests that `query_led_color_type_cli` returns the expected exceptions, return values, or outputs. """ self.assertTrue(nuc_wmi.cli.query_led.query_led_color_type is nuc_wmi_query_led_color_type) self.assertTrue(nuc_wmi.cli.query_led.exit is nuc_wmi_sys_exit) self.assertTrue(nuc_wmi.cli.query_led.print is nuc_wmi_print) # Branch 1: Test that query_led_color_type_cli returns the proper JSON response and exit # code for valid cli args # Return HDD LED color type of Dual-color Blue / White nuc_wmi_query_led_color_type.return_value = LED_COLOR_TYPE[ 'new'].index('Dual-color Blue / White') returned_query_led_color_type_cli = query_led_color_type_cli( [LED_TYPE['new'][1]]) nuc_wmi_query_led_color_type.assert_called_with( LED_TYPE['new'].index('HDD LED'), control_file=None) nuc_wmi_print.assert_called() self.assertEqual( json.loads(nuc_wmi_print.call_args.args[0]), { 'led': { 'type': LED_TYPE['new'][1], 'color_type': LED_COLOR_TYPE['new'][1] } }) self.assertEqual(returned_query_led_color_type_cli, None) # Reset nuc_wmi_query_led_color_type.reset_mock() nuc_wmi_sys_exit.reset_mock() nuc_wmi_print.reset_mock() # Branch 2: Test that query_led_color_type_cli captures raised errors and returns # the proper JSON error response and exit code. nuc_wmi_query_led_color_type.side_effect = NucWmiError( 'Error (Function not supported)') returned_query_led_color_type_cli = query_led_color_type_cli( [LED_TYPE['new'][1]]) nuc_wmi_query_led_color_type.assert_called_with( LED_TYPE['new'].index('HDD LED'), control_file=None) nuc_wmi_print.assert_called_with( '{"error": "Error (Function not supported)"}') nuc_wmi_sys_exit.assert_called_with(1) self.assertEqual(returned_query_led_color_type_cli, None)
def test_save_led_config_cli( self, nuc_wmi_cli_save_led_config, nuc_wmi_sys_exit, nuc_wmi_print ): """ Tests that `save_led_config_cli` returns the expected exceptions, return values, or outputs. """ self.assertTrue(nuc_wmi.cli.led_app_notification.save_led_config is nuc_wmi_cli_save_led_config) self.assertTrue(nuc_wmi.cli.led_app_notification.exit is nuc_wmi_sys_exit) self.assertTrue(nuc_wmi.cli.led_app_notification.print is nuc_wmi_print) # Branch 1: Test that save_led_config_cli returns the proper JSON response and exit # code for valid cli args returned_save_led_config_cli = save_led_config_cli([]) nuc_wmi_cli_save_led_config.assert_called_with( control_file=None ) nuc_wmi_print.assert_called() self.assertEqual( json.loads(nuc_wmi_print.call_args.args[0]), { 'led_app_notification': { 'type': 'save_led_config' } } ) self.assertEqual(returned_save_led_config_cli, None) # Reset nuc_wmi_cli_save_led_config.reset_mock() nuc_wmi_sys_exit.reset_mock() nuc_wmi_print.reset_mock() # Branch 2: Test that save_led_config_cli captures raised errors and returns # the proper JSON error response and exit code. nuc_wmi_cli_save_led_config.side_effect = NucWmiError('Error (Function not supported)') returned_save_led_config_cli = save_led_config_cli([]) nuc_wmi_cli_save_led_config.assert_called_with( control_file=None ) nuc_wmi_print.assert_called_with('{"error": "Error (Function not supported)"}') nuc_wmi_sys_exit.assert_called_with(1) self.assertEqual(returned_save_led_config_cli, None)
def query_led_control_items(led_type, led_indicator_option, control_file=None): """ Query the LED control items for the LED indicator option of the LED type. Args: control_file: Sets the control file to use if provided, otherwise `nuc_wmi.CONTROL_FILE` is used. led_indicator_option: The LED indicator option to use for the LED type when querying for the LED control items. led_type: The LED type for which to query the LED control items. Exceptions: Raises `nuc_wmi.NucWmiError` exception if kernel module returns an error code, or if `read_control_file` or `write_control_file` raise an exception. Returns: List of available `nuc_wmi.CONTROL_ITEM` indexes for the specified LED type and LED indicator option. """ led_color_type = query_led_color_type(led_type, control_file=control_file) query_led_control_item_byte_list = [ METHOD_ID, QUERY_TYPE.index('query_led_control_items'), led_type, led_indicator_option ] write_control_file(query_led_control_item_byte_list, control_file=control_file) # Bitmap [0:7], [8:15], [16:23] (error_code, led_control_item_bitmap_1, led_control_item_bitmap_2, led_control_item_bitmap_3) = read_control_file(control_file=control_file) if error_code > 0: raise NucWmiError( RETURN_ERROR.get(error_code, 'Error (Unknown NUC WMI error code)')) led_control_item_bitmaps = [ led_control_item_bitmap_3, led_control_item_bitmap_2, led_control_item_bitmap_1 ] led_control_item_bitmap = byte_list_to_bitmap( led_control_item_bitmaps)[::-1] if led_indicator_option == LED_INDICATOR_OPTION_DISABLED or \ CONTROL_ITEM[led_indicator_option][led_color_type] is None: return [] return [ index for index, bit in enumerate(led_control_item_bitmap) if int(bit) and index < len(CONTROL_ITEM[led_indicator_option][led_color_type]) ]
def set_led(led, brightness, frequency, color, control_file=None): """ Set LED state with regard to brightness, frequency, and color. Args: brightness: Controls the brightness level of the LED. color: Sets legacy RGB-color for LED. control_file: Sets the control file to use if provided, otherwise `nuc_wmi.CONTROL_FILE` is used. frequency: Sets the legacy LED frequency. led: Selects the legacy LED to set a state for. Exceptions: Raises `nuc_wmi.NucWmiError` exception if kernel module returns an error code, or if `read_control_file` or `write_control_file` raise an exception. """ set_led_byte_list = [METHOD_ID, led, brightness, frequency, color] write_control_file(set_led_byte_list, control_file=control_file) (brightness_error, frequency_error, color_error, reserved_byte) = read_control_file(control_file=control_file) if brightness_error > 0: raise NucWmiError( RETURN_ERROR.get(brightness_error, 'Error (Unknown NUC WMI error code)')) if frequency_error > 0: raise NucWmiError( RETURN_ERROR.get(frequency_error, 'Error (Unknown NUC WMI error code)')) if color_error > 0: raise NucWmiError( RETURN_ERROR.get(color_error, 'Error (Unknown NUC WMI error code)'))
def test_switch_led_type_cli(self, nuc_wmi_switch_led_type, nuc_wmi_sys_exit, nuc_wmi_print): """ Tests that `switch_led_type_cli` returns the expected exceptions, return values, or outputs. """ self.assertTrue(nuc_wmi.cli.switch_led_type.switch_led_type is \ nuc_wmi_switch_led_type) self.assertTrue(nuc_wmi.cli.switch_led_type.exit is nuc_wmi_sys_exit) self.assertTrue(nuc_wmi.cli.switch_led_type.print is nuc_wmi_print) # Branch 1: Test that switch_led_type_cli returns the proper JSON response and exit # code for valid cli args returned_switch_led_type_cli = switch_led_type_cli( [LED_COLOR_GROUP[0]]) nuc_wmi_switch_led_type.assert_called_with( LED_COLOR_GROUP.index('Single color LED'), control_file=None) nuc_wmi_print.assert_called() self.assertEqual(json.loads(nuc_wmi_print.call_args.args[0]), {"led_color_group": { "type": LED_COLOR_GROUP[0] }}) self.assertEqual(returned_switch_led_type_cli, None) # Reset nuc_wmi_switch_led_type.reset_mock() nuc_wmi_sys_exit.reset_mock() nuc_wmi_print.reset_mock() # Branch 2: Test that switch_led_type_cli captures raised errors and returns # the proper JSON error response and exit code. nuc_wmi_switch_led_type.side_effect = NucWmiError( 'Error (Function not supported)') returned_switch_led_type_cli = switch_led_type_cli( [LED_COLOR_GROUP[0]]) nuc_wmi_switch_led_type.assert_called_with( LED_COLOR_GROUP.index('Single color LED'), control_file=None) nuc_wmi_print.assert_called_with( '{"error": "Error (Function not supported)"}') nuc_wmi_sys_exit.assert_called_with(1) self.assertEqual(returned_switch_led_type_cli, None)
def test_query_leds_cli(self, nuc_wmi_query_leds, nuc_wmi_sys_exit, nuc_wmi_print): """ Tests that `query_leds_cli` returns the expected exceptions, return values, or outputs. """ self.assertTrue(nuc_wmi.cli.query_led.query_leds is nuc_wmi_query_leds) self.assertTrue(nuc_wmi.cli.query_led.exit is nuc_wmi_sys_exit) self.assertTrue(nuc_wmi.cli.query_led.print is nuc_wmi_print) # Branch 1: Test that query_leds_cli returns the proper JSON response and exit # code for valid cli args expected_leds = [0x00, 0x01] nuc_wmi_query_leds.return_value = expected_leds returned_query_leds_cli = query_leds_cli([]) nuc_wmi_query_leds.assert_called_with(control_file=None) nuc_wmi_print.assert_called() self.assertEqual( json.loads(nuc_wmi_print.call_args.args[0]), {'leds': [LED_TYPE['new'][led] for led in expected_leds]}) self.assertEqual(returned_query_leds_cli, None) # Reset nuc_wmi_query_leds.reset_mock() nuc_wmi_sys_exit.reset_mock() nuc_wmi_print.reset_mock() # Branch 2: Test that query_leds_cli captures raised errors and returns # the proper JSON error response and exit code. nuc_wmi_query_leds.side_effect = NucWmiError( 'Error (Function not supported)') returned_query_leds_cli = query_leds_cli([]) nuc_wmi_query_leds.assert_called_with(control_file=None) nuc_wmi_print.assert_called_with( '{"error": "Error (Function not supported)"}') nuc_wmi_sys_exit.assert_called_with(1) self.assertEqual(returned_query_leds_cli, None)
def switch_led_type(led_color_group, control_file=None): """ Switches the LED color group type. Args: led_color_group: The LED color group type to set. Exceptions: Raises `nuc_wmi.NucWmiError` exception if kernel module returns an error code, or if `read_control_file` or `write_control_file` raise an exception. """ switch_led_byte_list = [METHOD_ID, led_color_group] write_control_file(switch_led_byte_list, control_file=control_file) (error_code, reserved_byte_1, reserved_byte_2, reserved_byte_3) = read_control_file(control_file=control_file) if error_code > 0: raise NucWmiError( RETURN_ERROR.get(error_code, 'Error (Unknown NUC WMI error code)'))
def get_led_control_item(led_type, led_indicator_option, control_item, control_file=None): """ Get the current control item value for the control item of the indicator option for the specified LED type. Args: control_file: Sets the control file to use if provided, otherwise `nuc_wmi.CONTROL_FILE` is used. control_item: The control item of the specified LED type indicator option for which to retrieve the value. led_indicator_option: The indicator option for the specified LED type for which to retrieve the current control item value. led_type: The LED type for which to retrieve the current control item. Exceptions: Raises `nuc_wmi.NucWmiError` exception if kernel module returns an error code, or if `read_control_file` or `write_control_file` raise an exception. Returns: `nuc_wmi.CONTROL_ITEM` value for the control item of the indicator option for the specified LED type. """ get_led_control_item_byte_list = [ METHOD_ID, GET_LED_TYPE.index('get_led_control_item'), led_type, led_indicator_option, control_item ] write_control_file(get_led_control_item_byte_list, control_file=control_file) ( error_code, control_item_value, reserved_byte_1, reserved_byte_2 ) = read_control_file(control_file=control_file) if error_code > 0: raise NucWmiError(RETURN_ERROR.get(error_code, 'Error (Unknown NUC WMI error code)')) return control_item_value
def save_led_config(control_file=None): """ Send a save LED configuration LED app notification. Args: control_file: Sets the control file to use if provided, otherwise `nuc_wmi.CONTROL_FILE` is used. Exceptions: Raises `nuc_wmi.NucWmiError` exception if kernel module returns an error code, or if `read_control_file` or `write_control_file` raise an exception. """ notification_byte_list = [ METHOD_ID, NOTIFICATION_TYPE.index('save_led_config') ] write_control_file(notification_byte_list, control_file=control_file) (error_code, reserved_byte_1, reserved_byte_2, reserved_byte_3) = read_control_file(control_file=control_file) if error_code > 0: raise NucWmiError( RETURN_ERROR.get(error_code, 'Error (Unknown NUC WMI error code)'))
def write_control_file(int_byte_list, control_file=None): """ Converts the integer byte list into a hex byte string and writes it to the NUC control file. Args: control_file: Sets the control file to use if provided, otherwise `nuc_wmi.CONTROL_FILE` is used. int_byte_list: List of integers bytes to be converted into a hex byte string to send to the NUC control file. May be int byte strings. Integers must be 0-255. Exceptions: Raises normal `IOError`/`OSError` on failure to read the control file, `ValueError` for hex conversion error, or `nuc_wmi.NucWmiError` for input value errors. """ for int_byte in int_byte_list: if int(int_byte) < 0 or int(int_byte) > 255: raise NucWmiError('Error (NUC LED byte values must be 0-255)') raw_hex_byte_string = ' '.join([ '{:02x}'.format(int(int_byte)) for int_byte in (int_byte_list + ([0] * (5 - len(int_byte_list)))) ]) with open(control_file or CONTROL_FILE, 'w') as fout: fout.write(raw_hex_byte_string)
def query_led_color_type(led_type, control_file=None): """ Query the LED color type for the LED type. Args: control_file: Sets the control file to use if provided, otherwise `nuc_wmi.CONTROL_FILE` is used. led_type: The LED type for which to query the LED color type. Exceptions: Raises `nuc_wmi.NucWmiError` exception if kernel module returns an error code, or if `read_control_file` or `write_control_file` raise an exception. Returns: `nuc_wmi.LED_COLOR_TYPE` index of current LED color type. """ query_led_color_byte_list = [ METHOD_ID, QUERY_TYPE.index('query_led_color_type'), led_type ] write_control_file(query_led_color_byte_list, control_file=control_file) # Bitmap [0:7], [8:15], [16:23] (error_code, led_color_type_bitmap_1, led_color_type_bitmap_2, led_color_type_bitmap_3) = read_control_file(control_file=control_file) if error_code > 0: raise NucWmiError( RETURN_ERROR.get(error_code, 'Error (Unknown NUC WMI error code)')) led_color_type_bitmaps = [ led_color_type_bitmap_3, led_color_type_bitmap_2, led_color_type_bitmap_1 ] led_color_type_bitmap = byte_list_to_bitmap(led_color_type_bitmaps) return int(led_color_type_bitmap, 2)
def test_get_led_control_item_cli( self, nuc_wmi_get_led_control_item, nuc_wmi_query_led_indicator_options, nuc_wmi_query_led_color_type, nuc_wmi_sys_exit, nuc_wmi_print ): """ Tests that `get_led_control_item_cli` returns the expected exceptions, return values, or outputs. """ self.assertTrue(nuc_wmi.cli.get_led_new.get_led_control_item is nuc_wmi_get_led_control_item) self.assertTrue(nuc_wmi.cli.get_led_new.query_led_indicator_options is nuc_wmi_query_led_indicator_options) self.assertTrue(nuc_wmi.cli.get_led_new.query_led_color_type is nuc_wmi_query_led_color_type) self.assertTrue(nuc_wmi.cli.get_led_new.exit is nuc_wmi_sys_exit) self.assertTrue(nuc_wmi.cli.get_led_new.print is nuc_wmi_print) # Branch 1: Test that get_led_control_item_cli returns the proper JSON response and exit # code for valid cli args # Get HDD LED control item value of 37 for Brightness of HDD Activity Indicator expected_brightness = 0x37 nuc_wmi_query_led_color_type.return_value = LED_COLOR_TYPE['new'].index('Dual-color Blue / White') nuc_wmi_query_led_indicator_options.return_value = [0x01, 0x04] nuc_wmi_get_led_control_item.return_value = expected_brightness returned_get_led_control_item_cli = get_led_control_item_cli( [ LED_TYPE['new'][1], LED_INDICATOR_OPTION[1], CONTROL_ITEM_HDD_ACTIVITY_INDICATOR_MULTI_COLOR[0]['Control Item'] ] ) nuc_wmi_query_led_color_type.assert_called_with( LED_TYPE['new'].index('HDD LED'), control_file=None ) nuc_wmi_query_led_indicator_options.assert_called_with( LED_TYPE['new'].index('HDD LED'), control_file=None ) nuc_wmi_get_led_control_item.assert_called_with( LED_TYPE['new'].index('HDD LED'), LED_INDICATOR_OPTION.index('HDD Activity Indicator'), CONTROL_ITEM_HDD_ACTIVITY_INDICATOR_MULTI_COLOR.index( { 'Control Item': 'Brightness', 'Options': LED_BRIGHTNESS_MULTI_COLOR } ), control_file=None ) nuc_wmi_print.assert_called() self.assertEqual( json.loads(nuc_wmi_print.call_args.args[0]), { 'led': { 'type': LED_TYPE['new'][1], 'indicator_option': LED_INDICATOR_OPTION[1], 'control_item': CONTROL_ITEM_HDD_ACTIVITY_INDICATOR_MULTI_COLOR[0]['Control Item'], 'control_item_value': str(expected_brightness) } } ) self.assertEqual(returned_get_led_control_item_cli, None) # Reset nuc_wmi_query_led_color_type.reset_mock() nuc_wmi_query_led_indicator_options.reset_mock() nuc_wmi_get_led_control_item.reset_mock() nuc_wmi_sys_exit.reset_mock() nuc_wmi_print.reset_mock() # Branch 2: Test that get_led_control_item_cli captures raised errors and returns # the proper JSON error response and exit code. nuc_wmi_query_led_color_type.return_value = LED_COLOR_TYPE['new'].index('Dual-color Blue / White') nuc_wmi_query_led_indicator_options.return_value = [0x01, 0x04] nuc_wmi_get_led_control_item.side_effect = NucWmiError('Error (Function not supported)') returned_get_led_control_item_cli = get_led_control_item_cli( [ LED_TYPE['new'][1], LED_INDICATOR_OPTION[1], CONTROL_ITEM_HDD_ACTIVITY_INDICATOR_MULTI_COLOR[0]['Control Item'] ] ) nuc_wmi_query_led_color_type.assert_called_with( LED_TYPE['new'].index('HDD LED'), control_file=None ) nuc_wmi_query_led_indicator_options.assert_called_with( LED_TYPE['new'].index('HDD LED'), control_file=None ) nuc_wmi_get_led_control_item.assert_called_with( LED_TYPE['new'].index('HDD LED'), LED_INDICATOR_OPTION.index('HDD Activity Indicator'), CONTROL_ITEM_HDD_ACTIVITY_INDICATOR_MULTI_COLOR.index( { 'Control Item': 'Brightness', 'Options': LED_BRIGHTNESS_MULTI_COLOR } ), control_file=None ) nuc_wmi_print.assert_called_with('{"error": "Error (Function not supported)"}') nuc_wmi_sys_exit.assert_called_with(1) self.assertEqual(returned_get_led_control_item_cli, None) # Reset nuc_wmi_query_led_color_type.reset_mock() nuc_wmi_query_led_indicator_options.reset_mock() nuc_wmi_sys_exit.reset_mock() nuc_wmi_print.reset_mock() nuc_wmi_get_led_control_item = MagicMock() # Branch 3: Test that get_led_control_item_cli raises proper error when an invalid indicator # option for the current LED is chosen and returns he proper JSON error response and exit code. nuc_wmi_query_led_color_type.return_value = LED_COLOR_TYPE['new'].index('Dual-color Blue / White') nuc_wmi_query_led_indicator_options.return_value = [0x01, 0x04] returned_get_led_control_item_cli = get_led_control_item_cli( [ LED_TYPE['new'][1], LED_INDICATOR_OPTION[2], CONTROL_ITEM_HDD_ACTIVITY_INDICATOR_MULTI_COLOR[0]['Control Item'] ] ) nuc_wmi_query_led_color_type.assert_called_with( LED_TYPE['new'].index('HDD LED'), control_file=None ) nuc_wmi_query_led_indicator_options.assert_called_with( LED_TYPE['new'].index('HDD LED'), control_file=None ) nuc_wmi_get_led_control_item.assert_not_called() nuc_wmi_print.assert_called_with('{"error": "Invalid indicator option for the selected LED"}') nuc_wmi_sys_exit.assert_called_with(1) self.assertEqual(returned_get_led_control_item_cli, None) # Reset nuc_wmi_query_led_color_type.reset_mock() nuc_wmi_query_led_indicator_options.reset_mock() nuc_wmi_get_led_control_item.reset_mock() nuc_wmi_sys_exit.reset_mock() nuc_wmi_print.reset_mock() # Branch 4: Test that get_led_control_item_cli raises proper error when there are no control items for # for the current LED and indicator option chosen and returns he proper JSON error response and exit # code. nuc_wmi_query_led_color_type.return_value = LED_COLOR_TYPE['new'].index('Single-color LED') nuc_wmi_query_led_indicator_options.return_value = [0x01, 0x04, 0x05] returned_get_led_control_item_cli = get_led_control_item_cli( [ LED_TYPE['new'][0], LED_INDICATOR_OPTION[5], 'Brightness' ] ) nuc_wmi_query_led_color_type.assert_called_with( LED_TYPE['new'].index('Power Button LED'), control_file=None ) nuc_wmi_query_led_indicator_options.assert_called_with( LED_TYPE['new'].index('Power Button LED'), control_file=None ) nuc_wmi_get_led_control_item.assert_not_called() nuc_wmi_print.assert_called_with( '{"error": "No control items are available for the selected LED and indicator option"}' ) nuc_wmi_sys_exit.assert_called_with(1) self.assertEqual(returned_get_led_control_item_cli, None) # Reset nuc_wmi_query_led_color_type.reset_mock() nuc_wmi_query_led_indicator_options.reset_mock() nuc_wmi_get_led_control_item.reset_mock() nuc_wmi_sys_exit.reset_mock() nuc_wmi_print.reset_mock() # Branch 5: Test that get_led_control_item_cli raises proper error when an invalid control item for # for the current LED and indicator option is chosen and returns he proper JSON error response and # exit code. nuc_wmi_query_led_color_type.return_value = LED_COLOR_TYPE['new'].index('Dual-color Blue / White') nuc_wmi_query_led_indicator_options.return_value = [0x01, 0x04] returned_get_led_control_item_cli = get_led_control_item_cli( [ LED_TYPE['new'][1], LED_INDICATOR_OPTION[1], 'Indication Scheme' ] ) nuc_wmi_query_led_color_type.assert_called_with( LED_TYPE['new'].index('HDD LED'), control_file=None ) nuc_wmi_query_led_indicator_options.assert_called_with( LED_TYPE['new'].index('HDD LED'), control_file=None ) nuc_wmi_get_led_control_item.assert_not_called() nuc_wmi_print.assert_called_with( '{"error": "Invalid control item specified for the selected LED and indicator option"}' ) nuc_wmi_sys_exit.assert_called_with(1) self.assertEqual(returned_get_led_control_item_cli, None)
def test_set_led_indicator_option_cli( self, nuc_wmi_set_led_indicator_option, nuc_wmi_sys_exit, nuc_wmi_print ): """ Tests that `set_led_indicator_option_cli` returns the expected exceptions, return values, or outputs. """ self.assertTrue(nuc_wmi.cli.set_led_indicator_option.set_led_indicator_option is \ nuc_wmi_set_led_indicator_option) self.assertTrue(nuc_wmi.cli.set_led_indicator_option.exit is nuc_wmi_sys_exit) self.assertTrue(nuc_wmi.cli.set_led_indicator_option.print is nuc_wmi_print) # Branch 1: Test that set_led_indicator_option_cli returns the proper JSON response and exit # code for valid cli args # Set HDD LED indicator option to HDD Activity Indicator returned_set_led_indicator_option_cli = set_led_indicator_option_cli( [ LED_TYPE['new'][1], LED_INDICATOR_OPTION[1] ] ) nuc_wmi_set_led_indicator_option.assert_called_with( LED_TYPE['new'].index('HDD LED'), LED_INDICATOR_OPTION.index('HDD Activity Indicator'), control_file=None ) nuc_wmi_print.assert_called() self.assertEqual( json.loads(nuc_wmi_print.call_args.args[0]), { 'led': { 'type': LED_TYPE['new'][1], 'indicator_option': LED_INDICATOR_OPTION[1] } } ) self.assertEqual(returned_set_led_indicator_option_cli, None) # Reset nuc_wmi_set_led_indicator_option.reset_mock() nuc_wmi_sys_exit.reset_mock() nuc_wmi_print.reset_mock() # Branch 2: Test that set_led_indicator_option_cli captures raised errors and returns # the proper JSON error response and exit code. nuc_wmi_set_led_indicator_option.side_effect = NucWmiError('Error (Function not supported)') returned_set_led_indicator_option_cli = set_led_indicator_option_cli( [ LED_TYPE['new'][1], LED_INDICATOR_OPTION[1] ] ) nuc_wmi_set_led_indicator_option.assert_called_with( LED_TYPE['new'].index('HDD LED'), LED_INDICATOR_OPTION.index('HDD Activity Indicator'), control_file=None ) nuc_wmi_print.assert_called_with('{"error": "Error (Function not supported)"}') nuc_wmi_sys_exit.assert_called_with(1) self.assertEqual(returned_set_led_indicator_option_cli, None)
def test_query_led_control_items_cli(self, nuc_wmi_query_led_control_items, nuc_wmi_query_led_indicator_options, nuc_wmi_query_led_color_type, nuc_wmi_sys_exit, nuc_wmi_print): """ Tests that `query_led_control_items_cli` returns the expected exceptions, return values, or outputs. """ self.assertTrue(nuc_wmi.cli.query_led.query_led_control_items is nuc_wmi_query_led_control_items) self.assertTrue(nuc_wmi.cli.query_led.query_led_indicator_options is nuc_wmi_query_led_indicator_options) self.assertTrue(nuc_wmi.cli.query_led.query_led_color_type is nuc_wmi_query_led_color_type) self.assertTrue(nuc_wmi.cli.query_led.exit is nuc_wmi_sys_exit) self.assertTrue(nuc_wmi.cli.query_led.print is nuc_wmi_print) # Branch 1: Test that query_led_control_items_cli returns the proper JSON response and exit # code for valid cli args # Get control items for HDD LED set to HDD Activity Indicator expected_control_items = [0x00, 0x01, 0x02, 0x03] nuc_wmi_query_led_color_type.return_value = LED_COLOR_TYPE[ 'new'].index('Dual-color Blue / White') nuc_wmi_query_led_indicator_options.return_value = [0x01, 0x04] nuc_wmi_query_led_control_items.return_value = expected_control_items returned_query_led_control_items_cli = query_led_control_items_cli( [LED_TYPE['new'][1], LED_INDICATOR_OPTION[1]]) nuc_wmi_query_led_color_type.assert_called_with( LED_TYPE['new'].index('HDD LED'), control_file=None) nuc_wmi_query_led_indicator_options.assert_called_with( LED_TYPE['new'].index('HDD LED'), control_file=None) nuc_wmi_query_led_control_items.assert_called_with( LED_TYPE['new'].index('HDD LED'), LED_INDICATOR_OPTION.index('HDD Activity Indicator'), control_file=None) nuc_wmi_print.assert_called() self.assertEqual( json.loads(nuc_wmi_print.call_args.args[0]), { 'led': { 'type': LED_TYPE['new'][1], 'indicator_option': LED_INDICATOR_OPTION[1], 'control_items': [CONTROL_ITEM_HDD_ACTIVITY_INDICATOR_MULTI_COLOR[control_item]['Control Item'] \ for control_item in expected_control_items] } } ) self.assertEqual(returned_query_led_control_items_cli, None) # Reset nuc_wmi_query_led_color_type.reset_mock() nuc_wmi_query_led_indicator_options.reset_mock() nuc_wmi_query_led_control_items.reset_mock() nuc_wmi_sys_exit.reset_mock() nuc_wmi_print.reset_mock() # Branch 2: Test that query_led_control_items_cli captures raised errors and returns # the proper JSON error response and exit code. expected_control_items = [0x00, 0x01, 0x02, 0x03] nuc_wmi_query_led_color_type.return_value = LED_COLOR_TYPE[ 'new'].index('Dual-color Blue / White') nuc_wmi_query_led_indicator_options.return_value = [0x01, 0x04] nuc_wmi_query_led_control_items.side_effect = NucWmiError( 'Error (Function not supported)') returned_query_led_control_items_cli = query_led_control_items_cli( [LED_TYPE['new'][1], LED_INDICATOR_OPTION[1]]) nuc_wmi_query_led_color_type.assert_called_with( LED_TYPE['new'].index('HDD LED'), control_file=None) nuc_wmi_query_led_indicator_options.assert_called_with( LED_TYPE['new'].index('HDD LED'), control_file=None) nuc_wmi_query_led_control_items.assert_called_with( LED_TYPE['new'].index('HDD LED'), LED_INDICATOR_OPTION.index('HDD Activity Indicator'), control_file=None) nuc_wmi_print.assert_called_with( '{"error": "Error (Function not supported)"}') nuc_wmi_sys_exit.assert_called_with(1) self.assertEqual(returned_query_led_control_items_cli, None) # Reset nuc_wmi_query_led_color_type.reset_mock() nuc_wmi_query_led_indicator_options.reset_mock() nuc_wmi_query_led_control_items.reset_mock() nuc_wmi_sys_exit.reset_mock() nuc_wmi_print.reset_mock() # Branch 3: Tests that invalid LED indicator raises appropriate error. expected_control_items = [0x00, 0x01, 0x02, 0x03] nuc_wmi_query_led_color_type.return_value = LED_COLOR_TYPE[ 'new'].index('Dual-color Blue / White') nuc_wmi_query_led_indicator_options.return_value = [] nuc_wmi_query_led_control_items.return_value = expected_control_items returned_query_led_control_items_cli = query_led_control_items_cli( [LED_TYPE['new'][1], LED_INDICATOR_OPTION[1]]) nuc_wmi_query_led_color_type.assert_called_with( LED_TYPE['new'].index('HDD LED'), control_file=None) nuc_wmi_query_led_indicator_options.assert_called_with( LED_TYPE['new'].index('HDD LED'), control_file=None) nuc_wmi_query_led_control_items.assert_not_called() nuc_wmi_print.assert_called_with( '{"error": "Invalid indicator option for the selected LED"}') nuc_wmi_sys_exit.assert_called_with(1) self.assertEqual(returned_query_led_control_items_cli, None)
def test_set_led_cli(self, nuc_wmi_set_led, nuc_wmi_sys_exit, nuc_wmi_print): """ Tests that `set_led_cli` returns the expected exceptions, return values, or outputs. """ self.assertTrue(nuc_wmi.cli.set_led.set_led is \ nuc_wmi_set_led) self.assertTrue(nuc_wmi.cli.set_led.exit is nuc_wmi_sys_exit) self.assertTrue(nuc_wmi.cli.set_led.print is nuc_wmi_print) # Branch 1: Test that set_led_cli returns the proper JSON response and exit # code for valid cli args # Set S0 Ring LED to brightness of 47%, frequency of Always on, and color of Cyan returned_set_led_cli = set_led_cli([ LED_TYPE['legacy'][2], LED_BRIGHTNESS['legacy'][47], LED_BLINK_FREQUENCY['legacy'][4], LED_COLOR['legacy'][LED_COLOR_TYPE['legacy']['S0 Ring LED']][1] ]) nuc_wmi_set_led.assert_called_with( LED_TYPE['legacy'].index('S0 Ring LED'), str(LED_BRIGHTNESS['legacy'].index('47')), LED_BLINK_FREQUENCY['legacy'].index('Always on'), LED_COLOR['legacy'][LED_COLOR_TYPE['legacy']['S0 Ring LED']].index( 'Cyan'), control_file=None) nuc_wmi_print.assert_called() self.assertEqual( json.loads(nuc_wmi_print.call_args.args[0]), { 'led': { 'type': LED_TYPE['legacy'][2], 'brightness': str(LED_BRIGHTNESS['legacy'][47]), 'frequency': LED_BLINK_FREQUENCY['legacy'][4], 'color': LED_COLOR['legacy'][LED_COLOR_TYPE['legacy'] ['S0 Ring LED']][1] } }) self.assertEqual(returned_set_led_cli, None) # Reset nuc_wmi_set_led.reset_mock() nuc_wmi_sys_exit.reset_mock() nuc_wmi_print.reset_mock() # Branch 2: Test that set_led_cli captures raised errors and returns # the proper JSON error response and exit code. nuc_wmi_set_led.side_effect = NucWmiError( 'Error (Function not supported)') returned_set_led_cli = set_led_cli([ LED_TYPE['legacy'][2], LED_BRIGHTNESS['legacy'][47], LED_BLINK_FREQUENCY['legacy'][4], LED_COLOR['legacy'][LED_COLOR_TYPE['legacy']['S0 Ring LED']][1] ]) nuc_wmi_set_led.assert_called_with( LED_TYPE['legacy'].index('S0 Ring LED'), str(LED_BRIGHTNESS['legacy'].index('47')), LED_BLINK_FREQUENCY['legacy'].index('Always on'), LED_COLOR['legacy'][LED_COLOR_TYPE['legacy']['S0 Ring LED']].index( 'Cyan'), control_file=None) nuc_wmi_print.assert_called_with( '{"error": "Error (Function not supported)"}') nuc_wmi_sys_exit.assert_called_with(1) self.assertEqual(returned_set_led_cli, None) # Reset nuc_wmi_set_led.reset_mock() nuc_wmi_sys_exit.reset_mock() nuc_wmi_print.reset_mock() # Branch 3: Tests that invalid LED color raises appropriate error. returned_set_led_cli = set_led_cli([ LED_TYPE['legacy'][1], LED_BRIGHTNESS['legacy'][47], LED_BLINK_FREQUENCY['legacy'][4], LED_COLOR['legacy'][LED_COLOR_TYPE['legacy']['S0 Ring LED']][1] ]) nuc_wmi_set_led.assert_not_called() nuc_wmi_print.assert_called_with( '{"error": "Invalid color for the specified legacy LED"}') nuc_wmi_sys_exit.assert_called_with(1) self.assertEqual(returned_set_led_cli, None)