def set_led_indicator_option_cli(cli_args=None):
    """
    Creates a CLI interface on top of the `nuc_wmi.set_led_indicator_option` `set_led_indicator_option` function.

    Args:
       cli_args: If provided, overrides the CLI args to use for `argparse`.
    CLI Args:
       led_indicator_option: The indicator option for the specified LED type for which to set the indicator option.
       led: Selects the LED to set the indicator option for.
    CLI Options:
       --control_file <control_file>: Sets the control file to use if provided,
                                      otherwise `nuc_wmi.CONTROL_FILE` is used.
    Outputs:
       stdout: JSON object with the set indicator option of the selected LED or error message with
               failure error.
    Exit code:
       0 on successfully setting the selected LED's indicator option or 1 on error.
    """

    parser = ArgumentParser(
        description='Set the LED indicator option for the specified LED type,')

    parser.add_argument(
        '-c',
        '--control-file',
        default=None,
        help='The path to the NUC WMI control file. Defaults to ' +
        CONTROL_FILE + ' if not specified.')
    parser.add_argument('led',
                        choices=LED_TYPE['new'],
                        help='The LED for which to set the indicator option.')
    parser.add_argument('led_indicator_option',
                        choices=LED_INDICATOR_OPTION,
                        help='The LED indicator option to set for the LED.')

    try:
        args = parser.parse_args(args=cli_args)

        set_led_indicator_option(LED_TYPE['new'].index(args.led),
                                 LED_INDICATOR_OPTION.index(
                                     args.led_indicator_option),
                                 control_file=args.control_file)

        print(
            dumps({
                'led': {
                    'type': args.led,
                    'indicator_option': args.led_indicator_option
                }
            }))
    except Exception as err:
        print(dumps({'error': str(err)}))

        exit(1)
    def test_Set_led_control_item(self, nuc_wmi_write_control_file, nuc_wmi_read_control_file):
        """
        Tests that `Set_led_control_item` returns the expected exceptions, return values, or outputs.
        """

        self.assertTrue(nuc_wmi.set_led_control_item.read_control_file is nuc_wmi_read_control_file)
        self.assertTrue(nuc_wmi.set_led_control_item.write_control_file is nuc_wmi_write_control_file)

        # Branch 1: Test that set_led_control_item sends the expected byte string to the control file
        #           and that the returned control file response is properly processed.

        # Set HDD LED HDD Activity Indicator Brightness of 30% for multi color led
        expected_write_byte_list = [
            METHOD_ID,
            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
                }
            ),
            LED_BRIGHTNESS_MULTI_COLOR.index('30')
        ]
        read_byte_list = [
            0x00,
            0x00,
            0x00,
            0x00
        ]

        nuc_wmi_read_control_file.return_value = read_byte_list
        returned_set_led_control_item = set_led_control_item(
            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
                }
            ),
            LED_BRIGHTNESS_MULTI_COLOR.index('30')
        )

        nuc_wmi_write_control_file.assert_called_with(expected_write_byte_list, control_file=None)

        self.assertEqual(returned_set_led_control_item, None)

        # Reset
        nuc_wmi_read_control_file.reset_mock()
        nuc_wmi_write_control_file.reset_mock()

        # Branch 2: Test that set_led_control_item raises an exception when the control file returns an
        #           error code.

        # Incorrect led
        expected_write_byte_list = [
            METHOD_ID,
            len(LED_TYPE['new']), # Incorrect led
            LED_INDICATOR_OPTION.index('HDD Activity Indicator'),
            CONTROL_ITEM_HDD_ACTIVITY_INDICATOR_MULTI_COLOR.index(
                {
                    'Control Item': 'Brightness',
                    'Options': LED_BRIGHTNESS_MULTI_COLOR
                }
            ),
            LED_BRIGHTNESS_MULTI_COLOR.index('30')
        ]
        read_byte_list = [
            0xE4,
            0x00,
            0x00,
            0x00
        ]

        nuc_wmi_read_control_file.return_value = read_byte_list

        with self.assertRaises(NucWmiError) as err:
            returned_set_led_control_item = set_led_control_item(
                len(LED_TYPE['new']), # Incorrect led
                LED_INDICATOR_OPTION.index('HDD Activity Indicator'),
                CONTROL_ITEM_HDD_ACTIVITY_INDICATOR_MULTI_COLOR.index(
                    {
                        'Control Item': 'Brightness',
                        'Options': LED_BRIGHTNESS_MULTI_COLOR
                    }
                ),
                LED_BRIGHTNESS_MULTI_COLOR.index('30')
            )

        nuc_wmi_write_control_file.assert_called_with(expected_write_byte_list, control_file=None)

        self.assertEqual(str(err.exception), 'Error (Invalid Parameter)')
Exemplo n.º 3
0
    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)
