Пример #1
0
 def test_disk_output_colors(self):
     """Test `output` disk level coloring."""
     # This dict's values are tuples of used blocks, and the level's corresponding color.
     # For reference, this test uses a disk whose total block count is 100.
     levels = {
         'normal': (45.0, Colors.GREEN_NORMAL),
         'warning': (70.0, Colors.YELLOW_NORMAL),
         'danger': (95.0, Colors.RED_NORMAL)
     }
     for level, blocks_color_tuple in levels.items():
         with self.subTest(level):
             self.disk_instance_mock.value = {
                 'mount_point': {
                     'device_path': '/dev/my-cool-disk',
                     'used_blocks': blocks_color_tuple[0],
                     'total_blocks': 100
                 }
             }
             Disk.output(self.disk_instance_mock, self.output_mock)
             self.output_mock.append.assert_called_with(
                 'Disk',
                 f'{blocks_color_tuple[1]}{blocks_color_tuple[0]} KiB{Colors.CLEAR} / 100.0 KiB'
             )
Пример #2
0
    def test_disk_multiline_output(self):
        """Test `output`'s multi-line capability."""
        self.disk_instance_mock.value = {
            'first_mount_point': {
                'device_path': '/dev/my-cool-disk',
                'used_blocks': 10,
                'total_blocks': 10
            },
            'second_mount_point': {
                'device_path': '/dev/my-cooler-disk',
                'used_blocks': 10,
                'total_blocks': 30
            }
        }

        with self.subTest('Single-line combined output.'):
            Disk.output(self.disk_instance_mock, self.output_mock)
            self.output_mock.append.assert_called_once_with(
                'Disk',
                f'{Colors.YELLOW_NORMAL}20.0 KiB{Colors.CLEAR} / 40.0 KiB'
            )

        self.output_mock.reset_mock()

        with self.subTest('Multi-line output'):
            self.disk_instance_mock.options['combine_total'] = False
            Disk.output(self.disk_instance_mock, self.output_mock)
            self.assertEqual(self.output_mock.append.call_count, 2)
            self.output_mock.append.assert_has_calls(
                [
                    call(
                        'Disk',
                        f'{Colors.RED_NORMAL}10.0 KiB{Colors.CLEAR} / 10.0 KiB'
                    ),
                    call(
                        'Disk',
                        f'{Colors.GREEN_NORMAL}10.0 KiB{Colors.CLEAR} / 30.0 KiB'
                    )
                ]
            )

        self.output_mock.reset_mock()

        with self.subTest('Entry name labeling (device path with entry name)'):
            self.disk_instance_mock.options = {
                'combine_total': False,
                'disk_labels': 'device_paths'
            }

            Disk.output(self.disk_instance_mock, self.output_mock)
            self.assertEqual(self.output_mock.append.call_count, 2)
            self.output_mock.append.assert_has_calls(
                [
                    call(
                        'Disk (/dev/my-cool-disk)',
                        f'{Colors.RED_NORMAL}10.0 KiB{Colors.CLEAR} / 10.0 KiB'
                    ),
                    call(
                        'Disk (/dev/my-cooler-disk)',
                        f'{Colors.GREEN_NORMAL}10.0 KiB{Colors.CLEAR} / 30.0 KiB'
                    )
                ]
            )

        self.output_mock.reset_mock()

        with self.subTest('Entry name labeling (mount points without entry name)'):
            self.disk_instance_mock.options = {
                'combine_total': False,
                'disk_labels': 'mount_points',
                'hide_entry_name': True
            }

            Disk.output(self.disk_instance_mock, self.output_mock)
            self.assertEqual(self.output_mock.append.call_count, 2)
            self.output_mock.append.assert_has_calls(
                [
                    call(
                        '(first_mount_point)',
                        f'{Colors.RED_NORMAL}10.0 KiB{Colors.CLEAR} / 10.0 KiB'
                    ),
                    call(
                        '(second_mount_point)',
                        f'{Colors.GREEN_NORMAL}10.0 KiB{Colors.CLEAR} / 30.0 KiB'
                    )
                ]
            )

        self.output_mock.reset_mock()

        with self.subTest('Entry name labeling (without disk label nor entry name)'):
            self.disk_instance_mock.options = {
                'combine_total': False,
                'disk_labels': False,
                # `hide_entry_name` is being ignored as `disk_labels` evaluates to "falsy" too.
                'hide_entry_name': True
            }

            Disk.output(self.disk_instance_mock, self.output_mock)
            self.assertEqual(self.output_mock.append.call_count, 2)
            self.output_mock.append.assert_has_calls(
                [
                    call(
                        'Disk',
                        f'{Colors.RED_NORMAL}10.0 KiB{Colors.CLEAR} / 10.0 KiB'
                    ),
                    call(
                        'Disk',
                        f'{Colors.GREEN_NORMAL}10.0 KiB{Colors.CLEAR} / 30.0 KiB'
                    )
                ]
            )
