# vim: syntax=ruby:expandtab:shiftwidth=2:softtabstop=2:tabstop=2
# Copyright (c) 2016-present, Facebook, Inc.
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require './spec/spec_helper'
require_relative '../libraries/default'
grub1_contents = <<EOF
# 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/sda2
#          initrd /initrd-[generic-]version.img
boot=/dev/sda1
device (hd0,1) HD(1,800,80000,a7d234b6-5131-4dbf-b1c3-b53aedf4c486)
default=0
timeout=5
serial --speed=57600
terminal --timeout=5 serial console
title CentOS (4.0.9-58_fbk9_3937_gaee7b6f)
	root (hd0,1)
	kernel /vmlinuz-4.0.9-58_fbk9_3937_gaee7b6f ro root=LABEL=/ crashkernel=128M rhgb quiet nopat memmap=64M$1G selinux=0 biosdevname=0 net.ifnames=0 console=ttyS0,57600 LANG=en_US.UTF-8
	initrd /initramfs-4.0.9-58_fbk9_3937_gaee7b6f.img
EOF

grub2_search_statements = <<EOF
#
# 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 [ -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
}

serial --unit=1 --speed=57600
terminal_input serial console
terminal_output serial 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 root ${GRUB2_PASSWORD}
  fi
fi
### END /etc/grub.d/01_users ###

### BEGIN /etc/grub.d/10_linux ###
menuentry 'CentOS Linux (4.0.9-75_fbk14_4148_g37fe86f) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-327.18.2.el7.x86_64-advanced-e3beaa30-6664-49cf-b2eb-8a00aa6d7517' {
    load_video
    set gfxpayload=keep
    insmod gzio
    insmod part_gpt
    insmod ext2
    if [ x$feature_platform_search_hint = xy ]; then
      search --no-floppy --fs-uuid --set=root  221f5b2b-8d33-4f1b-80b3-571cd36b89ce
    else
      search --no-floppy --fs-uuid --set=root 221f5b2b-8d33-4f1b-80b3-571cd36b89ce
    fi
    linuxefi /vmlinuz-4.0.9-75_fbk14_4148_g37fe86f root=LABEL=/ ro crashkernel=128M console=tty0 pcie_pme=nomsi selinux=0 biosdevname=0 net.ifnames=0 console=ttyS1,57600 LANG=en_US.UTF-8
    initrdefi /initramfs-4.0.9-75_fbk14_4148_g37fe86f.img
}

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

### 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/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 ###
EOF

grub2_contents = <<EOF
#
# 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 [ -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
}

serial --unit=1 --speed=57600
terminal_input serial console
terminal_output serial 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 root ${GRUB2_PASSWORD}
  fi
fi
### END /etc/grub.d/01_users ###

### BEGIN /etc/grub.d/10_linux ###
menuentry 'CentOS Linux (4.0.9-75_fbk14_4148_g37fe86f) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-327.18.2.el7.x86_64-advanced-4803ec21-c800-4828-96e9-36e43c3fd35c' {
	load_video
	set gfxpayload=keep
	insmod gzio
	insmod part_gpt
	insmod ext2
	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  4a7e68d7-1c84-4b3e-933b-7440e12e35da
	else
	  search --no-floppy --fs-uuid --set=root 4a7e68d7-1c84-4b3e-933b-7440e12e35da
	fi
	linuxefi /vmlinuz-4.0.9-75_fbk14_4148_g37fe86f root=LABEL=/ ro crashkernel=128M console=tty0 pcie_pme=nomsi selinux=0 biosdevname=0 net.ifnames=0 console=ttyS1,57600 LANG=en_US.UTF-8
	initrdefi /initramfs-4.0.9-75_fbk14_4148_g37fe86f.img
}

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

### 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/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 ###
EOF

describe FB::Grub do
  it 'should extract the root drive correctly from a grub 1 config' do
    extracted = FB::Grub.extract_root_device(grub1_contents)
    expect(extracted).to eq('hd0,1')
  end

  it 'should not pay attention to root drive comments' do
    grub1_with_comment = "# root(hd9,99)\n" + grub1_contents
    extracted = FB::Grub.extract_root_device(grub1_with_comment)
    expect(extracted).to eq('hd0,1')
  end

  it 'should extract the root drive correctly from a grub 2 config' do
    extracted = FB::Grub.extract_root_device(grub2_contents)
    expect(extracted).to eq('hd0,gpt2')
  end

  it 'should extract device hints correctly from a grub 1 config' do
    extracted = FB::Grub.extract_device_hints(grub1_contents)
    expect(extracted).to eq(
      ['device (hd0,1) HD(1,800,80000,a7d234b6-5131-4dbf-b1c3-b53aedf4c486)'],
    )
  end

  it 'should not pay attention to commented out device hints' do
    grub1_with_comment = "# device (hd0,1) HD(123)\n" + grub1_contents
    extracted = FB::Grub.extract_device_hints(grub1_with_comment)
    expect(extracted).to eq(
      ['device (hd0,1) HD(1,800,80000,a7d234b6-5131-4dbf-b1c3-b53aedf4c486)'],
    )
  end

  it 'should extract device hints correctly from a grub 2 config' do
    extracted = FB::Grub.extract_device_hints(grub2_contents)
    expect(extracted).to eq([])
  end

  it 'should not extract random stuff from search statements' do
    extracted = FB::Grub.extract_device_hints(grub2_search_statements)
    expect(extracted).to eq([])
  end
end
