---
- name: Test tangd_custom_port
  hosts: all
  vars:
    nbde_server_port: 7500
    nbde_server_firewall_zone: public
    nbde_server_manage_firewall: true
    nbde_server_manage_selinux: true
  tasks:
    - name: Run tests
      block:
        - name: Install with custom port and firewall zone
          include_role:
            name: linux-system-roles.nbde_server
            public: true

        - name: Ensure iproute package for ss command
          package:
            name: iproute
            state: present
            use: "{{ (__nbde_server_is_ostree | d(false)) |
                    ternary('ansible.posix.rhel_rpm_ostree', omit) }}"

        - name: Check if port is open
          shell:
            cmd: |-
              set -euo pipefail
              ss -tulpn | grep ':{{ nbde_server_port }} ' | \
                awk -F' ' '{print $5}'
          register: __open_ports_output
          failed_when: not __open_ports_output.stdout is
            search(':' ~ (nbde_server_port | string) ~ '$')
          changed_when: false

        - name: Check if port TCP is open
          shell:
            cmd: |-
              set -euo pipefail
              ss -tulpn | grep ':{{ nbde_server_port }} ' | \
                awk -F' ' '{print $1}'
          register: __open_ports_output
          failed_when: __open_ports_output.stdout != "tcp"
          changed_when: false

        - name: Check if port is opened in firewall
          command: >-
            firewall-cmd --zone {{ nbde_server_firewall_zone }} --query-port
            {{ nbde_server_port }}/tcp
          register: __firewall_output
          changed_when: false

        - name: Check for ansible_managed, fingerprint in generated files
          include_tasks: tasks/check_header.yml
          vars:
            __file: "{{ __nbde_server_tangd_socket_file_path }}"
            __fingerprint: "system_role:nbde_server"

        - name: Install with default port and firewall zone
          include_role:
            name: linux-system-roles.nbde_server
            public: true
          vars:
            nbde_server_port: 80
            nbde_server_firewall_zone: public
            nbde_server_manage_firewall: true
            nbde_server_manage_selinux: true

        - name: Check if port is open
          shell:
            cmd: |-
              set -euo pipefail
              ss -tulpn | grep ':80 ' | awk -F' ' '{print $5}'
          register: __open_ports_output
          failed_when: not __open_ports_output.stdout is
            search(':80$')
          changed_when: false

        - name: Check if port TCP is open
          shell:
            cmd: |-
              set -euo pipefail
              ss -tulpn | grep ':80 ' | awk -F' ' '{print $1}'
          register: __open_ports_output
          failed_when: __open_ports_output.stdout != "tcp"
          changed_when: false

        - name: Check if port is opened in firewall
          command: >-
            firewall-cmd --zone {{ nbde_server_firewall_zone }} --query-port
            80/tcp
          register: __firewall_output
          changed_when: false
      always:
        - name: Cleanup
          tags: tests::cleanup
          include_tasks: tasks/cleanup.yml

# vim:set ts=2 sw=2 et:
