from insights.combiners.grub_conf import GrubConf, BootLoaderEntries
from insights.combiners.grubby import Grubby, IsUEFIBoot
from insights.parsers.grub_conf import (
    Grub1Config,
    Grub2Config,
    Grub2EFIConfig,
    Grub1EFIConfig,
    BootLoaderEntries as BLE,
)
from insights.parsers.grubby import GrubbyDefaultIndex, GrubbyInfoAll
from insights.parsers.grubenv import GrubEnv
from insights.parsers.ls import LSlanFiltered
from insights.parsers.installed_rpms import InstalledRpms
from insights.parsers.cmdline import CmdLine
from insights.tests import context_wrap
import pytest

GRUB1_TEMPLATE = """
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/mapper/VolGroup-lv_root
#          initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title Red Hat Enterprise Linux 6 (2.6.32-642.el6.x86_64)
        root (hd0,0)
        kernel /vmlinuz-2.6.32-642.el6.x86_64 {kernel_boot_options} ro root=/dev/mapper/VolGroup-lv_root intel_iommu=off rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD rd_LVM_LV=VolGroup/lv_swap SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=VolGroup/lv_root  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
        initrd /initramfs-2.6.32-642.el6.x86_64.img
title Red Hat Enterprise Linux 6 (2.6.32-642.el6.x86_64-2)
        root (hd0,0)
        kernel /vmlinuz-2.6.32-642.el6.x86_64 {kernel_boot_options} ro root=/dev/mapper/VolGroup-lv_root intel_iommu=on rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD rd_LVM_LV=VolGroup/lv_swap SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=VolGroup/lv_root  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
        initrd /initramfs-2.6.32-642.el6.x86_64.img
title Red Hat Enterprise Linux 6 (2.6.32-642.el6.x86_64-2)
        root (hd0,0)
        kernel /vmlinuz-2.6.32-642.el6.x86_64 {kernel_boot_options} ro root=/dev/mapper/VolGroup-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD rd_LVM_LV=VolGroup/lv_swap SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=VolGroup/lv_root  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
        initrd /initramfs-2.6.32-642.el6.x86_64.img
""".strip()  # noqa

# rhel-7
GRUB2_TEMPLATE = """
#
# DO NOT EDIT THIS FILE
#
# It is automatically generated by grub2-mkconfig using templates
# from /etc/grub.d and settings from /etc/default/grub
#

### BEGIN /etc/grub.d/00_header ###
set pager=1

terminal_output console
### END /etc/grub.d/00_header ###

### BEGIN /etc/grub.d/00_tuned ###
set tuned_params=""
### END /etc/grub.d/00_tuned ###

### BEGIN /etc/grub.d/01_users ###

### END /etc/grub.d/01_users ###

### BEGIN /etc/grub.d/10_linux ###
menuentry 'Red Hat Enterprise Linux Server (3.10.0-327.el7.x86_64) 7.2 (Maipo)' --class red --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-327.el7.x86_64-advanced-4f80b3d4-90ba-4545-869c-febdecc586ce' {
        load_video
        set gfxpayload=keep
        insmod gzio
        insmod part_msdos
        insmod xfs
        set root='hd0,msdos1'
        if [ x$feature_platform_search_hint = xy ]; then
          search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1'  860a7b56-dbdd-498a-b085-53dc93e4650b
        else
          search --no-floppy --fs-uuid --set=root 860a7b56-dbdd-498a-b085-53dc93e4650b
        fi
        linux16 /vmlinuz-3.10.0-327.el7.x86_64 %s root=/dev/mapper/rhel-root ro crashkernel=auto rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet LANG=en_US.UTF-8
        initrd16 /initramfs-3.10.0-327.el7.x86_64.img
}
menuentry 'Red Hat Enterprise Linux Server (0-rescue-9f20b35c9faa49aebe171f62a11b236f) 7.2 (Maipo)' --class red --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-0-rescue-9f20b35c9faa49aebe171f62a11b236f-advanced-4f80b3d4-90ba-4545-869c-febdecc586ce' {
        load_video
        insmod gzio
        insmod part_msdos
        insmod xfs
        set root='hd0,msdos1'
        if [ x$feature_platform_search_hint = xy ]; then
          search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1'  860a7b56-dbdd-498a-b085-53dc93e4650b
        else
          search --no-floppy --fs-uuid --set=root 860a7b56-dbdd-498a-b085-53dc93e4650b
        fi
        linux16 /vmlinuz-0-rescue-9f20b35c9faa49aebe171f62a11b236f %s root=/dev/mapper/rhel-root ro crashkernel=auto rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet
        initrd16 /initramfs-0-rescue-9f20b35c9faa49aebe171f62a11b236f.img
}
""".strip()  # noqa

GRUB2_TEMPLATE_BLSCFG = """
#
# DO NOT EDIT THIS FILE
#
# It is automatically generated by grub2-mkconfig using templates
# from /etc/grub.d and settings from /etc/default/grub
#

### BEGIN /etc/grub.d/00_header ###
set pager=1

terminal_output console
### END /etc/grub.d/00_header ###

### BEGIN /etc/grub.d/00_tuned ###
set tuned_params=""
### END /etc/grub.d/00_tuned ###

### BEGIN /etc/grub.d/01_users ###

### END /etc/grub.d/01_users ###
insmod blscfg
blscfg
if [ -s $prefix/grubenv ]; then
  load_env
fi

if [ -z "${kernelopts}" ]; then
  set kernelopts="root=/dev/mapper/rhel-root ro crashkernel=auto resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet transparent_hugepage=never "
fi
""".strip()  # noqa

