# SPDX-License-Identifier: MIT
---
- name: Test bootloader_settings variable
  hosts: all
  gather_facts: false
  tags:
    - tests::reboot
  vars:
    bootloader_reboot_ok: true
  tasks:
    - name: Run in a block to clean up afterwards
      block:
        - name: Skip on s390x architecture
          include_tasks: tasks/skip_on_s390x.yml

        - name: Get bootloader_facts
          vars:
            bootloader_gather_facts: true
          include_role:
            name: linux-system-roles.bootloader

        - name: Skip test on ostree systems
          meta: end_host
          when: __bootloader_is_ostree

        # Images in CI might have a grub timeout set to a different other than
        # the default 5 value.
        # In this case, the above invocation require handlers to be flushed.
        - name: Flush handlers
          meta: flush_handlers

        - name: Set fact with the default kernel to use for clones
          set_fact:
            __default_kernel: "{{ bootloader_facts | rejectattr('initrd', 'undefined')
              | selectattr('default') | first }}"

        - name: Clone kernel to Clone1
          vars:
            __bootloader_kernel_source: "{{ __default_kernel }}"
            __bootloader_clone_num: 1
            __bootloader_copy_default: true
            __bootloader_make_default: true
          include_tasks: tasks/clone_kernel.yml

        - name: Set fact with the test clone
          set_fact:
            cloned_kernel: "{{
              bootloader_facts | selectattr('title', 'defined') |
              selectattr('title', 'search', 'Clone1') |
              first }}"

        - name: >-
            Verify that the clone kernel is default
          vars:
            default_bootloader: "{{
              (bootloader_facts | selectattr('default') |
              first) }}"
          assert:
            that: cloned_kernel == default_bootloader

        - name: Replace configuration with settings using kernel path
          vars:
            bootloader_gather_facts: true
            bootloader_settings:
              - kernel:
                  path: "{{ cloned_kernel.kernel }}"
                options:
                  - name: console
                    value: tty0
                  - name: print-fatal-signals
                    value: 1
                  - name: no_timer_check
                    state: present
                  - name: option_without_value
                  - name: option_with_value
                    value: option_value
                  - previous: replaced
              - kernel: ALL
                options:
                  - name: option_set_from_ALL
                    state: present
              - kernel:
                  path: "{{ __default_kernel.kernel }}"
                default: true
            bootloader_timeout: 6
          include_role:
            name: linux-system-roles.bootloader

        - name: Flush handlers
          meta: flush_handlers

        - name: Ensure bootloader_reboot_required is set to false
          assert:
            that: not (bootloader_reboot_required | d(false))

        - name: Verify clone kernel arguments and that it is not default
          vars:
            default_bootloader: "{{
              (bootloader_facts | selectattr('default') |
              first) }}"
          assert:
            that:
              - cloned_kernel != default_bootloader
              - >-
                (bootloader_facts |
                selectattr('kernel', 'search', cloned_kernel.kernel) | first).args |
                regex_search('^.*console=tty0 print-fatal-signals=1 no_timer_check
                option_without_value option_with_value=option_value option_set_from_ALL( |)$') | length > 0

        - name: Verify boot loader timeout configuration
          command: cat {{ __bootloader_grub_conf }}
          failed_when: >-
            not __bootloader_default_grub_content.stdout is
            regex('set timeout=6')
          register: __bootloader_default_grub_content
          changed_when: false

        - name: Change some settings using kernel title
          vars:
            bootloader_gather_facts: true
            bootloader_settings:
              - kernel:
                  title: "{{ cloned_kernel.title }}"
                options:
                  - name: option_without_value
                    state: absent
                  - name: option_set_from_ALL
                    state: absent
                  - name: option_with_value
                    state: absent
            bootloader_timeout: 4
          include_role:
            name: linux-system-roles.bootloader

        - name: Flush handlers
          meta: flush_handlers

        - name: Ensure bootloader_reboot_required is set to false
          assert:
            that: not (bootloader_reboot_required | d(false))

        - name: Verify that existing settings remained and removed settings are not present
          vars:
            __settings: "{{ (bootloader_facts | selectattr('kernel', 'search', cloned_kernel.kernel) | first).args }}"
          assert:
            that:
              - __settings | regex_search('^.*console=tty0 print-fatal-signals=1 no_timer_check( |)$') | length > 0
              - __settings | regex_search('option_without_value') == none
              - __settings | regex_search('option_set_from_ALL')  == none
              - __settings | regex_search('option_with_value') == none

        - name: Verify boot loader timeout configuration
          command: cat {{ __bootloader_grub_conf }}
          failed_when: >-
            not __bootloader_default_grub_content.stdout is
            regex('set timeout=4')
          register: __bootloader_default_grub_content
          changed_when: false

        - name: >-
            Set existing variable using kernel path, should report not changed
          vars:
            bootloader_settings:
              - kernel:
                  path: "{{ cloned_kernel.kernel }}"
                options:
                  - name: console
                    value: tty0
                    state: present
            bootloader_timeout: 4
          include_role:
            name: linux-system-roles.bootloader

        - name: Flush handlers
          meta: flush_handlers

        - name: Ensure bootloader_reboot_required is not set to true
          assert:
            that: not bootloader_reboot_required

        - name: Verify settings
          assert:
            that: >-
              (bootloader_facts |
              selectattr('kernel', 'search', cloned_kernel.kernel) | first).args |
              regex_search('^.*console=tty0 print-fatal-signals=1
              no_timer_check( |)$') | length > 0

        - name: Verify boot loader timeout configuration
          command: cat {{ __bootloader_grub_conf }}
          failed_when: >-
            not __bootloader_default_grub_content.stdout is
            regex('set timeout=4')
          register: __bootloader_default_grub_content
          changed_when: false
      always:
        - name: Remove cloned kernels
          command: >-
            grubby --remove-kernel={{ __default_kernel.kernel }}_clone1
          changed_when: true
          failed_when: false
