# SPDX-License-Identifier: MIT
---
- name: Test creating, modifying, and removing kernels
  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: Clone kernel to Clone1
          vars:
            __bootloader_clone_num: 1
            __bootloader_copy_default: true
          include_tasks: tasks/clone_kernel.yml

        - name: Verify settings
          vars:
            __default_args: "{{
              (bootloader_facts | selectattr('title', 'defined') |
              selectattr('default') |
              first).args }}"
          assert:
            that: >-
              (bootloader_facts | selectattr('title', 'defined') |
              selectattr('title', 'search', 'Clone1') |
              first).args == __default_args ~ ' test=kernel'

        - name: Remove Clone1 kernel with 3 kernel keys
          vars:
            bootloader_gather_facts: true
            bootloader_settings:
              - kernel:
                  path: "{{ test_kernel.kernel }}_clone1"
                  initrd: "{{ test_kernel.initrd |
                    regex_replace(' .*$', '') }}_clone1"
                  title: Clone1
                options:
                  - name: console
                    value: tty0
                    state: present
                  - copy_default: true
                state: absent
          include_role:
            name: linux-system-roles.bootloader

        - name: Verify that Clone1 kernel is removed
          assert:
            that: bootloader_facts | selectattr('title', 'defined') |
              selectattr('title', 'search', 'Clone1') |
              list | length == 0

        - name: Clone kernel to Clone2
          vars:
            __bootloader_clone_num: 2
            __bootloader_copy_default: false
          include_tasks: tasks/clone_kernel.yml

        - name: Verify settings
          assert:
            that: >-
              (bootloader_facts | selectattr('title', 'defined') |
              selectattr('title', 'search', 'Clone2') |
              first).args == 'test=kernel'

        - name: Remove Clone2 kernel with kernel path
          vars:
            bootloader_gather_facts: true
            bootloader_settings:
              - kernel:
                  path: "{{ test_kernel.kernel }}_clone2"
                state: absent
          include_role:
            name: linux-system-roles.bootloader

        - name: Verify that Clone2 kernel is removed
          assert:
            that: bootloader_facts | selectattr('title', 'defined') |
              selectattr('title', 'search', 'Clone2') |
              list | length == 0
      always:
        - name: Remove cloned kernels
          command: >-
            grubby --remove-kernel={{ test_kernel.kernel }}_clone{{ item }}
          loop:
            - 1
            - 2
          changed_when: true
          failed_when: false