GRUB2_TEMPLATE_NO_BLSCFG = """
#
# DO NOT EDIT THIS FILE
#
# It is automatically generated by grub2-mkconfig using templates
# from /etc/grub.d and settings from /etc/default/grub
#

### BEGIN /etc/grub.d/00_header ###
set pager=1

terminal_output console
### END /etc/grub.d/00_header ###

### BEGIN /etc/grub.d/00_tuned ###
set tuned_params=""
### END /etc/grub.d/00_tuned ###

### BEGIN /etc/grub.d/01_users ###

### END /etc/grub.d/01_users ###
### BEGIN /etc/grub.d/10_linux ###
menuentry 'Red Hat Enterprise Linux Server (4.18.0-240.el8.x86_64) 8.3 (Maipo)' --class red --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-327.el7.x86_64-advanced-4f80b3d4-90ba-4545-869c-febdecc586ce' {
        load_video
        set gfxpayload=keep
        insmod gzio
        insmod part_msdos
        insmod xfs
        set root='hd0,msdos1'
        if [ x$feature_platform_search_hint = xy ]; then
          search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1'  860a7b56-dbdd-498a-b085-53dc93e4650b
        else
          search --no-floppy --fs-uuid --set=root 860a7b56-dbdd-498a-b085-53dc93e4650b
        fi
        linux16 /vmlinuz-4.18.0-240.el8.x86_64 %s root=/dev/mapper/rhel-root ro crashkernel=auto rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet LANG=en_US.UTF-8
        initrd16 /initramfs-4.18.0-240.el8.x86_64.img
}
menuentry 'Red Hat Enterprise Linux Server (0-rescue-9f20b35c9faa49aebe171f62a11b236f) 8.3 (Maipo)' --class red --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-0-rescue-9f20b35c9faa49aebe171f62a11b236f-advanced-4f80b3d4-90ba-4545-869c-febdecc586ce' {
        load_video
        insmod gzio
        insmod part_msdos
        insmod xfs
        set root='hd0,msdos1'
        if [ x$feature_platform_search_hint = xy ]; then
          search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1'  860a7b56-dbdd-498a-b085-53dc93e4650b
        else
          search --no-floppy --fs-uuid --set=root 860a7b56-dbdd-498a-b085-53dc93e4650b
        fi
        linux16 /vmlinuz-0-rescue-9f20b35c9faa49aebe171f62a11b236f %s root=/dev/mapper/rhel-root ro crashkernel=auto rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet
        initrd16 /initramfs-0-rescue-9f20b35c9faa49aebe171f62a11b236f.img
}
""".strip()  # noqa

GRUB2_EFI_CFG = """
### BEGIN /etc/grub.d/10_linux ###
menuentry 'Red Hat Enterprise Linux Server (3.10.0-514.16.1.el7.x86_64) 7.3 (Maipo)' --class red --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-514.el7.x86_64-advanced-9727cab4-12c2-41a8-9527-9644df34e586' {
    load_video
    set gfxpayload=keep
    insmod gzio
    insmod part_gpt
    insmod xfs
    set root='hd0,gpt2'
    if [ x$feature_platform_search_hint = xy ]; then
      search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt2 --hint-efi=hd0,gpt2 --hint-baremetal=ahci0,gpt2  d80fa96c-ffa1-4894-9282-aeda37f0befe
    else
      search --no-floppy --fs-uuid --set=root d80fa96c-ffa1-4894-9282-aeda37f0befe
    fi
    linuxefi /vmlinuz-3.10.0-514.16.1.el7.x86_64 root=/dev/mapper/rhel-root ro rd.luks.uuid=luks-a40b320e-0711-4cd6-8f9e-ce32810e2a79 rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet LANG=en_US.UTF-8
    initrdefi /initramfs-3.10.0-514.16.1.el7.x86_64.img
}
menuentry 'Red Hat Enterprise Linux Server (3.10.0-514.10.2.el7.x86_64) 7.3 (Maipo)' --class red --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-514.el7.x86_64-advanced-9727cab4-12c2-41a8-9527-9644df34e586' {
    load_video
    set gfxpayload=keep
    insmod gzio
    insmod part_gpt
    insmod xfs
    set root='hd0,gpt2'
    if [ x$feature_platform_search_hint = xy ]; then
      search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt2 --hint-efi=hd0,gpt2 --hint-baremetal=ahci0,gpt2  d80fa96c-ffa1-4894-9282-aeda37f0befe
    else
      search --no-floppy --fs-uuid --set=root d80fa96c-ffa1-4894-9282-aeda37f0befe
    fi
    linuxefi /vmlinuz-3.10.0-514.10.2.el7.x86_64 root=/dev/mapper/rhel-root ro rd.luks.uuid=luks-a40b320e-0711-4cd6-8f9e-ce32810e2a79 rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet LANG=en_US.UTF-8
    initrdefi /initramfs-3.10.0-514.10.2.el7.x86_64.img
}
menuentry 'Red Hat Enterprise Linux Server (3.10.0-514.el7.x86_64) 7.3 (Maipo)' --class red --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-514.el7.x86_64-advanced-9727cab4-12c2-41a8-9527-9644df34e586' {
    load_video
    set gfxpayload=keep
    insmod gzio
    insmod part_gpt
    insmod xfs
    set root='hd0,gpt2'
    if [ x$feature_platform_search_hint = xy ]; then
      search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt2 --hint-efi=hd0,gpt2 --hint-baremetal=ahci0,gpt2  d80fa96c-ffa1-4894-9282-aeda37f0befe
    else
      search --no-floppy --fs-uuid --set=root d80fa96c-ffa1-4894-9282-aeda37f0befe
    fi
    linuxefi /vmlinuz-3.10.0-514.el7.x86_64 root=/dev/mapper/rhel-root ro rd.luks.uuid=luks-a40b320e-0711-4cd6-8f9e-ce32810e2a79 rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet LANG=en_US.UTF-8
    initrdefi /initramfs-3.10.0-514.el7.x86_64.img
}
menuentry 'Red Hat Enterprise Linux Server (0-rescue-f1340b5dd6ee4c26b587621566111421) 7.3 (Maipo)' --class red --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-0-rescue-f1340b5dd6ee4c26b587621566111421-advanced-9727cab4-12c2-41a8-9527-9644df34e586' {
    load_video
    insmod gzio
    insmod part_gpt
    insmod xfs
    set root='hd0,gpt2'
    if [ x$feature_platform_search_hint = xy ]; then
      search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt2 --hint-efi=hd0,gpt2 --hint-baremetal=ahci0,gpt2  d80fa96c-ffa1-4894-9282-aeda37f0befe
    else
      search --no-floppy --fs-uuid --set=root d80fa96c-ffa1-4894-9282-aeda37f0befe
    fi
    linuxefi /vmlinuz-0-rescue-f1340b5dd6ee4c26b587621566111421 root=/dev/mapper/rhel-root ro rd.luks.uuid=luks-a40b320e-0711-4cd6-8f9e-ce32810e2a79 rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet
    initrdefi /initramfs-0-rescue-f1340b5dd6ee4c26b587621566111421.img
}

### END /etc/grub.d/10_linux ###
""".strip()  # noqa