Exemplo n.º 4
0
    def test_get_led_control_item_cli2(
            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 6: Test getting control item that uses color

        # Get HDD LED control item color of White for Color of HDD Activity Indicator
        expected_color = LED_COLOR['new']['Dual-color Blue / White'].index('White')
        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_color
        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[1]['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': 'Color',
                    'Options': LED_COLOR['new']
                }
            ),
            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[1]['Control Item'],
                    'control_item_value': LED_COLOR['new']['Dual-color Blue / White'][expected_color]
                }
            }
        )

        self.assertEqual(returned_get_led_control_item_cli, None)
Exemplo n.º 5
0
    def test_set_led_indicator_option(self, nuc_wmi_write_control_file, nuc_wmi_read_control_file):
        """
        Tests that `set_led_indicator_option` returns the expected exceptions, return values, or outputs.
        """

        self.assertTrue(nuc_wmi.set_led_indicator_option.read_control_file is nuc_wmi_read_control_file)
        self.assertTrue(nuc_wmi.set_led_indicator_option.write_control_file is nuc_wmi_write_control_file)

        # Branch 1: Test that set_led_indicator_option sends the expected byte string to the control file
        #           and that the returned control file response is properly processed.

        # Set HDD LED with HDD Activity Indicator
        expected_write_byte_list = [
            METHOD_ID,
            LED_TYPE['new'].index('HDD LED'),
            LED_INDICATOR_OPTION.index('HDD Activity Indicator')
        ]
        read_byte_list = [
            0x00,
            0x00,
            0x00,
            0x00
        ]

        nuc_wmi_read_control_file.return_value = read_byte_list
        returned_set_led_indicator_option = set_led_indicator_option(
            LED_TYPE['new'].index('HDD LED'),
            LED_INDICATOR_OPTION.index('HDD Activity Indicator')
        )

        nuc_wmi_write_control_file.assert_called_with(expected_write_byte_list, control_file=None)

        self.assertEqual(returned_set_led_indicator_option, None)

        # Reset
        nuc_wmi_read_control_file.reset_mock()
        nuc_wmi_write_control_file.reset_mock()

        # Branch 2: Test that set_led_indicator_option raises an exception when the control file returns an
        #           error code.

        # Incorrect led
        expected_write_byte_list = [
            METHOD_ID,
            len(LED_TYPE['new']), # Incorrect led
            LED_INDICATOR_OPTION.index('HDD Activity Indicator')
        ]
        read_byte_list = [
            0xE4,
            0x00,
            0x00,
            0x00
        ]

        nuc_wmi_read_control_file.return_value = read_byte_list

        with self.assertRaises(NucWmiError) as err:
            returned_set_led_indicator_option = set_led_indicator_option(
                len(LED_TYPE['new']), # Incorrect led
                LED_INDICATOR_OPTION.index('HDD Activity Indicator')
            )

        nuc_wmi_write_control_file.assert_called_with(expected_write_byte_list, control_file=None)

        self.assertEqual(str(err.exception), 'Error (Invalid Parameter)')
def set_led_control_item_cli(cli_args=None):
    """
    Creates a CLI interface on top of the `nuc_wmi.set_led_control` `set_led_control_item` function.

    Args:
       cli_args: If provided, overrides the CLI args to use for `argparse`.
    CLI Args:
       control_item: The control item of the specified LED type indicator option for which to set the value.
       control_item_value: The value for the control item to set.
       led_indicator_option: The indicator option for the specified LED type for which to set the control
                            item value.
       led: Selects the LED to set the control item for.
    CLI Options:
       --control_file <control_file>: Sets the control file to use if provided,
                                      otherwise `nuc_wmi.CONTROL_FILE` is used.
    Outputs:
       stdout: JSON object with control item value for the control item of the indicator option for the selected LED or
               error message with failure error.
    Exit code:
       0 on successfully setting the control item value or 1 on error.
    """

    control_item_labels = list()
    control_item_values = list()

    for indicator_option in CONTROL_ITEM:
        if indicator_option is None:
            continue

        for control_items in indicator_option:
            if control_items is None:
                continue

            for control_item in control_items:
                control_item_labels.append(control_item['Control Item'])

                if control_item['Options'] is not None and control_item[
                        'Options'] != LED_COLOR['new']:
                    control_item_values.extend(control_item['Options'])

    control_item_values.extend(LED_COLOR['new']['Dual-color Blue / Amber'])
    control_item_values.extend(LED_COLOR['new']['Dual-color Blue / White'])

    parser = ArgumentParser(
        description='Set the control item value for the control item of the indicator option ' + \
        'for the specified LED type.'
    )

    parser.add_argument(
        '-c',
        '--control-file',
        default=None,
        help='The path to the NUC WMI control file. Defaults to ' +
        CONTROL_FILE + ' if not specified.')
    parser.add_argument(
        'led',
        choices=LED_TYPE['new'],
        help='The LED for which to set the control item value.')
    parser.add_argument('led_indicator_option',
                        choices=LED_INDICATOR_OPTION,
                        help='The LED indicator option for the LED.')
    parser.add_argument(
        'control_item',
        choices=set(control_item_labels),
        help='The control item for the LED indicator option that is being set.'
    )
    parser.add_argument(
        'control_item_value',
        choices=set(control_item_values),
        help=
        'The control item value for the control item for the LED indicator option that is being set.'
    )

    try:
        args = parser.parse_args(args=cli_args)

        available_indicator_options = query_led_indicator_options(
            LED_TYPE['new'].index(args.led), control_file=args.control_file)

        led_color_type = query_led_color_type(LED_TYPE['new'].index(args.led),
                                              control_file=args.control_file)

        indicator = LED_INDICATOR_OPTION.index(args.led_indicator_option)

        if indicator not in available_indicator_options:
            raise ValueError('Invalid indicator option for the selected LED')

        control_items = CONTROL_ITEM[indicator][led_color_type]

        if control_items is None:
            raise ValueError(
                'No control items are available for the selected LED and indicator option'
            )

        control_item_index = None

        for index, control_item in enumerate(control_items):
            if control_item['Control Item'] == args.control_item:
                control_item_index = index

        if control_item_index is None:
            raise ValueError(
                'Invalid control item specified for the selected LED and indicator option'
            )

        try:
            # Convert the control item value into its index
            if control_items[control_item_index]['Options'] == LED_COLOR[
                    'new']:
                control_item_value_index = control_items[control_item_index][
                    'Options'][LED_COLOR_TYPE['new'][led_color_type]].index(
                        args.control_item_value)
            else:
                control_item_value_index = control_items[control_item_index][
                    'Options'].index(args.control_item_value)
        except ValueError as err:
            raise ValueError(
                'Invalid control item value for the specified control item')

        set_led_control_item(LED_TYPE['new'].index(args.led),
                             indicator,
                             control_item_index,
                             control_item_value_index,
                             control_file=args.control_file)

        print(
            dumps({
                'led': {
                    'type': args.led,
                    'indicator_option': args.led_indicator_option,
                    'control_item': args.control_item,
                    'control_item_value': args.control_item_value
                }
            }))
    except Exception as err:
        print(dumps({'error': str(err)}))

        exit(1)
Exemplo n.º 7
0
    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)
