---
- name: Test changing settings
  hosts: all
  tags:
    - tests::reboot
  tasks:
  # procps-ng for sysctl cmd
    - name: Ensure required packages are installed
      package:
        name: [tuned, procps-ng]
        state: present

    - name: Ensure kernel settings profile directory exists
      file:
        path: /etc/tuned/kernel_settings
        state: directory
        mode: 0755

    - name: Generate a configuration for kernel settings
      copy:
        src: tuned/etc/tuned/change_settings/tuned.conf
        dest: /etc/tuned/kernel_settings/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: redhat.rhel_system_roles.kernel_settings
      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: 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: Show cmdline
      command: cat /proc/cmdline
      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: redhat.rhel_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: 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: redhat.rhel_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: 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: redhat.rhel_system_roles.kernel_settings
      vars:
        kernel_settings_reboot_ok: true
        kernel_settings_sysctl:
          state: empty
        kernel_settings_sysfs:
          state: empty

    - 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: 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
