Add fixtures and unit tests for auto suspend functionality

- Introduced fixture files for AMD and NVIDIA GPU monitoring outputs, CPU usage statistics, and user sessions.
- Created a comprehensive unit test suite for the auto_suspend module, covering state management, CPU and GPU measurement, user activity detection, and evaluation logic.
- Updated Ansible tasks to install Python instead of bc, create necessary directories and log files, and ensure proper SELinux context for the auto suspend script.
- Modified the systemd service and timer configurations to improve the auto suspend functionality, including command-line arguments for the Python script.
This commit is contained in:
2026-06-08 22:09:21 +02:00
parent 25d3eafa5b
commit 98b0f215aa
9 changed files with 1123 additions and 86 deletions
+70 -25
View File
@@ -6,7 +6,7 @@
state: present
loop:
- sysstat
- bc
- python3
- name: Enable sysstat service
become: true
@@ -25,18 +25,13 @@
ansible.builtin.set_fact:
existing_cmdline: "{{ grub_line.stdout }}"
- name: Set fedora grub file locations
ansible.builtin.set_fact:
grub_config_path_bios: /boot/grub2/grub.cfg
when: ansible_distribution == "Fedora" and ansible_os_family == "RedHat"
- name: Show current GRUB_CMDLINE_LINUX
ansible.builtin.debug:
msg: "Current GRUB_CMDLINE_LINUX: {{ existing_cmdline }}"
- name: Updated GRUB_CMDLINE_LINUX
ansible.builtin.debug:
msg: "Current GRUB_CMDLINE_LINUX: {{ existing_cmdline | regex_replace(' *mem_sleep_default=\\S+', '') }} mem_sleep_default=deep"
msg: "Updated GRUB_CMDLINE_LINUX: {{ existing_cmdline | regex_replace(' *mem_sleep_default=\\S+', '') }} mem_sleep_default=deep"
- name: Ensure mem_sleep_default=deep is set in GRUB_CMDLINE_LINUX
become: true
@@ -45,24 +40,57 @@
GRUB_CMDLINE_LINUX="{{ existing_cmdline | regex_replace(' *mem_sleep_default=\\S+', '') }} mem_sleep_default=deep"
ansible.builtin.lineinfile:
path: /etc/default/grub
regexp: '^GRUB_CMDLINE_LINUX='
regexp: "^GRUB_CMDLINE_LINUX="
line: "{{ grub_cmdline_linux_line }}"
- name: Generate grub config for BIOS
- name: Generate GRUB config
become: true
changed_when: false
ansible.builtin.command: grub2-mkconfig -o {{ grub_config_path_bios }}
ansible.builtin.command: grub2-mkconfig -o {{ auto_suspend_grub_config_path_bios }}
- name: Create idle check and suspend script
- name: Create state directory
become: true
ansible.builtin.copy:
dest: /usr/local/bin/auto_suspend_script.sh
mode: '0755'
ansible.builtin.file:
path: "{{ auto_suspend_state_dir }}"
state: directory
owner: root
group: root
content: "{{ lookup('file', 'scripts/auto_suspend.sh') }}"
mode: "0755"
- name: Create systemd service for auto suspend
- name: Create log file
become: true
ansible.builtin.file:
path: "{{ auto_suspend_log_file }}"
state: touch
owner: root
group: root
mode: "0644"
- name: Create auto-suspend Python script
become: true
ansible.builtin.copy:
src: auto_suspend.py
dest: /usr/local/bin/auto_suspend.py
mode: "0755"
owner: root
group: root
- name: Set SELinux file context on script
become: true
community.general.sefcontext:
path: /usr/local/bin/auto_suspend.py
setype: bin_t
state: present
ignore_errors: true
- name: Restore SELinux context on script
become: true
ansible.builtin.command: restorecon -v /usr/local/bin/auto_suspend.py
changed_when: true
failed_when: false
ignore_errors: true
- name: Create systemd service for auto-suspend
become: true
ansible.builtin.copy:
dest: /etc/systemd/system/auto-suspend.service
@@ -73,10 +101,22 @@
[Service]
Type=oneshot
ExecStart=/usr/local/bin/auto_suspend_script.sh
ExecStart=/usr/local/bin/auto_suspend.py \
--alpha {{ auto_suspend_alpha }} \
--min-samples {{ auto_suspend_min_samples }} \
--idle-timeout-cycles {{ auto_suspend_idle_timeout_cycles }} \
--cpu-idle-threshold {{ auto_suspend_cpu_idle_threshold }} \
--gpu-util-threshold {{ auto_suspend_gpu_util_threshold }} \
--state-dir {{ auto_suspend_state_dir }} \
--log-file {{ auto_suspend_log_file }}
Restart=on-failure
RestartSec=30
[Install]
WantedBy=multi-user.target
owner: root
group: root
mode: '0644'
mode: "0644"
- name: Create sleep.conf.d folder if it doesn't exist
become: true
@@ -85,7 +125,7 @@
state: directory
owner: root
group: root
mode: '0755'
mode: "0755"
- name: Create sleep.conf.d for deep sleep
become: true
@@ -96,26 +136,26 @@
MemorySleepMode=deep
owner: root
group: root
mode: '0644'
mode: "0644"
- name: Create systemd timer for auto suspend
- name: Create systemd timer for auto-suspend
become: true
ansible.builtin.copy:
dest: /etc/systemd/system/auto-suspend.timer
content: |
[Unit]
Description=Run idle suspend check every 10 minutes
Description=Run idle suspend check every 5 minutes
[Timer]
OnBootSec=10min
OnUnitActiveSec=10min
OnBootSec=5min
OnUnitActiveSec=5min
Unit=auto-suspend.service
[Install]
WantedBy=timers.target
owner: root
group: root
mode: '0644'
mode: "0644"
- name: Reload systemd daemon
become: true
@@ -128,3 +168,8 @@
name: auto-suspend.timer
enabled: true
state: started
- name: Verify script is executable
become: true
ansible.builtin.command: /usr/local/bin/auto_suspend.py --dry-run
changed_when: false