Exemplo n.º 8
0
    def test_query_led_control_items(self, nuc_wmi_write_control_file,
                                     nuc_wmi_read_control_file,
                                     nuc_wmi_query_led_color_type):
        """
        Tests that `query_led_control_items` returns the expected exceptions, return values, or
        outputs.
        """

        self.assertTrue(
            nuc_wmi.query_led.read_control_file is nuc_wmi_read_control_file)
        self.assertTrue(
            nuc_wmi.query_led.write_control_file is nuc_wmi_write_control_file)
        self.assertTrue(nuc_wmi.query_led.query_led_color_type is
                        nuc_wmi_query_led_color_type)

        # Branch 1: Test that query_led_control_items send the expected byte string to the control file
        #           and that the returned control file response is properly processed.

        # Query control items of HDD LED with HDD Activity Indicator
        expected_query_led_control_items = [0x00, 0x01, 0x02, 0x03]
        expected_write_byte_list = [
            METHOD_ID,
            QUERY_TYPE.index('query_led_control_items'),
            LED_TYPE['new'].index('HDD LED'),
            LED_INDICATOR_OPTION.index('HDD Activity Indicator')
        ]
        read_byte_list = [0x00, 0x0F, 0x00, 0x00]

        nuc_wmi_read_control_file.return_value = read_byte_list
        nuc_wmi_query_led_color_type.return_value = LED_COLOR_TYPE[
            'new'].index('Dual-color Blue / White')
        returned_query_led_control_items = query_led_control_items(
            LED_TYPE['new'].index('HDD LED'),
            LED_INDICATOR_OPTION.index('HDD Activity Indicator'))

        nuc_wmi_write_control_file.assert_called_with(expected_write_byte_list,
                                                      control_file=None)
        nuc_wmi_query_led_color_type.assert_called_with(
            LED_TYPE['new'].index('HDD LED'), control_file=None)

        self.assertEqual(returned_query_led_control_items,
                         expected_query_led_control_items)

        # Reset
        nuc_wmi_read_control_file.reset_mock()
        nuc_wmi_write_control_file.reset_mock()
        nuc_wmi_query_led_color_type.reset_mock()

        # Query control items of HDD LED with Disabled Indicator
        expected_query_led_control_items = []
        expected_write_byte_list = [
            METHOD_ID,
            QUERY_TYPE.index('query_led_control_items'),
            LED_TYPE['new'].index('HDD LED'), LED_INDICATOR_OPTION_DISABLED
        ]
        read_byte_list = [0x00, 0x00, 0x00, 0x00]

        nuc_wmi_read_control_file.return_value = read_byte_list
        nuc_wmi_query_led_color_type.return_value = LED_COLOR_TYPE[
            'new'].index('Dual-color Blue / White')
        returned_query_led_control_items = query_led_control_items(
            LED_TYPE['new'].index('HDD LED'), LED_INDICATOR_OPTION_DISABLED)

        nuc_wmi_write_control_file.assert_called_with(expected_write_byte_list,
                                                      control_file=None)
        nuc_wmi_query_led_color_type.assert_called_with(
            LED_TYPE['new'].index('HDD LED'), control_file=None)

        self.assertEqual(returned_query_led_control_items,
                         expected_query_led_control_items)

        # Reset
        nuc_wmi_read_control_file.reset_mock()
        nuc_wmi_write_control_file.reset_mock()
        nuc_wmi_query_led_color_type.reset_mock()

        # Branch 2: Test that query_led_control_items raises an exception when the control file returns an
        #           error code.

        # Incorrect led
        expected_query_led_control_items = [0x00, 0x01, 0x02, 0x03]
        expected_write_byte_list = [
            METHOD_ID,
            QUERY_TYPE.index('query_led_control_items'),
            len(LED_TYPE['new']),  # Invalid led index
            LED_INDICATOR_OPTION.index('HDD Activity Indicator')
        ]
        read_byte_list = [0xE4, 0x00, 0x00, 0x00]

        nuc_wmi_read_control_file.return_value = read_byte_list
        nuc_wmi_query_led_color_type.return_value = LED_COLOR_TYPE[
            'new'].index('Dual-color Blue / White')

        with self.assertRaises(NucWmiError) as err:
            returned_query_led_control_items = query_led_control_items(
                len(LED_TYPE['new']),  # Invalid led index
                LED_INDICATOR_OPTION.index('HDD Activity Indicator'))

        nuc_wmi_write_control_file.assert_called_with(expected_write_byte_list,
                                                      control_file=None)
        nuc_wmi_query_led_color_type.assert_called_with(len(LED_TYPE['new']),
                                                        control_file=None)

        self.assertEqual(str(err.exception), 'Error (Invalid Parameter)')
Exemplo n.º 9
0
def query_led_control_items_cli(cli_args=None):
    """
    Creates a CLI interface on top of the `nuc_wmi.query_led` `query_led_control_items` function.

    Args:
       cli_args: If provided, overrides the CLI args to use for `argparse`.
    CLI Args:
       led_indicator_option: The indicator option for the specified LED type for which to retrieve the available control
                             items.
       led: Selects the LED to get the available control items for.
    CLI Options:
       --control_file <control_file>: Sets the control file to use if provided,
                                      otherwise `nuc_wmi.CONTROL_FILE` is used.
    Outputs:
       stdout: JSON object with control items for the selected LED indicator option or
               error message with failure error.
    Exit code:
       0 on successfully retrieving the control items or 1 on error.
    """

    parser = ArgumentParser(
        description=
        'Query the LED control items for the LED indicator option of the LED type.'
    )

    parser.add_argument(
        '-c',
        '--control-file',
        default=None,
        help='The path to the NUC WMI control file. Defaults to ' +
        CONTROL_FILE + ' if not specified.')
    parser.add_argument('led',
                        choices=LED_TYPE['new'],
                        help='The LED for which to get the control items.')
    parser.add_argument(
        'led_indicator_option',
        choices=LED_INDICATOR_OPTION,
        help=
        'The LED indicator option for the LED for which to get control items.')

    try:
        args = parser.parse_args(args=cli_args)

        led_color_type = query_led_color_type(LED_TYPE['new'].index(args.led),
                                              control_file=args.control_file)

        available_indicator_options = query_led_indicator_options(
            LED_TYPE['new'].index(args.led), control_file=args.control_file)

        indicator = LED_INDICATOR_OPTION.index(args.led_indicator_option)

        if indicator not in available_indicator_options:
            raise ValueError('Invalid indicator option for the selected LED')

        control_items = query_led_control_items(LED_TYPE['new'].index(
            args.led),
                                                indicator,
                                                control_file=args.control_file)

        print(
            dumps(
                {
                    'led': {
                        'type': args.led,
                        'indicator_option': args.led_indicator_option,
                        'control_items': [CONTROL_ITEM[indicator][led_color_type][control_item]['Control Item'] \
                                          for control_item in control_items]
                    }
                }
            )
        )
    except Exception as err:
        print(dumps({'error': str(err)}))

        exit(1)
    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)