---
- name: Test changing settings
  hosts: all
  tags:
    - tests::reboot
  tasks:
    - name: Run test
      block:
        - name: Determine if system is ostree and set flag
          when: not __kernel_settings_is_ostree is defined
          block:
            - name: Check if system is ostree
              stat:
                path: /run/ostree-booted
              register: __ostree_booted_stat

            - name: Set flag to indicate system is ostree
              set_fact:
                __kernel_settings_is_ostree: "{{
                  __ostree_booted_stat.stat.exists }}"

        # procps-ng for sysctl cmd
        - name: Ensure required packages are installed
          package:
            name: [tuned, procps-ng]
            state: present
            use: "{{ (__kernel_settings_is_ostree | d(false)) |
                    ternary('ansible.posix.rhel_rpm_ostree', omit) }}"

        - name: See if tuned has a profile subdir
          stat:
            path: /etc/tuned/profiles
          register: __tuned_profiles

        - name: Set profile dir
          set_fact:
            __profile_dir: "{{ __dir ~ '/kernel_settings' }}"
          vars:
            __dir: "{{ '/etc/tuned/profiles' if __tuned_profiles.stat.exists
              else '/etc/tuned' }}"

        - name: Ensure kernel settings profile directory exists
          file:
            path: "{{ __profile_dir }}"
            state: directory
            mode: "0755"

        - name: Generate a configuration for kernel settings
          copy:
            src: tuned/etc/tuned/change_settings/tuned.conf
            dest: "{{ __profile_dir }}/tuned.conf"
            mode: "0644"

        - name: Ensure required services are enabled and started
          service:
            name: tuned
            state: restarted
            enabled: true

        - name: Apply kernel_settings
          include_role:
            name: linux-system-roles.kernel_settings
            public: true
          vars:
            kernel_settings_sysctl:
              - name: fs.file-max
                value: 400000
              - name: kernel.threads-max
                state: absent
            kernel_settings_sysfs:
              - name: /sys/class/net/lo/mtu
                value: 65000

        - name: Ensure kernel_settings_reboot_required is unset or undefined
          assert:
            that: not kernel_settings_reboot_required | d(false)

        - name: Ensure role reported changed
          assert:
            that: __kernel_settings_changed | d(false)

        - name: Check sysfs after role runs
          command: grep -x 65000 /sys/class/net/lo/mtu
          changed_when: false

        - name: Check sysctl after role runs
          shell: |-
            set -euo pipefail
            sysctl -n fs.file-max | grep -x 400000
          changed_when: false

        - name: Check sysctl after role runs
          shell: |-
            set -euo pipefail
            sysctl -n kernel.threads-max | grep -Lxvq 29968
          changed_when: false

        - name: Reboot the machine - see if settings persist after reboot
          reboot:
            test_command: tuned-adm active

        - name: Check sysctl after reboot
          shell: |-
            set -euo pipefail
            sysctl -n fs.file-max | grep -x 400000
          changed_when: false

        - name: Check sysfs after reboot
          command: grep -x 65000 /sys/class/net/lo/mtu
          changed_when: false

        - name: Check sysctl after reboot
          shell: |-
            set -euo pipefail
            sysctl -n kernel.threads-max | grep -Lxvq 29968
          changed_when: false

        - name: Check with tuned verify
          command: tuned-adm verify -i
          changed_when: false

        - name: Apply role again and remove settings
          include_role:
            name: linux-system-roles.kernel_settings
          vars:
            kernel_settings_reboot_ok: true
            kernel_settings_sysctl:
              - name: fs.file-max
                state: absent
              - name: kernel.threads-max
                state: absent

        - name: Force handlers
          meta: flush_handlers

        - name: Ensure kernel_settings_reboot_required is not set or is false
          assert:
            that: not kernel_settings_reboot_required | d(false)

        - name: Ensure role reported changed
          assert:
            that: __kernel_settings_changed | d(false)

        - name: Check sysctl after reboot
          shell: |-
            set -euo pipefail
            sysctl -n fs.file-max | grep -Lxvq 400000
          changed_when: false

        - name: Apply kernel_settings for removing
          include_role:
            name: linux-system-roles.kernel_settings
          vars:
            kernel_settings_reboot_ok: true
            kernel_settings_sysctl:
              - name: fs.file-max
                value: 400001
            kernel_settings_sysfs:
              - name: /sys/class/net/lo/mtu
                value: 60666

        - name: Force handlers
          meta: flush_handlers

        - name: Ensure kernel_settings_reboot_required is not set or is false
          assert:
            that: not kernel_settings_reboot_required | d(false)

        - name: Ensure role reported changed
          assert:
            that: __kernel_settings_changed | d(false)

        - name: Check sysctl
          shell: |-
            set -euo pipefail
            sysctl -n fs.file-max | grep -qx 400001
          changed_when: false

        - name: Check sysfs after role runs
          command: grep -x 60666 /sys/class/net/lo/mtu
          changed_when: false

        - name: Apply kernel_settings for removing section
          include_role:
            name: linux-system-roles.kernel_settings
          vars:
            kernel_settings_reboot_ok: true
            kernel_settings_sysctl:
              state: empty
            kernel_settings_sysfs:
              state: empty
            kernel_settings_transparent_hugepages: never

        - name: Force handlers
          meta: flush_handlers

        - name: Ensure kernel_settings_reboot_required is not set or is false
          assert:
            that: not kernel_settings_reboot_required | d(false)

        - name: Ensure role reported changed
          assert:
            that: __kernel_settings_changed | d(false)

        - name: Check sysctl
          shell: |-
            set -euo pipefail
            sysctl -n fs.file-max | grep -Lvxq 400001
          changed_when: false

        - name: Check sysfs after role runs
          command: grep -Lxqv 60666 /sys/class/net/lo/mtu
          changed_when: false

      always:
        - name: Cleanup
          tags:
            - tests::cleanup
          include_tasks: tasks/cleanup.yml
