Example #1
0
    def test_disk_df_output_dict(self, run_mock):
        """Test method to get `df` output as a dict by mocking calls to `subprocess.run`"""
        with self.subTest('`df` regular output.'):
            run_mock.return_value.stdout = '\n'.join([
                "Filesystem               1024-blocks      Used     Available Capacity Mounted on",
                "/dev/nvme0n1p2             499581952 427458276      67779164      87% /",
                "tmpfs                        8127236       292       8126944       1% /tmp",
                "/dev/nvme0n1p1                523248     35908        487340       7% /boot",
                ""
            ])
            self.assertDictEqual(
                Disk._get_df_output_dict(),  # pylint: disable=protected-access
                {
                    '/': {
                        'device_path': '/dev/nvme0n1p2',
                        'used_blocks': 427458276,
                        'total_blocks': 499581952
                    },
                    '/tmp': {
                        'device_path': 'tmpfs',
                        'used_blocks': 292,
                        'total_blocks': 8127236
                    },
                    '/boot': {
                        'device_path': '/dev/nvme0n1p1',
                        'used_blocks': 35908,
                        'total_blocks': 523248
                    }
                })

        with self.subTest('`df` missing from system.'):
            run_mock.side_effect = FileNotFoundError()
            self.assertDictEqual(
                Disk._get_df_output_dict(),  # pylint: disable=protected-access
                {})
Example #2
0
    def test_disk_get_local_filesystems(self):
        """Tests `Disk._get_local_filesystems`."""
        # This minimal `_disk_dict` contains everything this method touches.
        self.disk_instance_mock._disk_dict = {  # pylint: disable=protected-access
            '/very/good/mountpoint': {
                'device_path': '/dev/sda1'
            },
            '/mounted/here/too': {
                'device_path': '/dev/sda1'
            },
            '/other/acceptable/device/paths': {
                'device_path': '/dev/anything-really'
            },
            '/a/samba/share': {
                'device_path':
                '//server.local/cool_share'  # ignored - not `/dev/...`
            },
            '/linux/loop/device/one': {
                'device_path': '/dev/loop0'  # ignored - loop device
            },
            '/linux/loop/device/two': {
                'device_path': '/dev/blah/loop0'  # ignored - loop device
            },
            '/bsd/s/loop/device/one': {
                'device_path': '/dev/svnd'  # ignored - loop device
            },
            '/bsd/s/loop/device/two': {
                'device_path': '/dev/blah/svnd1'  # ignored - loop device
            },
            '/bsd/r/loop/device/one': {
                'device_path': '/dev/rvnd'  # ignored - loop device
            },
            '/bsd/r/loop/device/two': {
                'device_path': '/dev/blah/rvnd1'  # ignored - loop device
            },
            '/solaris/loop/device/one': {
                'device_path': '/dev/lofi1'  # ignored - loop device
            },
            '/solaris/loop/device/two': {
                'device_path': '/dev/blah/lofi'  # ignored - loop device
            },
            '/linux/device/mapper': {
                'device_path': '/dev/dm-1'  # ignored - device mapper
            }
        }

        result_disk_dict = Disk._get_local_filesystems(self.disk_instance_mock)  # pylint: disable=protected-access
        # Python < 3.6 doesn't guarantee dict ordering,
        # so we can't know which `/dev/sda1` mount point was used.
        self.assertEqual(
            len(result_disk_dict),
            2  # (/dev/sda1 is de-duplicated)
        )
        self.assertIn('/other/acceptable/device/paths', result_disk_dict)

        # If we can now find `/dev/sda1`, then we logically must have the correct result.
        self.assertTrue(any(disk_data['device_path'] == '/dev/sda1'
                            for disk_data in result_disk_dict.values()),
                        msg='`/dev/sda1` missing from results dict')
Example #3
0
    def test_disk_df_output_dict(self, run_mock):
        """Test method to get `df` output as a dict by mocking calls to `subprocess.run`"""
        with self.subTest('`df` regular output.'):
            run_mock.return_value.stdout = '\n'.join([
                "Filesystem               1024-blocks      Used     Available Capacity Mounted on",
                "/dev/nvme0n1p2             499581952 427458276      67779164      87% /",
                "tmpfs                        8127236       292       8126944       1% /tmp",
                "/dev/nvme0n1p1                523248     35908        487340       7% /boot",
                "/dev/sda1                       1624        42          1582       1% /what is  this",             # pylint: disable=line-too-long
                "map auto_home                      0         0             0     100% /System/Volumes/Data/home",  # pylint: disable=line-too-long
                "/Applications/Among Us.app/Wrapper 0         0             0     100% /System/Volumes/Data/game",  # pylint: disable=line-too-long
                ""
            ])
            self.assertDictEqual(
                Disk._get_df_output_dict(),  # pylint: disable=protected-access
                {
                    '/': {
                        'device_path': '/dev/nvme0n1p2',
                        'used_blocks': 427458276,
                        'total_blocks': 499581952
                    },
                    '/tmp': {
                        'device_path': 'tmpfs',
                        'used_blocks': 292,
                        'total_blocks': 8127236
                    },
                    '/boot': {
                        'device_path': '/dev/nvme0n1p1',
                        'used_blocks': 35908,
                        'total_blocks': 523248
                    },
                    '/what is  this': {
                        'device_path': '/dev/sda1',
                        'used_blocks': 42,
                        'total_blocks': 1624
                    }
                }
            )

        with self.subTest('`df` missing from system.'):
            run_mock.side_effect = FileNotFoundError()
            self.assertDictEqual(
                Disk._get_df_output_dict(),  # pylint: disable=protected-access
                {}
            )
