Ejemplo n.º 1
0
    def test_with_default_parser(self) -> None:
        builder = CommandBuilder()
        self.assertIsNone(builder._parser)

        builder = CommandBuilder().with_default_parser()
        self.assertIsInstance(builder._parser, SimpleParser)

        builder = CommandBuilder().verbose().with_default_parser()
        self.assertIsInstance(builder._parser, VerboseParser)
Ejemplo n.º 2
0
    def test_with_pcilib_params_dict(self, lspci_mock: MagicMock) -> None:
        with self.assertRaisesRegex(AssertionError, 'dict or keyword'):
            CommandBuilder().with_pcilib_params({'a': 'b'}, c='d')
        with self.assertRaisesRegex(AssertionError, 'Only one positional'):
            CommandBuilder().with_pcilib_params({'a': 'b'}, {'c': 'd'})

        lspci_mock.return_value = ['a', 'b']
        builder = CommandBuilder().with_pcilib_params({'a': 'b'})
        self.assertListEqual(list(builder), ['a', 'b'])
        self.assertEqual(lspci_mock.call_count, 1)
        self.assertEqual(lspci_mock.call_args, call(pcilib_params={'a': 'b'}))
Ejemplo n.º 3
0
    def test_with_names(self, lspci_mock: MagicMock) -> None:
        lspci_mock.return_value = ['a', 'b']
        builder = CommandBuilder().with_names(False)
        self.assertListEqual(list(builder), ['a', 'b'])
        self.assertEqual(lspci_mock.call_count, 1)
        self.assertEqual(lspci_mock.call_args,
                         call(id_resolve_option=IDResolveOption.IDOnly, ))

        lspci_mock.reset_mock()
        builder = builder.with_names()
        self.assertListEqual(list(builder), ['a', 'b'])
        self.assertEqual(lspci_mock.call_count, 1)
        self.assertEqual(lspci_mock.call_args,
                         call(id_resolve_option=IDResolveOption.Both, ))
Ejemplo n.º 4
0
    def test_device_filter_str(self, lspci_mock: MagicMock,
                               filter_mock: MagicMock) -> None:
        with self.assertRaisesRegex(AssertionError, 'Only one positional'):
            CommandBuilder().device_filter('something', 'something else')
        with self.assertRaisesRegex(AssertionError, 'Use either'):
            CommandBuilder().device_filter('something', vendor=0xb)

        filter_mock.parse.return_value = 'lefilter'
        lspci_mock.return_value = ['a', 'b']
        builder = CommandBuilder().device_filter('something')
        self.assertListEqual(list(builder), ['a', 'b'])
        self.assertEqual(lspci_mock.call_count, 1)
        self.assertEqual(lspci_mock.call_args, call(device_filter='lefilter'))
        self.assertEqual(filter_mock.parse.call_count, 1)
        self.assertEqual(filter_mock.parse.call_args, call('something'))
Ejemplo n.º 5
0
 def test_use_pcimap_check(self, lspci_mock: MagicMock,
                           isfile_mock: MagicMock) -> None:
     lspci_mock.return_value = ['a', 'b']
     isfile_mock.return_value = False
     with self.assertRaisesRegex(AssertionError, 'not found'):
         CommandBuilder().use_pcimap('somefile')
     self.assertEqual(isfile_mock.call_count, 1)
Ejemplo n.º 6
0
 def test_list_pcilib_params_raw(self, list_mock: MagicMock) -> None:
     list_mock.return_value = ['a', 'b']
     builder = CommandBuilder() \
         .list_access_methods() \
         .list_pcilib_params(raw=True)
     self.assertListEqual(list(builder), ['a', 'b'])
     self.assertEqual(list_mock.call_count, 1)
     self.assertEqual(list_mock.call_args, call())
Ejemplo n.º 7
0
 def test_use_access_method(self, lspci_mock: MagicMock) -> None:
     lspci_mock.return_value = ['a', 'b']
     builder = CommandBuilder() \
         .use_access_method('one') \
         .use_access_method('two')
     self.assertListEqual(list(builder), ['a', 'b'])
     self.assertEqual(lspci_mock.call_count, 1)
     self.assertEqual(lspci_mock.call_args, call(access_method='two'))