GRUB1_EFI_CFG = """
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,1)
#          kernel /vmlinuz-version ro root=/dev/mapper/VolGroup-lv_root
#          initrd /initrd-[generic-]version.img
#boot=/dev/mpathap1
default=0
timeout=5
splashimage=(hd0,1)/grub/splash.xpm.gz
hiddenmenu
title Red Hat Enterprise Linux (2.6.32-71.el6.x86_64)
        root (hd0,1)
        kernel /vmlinuz-2.6.32-71.el6.x86_64 ro root=/dev/mapper/VolGroup-lv_root rd_LVM_LV=VolGroup/lv_root rd_LVM_LV=VolGroup/lv_swap rd_NO_LUKS rd_NO_MD rd_NO_DM LANG=en_US.UTF-8 SYSFONT=latarcyrheb-sun16 KEYBOARDTYPE=pc KEYTABLE=us crashkernel=auto rhgb quiet
        initrd /initramfs-2.6.32-71.el6.x86_64.img
""".strip()  # noqa

SYS_FIRMWARE_DIR_NOEFI = """
/sys/firmware:
total 0
drwxr-xr-x.  5 0 0 0 May 30 11:50 .
dr-xr-xr-x. 13 0 0 0 May 30 11:50 ..
drwxr-xr-x.  5 0 0 0 May 30 11:50 acpi
drwxr-xr-x.  3 0 0 0 May 30 11:51 dmi
drwxr-xr-x.  7 0 0 0 May 30 12:31 memmap

/sys/firmware/acpi:
total 0
drwxr-xr-x. 5 0 0    0 May 30 11:50 .
drwxr-xr-x. 5 0 0    0 May 30 11:50 ..
drwxr-xr-x. 6 0 0    0 May 30 12:31 hotplug
drwxr-xr-x. 2 0 0    0 May 30 12:31 interrupts
-r--r--r--. 1 0 0 4096 May 30 12:31 pm_profile
drwxr-xr-x. 3 0 0    0 May 30 11:50 tables
""".strip()

SYS_FIRMWARE_DIR_EFI = """
/sys/firmware:
total 0
drwxr-xr-x.  5 0 0 0 May 30 11:50 .
dr-xr-xr-x. 13 0 0 0 May 30 11:50 ..
drwxr-xr-x.  5 0 0 0 May 30 11:50 acpi
drwxr-xr-x.  3 0 0 0 May 30 11:51 dmi
drwxr-xr-x.  7 0 0 0 May 30 12:31 efi

/sys/firmware/efi:
total 0
drwxr-xr-x. 5 0 0    0 May 30 11:50 .
drwxr-xr-x. 5 0 0    0 May 30 11:50 ..
""".strip()

INSTALLED_RPMS_V1 = """
grub-0.97-94.el6.x86_64                     Mon Jan  8 18:35:25 2018
libreport-compat-2.0.9-24.el6.x86_64        Mon Jan  8 18:32:59 2018
make-3.81-20.el6.x86_64                     Mon Jan  8 18:31:49 2018
""".strip()

INSTALLED_RPMS_V2 = """
grub2-2.02-0.44.el7.x86_64                  Wed May 10 14:10:30 2017
libwbclient-4.4.4-12.el7_3.x86_64           Wed May 10 14:08:10 2017
xorg-x11-drv-vmmouse-13.0.0-12.el7.x86_64   Wed May 10 14:10:36 2017
""".strip()

CMDLINE_V1 = """
ro root=/dev/mapper/vg_rhel6box-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_LVM_LV=vg_rhel6box/lv_swap rd_LVM_LV=vg_rhel6box/lv_root rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=129M@0M  KEYBOAR DTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
""".strip()  # noqa

CMDLINE_V2 = """
BOOT_IMAGE=/vmlinuz-3.10.0-514.10.2.el7.x86_64 root=/dev/mapper/vg_system-lv_root ro crashkernel=auto rd.lvm.lv=vg_system/lv_root rd.lvm.lv=vg_system/lv_swap rhgb quiet LANG=en_US.UTF-8
""".strip()  # noqa

BOOT_LOADER_ENTRIES_1 = """
title Red Hat Enterprise Linux (4.18.0-80.1.2.el8_0.x86_64) 8.0 (Ootpa)
version 4.18.0-80.1.2.el8_0.x86_64
linux /vmlinuz-4.18.0-80.1.2.el8_0.x86_64
initrd /initramfs-4.18.0-80.1.2.el8_0.x86_64.img $tuned_initrd
options root=/dev/mapper/rhel_vm37--146-root ro crashkernel=auto resume=/dev/mapper/rhel_vm37--146-swap rd.lvm.lv=rhel_vm37-146/root rd.lvm.lv=rhel_vm37-146/swap $tuned_params noapic
id rhel-20190428101407-4.18.0-80.1.2.el8_0.x86_64
grub_users $grub_users
grub_arg --unrestricted
grub_class kernel
""".strip()  # noqa

BOOT_LOADER_ENTRIES_2 = """
title Red Hat Enterprise Linux (4.18.0-32.el8.x86_64) 8.0 (Ootpa)
version 4.18.0-32.el8.x86_64
linux /vmlinuz-4.18.0-32.el8.x86_64
initrd /initramfs-4.18.0-32.el8.x86_64.img
options root=/dev/mapper/rhel_rhel8-root ro elevator=noop no_timer_check crashkernel=auto resume=/dev/mapper/rhel_rhel8-swap rd.lvm.lv=rhel_rhel8/root rd.lvm.lv=rhel_rhel8/swap biosdevname=0 net.ifnames=0 rhgb
id rhel-20181027203430-4.18.0-32.el8.x86_64
grub_users $grub_users
grub_arg --unrestricted
grub_class kernel
""".strip()  # noqa

BOOT_LOADER_ENTRIES_3 = """
title Red Hat Enterprise Linux (4.18.0-305.el8.x86_64) 8.4 (Ootpa)
version 4.18.0-305.el8.x86_64
linux /vmlinuz-4.18.0-305.el8.x86_64
initrd /initramfs-4.18.0-305.el8.x86_64.img $tuned_initrd
options $kernelopts $tuned_params
id rhel-20210429130346-4.18.0-305.el8.x86_64
grub_users $grub_users
grub_arg --unrestricted
grub_class kernel
""".strip()

GRUBENV_WITH_TUNED_PARAMS = """
# GRUB Environment Block
saved_entry=295e1ba1696e4fad9e062f096f92d147-4.18.0-305.el8.x86_64
kernelopts=root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200 noapic
boot_success=0
boot_indeterminate=2
tuned_params=transparent_hugepages=never
tuned_initrd=
###############################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################
""".strip()  # noqa

RHEL9_UEFI_HOST_GRUB_ENV = """
saved_entry=263d7f69235042b890394d6fe28ddeaf-5.14.0-570.30.1.el9_6.x86_64
menu_auto_hide=1
boot_success=1
boot_indeterminate=0
""".strip()