Example #4
0
    def test_disk_get_local_filesystems(self):
        """Tests `Disk._get_local_filesystems`."""
        # This minimal `_disk_dict` contains everything this method touches.
        self.disk_instance_mock._disk_dict = {  # pylint: disable=protected-access
            '/very/good/mountpoint': {
                'device_path': '/dev/sda1'
            },
            '/mounted/here/too': {
                'device_path': '/dev/sda1'
            },
            '/other/acceptable/device/paths': {
                'device_path': '/dev/anything-really'
            },
            '/a/samba/share': {
                'device_path': '//server.local/cool_share'  # ignored - not `/dev/...`
            },
            '/linux/loop/device/one': {
                'device_path': '/dev/loop0'  # ignored - loop device
            },
            '/linux/loop/device/two': {
                'device_path': '/dev/blah/loop0'  # ignored - loop device
            },
            '/bsd/s/loop/device/one': {
                'device_path': '/dev/svnd'  # ignored - loop device
            },
            '/bsd/s/loop/device/two': {
                'device_path': '/dev/blah/svnd1'  # ignored - loop device
            },
            '/bsd/r/loop/device/one': {
                'device_path': '/dev/rvnd'  # ignored - loop device
            },
            '/bsd/r/loop/device/two': {
                'device_path': '/dev/blah/rvnd1'  # ignored - loop device
            },
            '/solaris/loop/device/one': {
                'device_path': '/dev/lofi1'  # ignored - loop device
            },
            '/solaris/loop/device/two': {
                'device_path': '/dev/blah/lofi'  # ignored - loop device
            },
            '/linux/device/mapper': {
                'device_path': '/dev/dm-1'  # ignored - device mapper
            }
        }

        self.assertDictEqual(
            Disk._get_local_filesystems(self.disk_instance_mock),  # pylint: disable=protected-access
            {
                '/very/good/mountpoint': {
                    'device_path': '/dev/sda1'
                },
                '/other/acceptable/device/paths': {
                    'device_path': '/dev/anything-really'
                }
            }
        )
Example #5
0
    def test_disk_get_specified_filesystems(self):
        """Tests `Disk._get_specified_filesystems`."""
        # This minimal `_disk_dict` contains everything this method touches.
        self.disk_instance_mock._disk_dict = {  # pylint: disable=protected-access
            '/very/good/mountpoint': {
                'device_path': '/dev/sda1'
            },
            '/mounted/here/too': {
                'device_path': '/dev/sda1'
            },
            '/less/good/mountpoint': {
                'device_path': '/dev/sda2'
            },
            '/a/samba/share': {
                'device_path': '//server.local/cool_share'
            }
        }

        with self.subTest('Get all filesystems with mount points.'):
            # pylint: disable=protected-access
            self.assertDictEqual(
                Disk._get_specified_filesystems(
                    self.disk_instance_mock,
                    self.disk_instance_mock._disk_dict  # recall dicts are iterables of their keys.
                ),
                self.disk_instance_mock._disk_dict
            )
            # pylint: enable=protected-access

        with self.subTest('Get only `/dev/sda1` filesystems.'):
            self.assertDictEqual(
                Disk._get_specified_filesystems(  # pylint: disable=protected-access
                    self.disk_instance_mock,
                    ('/dev/sda1',)
                ),
                {
                    '/very/good/mountpoint': {
                        'device_path': '/dev/sda1'
                    }
                }
            )
Example #6
0
    def test_disk_get_specified_filesystems(self):
        """Tests `Disk._get_specified_filesystems`."""
        # This minimal `_disk_dict` contains everything this method touches.
        self.disk_instance_mock._disk_dict = {  # pylint: disable=protected-access
            '/very/good/mountpoint': {
                'device_path': '/dev/sda1'
            },
            '/mounted/here/too': {
                'device_path': '/dev/sda1'
            },
            '/less/good/mountpoint': {
                'device_path': '/dev/sda2'
            },
            '/a/samba/share': {
                'device_path': '//server.local/cool_share'
            }
        }

        with self.subTest('Get all filesystems with mount points.'):
            # pylint: disable=protected-access
            self.assertDictEqual(
                Disk._get_specified_filesystems(
                    self.disk_instance_mock,
                    self.disk_instance_mock.
                    _disk_dict  # recall dicts are iterables of their keys.
                ),
                self.disk_instance_mock._disk_dict)
            # pylint: enable=protected-access

        with self.subTest('Get only `/dev/sda1` filesystems.'):
            result_disk_dict = Disk._get_specified_filesystems(  # pylint: disable=protected-access
                self.disk_instance_mock, ('/dev/sda1', ))

            # With Python < 3.6, dict ordering isn't guaranteed,
            # so we don't know which disk will be selected.
            self.assertEqual(len(result_disk_dict), 1)
            # As long as `device_path` is also correct, this passes.
            self.assertEqual(
                result_disk_dict[list(
                    result_disk_dict.keys())[0]]['device_path'], '/dev/sda1')