Ejemplo n.º 8
0
 def test_with_parser(self, lspci_mock: MagicMock) -> None:
     lspci_mock.return_value = ['a', 'b']
     parser_mock = MagicMock(spec=SimpleParser)
     parser_mock.parse.return_value = ('parsed_a', 'parsed_b')
     builder = CommandBuilder().with_parser(parser_mock)
     self.assertListEqual(list(builder), ['parsed_a', 'parsed_b'])
     self.assertEqual(parser_mock.parse.call_count, 1)
     self.assertEqual(parser_mock.parse.call_args, call(['a', 'b']))
Ejemplo n.º 9
0
 def test_include_bridge_paths(self, lspci_mock: MagicMock) -> None:
     lspci_mock.return_value = ['a', 'b']
     builder = CommandBuilder() \
         .include_bridge_paths(False) \
         .include_bridge_paths()
     self.assertListEqual(list(builder), ['a', 'b'])
     self.assertEqual(lspci_mock.call_count, 1)
     self.assertEqual(lspci_mock.call_args, call(bridge_paths=True, ))
Ejemplo n.º 10
0
 def test_use_pcimap_no_check(self, lspci_mock: MagicMock,
                              isfile_mock: MagicMock) -> None:
     lspci_mock.return_value = ['a', 'b']
     isfile_mock.return_value = False
     builder = CommandBuilder().use_pcimap('somefile', check=False)
     self.assertListEqual(list(builder), ['a', 'b'])
     self.assertEqual(lspci_mock.call_count, 1)
     self.assertEqual(lspci_mock.call_args, call(pcimap=Path('somefile')))
     self.assertFalse(isfile_mock.called)
Ejemplo n.º 11
0
 def test_use_pciids(self, lspci_mock: MagicMock,
                     isfile_mock: MagicMock) -> None:
     lspci_mock.return_value = ['a', 'b']
     isfile_mock.return_value = True
     builder = CommandBuilder().use_pciids('somefile')
     self.assertListEqual(list(builder), ['a', 'b'])
     self.assertEqual(lspci_mock.call_count, 1)
     self.assertEqual(lspci_mock.call_args, call(pciids=Path('somefile')))
     self.assertEqual(isfile_mock.call_count, 1)
Ejemplo n.º 12
0
 def test_iter_str(self, lspci_mock: MagicMock) -> None:
     """
     Test iterating the CommandBuilder when lspci returns a single string
     returns an iterator for a list made of a single string, not an iterator
     for each character of the string
     """
     lspci_mock.return_value = 'a\nb'
     self.assertListEqual(list(CommandBuilder()), [
         'a\nb',
     ])
     self.assertEqual(lspci_mock.call_count, 1)
     self.assertEqual(lspci_mock.call_args, call())
Ejemplo n.º 13
0
 def test_include_kernel_drivers(self, lspci_mock: MagicMock) -> None:
     lspci_mock.return_value = ['a', 'b']
     builder = CommandBuilder() \
         .include_kernel_drivers(False) \
         .include_kernel_drivers()
     self.assertListEqual(list(builder), ['a', 'b'])
     self.assertEqual(lspci_mock.call_count, 1)
     self.assertEqual(lspci_mock.call_args,
                      call(
                          verbose=True,
                          kernel_drivers=True,
                      ))
Ejemplo n.º 14
0
 def test_with_pcilib_params_kwargs(self, lspci_mock: MagicMock) -> None:
     lspci_mock.return_value = ['a', 'b']
     builder = CommandBuilder() \
         .with_pcilib_params(a='1', b='2') \
         .with_pcilib_params(b='3', c='4')
     self.assertListEqual(list(builder), ['a', 'b'])
     self.assertEqual(lspci_mock.call_count, 1)
     self.assertEqual(lspci_mock.call_args,
                      call(pcilib_params={
                          'a': '1',
                          'b': '3',
                          'c': '4',
                      }))
