def test_initialise_driver_fail_modprobe_zfs(self):
        self.add_commands(
            CommandCaptureCommand(('genhostid', )),
            CommandCaptureCommand(('systemctl', 'status', 'zfs.target')),
            CommandCaptureCommand(('systemctl', 'disable', 'zfs.target')),
            CommandCaptureCommand(('systemctl', 'status', 'zfs-import-scan')),
            CommandCaptureCommand(('systemctl', 'disable', 'zfs-import-scan')),
            CommandCaptureCommand(('systemctl', 'status', 'zfs-import-cache')),
            CommandCaptureCommand(
                ('systemctl', 'disable', 'zfs-import-cache')),
            CommandCaptureCommand(('systemctl', 'status', 'zfs-mount')),
            CommandCaptureCommand(('systemctl', 'disable', 'zfs-mount')),
            CommandCaptureCommand(('rpm', '-qi', 'spl'),
                                  stdout=self.rpm_qi_spl_stdout),
            CommandCaptureCommand(('dkms', 'install', 'spl/1.2.3.4')),
            CommandCaptureCommand(('modprobe', 'spl')),
            CommandCaptureCommand(('rpm', '-qi', 'zfs'),
                                  stdout=self.rpm_qi_zfs_stdout),
            CommandCaptureCommand(('dkms', 'install', 'zfs/0.6.5.7')),
            CommandCaptureCommand(('modprobe', 'zfs'),
                                  rc=1,
                                  stderr='sample modprobe error text'))

        with mock.patch.object(path, 'isfile', return_value=False):
            result = BlockDeviceZfs.initialise_driver(True)

        self.assertIn('sample modprobe error text', result)
        self.assertRanAllCommandsInOrder()
    def test_initialise_driver_file_exists(self):
        self.add_commands(
            CommandCaptureCommand(('systemctl', 'status', 'zfs.target')),
            CommandCaptureCommand(('systemctl', 'disable', 'zfs.target')),
            CommandCaptureCommand(('systemctl', 'status', 'zfs-import-scan')),
            CommandCaptureCommand(('systemctl', 'disable', 'zfs-import-scan')),
            CommandCaptureCommand(('systemctl', 'status', 'zfs-import-cache')),
            CommandCaptureCommand(
                ('systemctl', 'disable', 'zfs-import-cache')),
            CommandCaptureCommand(('systemctl', 'status', 'zfs-mount')),
            CommandCaptureCommand(('systemctl', 'disable', 'zfs-mount')),
            CommandCaptureCommand(('rpm', '-qi', 'spl'),
                                  stdout=self.rpm_qi_spl_stdout),
            CommandCaptureCommand(('dkms', 'install', 'spl/1.2.3.4')),
            CommandCaptureCommand(('modprobe', 'spl')),
            CommandCaptureCommand(('rpm', '-qi', 'zfs'),
                                  stdout=self.rpm_qi_zfs_stdout),
            CommandCaptureCommand(('dkms', 'install', 'zfs/0.6.5.7')),
            CommandCaptureCommand(('modprobe', 'zfs')))

        with mock.patch.object(path, 'isfile', return_value=True):
            result = BlockDeviceZfs.initialise_driver(True)

        self.assertEqual(result, None)
        self.assertRanAllCommandsInOrder()
    def test_initialise_driver_fail_genhostid(self):
        self.add_commands(
            CommandCaptureCommand(('genhostid', ),
                                  rc=1,
                                  stderr='sample genhostid error text'))

        with mock.patch.object(path, 'isfile', return_value=False):
            result = BlockDeviceZfs.initialise_driver(True)

        self.assertIn('sample genhostid error text', result)
        self.assertRanAllCommandsInOrder()
    def test_initialise_driver_monitor_mode(self):
        result = BlockDeviceZfs.initialise_driver(False)

        self.assertEqual(result, None)
        self.assertRanAllCommandsInOrder()