# RHEL9 UEFI host example: boot/efi/EFI/redhat/grub.cfg
RHEL9_UEFI_HOST_EFI_GRUB_CFG = """
search --no-floppy --fs-uuid --set=dev 5430c32f-3d13-477b-bb2b-13578b92bce8
set prefix=($dev)/grub2

export $prefix
configfile $prefix/grub.cfg
""".strip()

# RHEL9 UEFI host example: boot/grub2/grub.cfg
RHEL9_UEFI_HOST_GRUB2_CFG = """
#
# DO NOT EDIT THIS FILE
#
# It is automatically generated by grub2-mkconfig using templates
# from /etc/grub.d and settings from /etc/default/grub
#

### BEGIN /etc/grub.d/00_header ###
set pager=1

if [ -f ${config_directory}/grubenv ]; then
  load_env -f ${config_directory}/grubenv
elif [ -s $prefix/grubenv ]; then
  load_env
fi
if [ "${next_entry}" ] ; then
   set default="${next_entry}"
   set next_entry=
   save_env next_entry
   set boot_once=true
else
   set default="${saved_entry}"
fi

if [ x"${feature_menuentry_id}" = xy ]; then
  menuentry_id_option="--id"
else
  menuentry_id_option=""
fi

export menuentry_id_option

if [ "${prev_saved_entry}" ]; then
  set saved_entry="${prev_saved_entry}"
  save_env saved_entry
  set prev_saved_entry=
  save_env prev_saved_entry
  set boot_once=true
fi

function savedefault {
  if [ -z "${boot_once}" ]; then
    saved_entry="${chosen}"
    save_env saved_entry
  fi
}

function load_video {
  if [ x$feature_all_video_module = xy ]; then
    insmod all_video
  else
    insmod efi_gop
    insmod efi_uga
    insmod ieee1275_fb
    insmod vbe
    insmod vga
    insmod video_bochs
    insmod video_cirrus
  fi
}

terminal_output console
if [ x$feature_timeout_style = xy ] ; then
  set timeout_style=menu
  set timeout=5
# Fallback normal timeout code in case the timeout_style feature is
# unavailable.
else
  set timeout=5
fi
### END /etc/grub.d/00_header ###

### BEGIN /etc/grub.d/01_users ###
if [ -f ${prefix}/user.cfg ]; then
  source ${prefix}/user.cfg
  if [ -n "${GRUB2_PASSWORD}" ]; then
    set superusers="root"
    export superusers
    password_pbkdf2 ******** ${GRUB2_PASSWORD}
  fi
fi
### END /etc/grub.d/01_users ###

### BEGIN /etc/grub.d/08_fallback_counting ###
insmod increment
# Check if boot_counter exists and boot_success=0 to activate this behaviour.
if [ -n "${boot_counter}" -a "${boot_success}" = "0" ]; then
  # if countdown has ended, choose to boot rollback deployment,
  # i.e. default=1 on OSTree-based systems.
  if  [ "${boot_counter}" = "0" -o "${boot_counter}" = "-1" ]; then
    set default=1
    set boot_counter=-1
  # otherwise decrement boot_counter
  else
    decrement boot_counter
  fi
  save_env boot_counter
fi
### END /etc/grub.d/08_fallback_counting ###

### BEGIN /etc/grub.d/10_linux ###
insmod part_gpt
insmod xfs
search --no-floppy --fs-uuid --set=root 5430c32f-3d13-477b-bb2b-13578b92bce8
insmod part_gpt
insmod fat
search --no-floppy --fs-uuid --set=boot F900-6DBE

# This section was generated by a script. Do not modify the generated file - all changes
# will be lost the next time file is regenerated. Instead edit the BootLoaderSpec files.
#
# The blscfg command parses the BootLoaderSpec files stored in /boot/loader/entries and
# populates the boot menu. Please refer to the Boot Loader Specification documentation
# for the files format: https://systemd.io/BOOT_LOADER_SPECIFICATION/.

# The kernelopts variable should be defined in the grubenv file. But to ensure that menu
# entries populated from BootLoaderSpec files that use this variable work correctly even
# without a grubenv file, define a fallback kernelopts variable if this has not been set.
#
# The kernelopts variable in the grubenv file can be modified using the grubby tool or by
# executing the grub2-mkconfig tool. For the latter, the values of the GRUB_CMDLINE_LINUX
# and GRUB_CMDLINE_LINUX_DEFAULT options from /etc/default/grub file are used to set both
# the kernelopts variable in the grubenv file and the fallback kernelopts variable.
if [ -z "${kernelopts}" ]; then
  set kernelopts="root=/dev/mapper/rhel_vm--212--182-root ro crashkernel=1G-4G:192M,4G-64G:256M,64G-:512M resume=/dev/mapper/rhel_vm--212--182-swap rd.lvm.lv=rhel_vm-212-182/root rd.lvm.lv=rhel_vm-212-182/swap "
fi

insmod blscfg
blscfg
### END /etc/grub.d/10_linux ###

### BEGIN /etc/grub.d/10_reset_boot_success ###
# Hiding the menu is ok if last boot was ok or if this is a first boot attempt to boot the entry
if [ "${boot_success}" = "1" -o "${boot_indeterminate}" = "1" ]; then
  set menu_hide_ok=1
else
  set menu_hide_ok=0
fi
# Reset boot_indeterminate after a successful boot
if [ "${boot_success}" = "1" ] ; then
  set boot_indeterminate=0
# Avoid boot_indeterminate causing the menu to be hidden more than once
elif [ "${boot_indeterminate}" = "1" ]; then
  set boot_indeterminate=2
fi
# Reset boot_success for current boot
set boot_success=0
save_env boot_success boot_indeterminate
### END /etc/grub.d/10_reset_boot_success ###

### BEGIN /etc/grub.d/12_menu_auto_hide ###
if [ x$feature_timeout_style = xy ] ; then
  if [ "${menu_show_once}" ]; then
    unset menu_show_once
    save_env menu_show_once
    set timeout_style=menu
    set timeout=60
  elif [ "${menu_auto_hide}" -a "${menu_hide_ok}" = "1" ]; then
    set orig_timeout_style=${timeout_style}
    set orig_timeout=${timeout}
    if [ "${fastboot}" = "1" ]; then
      # timeout_style=menu + timeout=0 avoids the countdown code keypress check
      set timeout_style=menu
      set timeout=0
    else
      set timeout_style=hidden
      set timeout=1
    fi
  fi
fi
### END /etc/grub.d/12_menu_auto_hide ###

### BEGIN /etc/grub.d/14_menu_show_once ###
if [ x$feature_timeout_style = xy ]; then
  if [ "${menu_show_once_timeout}" ]; then
    set timeout_style=menu
    set timeout="${menu_show_once_timeout}"
    unset menu_show_once_timeout
    save_env menu_show_once_timeout
  fi
fi
### END /etc/grub.d/14_menu_show_once ###

### BEGIN /etc/grub.d/20_linux_xen ###
### END /etc/grub.d/20_linux_xen ###

### BEGIN /etc/grub.d/20_ppc_terminfo ###
### END /etc/grub.d/20_ppc_terminfo ###

### BEGIN /etc/grub.d/30_os-prober ###
### END /etc/grub.d/30_os-prober ###

### BEGIN /etc/grub.d/30_uefi-firmware ###
if [ "$grub_platform" = "efi" ]; then
  menuentry 'UEFI Firmware Settings' $menuentry_id_option 'uefi-firmware' {
    fwsetup
  }
fi
### END /etc/grub.d/30_uefi-firmware ###

### BEGIN /etc/grub.d/35_fwupd ###
### END /etc/grub.d/35_fwupd ###

### BEGIN /etc/grub.d/40_custom ###
# This file provides an easy way to add custom menu entries.  Simply type the
# menu entries you want to add after this comment.  Be careful not to change
# the 'exec tail' line above.
### END /etc/grub.d/40_custom ###

### BEGIN /etc/grub.d/41_custom ###
if [ -f  ${config_directory}/custom.cfg ]; then
  source ${config_directory}/custom.cfg
elif [ -z "${config_directory}" -a -f  $prefix/custom.cfg ]; then
  source $prefix/custom.cfg
fi
### END /etc/grub.d/41_custom ###
"""