Ejemplo n.º 15
0
def main() -> None:
    parser: argparse.ArgumentParser = get_parser()
    args: Dict[str, Any] = vars(parser.parse_args())

    # Specific parsing required
    use_parser: bool = args.pop('json', True)
    kernel_modules: bool = args.pop('kernel_modules', False)
    access_method: Optional[str] = args.pop('access_method', None)
    pcilib_params = args.pop('pcilib_params', []) or []

    builder: CommandBuilder = CommandBuilder(**args)
    if kernel_modules:
        builder = builder.include_kernel_drivers()

    if access_method:
        if access_method.strip().lower() == 'help':
            builder = builder.list_access_methods()
        else:
            builder = builder.use_access_method(access_method)

    for param in pcilib_params:
        if param.strip().lower() == 'help':
            builder = builder.list_pcilib_params(raw=not use_parser)
            break
        if '=' not in param:
            parser.error(
                'Invalid PCI access parameter syntax for {!r}'.format(param))
        key, value = map(str.strip, param.split('=', 2))
        builder = builder.with_pcilib_params(**{key: value})

    if use_parser:
        builder = builder.with_default_parser()

    result = list(builder)
    if not use_parser:  # Raw mode
        for item in result:
            print(item)
        return

    def _item_handler(item: Any) -> Any:
        if hasattr(item, '_asdict'):
            return item._asdict()
        return item

    print(json.dumps(
        list(map(_item_handler, result)),
        default=vars,
    ))
Ejemplo n.º 16
0
 def test_device_filter_kwargs(self, lspci_mock: MagicMock,
                               filter_mock: MagicMock) -> None:
     filter_mock.return_value = 'lefilter'
     lspci_mock.return_value = ['a', 'b']
     builder = CommandBuilder().device_filter(
         cls=0xa,
         vendor=0xb,
         device=0xc,
     )
     self.assertListEqual(list(builder), ['a', 'b'])
     self.assertEqual(lspci_mock.call_count, 1)
     self.assertEqual(lspci_mock.call_args, call(device_filter='lefilter'))
     self.assertEqual(filter_mock.call_count, 1)
     self.assertEqual(filter_mock.call_args,
                      call(
                          cls=0xa,
                          vendor=0xb,
                          device=0xc,
                      ))
Ejemplo n.º 17
0
 def test_slot_filter_kwargs(self, lspci_mock: MagicMock,
                             filter_mock: MagicMock) -> None:
     filter_mock.return_value = 'lefilter'
     lspci_mock.return_value = ['a', 'b']
     builder = CommandBuilder().slot_filter(
         domain=0xa,
         bus=0xb,
         device=0xc,
         function=0xd,
     )
     self.assertListEqual(list(builder), ['a', 'b'])
     self.assertEqual(lspci_mock.call_count, 1)
     self.assertEqual(lspci_mock.call_args, call(slot_filter='lefilter'))
     self.assertEqual(filter_mock.call_count, 1)
     self.assertEqual(filter_mock.call_args,
                      call(
                          domain=0xa,
                          bus=0xb,
                          device=0xc,
                          function=0xd,
                      ))
Ejemplo n.º 18
0
 def test_verbose(self, lspci_mock: MagicMock) -> None:
     lspci_mock.return_value = ['a', 'b']
     builder = CommandBuilder().verbose()
     self.assertListEqual(list(builder), ['a', 'b'])
     self.assertEqual(lspci_mock.call_count, 1)
     self.assertEqual(lspci_mock.call_args, call(verbose=True))
Ejemplo n.º 19
0
 def test_default(self, lspci_mock: MagicMock) -> None:
     lspci_mock.return_value = ['a', 'b']
     self.assertListEqual(list(CommandBuilder()), ['a', 'b'])
     self.assertEqual(lspci_mock.call_count, 1)
     self.assertEqual(lspci_mock.call_args, call())
Ejemplo n.º 20
0
 def test_hide_single_domain(self, lspci_mock: MagicMock) -> None:
     lspci_mock.return_value = ['a', 'b']
     builder = CommandBuilder().hide_single_domain()
     self.assertListEqual(list(builder), ['a', 'b'])
     self.assertEqual(lspci_mock.call_count, 1)
     self.assertEqual(lspci_mock.call_args, call(hide_single_domain=True))