Пример #3
0
    def test_disk_multiline_output(self):
        """Test `output`'s multi-line capability."""
        self.disk_instance_mock.value = {
            'first_mount_point': {
                'device_path': '/dev/my-cool-disk',
                'used_blocks': 10,
                'total_blocks': 10
            },
            'second_mount_point': {
                'device_path': '/dev/my-cooler-disk',
                'used_blocks': 10,
                'total_blocks': 30
            }
        }

        with self.subTest('Single-line combined output.'):
            Disk.output(self.disk_instance_mock, self.output_mock)
            self.output_mock.append.assert_called_once_with(
                'Disk',
                '{0}20.0 KiB{1} / 40.0 KiB'.format(Colors.YELLOW_NORMAL,
                                                   Colors.CLEAR))

        self.output_mock.reset_mock()

        with self.subTest('Multi-line output'):
            self.disk_instance_mock._configuration['disk'][
                'combine_total'] = False  # pylint: disable=protected-access
            Disk.output(self.disk_instance_mock, self.output_mock)
            self.assertEqual(self.output_mock.append.call_count, 2)
            self.output_mock.append.assert_has_calls(
                [
                    call(
                        'Disk', '{0}10.0 KiB{1} / 10.0 KiB'.format(
                            Colors.RED_NORMAL, Colors.CLEAR)),
                    call(
                        'Disk', '{0}10.0 KiB{1} / 30.0 KiB'.format(
                            Colors.GREEN_NORMAL, Colors.CLEAR))
                ],
                any_order=
                True  # Since Python < 3.6 doesn't have definite `dict` ordering.
            )

        self.output_mock.reset_mock()

        with self.subTest('Entry name labeling (device path with entry name)'):
            self.disk_instance_mock._configuration['disk'][
                'combine_total'] = False  # pylint: disable=protected-access
            self.disk_instance_mock._configuration['disk'][
                'disk_labels'] = 'device_paths'  # pylint: disable=protected-access

            Disk.output(self.disk_instance_mock, self.output_mock)
            self.assertEqual(self.output_mock.append.call_count, 2)
            self.output_mock.append.assert_has_calls(
                [
                    call(
                        'Disk (/dev/my-cool-disk)',
                        '{0}10.0 KiB{1} / 10.0 KiB'.format(
                            Colors.RED_NORMAL, Colors.CLEAR)),
                    call(
                        'Disk (/dev/my-cooler-disk)',
                        '{0}10.0 KiB{1} / 30.0 KiB'.format(
                            Colors.GREEN_NORMAL, Colors.CLEAR))
                ],
                any_order=
                True  # Since Python < 3.6 doesn't have definite `dict` ordering.
            )

        self.output_mock.reset_mock()

        with self.subTest(
                'Entry name labeling (mount points without entry name)'):
            self.disk_instance_mock._configuration['disk'][
                'combine_total'] = False  # pylint: disable=protected-access
            self.disk_instance_mock._configuration['disk'][
                'disk_labels'] = 'mount_points'  # pylint: disable=protected-access
            self.disk_instance_mock._configuration['disk'][
                'hide_entry_name'] = True  # pylint: disable=protected-access

            Disk.output(self.disk_instance_mock, self.output_mock)
            self.assertEqual(self.output_mock.append.call_count, 2)
            self.output_mock.append.assert_has_calls(
                [
                    call(
                        '(first_mount_point)',
                        '{0}10.0 KiB{1} / 10.0 KiB'.format(
                            Colors.RED_NORMAL, Colors.CLEAR)),
                    call(
                        '(second_mount_point)',
                        '{0}10.0 KiB{1} / 30.0 KiB'.format(
                            Colors.GREEN_NORMAL, Colors.CLEAR))
                ],
                any_order=
                True  # Since Python < 3.6 doesn't have definite `dict` ordering.
            )

        self.output_mock.reset_mock()

        with self.subTest(
                'Entry name labeling (without disk label nor entry name)'):
            self.disk_instance_mock._configuration['disk'][
                'combine_total'] = False  # pylint: disable=protected-access
            self.disk_instance_mock._configuration['disk'][
                'disk_labels'] = False  # pylint: disable=protected-access
            # `hide_entry_name` is being ignored as `disk_labels` evaluates to "falsy" too.
            self.disk_instance_mock._configuration['disk'][
                'hide_entry_name'] = True  # pylint: disable=protected-access

            Disk.output(self.disk_instance_mock, self.output_mock)
            self.assertEqual(self.output_mock.append.call_count, 2)
            self.output_mock.append.assert_has_calls(
                [
                    call(
                        'Disk', '{0}10.0 KiB{1} / 10.0 KiB'.format(
                            Colors.RED_NORMAL, Colors.CLEAR)),
                    call(
                        'Disk', '{0}10.0 KiB{1} / 30.0 KiB'.format(
                            Colors.GREEN_NORMAL, Colors.CLEAR))
                ],
                any_order=
                True  # Since Python < 3.6 doesn't have definite `dict` ordering.
            )