Example #7
0
    def test_disk_df_output_dict(self, _):
        """Test method to get `df` output as a dict by mocking calls to `check_output`."""
        self.assertDictEqual(
            Disk.get_df_output_dict(), {
                '/': {
                    'device_path': '/dev/nvme0n1p2',
                    'used_blocks': 427458276,
                    'total_blocks': 499581952
                },
                '/tmp': {
                    'device_path': 'tmpfs',
                    'used_blocks': 292,
                    'total_blocks': 8127236
                },
                '/boot': {
                    'device_path': '/dev/nvme0n1p1',
                    'used_blocks': 35908,
                    'total_blocks': 523248
                }
            })

        with self.subTest('Missing `df` from system.'):
            self.assertDictEqual(Disk.get_df_output_dict(), {})
Example #8
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'
             )
 def test_disk_blocks_to_human_readable(self):
     """Test method to convert 1024-byte blocks to a human readable format."""
     # Each tuple is a number of blocks followed by the expected output.
     test_cases = (
         (1, '1.0 KiB'),
         (1024, '1.0 MiB'),
         (2048, '2.0 MiB'),
         (95604, '93.4 MiB'),
         (1048576, '1.0 GiB'),
         (2097152, '2.0 GiB'),
         (92156042, '87.9 GiB'),
         (1073742000, '1.0 TiB'),
         (2147484000, '2.0 TiB'),
         (458028916298, '426.6 TiB'),
         (1099512000000, '1.0 PiB'),
         (2199023000000, '2.0 PiB')  # I think we can safely stop here :)
     )
     for test_case in test_cases:
         with self.subTest(test_case[1]):
             self.assertEqual(
                 Disk._blocks_to_human_readable(test_case[0]),  # pylint: disable=protected-access
                 test_case[1])
Example #10
0
 def test_df_only(self, _, __):
     """Test computations around `df` output at disk regular level"""
     disk = Disk().value
     self.assertTrue(
         all(i in disk
             for i in [str(Colors.GREEN_NORMAL), '45.9', '298.6']))
Example #11
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'
                    )
                ]
            )
Example #12
0
 def test(self, _):
     """Test computations around `df` output"""
     self.assertIn('61', Disk().value)
     self.assertIn('1024', Disk().value)
Example #13
0
 def test_df_and_btrfs(self, _, __):
     """Test computations around `df` and `btrfs` outputs"""
     disk = Disk().value
     self.assertTrue(
         all(i in disk
             for i in [str(Colors.GREEN_NORMAL), '989.3', '4501.1']))
Example #14
0
 def test_danger(self, _, __):
     """Test computations around `df` output at disk danger level and tweaked limits"""
     disk = Disk().value
     self.assertTrue(all(i in disk for i in ['\x1b[0;33m', '861', '1024']))
Example #15
0
 def test_warning(self, _, __):
     """Test computations around `df` output at disk warning level"""
     disk = Disk().value
     self.assertTrue(all(i in disk for i in ['\x1b[0;33m', '661', '1024']))
Example #16
0
 def test(self, _, __):
     """Test computations around `df` output at disk normal level"""
     disk = Disk().value
     self.assertTrue(all(i in disk for i in ['\x1b[0;32m', '61', '1024']))
Example #17
0
 def test_df_only_warning(self, _, __):
     """Test computations around `df` output at disk warning level"""
     disk = Disk().value
     self.assertTrue(
         all(i in disk
             for i in [str(Colors.YELLOW_NORMAL), '251.6', '298.6']))
Example #18
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.
            )
Example #19
0
 def test_no_recognised_disks(self, _, __):
     """Test df failing to detect any valid file-systems"""
     self.assertEqual(Disk().value, 'Not detected')
Example #20
0
 def test_df_failing(self, _, __):
     """Test df call failing against the BusyBox implementation"""
     self.assertEqual(Disk().value, 'Not detected')
Example #21
0
 def test_btrfs_only_with_raid_configuration(self, _, __):
     """Test computations around `btrfs` outputs with a RAID-1 setup"""
     disk = Disk().value
     self.assertTrue(
         all(i in disk
             for i in [str(Colors.GREEN_NORMAL), '943.4', '4202.5']))