# boot/loader/entries/3b1bf67095e94696b600ed25416e97a8-5.14.0-503.11.1.el9_5.x86_64.conf
RHEL9_UEFI_HOST_BOOT_LOADER_ENTRY_0 = """
title Red Hat Enterprise Linux (5.14.0-503.11.1.el9_5.x86_64) 9.5 (Plow)
version 5.14.0-503.11.1.el9_5.x86_64
linux /vmlinuz-5.14.0-503.11.1.el9_5.x86_64
initrd /initramfs-5.14.0-503.11.1.el9_5.x86_64.img
options root=/dev/mapper/rhel_vm--212--182-root ro crashkernel=1G-4G:192M,4G-64G:256M,64G-:512M resume=/dev/mapper/rhel_vm--212--182-swap rd.lvm.lv=rhel_vm-212-182/root rd.lvm.lv=rhel_vm-212-182/swap
grub_users $grub_users
grub_arg --unrestricted
grub_class rhel
""".strip()

# boot/loader/entries/3b1bf67095e94696b600ed25416e97a8-0-rescue.conf
RHEL9_UEFI_HOST_BOOT_LOADER_ENTRY_1 = """
title Red Hat Enterprise Linux (0-rescue-3b1bf67095e94696b600ed25416e97a8) 9.5 (Plow)
version 0-rescue-3b1bf67095e94696b600ed25416e97a8
linux /vmlinuz-0-rescue-3b1bf67095e94696b600ed25416e97a8
initrd /initramfs-0-rescue-3b1bf67095e94696b600ed25416e97a8.img
options root=/dev/mapper/rhel_vm--212--182-root ro crashkernel=1G-4G:192M,4G-64G:256M,64G-:512M resume=/dev/mapper/rhel_vm--212--182-swap rd.lvm.lv=rhel_vm-212-182/root rd.lvm.lv=rhel_vm-212-182/swap
grub_users $grub_users
grub_arg --unrestricted
grub_class rhel
""".strip()

RHEL9_UEFI_HOST_LS_SYS_FIRMWARE_EFI = """
/sys/firmware:
total 0
drwxr-xr-x  6 0 0 0 Aug 19 14:57 efi
""".strip()

RHEL9_UEFI_HOST_GRUBBY_DEFAULT_INDEX = """
0
""".strip()

RHEL9_UEFI_HOST_GRUBBY_INFO_ALL = """
index=0
kernel="/boot/vmlinuz-5.14.0-503.11.1.el9_5.x86_64"
args="ro crashkernel=1G-4G:192M,4G-64G:256M,64G-:512M resume=/dev/mapper/rhel_vm--212--182-swap rd.lvm.lv=rhel_vm-212-182/root rd.lvm.lv=rhel_vm-212-182/swap"
root="/dev/mapper/rhel_vm--212--182-root"
initrd="/boot/initramfs-5.14.0-503.11.1.el9_5.x86_64.img"
title="Red Hat Enterprise Linux (5.14.0-503.11.1.el9_5.x86_64) 9.5 (Plow)"
id="3b1bf67095e94696b600ed25416e97a8-5.14.0-503.11.1.el9_5.x86_64"
index=1
kernel="/boot/vmlinuz-0-rescue-3b1bf67095e94696b600ed25416e97a8"
args="ro crashkernel=1G-4G:192M,4G-64G:256M,64G-:512M resume=/dev/mapper/rhel_vm--212--182-swap rd.lvm.lv=rhel_vm-212-182/root rd.lvm.lv=rhel_vm-212-182/swap"
root="/dev/mapper/rhel_vm--212--182-root"
initrd="/boot/initramfs-0-rescue-3b1bf67095e94696b600ed25416e97a8.img"
title="Red Hat Enterprise Linux (0-rescue-3b1bf67095e94696b600ed25416e97a8) 9.5 (Plow)"
id="3b1bf67095e94696b600ed25416e97a8-0-rescue
""".strip()


def test_grub1_only1():
    grub1 = Grub1Config(context_wrap(GRUB1_TEMPLATE))
    cmdline = CmdLine(context_wrap(CMDLINE_V1))
    result = GrubConf(None, grub1, None, None, None, None, None, cmdline, IsUEFIBoot(None, None))
    assert result.kernel_initrds['grub_kernels'][0] == 'vmlinuz-2.6.32-642.el6.x86_64'
    assert result.kernel_initrds['grub_initrds'][0] == 'initramfs-2.6.32-642.el6.x86_64.img'
    assert result.is_kdump_iommu_enabled is True
    assert result.get_grub_cmdlines() == result.get_grub_cmdlines('/vmlinuz')
    assert len(result.get_grub_cmdlines()) == 3
    assert result.version == 1
    assert result.is_efi is False


