Пример #1
0
    def test_handle_raises_error_if_no_drivers_found(
        self,
        m_which,
        m_subp,
        m_tmp,
        m_debconf,
        caplog,
        tmpdir,
        cfg_accepted,
        install_gpgpu,
    ):
        """If ubuntu-drivers doesn't install any drivers, raise an error."""
        tdir = tmpdir
        debconf_file = os.path.join(tdir, "nvidia.template")
        m_tmp.return_value = tdir
        myCloud = mock.MagicMock()

        m_subp.side_effect = ProcessExecutionError(
            stdout="No drivers found for installation.\n", exit_code=1
        )

        with pytest.raises(Exception):
            drivers.handle("ubuntu_drivers", cfg_accepted, myCloud, None, None)
        assert [
            mock.call(drivers.X_LOADTEMPLATEFILE, debconf_file)
        ] == m_debconf.DebconfCommunicator().__enter__().command.call_args_list
        assert [
            mock.call(["ubuntu-drivers-common"])
        ] == myCloud.distro.install_packages.call_args_list
        assert [mock.call(install_gpgpu)] == m_subp.call_args_list
        assert (
            "ubuntu-drivers found no drivers for installation" in caplog.text
        )
Пример #2
0
 def test_returns_none_on_error(self, m_subp):
     """On 'file' execution error, None should be returned."""
     m_subp.side_effect = ProcessExecutionError("FILE_FAILED", exit_code=99)
     fname = self.tmp_path("myfile")
     write_file(fname, "plain text content here\n")
     self.assertEqual(None, identify_file(fname))
     self.assertEqual(
         [mock.call(["file", "--brief", "--mime-type", fname])],
         m_subp.call_args_list)
Пример #3
0
 def test_cannot_skip_ufs_growfs_exception(self, m_subp):
     fs_type = "ufs"
     resize_what = "/"
     devpth = "/dev/da0p2"
     err = "growfs: /dev/da0p2 is not clean - run fsck.\n"
     exception = ProcessExecutionError(stderr=err, exit_code=1)
     m_subp.side_effect = exception
     with self.assertRaises(ProcessExecutionError):
         can_skip_resize(fs_type, resize_what, devpth)
Пример #4
0
 def test_skip_ufs_resize(self, m_subp):
     fs_type = "ufs"
     resize_what = "/"
     devpth = "/dev/da0p2"
     err = ("growfs: requested size 2.0GB is not larger than the "
            "current filesystem size 2.0GB\n")
     exception = ProcessExecutionError(stderr=err, exit_code=1)
     m_subp.side_effect = exception
     res = can_skip_resize(fs_type, resize_what, devpth)
     self.assertTrue(res)
Пример #5
0
    def test_install_drivers_handles_old_ubuntu_drivers_gracefully(
        self,
        m_which,
        m_subp,
        m_tmp,
        m_debconf,
        caplog,
        tmpdir,
        cfg_accepted,
        install_gpgpu,
    ):
        """Older ubuntu-drivers versions should emit message and raise error"""
        debconf_file = tmpdir.join("nvidia.template")
        m_tmp.return_value = tmpdir
        myCloud = mock.MagicMock()

        m_subp.side_effect = ProcessExecutionError(
            stderr=OLD_UBUNTU_DRIVERS_ERROR_STDERR, exit_code=2
        )

        with pytest.raises(Exception):
            drivers.handle("ubuntu_drivers", cfg_accepted, myCloud, None, None)
        assert [
            mock.call(drivers.X_LOADTEMPLATEFILE, debconf_file)
        ] == m_debconf.DebconfCommunicator().__enter__().command.call_args_list
        assert [
            mock.call(["ubuntu-drivers-common"])
        ] == myCloud.distro.install_packages.call_args_list
        assert [mock.call(install_gpgpu)] == m_subp.call_args_list
        assert (
            MPATH[:-1],
            log.WARNING,
            (
                "the available version of ubuntu-drivers is"
                " too old to perform requested driver installation"
            ),
        ) == caplog.record_tuples[-1]
Пример #6
0
class TestFetchIdevs:
    """Tests cc_grub_dpkg.fetch_idevs()"""

    # Note: udevadm info returns devices in a large single line string
    @pytest.mark.parametrize(
        "grub_output,path_exists,expected_log_call,udevadm_output"
        ",expected_idevs",
        [
            # Inside a container, grub not installed
            (
                ProcessExecutionError(reason=FileNotFoundError()),
                False,
                mock.call("'grub-probe' not found in $PATH"),
                "",
                "",
            ),
            # Inside a container, grub installed
            (
                ProcessExecutionError(stderr="failed to get canonical path"),
                False,
                mock.call("grub-probe 'failed to get canonical path'"),
                "",
                "",
            ),
            # KVM Instance
            (
                ["/dev/vda"],
                True,
                None,
                (
                    "/dev/disk/by-path/pci-0000:00:00.0 ",
                    "/dev/disk/by-path/virtio-pci-0000:00:00.0 ",
                ),
                "/dev/vda",
            ),
            # Xen Instance
            (
                ["/dev/xvda"],
                True,
                None,
                "",
                "/dev/xvda",
            ),
            # NVMe Hardware Instance
            (
                ["/dev/nvme1n1"],
                True,
                None,
                (
                    "/dev/disk/by-id/nvme-Company_hash000 ",
                    "/dev/disk/by-id/nvme-nvme.000-000-000-000-000 ",
                    "/dev/disk/by-path/pci-0000:00:00.0-nvme-0 ",
                ),
                "/dev/disk/by-id/nvme-Company_hash000",
            ),
            # SCSI Hardware Instance
            (
                ["/dev/sda"],
                True,
                None,
                (
                    "/dev/disk/by-id/company-user-1 ",
                    "/dev/disk/by-id/scsi-0Company_user-1 ",
                    "/dev/disk/by-path/pci-0000:00:00.0-scsi-0:0:0:0 ",
                ),
                "/dev/disk/by-id/company-user-1",
            ),
        ],
    )
    @mock.patch("cloudinit.config.cc_grub_dpkg.util.logexc")
    @mock.patch("cloudinit.config.cc_grub_dpkg.os.path.exists")
    @mock.patch("cloudinit.config.cc_grub_dpkg.subp.subp")
    def test_fetch_idevs(
        self,
        m_subp,
        m_exists,
        m_logexc,
        grub_output,
        path_exists,
        expected_log_call,
        udevadm_output,
        expected_idevs,
    ):
        """Tests outputs from grub-probe and udevadm info against grub-dpkg"""
        m_subp.side_effect = [grub_output, ["".join(udevadm_output)]]
        m_exists.return_value = path_exists
        log = mock.Mock(spec=Logger)
        idevs = fetch_idevs(log)
        assert expected_idevs == idevs
        if expected_log_call is not None:
            assert expected_log_call in log.debug.call_args_list
 def fake_subp(cmd):
     if cmd[0].startswith(tdir):
         return
     raise ProcessExecutionError(
         stdout="No drivers found for installation.\n", exit_code=1)
 def fake_subp(cmd):
     if cmd[0].startswith(tdir):
         return
     raise ProcessExecutionError(stderr=OLD_UBUNTU_DRIVERS_ERROR_STDERR,
                                 exit_code=2)
 def subp_side_effect(cmd, *args, **kwargs):
     # Mock fallocate failing, to initiate fallback
     if cmd[0] == "fallocate":
         raise ProcessExecutionError()