Exemplo n.º 1
0
 def test_regular(self, _):
     """Sometimes, it could be quite simple..."""
     self.assertEqual(Model().value, 'MY-LAPTOP-MODEL')
Exemplo n.º 2
0
 def test_virtual_environment_systemd_and_dmidecode(self, _, __, ___):
     """Test for virtual environments, with systemd tools and `dmidecode`"""
     self.assertEqual(Model().value, 'MY-LAPTOP-MODEL (systemd-nspawn)')
Exemplo n.º 3
0
 def test_virtual_environment_systemd_alone(self, _, __, ___):
     """Test for virtual environments, with systemd tools and `dmidecode`"""
     self.assertEqual(Model().value, 'Virtual Environment (systemd-nspawn)')
Exemplo n.º 4
0
    def test_fetch_virtual_env_info(self, check_output_mock, getuid_mock):
        """Test `_fetch_virtual_env_info` method"""
        model_mock = HelperMethods.entry_mock(Model)

        with self.subTest('Detected virtual environment.'):
            check_output_mock.side_effect = [
                FileNotFoundError(),  # `systemd-detect-virt` is not available.
                'xen\nxen-domU\n',  # `virt-what` example output.
                'HYPERVISOR-NAME\n'  # `dmidecode` example output.
            ]
            getuid_mock.return_value = 0

            self.assertEqual(
                Model._fetch_virtual_env_info(model_mock),  # pylint: disable=protected-access
                'HYPERVISOR-NAME (xen, xen-domU)')

        with self.subTest('Virtual environment without `dmidecode`.'):
            check_output_mock.reset_mock()
            getuid_mock.reset_mock()
            check_output_mock.side_effect = [
                FileNotFoundError(),  # `systemd-detect-virt` is not available.
                'xen\nxen-domU\n',  # `virt-what` example output.
                FileNotFoundError()  # `dmidecode` will fail.
            ]
            getuid_mock.return_value = 0

            self.assertEqual(
                Model._fetch_virtual_env_info(model_mock),  # pylint: disable=protected-access
                f"{DEFAULT_CONFIG['default_strings']['virtual_environment']} (xen, xen-domU)"
            )

        with self.subTest('Virtual environment with systemd only.'):
            check_output_mock.reset_mock()
            getuid_mock.reset_mock()
            check_output_mock.side_effect = [
                'systemd-nspawn\n'  # `systemd-detect-virt` output.
            ]
            getuid_mock.return_value = 1000  # `virt-what` and `dmidecode` won't be called.

            self.assertEqual(
                Model._fetch_virtual_env_info(model_mock),  # pylint: disable=protected-access
                f"{DEFAULT_CONFIG['default_strings']['virtual_environment']} (systemd-nspawn)"
            )

        with self.subTest('Virtual environment with systemd and `dmidecode`.'):
            check_output_mock.reset_mock()
            getuid_mock.reset_mock()
            check_output_mock.side_effect = [
                'systemd-nspawn\n',  # `systemd-detect-virt` example output.
                # `virt-what` won't be called (systemd call succeeded).
                'HYPERVISOR-NAME\n'  # `dmidecode` example output.
            ]
            getuid_mock.return_value = 0

            self.assertEqual(
                Model._fetch_virtual_env_info(model_mock),  # pylint: disable=protected-access
                'HYPERVISOR-NAME (systemd-nspawn)')

        with self.subTest('Not a virtual environment (systemd).'):
            check_output_mock.reset_mock()
            getuid_mock.reset_mock()
            check_output_mock.side_effect = CalledProcessError(
                1, 'systemd-detect-virt', 'none\n')

            self.assertIsNone(
                Model._fetch_virtual_env_info(model_mock)  # pylint: disable=protected-access
            )

        with self.subTest('Not a virtual environment (virt-what).'):
            check_output_mock.reset_mock()
            getuid_mock.reset_mock()
            check_output_mock.side_effect = [
                FileNotFoundError(
                ),  # `systemd-detect-virt` won't be available.
                '\n'  # `virt-what` won't detect anything.
                # `dmidecode` won't even be called.
            ]
            getuid_mock.return_value = 0

            self.assertIsNone(
                Model._fetch_virtual_env_info(model_mock)  # pylint: disable=protected-access
            )

        with self.subTest('Not a virtual environment (no tools, no root)'):
            check_output_mock.reset_mock()
            getuid_mock.reset_mock()
            check_output_mock.side_effect = [
                FileNotFoundError(
                )  # `systemd-detect-virt` won't be available.
            ]
            getuid_mock.return_value = 1000  # `virt-what` and `dmidecode` won't be called.

            self.assertIsNone(
                Model._fetch_virtual_env_info(model_mock)  # pylint: disable=protected-access
            )
Exemplo n.º 5
0
 def test_virtual_environment_without_dmidecode(self, _, __, ___):
     """Test for virtual machine (with a failing `dmidecode` call)"""
     self.assertEqual(
         Model().value,
         'Virtual Environment (xen, xen-domU)'
     )
Exemplo n.º 6
0
 def test_virtual_environment(self, _, __):
     """Test for virtual machine"""
     self.assertEqual(
         Model().value,
         'MY-LAPTOP-MODEL (xen, xen-domU)'
     )