def test_grub1_cmdline():
    grub1 = Grub1Config(context_wrap(GRUB1_TEMPLATE))
    grub2 = Grub2Config(context_wrap(GRUB2_TEMPLATE))
    grub1e = Grub1EFIConfig(context_wrap(GRUB1_EFI_CFG))
    grub2e = Grub2EFIConfig(context_wrap(GRUB2_EFI_CFG))
    cmdline = CmdLine(context_wrap(CMDLINE_V1))
    ls_lan = LSlanFiltered(context_wrap(SYS_FIRMWARE_DIR_NOEFI))
    result = GrubConf(
        None, grub1, grub2, grub1e, grub2e, None, None, cmdline, IsUEFIBoot(ls_lan, None)
    )
    assert result.kernel_initrds['grub_kernels'][0] == 'vmlinuz-2.6.32-642.el6.x86_64'
    assert result.kernel_initrds['grub_initrds'][0] == 'initramfs-2.6.32-642.el6.x86_64.img'
    assert result.is_kdump_iommu_enabled is True
    assert result.get_grub_cmdlines() == result.get_grub_cmdlines('/vmlinuz')
    assert len(result.get_grub_cmdlines()) == 3
    assert result.version == 1
    assert result.is_efi is False


def test_grub1_efi_cmdline():
    grub1 = Grub1Config(context_wrap(GRUB1_TEMPLATE))
    grub2 = Grub2Config(context_wrap(GRUB2_TEMPLATE))
    grub1e = Grub1EFIConfig(context_wrap(GRUB1_EFI_CFG))
    grub2e = Grub2EFIConfig(context_wrap(GRUB2_EFI_CFG))
    cmdline = CmdLine(context_wrap(CMDLINE_V1))
    ls_lan = LSlanFiltered(context_wrap(SYS_FIRMWARE_DIR_EFI))
    result = GrubConf(
        None, grub1, grub2, grub1e, grub2e, None, None, cmdline, IsUEFIBoot(ls_lan, None)
    )
    assert result.kernel_initrds['grub_kernels'][0] == 'vmlinuz-2.6.32-71.el6.x86_64'
    assert result.kernel_initrds['grub_initrds'][0] == 'initramfs-2.6.32-71.el6.x86_64.img'
    assert result.is_kdump_iommu_enabled is False
    assert len(result.get_grub_cmdlines()) == 1
    assert result.version == 1
    assert result.is_efi is True


def test_grub1_rpms():
    grub1 = Grub1Config(context_wrap(GRUB1_TEMPLATE))
    grub2 = Grub2Config(context_wrap(GRUB2_TEMPLATE))
    grub1e = Grub1EFIConfig(context_wrap(GRUB1_EFI_CFG))
    grub2e = Grub2EFIConfig(context_wrap(GRUB2_EFI_CFG))
    rpms = InstalledRpms(context_wrap(INSTALLED_RPMS_V1))
    cmdline = CmdLine(context_wrap(CMDLINE_V2))
    ls_lan = LSlanFiltered(context_wrap(SYS_FIRMWARE_DIR_NOEFI))
    result = GrubConf(
        None, grub1, grub2, grub1e, grub2e, None, rpms, cmdline, IsUEFIBoot(ls_lan, None)
    )
    assert result.kernel_initrds['grub_kernels'][0] == 'vmlinuz-2.6.32-642.el6.x86_64'
    assert result.kernel_initrds['grub_initrds'][0] == 'initramfs-2.6.32-642.el6.x86_64.img'
    assert result.is_kdump_iommu_enabled is True
    assert result.get_grub_cmdlines() == result.get_grub_cmdlines('/vmlinuz')
    assert len(result.get_grub_cmdlines()) == 3
    assert result.version == 1
    assert result.is_efi is False


def test_grub1_efi_rpms():
    grub1 = Grub1Config(context_wrap(GRUB1_TEMPLATE))
    grub2 = Grub2Config(context_wrap(GRUB2_TEMPLATE))
    grub1e = Grub1EFIConfig(context_wrap(GRUB1_EFI_CFG))
    grub2e = Grub2EFIConfig(context_wrap(GRUB2_EFI_CFG))
    rpms = InstalledRpms(context_wrap(INSTALLED_RPMS_V1))
    cmdline = CmdLine(context_wrap(CMDLINE_V2))
    ls_lan = LSlanFiltered(context_wrap(SYS_FIRMWARE_DIR_EFI))
    result = GrubConf(
        None, grub1, grub2, grub1e, grub2e, None, rpms, cmdline, IsUEFIBoot(ls_lan, None)
    )
    assert result.kernel_initrds['grub_kernels'][0] == 'vmlinuz-2.6.32-71.el6.x86_64'
    assert result.kernel_initrds['grub_initrds'][0] == 'initramfs-2.6.32-71.el6.x86_64.img'
    assert result.is_kdump_iommu_enabled is False
    assert result.get_grub_cmdlines() == result.get_grub_cmdlines('/vmlinuz')
    assert len(result.get_grub_cmdlines()) == 1
    assert result.version == 1
    assert result.is_efi is True


def test_grub2_cmdline():
    grub1 = Grub1Config(context_wrap(GRUB1_TEMPLATE))
    grub2 = Grub2Config(context_wrap(GRUB2_TEMPLATE))
    grub1e = Grub1EFIConfig(context_wrap(GRUB1_EFI_CFG))
    grub2e = Grub2EFIConfig(context_wrap(GRUB2_EFI_CFG))
    cmdline = CmdLine(context_wrap(CMDLINE_V2))
    ls_lan = LSlanFiltered(context_wrap(SYS_FIRMWARE_DIR_NOEFI))
    result = GrubConf(
        None, grub1, grub2, grub1e, grub2e, None, None, cmdline, IsUEFIBoot(ls_lan, None)
    )
    assert result.kernel_initrds['grub_kernels'][0] == 'vmlinuz-3.10.0-327.el7.x86_64'
    assert result.kernel_initrds['grub_initrds'][0] == 'initramfs-3.10.0-327.el7.x86_64.img'
    assert result.is_kdump_iommu_enabled is False
    assert (
        result.get_grub_cmdlines('/vmlinuz-3.10.0')[0].name
        == "'Red Hat Enterprise Linux Server (3.10.0-327.el7.x86_64) 7.2 (Maipo)' --class red --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-327.el7.x86_64-advanced-4f80b3d4-90ba-4545-869c-febdecc586ce'"
    )  # noqa
    assert result.get_grub_cmdlines('test') == []
    assert result.get_grub_cmdlines('') == []
    assert len(result.get_grub_cmdlines()) == 2
    assert result.version == 2
    assert result.is_efi is False


