def test_grub1_mbr_install_differs_boot(self, mock_exec, mock_chmod, mock_guess):
        mock_guess.return_value = "/sbin/grub"
        mock_exec.return_value = ("stdout", "stderr")

        # install_device != boot_disk
        batch = "device (hd0) /dev/foo\n"
        batch += "geometry (hd0) 130 255 63\n"
        batch += "device (hd1) /dev/bar\n"
        batch += "geometry (hd1) 130 255 63\n"
        batch += "root (hd1,0)\n"
        batch += "setup (hd0)\n"
        batch += "quit\n"
        script = "cat /tmp/grub.batch | /sbin/grub --no-floppy --batch"

        mock_open = mock.mock_open()
        with mock.patch(OPEN_FUNCTION_NAME, new=mock_open, create=True):
            gu.grub1_mbr("/dev/foo", "/dev/bar", "0", chroot="/target")
        self.assertEqual(
            mock_open.call_args_list,
            [mock.call("/target/tmp/grub.batch", "wb"), mock.call("/target/tmp/grub.sh", "wb")],
        )
        mock_open_file = mock_open()
        self.assertEqual(mock_open_file.write.call_args_list, [mock.call(batch), mock.call(script)])
        mock_chmod.assert_called_once_with("/target/tmp/grub.sh", 0o755)
        mock_exec.assert_called_once_with("chroot", "/target", "/tmp/grub.sh", run_as_root=True, check_exit_code=[0])
    def test_grub1_mbr_install_same_as_boot(self, mock_exec, mock_chmod,
                                            mock_guess):
        mock_guess.return_value = '/sbin/grub'
        mock_exec.return_value = ('stdout', 'stderr')

        # install_device == boot_disk
        batch = 'device (hd0) /dev/foo\n'
        batch += 'geometry (hd0) 130 255 63\n'
        batch += 'root (hd0,0)\n'
        batch += 'setup (hd0)\n'
        batch += 'quit\n'
        script = 'cat /tmp/grub.batch | /sbin/grub --no-floppy --batch'

        mock_open = mock.mock_open()
        with mock.patch('fuel_agent.utils.grub.open',
                        new=mock_open,
                        create=True):
            gu.grub1_mbr('/dev/foo', '/dev/foo', '0', chroot='/target')
        self.assertEqual(mock_open.call_args_list, [
            mock.call('/target/tmp/grub.batch', 'wt', encoding='utf-8'),
            mock.call('/target/tmp/grub.sh', 'wt', encoding='utf-8')
        ])
        mock_open_file = mock_open()
        self.assertEqual(
            mock_open_file.write.call_args_list,
            [mock.call(batch), mock.call(script)])
        mock_chmod.assert_called_once_with('/target/tmp/grub.sh', 0o755)
        mock_exec.assert_called_once_with('chroot',
                                          '/target',
                                          '/tmp/grub.sh',
                                          run_as_root=True,
                                          check_exit_code=[0])
    def test_grub1_mbr_install_same_as_boot(self, mock_exec,
                                            mock_chmod, mock_guess):
        mock_guess.return_value = '/sbin/grub'
        mock_exec.return_value = ('stdout', 'stderr')

        # install_device == boot_disk
        batch = 'device (hd0) /dev/foo\n'
        batch += 'geometry (hd0) 130 255 63\n'
        batch += 'root (hd0,0)\n'
        batch += 'setup (hd0)\n'
        batch += 'quit\n'
        script = 'cat /tmp/grub.batch | /sbin/grub --no-floppy --batch'

        mock_open = mock.mock_open()
        with mock.patch('fuel_agent.utils.grub.open', new=mock_open,
                        create=True):
            gu.grub1_mbr('/dev/foo', '/dev/foo', '0', chroot='/target')
        self.assertEqual(
            mock_open.call_args_list,
            [mock.call('/target/tmp/grub.batch', 'wt', encoding='utf-8'),
             mock.call('/target/tmp/grub.sh', 'wt', encoding='utf-8')]
        )
        mock_open_file = mock_open()
        self.assertEqual(
            mock_open_file.write.call_args_list,
            [mock.call(batch), mock.call(script)]
        )
        mock_chmod.assert_called_once_with('/target/tmp/grub.sh', 0o755)
        mock_exec.assert_called_once_with(
            'chroot', '/target', '/tmp/grub.sh',
            run_as_root=True, check_exit_code=[0])