# SPDX-License-Identifier: MIT
---
- name: Simple journald role invocation
  hosts: all
  vars:
    journald_persistent: true
    journald_max_disk_size: 2048
    journald_per_user: true
    journald_sync_interval: 1
  roles:
    - redhat.rhel_system_roles.journald

  tasks:
  # Make sure services are restarted before we verify system state is correct
    - name: Flush handlers
      meta: flush_handlers

    - name: Get systemd-journald service state
      command:
      # noqa command-instead-of-module
        cmd: systemctl show --property SubState systemd-journald.service
      register: systemctl
      changed_when: false

    - name: Verify that systemd-journald is running
      assert:
        that: systemctl.stdout == 'SubState=running'

    - name: Verify that /var/log/journal/$MACHINE_ID/system.journal exists
      stat:
        path: /var/log/journal/{{ ansible_machine_id }}/system.journal
      register: __stat
      failed_when: not __stat.stat.exists

    - name: Verify that system.journal is in online state
      shell:
        cmd: >
          journalctl --header
          --file /var/log/journal/{{ ansible_machine_id }}/system.journal |
          grep -q 'State: ONLINE'
      changed_when: false

    - name: Verify that system.journal is compressed
      shell:
        cmd: >
          journalctl --header
          --file /var/log/journal/{{ ansible_machine_id }}/system.journal |
          grep -q COMPRESSED
      changed_when: false

    - name: Verify that system journal can have maximum size of 2.0G
      shell:
        cmd: >
          journalctl -b -u systemd-journald -p info | grep max | tail -n 1 |
          grep -q -E 'max( allowed)? 2.0G'
      changed_when: false