def test_grub2_efi_cmdline():
    grub1 = Grub1Config(context_wrap(GRUB1_TEMPLATE))
    grub2 = Grub2Config(context_wrap(GRUB2_TEMPLATE))
    grub1e = Grub1EFIConfig(context_wrap(GRUB1_EFI_CFG))
    grub2e = Grub2EFIConfig(context_wrap(GRUB2_EFI_CFG))
    cmdline = CmdLine(context_wrap(CMDLINE_V2))
    ls_lan = LSlanFiltered(context_wrap(SYS_FIRMWARE_DIR_EFI))
    result = GrubConf(
        None, grub1, grub2, grub1e, grub2e, None, None, cmdline, IsUEFIBoot(ls_lan, None)
    )
    assert result.get_grub_cmdlines() == result.get_grub_cmdlines('/vmlinuz')
    assert result.get_grub_cmdlines('rescue')[0].name.startswith(
        "'Red Hat Enterprise Linux Server (0-rescue"
    )
    assert len(result.get_grub_cmdlines()) == 4
    assert result.version == 2
    assert result.is_efi is True


def test_grub2_rpms():
    grub1 = Grub1Config(context_wrap(GRUB1_TEMPLATE))
    grub2 = Grub2Config(context_wrap(GRUB2_TEMPLATE))
    grub1e = Grub1EFIConfig(context_wrap(GRUB1_EFI_CFG))
    grub2e = Grub2EFIConfig(context_wrap(GRUB2_EFI_CFG))
    rpms = InstalledRpms(context_wrap(INSTALLED_RPMS_V2))
    cmdline = CmdLine(context_wrap(CMDLINE_V1))
    result = GrubConf(
        None, grub1, grub2, grub1e, grub2e, None, rpms, cmdline, IsUEFIBoot(None, None)
    )
    assert result.kernel_initrds['grub_kernels'][0] == 'vmlinuz-3.10.0-327.el7.x86_64'
    assert result.kernel_initrds['grub_initrds'][0] == 'initramfs-3.10.0-327.el7.x86_64.img'
    assert result.is_kdump_iommu_enabled is False
    assert (
        result.get_grub_cmdlines('/vmlinuz-3.10.0')[0].name
        == "'Red Hat Enterprise Linux Server (3.10.0-327.el7.x86_64) 7.2 (Maipo)' --class red --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-327.el7.x86_64-advanced-4f80b3d4-90ba-4545-869c-febdecc586ce'"
    )  # noqa
    assert result.get_grub_cmdlines('test') == []
    assert result.get_grub_cmdlines('') == []
    assert len(result.get_grub_cmdlines()) == 2
    assert result.version == 2
    assert result.is_efi is False


def test_grub2_efi_rpms():
    grub1 = Grub1Config(context_wrap(GRUB1_TEMPLATE))
    grub2 = Grub2Config(context_wrap(GRUB2_TEMPLATE))
    grub1e = Grub1EFIConfig(context_wrap(GRUB1_EFI_CFG))
    grub2e = Grub2EFIConfig(context_wrap(GRUB2_EFI_CFG))
    rpms = InstalledRpms(context_wrap(INSTALLED_RPMS_V2))
    cmdline = CmdLine(context_wrap(CMDLINE_V1))
    ls_lan = LSlanFiltered(context_wrap(SYS_FIRMWARE_DIR_EFI))
    result = GrubConf(
        None, grub1, grub2, grub1e, grub2e, None, rpms, cmdline, IsUEFIBoot(ls_lan, None)
    )
    assert result.kernel_initrds['grub_initrds'][0] == 'initramfs-3.10.0-514.16.1.el7.x86_64.img'
    assert result.get_grub_cmdlines() == result.get_grub_cmdlines('/vmlinuz')
    assert result.get_grub_cmdlines('rescue')[0].name.startswith(
        "'Red Hat Enterprise Linux Server (0-rescue"
    )
    assert len(result.get_grub_cmdlines()) == 4
    assert result.version == 2
    assert result.is_efi is True


def test_get_grub_cmdlines_none():
    grub1 = Grub1Config(context_wrap(GRUB1_TEMPLATE))
    grub2 = Grub2Config(context_wrap(GRUB2_TEMPLATE))
    cmdline = CmdLine(context_wrap(CMDLINE_V2))
    ls_lan = LSlanFiltered(context_wrap(SYS_FIRMWARE_DIR_EFI))
    with pytest.raises(Exception) as pe:
        GrubConf(None, grub1, grub2, None, None, None, None, cmdline, IsUEFIBoot(ls_lan, None))
    assert "No valid grub configuration is found." in str(pe.value)

    grub1e = Grub1EFIConfig(context_wrap(GRUB1_TEMPLATE))
    grub2e = Grub2EFIConfig(context_wrap(GRUB2_TEMPLATE))
    rpms = InstalledRpms(context_wrap(INSTALLED_RPMS_V2))
    with pytest.raises(Exception) as pe:
        GrubConf(None, None, None, grub1e, grub2e, None, rpms, None, IsUEFIBoot(None, None))
    assert "No valid grub configuration is found." in str(pe.value)

    grub2e = Grub2EFIConfig(context_wrap(GRUB2_EFI_CFG))
    with pytest.raises(Exception) as pe:
        GrubConf(None, grub1, None, grub1e, grub2e, None, rpms, None, IsUEFIBoot(None, None))
    assert "No valid grub configuration is found." in str(pe.value)


def test_grub2_grubenv():
    grubenv = GrubEnv(context_wrap(GRUBENV_WITH_TUNED_PARAMS))
    grub2 = Grub2Config(context_wrap(GRUB2_TEMPLATE))
    grub_ble1 = BLE(context_wrap(BOOT_LOADER_ENTRIES_1))
    grub_ble2 = BLE(context_wrap(BOOT_LOADER_ENTRIES_2))
    grub_bles = BootLoaderEntries([grub_ble1, grub_ble2], grubenv, IsUEFIBoot(None, None))
    rpms = InstalledRpms(context_wrap(INSTALLED_RPMS_V2))
    ls_lan = LSlanFiltered(context_wrap(SYS_FIRMWARE_DIR_NOEFI))
    result = GrubConf(
        None, None, grub2, None, None, grub_bles, rpms, None, IsUEFIBoot(ls_lan, None)
    )
    assert len(result.get_grub_cmdlines()) == 2
    assert 'noapic' not in result.get_grub_cmdlines()[1]['cmdline']
    assert 'transparent_hugepages' not in result.get_grub_cmdlines()[0]['cmdline']
    assert result.version == 2
    assert not result.is_efi


