---
- name: Configure time synchronization with single PTP domain
  hosts: all
  gather_facts: true
  vars:
    timesync_ptp_domains:
      - number: 3
        interfaces: "{{ [ ansible_default_ipv4['interface'] ] }}"
  roles:
    - fedora.linux_system_roles.timesync

  tasks:
    - name: Run test
      tags: tests::verify
      block:
        - name: Flush handlers
          meta: flush_handlers

        - name: Common test setup tasks
          include_tasks: tasks/setup.yml

        - name: Ensure ethtool is installed
          package:
            name: ethtool
            state: present
            use: "{{ (__timesync_is_ostree | d(false)) | ternary('ansible.posix.rhel_rpm_ostree', omit) }}"

        - name: Check if SW/HW timestamping is supported
          command: ethtool -T "{{ ansible_default_ipv4['interface'] }}"
          register: ethtool
          ignore_errors: true # noqa ignore-errors
          changed_when: false

        - name: Run test
          when: "'ware-transmit' in ethtool.stdout"
          block:
            - name: Wait for services to start
              wait_for:
                timeout: 2

            - name: Run pmc
              command: pmc -u -b 0 -d 3 'GET DOMAIN' 'GET PORT_DATA_SET'
              register: pmc
              changed_when: false

            - name: Check PTP domain
              assert:
                that:
                  - "'domainNumber 3' in pmc.stdout"

            - name: Check PTP port state
              assert:
                that:
                  - pmc.stdout is search("portState\s+LISTENING")
              when:
                - ansible_distribution not in ['RedHat', 'CentOS'] or ansible_distribution_version is not version('8.3', '<')