def test_grub2_grubenv_with_kernelopts():
    grubenv = GrubEnv(context_wrap(GRUBENV_WITH_TUNED_PARAMS))
    grub2 = Grub2Config(context_wrap(GRUB2_TEMPLATE_BLSCFG))
    grub_ble1 = BLE(context_wrap(BOOT_LOADER_ENTRIES_1))
    grub_ble2 = BLE(context_wrap(BOOT_LOADER_ENTRIES_2))
    grub_ble3 = BLE(context_wrap(BOOT_LOADER_ENTRIES_3))
    grub_bles = BootLoaderEntries(
        [grub_ble1, grub_ble2, grub_ble3], grubenv, IsUEFIBoot(None, None)
    )
    rpms = InstalledRpms(context_wrap(INSTALLED_RPMS_V2))
    ls_lan = LSlanFiltered(context_wrap(SYS_FIRMWARE_DIR_NOEFI))
    result = GrubConf(
        None, None, grub2, None, None, grub_bles, rpms, None, IsUEFIBoot(ls_lan, None)
    )
    assert len(result.get_grub_cmdlines()) == 3
    assert 'noapic' in result.get_grub_cmdlines()[2]['cmdline']
    assert 'transparent_hugepages' in result.get_grub_cmdlines()[2]['cmdline']
    assert result.version == 2
    assert not result.is_efi


def test_grub2_with_blscfg():
    grub2 = Grub2Config(context_wrap(GRUB2_TEMPLATE_BLSCFG))
    grub_ble1 = BLE(context_wrap(BOOT_LOADER_ENTRIES_1))
    grub_ble2 = BLE(context_wrap(BOOT_LOADER_ENTRIES_2))
    grub_ble3 = BLE(context_wrap(BOOT_LOADER_ENTRIES_3))
    grub_bles = BootLoaderEntries([grub_ble1, grub_ble2, grub_ble3], None, IsUEFIBoot(None, None))
    rpms = InstalledRpms(context_wrap(INSTALLED_RPMS_V2))
    ls_lan = LSlanFiltered(context_wrap(SYS_FIRMWARE_DIR_NOEFI))
    result = GrubConf(
        None, None, grub2, None, None, grub_bles, rpms, None, IsUEFIBoot(ls_lan, None)
    )
    assert len(result.get_grub_cmdlines()) == 3
    assert 'noapic' in result.get_grub_cmdlines()[0]['cmdline']
    assert 'transparent_hugepages' not in result.get_grub_cmdlines()[0]['cmdline']
    assert result.version == 2
    assert not result.is_efi


def test_grub2_boot_loader_entries():
    grub2 = Grub2Config(context_wrap(GRUB2_TEMPLATE_BLSCFG))
    grub_ble1 = BLE(context_wrap(BOOT_LOADER_ENTRIES_1))
    grub_ble2 = BLE(context_wrap(BOOT_LOADER_ENTRIES_2))
    grub_bles = BootLoaderEntries([grub_ble1, grub_ble2], None, IsUEFIBoot(None, None))
    rpms = InstalledRpms(context_wrap(INSTALLED_RPMS_V2))
    ls_lan = LSlanFiltered(context_wrap(SYS_FIRMWARE_DIR_NOEFI))
    result = GrubConf(
        None, None, grub2, None, None, grub_bles, rpms, None, IsUEFIBoot(ls_lan, None)
    )
    assert len(result.get_grub_cmdlines()) == 2
    assert 'noapic' in result.get_grub_cmdlines()[0]['cmdline']
    assert result.version == 2
    assert not result.is_efi


def test_grub2_boot_loader_entries_with_grubenv():
    grubenv = GrubEnv(context_wrap(GRUBENV_WITH_TUNED_PARAMS))
    grub2 = Grub2Config(context_wrap(GRUB2_TEMPLATE_BLSCFG))
    grub_ble1 = BLE(context_wrap(BOOT_LOADER_ENTRIES_1))
    grub_ble3 = BLE(context_wrap(BOOT_LOADER_ENTRIES_3))
    grub_bles = BootLoaderEntries([grub_ble1, grub_ble3], grubenv, IsUEFIBoot(None, None))
    rpms = InstalledRpms(context_wrap(INSTALLED_RPMS_V2))
    ls_lan = LSlanFiltered(context_wrap(SYS_FIRMWARE_DIR_NOEFI))
    result = GrubConf(
        None, None, grub2, None, None, grub_bles, rpms, None, IsUEFIBoot(ls_lan, None)
    )
    assert len(result.get_grub_cmdlines()) == 2
    assert 'noapic' in result.get_grub_cmdlines()[0]['cmdline']
    assert 'transparent_hugepages' in result.get_grub_cmdlines()[0]['cmdline']
    assert 'noapic' in result.get_grub_cmdlines()[1]['cmdline']
    assert 'transparent_hugepages' in result.get_grub_cmdlines()[1]['cmdline']
    assert result.version == 2
    assert not result.is_efi


def test_grub2_rhel9_efi_grubby():
    grubenv = GrubEnv(context_wrap(RHEL9_UEFI_HOST_GRUB_ENV))
    grub2 = Grub2Config(context_wrap(RHEL9_UEFI_HOST_GRUB2_CFG))
    grub2_efi = Grub2EFIConfig(context_wrap(RHEL9_UEFI_HOST_EFI_GRUB_CFG))
    grub_ble0 = BLE(context_wrap(RHEL9_UEFI_HOST_BOOT_LOADER_ENTRY_0))
    grub_ble1 = BLE(context_wrap(RHEL9_UEFI_HOST_BOOT_LOADER_ENTRY_1))
    grub_bles = BootLoaderEntries([grub_ble0, grub_ble1], grubenv, IsUEFIBoot(None, None))
    ls_lan = LSlanFiltered(context_wrap(RHEL9_UEFI_HOST_LS_SYS_FIRMWARE_EFI))

    grubby_info_all = GrubbyInfoAll(context_wrap(RHEL9_UEFI_HOST_GRUBBY_INFO_ALL))
    grubby_default_index = GrubbyDefaultIndex(context_wrap(RHEL9_UEFI_HOST_GRUBBY_DEFAULT_INDEX))
    grubby = Grubby(grubby_info_all, grubby_default_index, grubenv, IsUEFIBoot(ls_lan, None))

    result = GrubConf(
        grubby, None, grub2, None, grub2_efi, grub_bles, None, None, IsUEFIBoot(ls_lan, None)
    )
    assert len(result.get_grub_cmdlines()) == 2
    assert 'root=/dev/mapper/rhel_vm--212--182-root' in result.get_grub_cmdlines()[0]['cmdline']
    assert (
        'crashkernel=1G-4G:192M,4G-64G:256M,64G-:512M' in result.get_grub_cmdlines()[1]['cmdline']
    )
    assert result.version == 2
    assert result.is_efi
