# SPDX-License-Identifier: MIT
---
- name: Configure node attributes
  hosts: all
  vars_files: vars/main.yml

  tasks:
    - name: Run test
      tags: tests::verify
      block:
        - name: Set up test environment
          include_role:
            name: linux-system-roles.ha_cluster
            tasks_from: test_setup.yml

        - name: Find first node name
          set_fact:
            __test_first_node: "{{
                (ansible_play_hosts_all | length == 1)
                | ternary('localhost', ansible_play_hosts[0]) }}"

        - name: Run HA Cluster role
          include_role:
            name: linux-system-roles.ha_cluster
            public: true
          vars:
            ha_cluster_cluster_name: test-cluster
            ha_cluster_manage_firewall: true
            ha_cluster_manage_selinux: true
            ha_cluster_node_options:
              - node_name: "{{ __test_first_node }}"
                attributes:
                  - attrs:
                      - name: attr1
                        value: val1
                      - name: attr2
                        value: val2

        - name: Fetch versions of cluster components
          include_tasks: tasks/fetch_versions.yml

        - name: Verify node attributes
          when:
            - '"node.attributes.output-formats" not in __test_pcs_capabilities'
          vars:
            __test_expected_lines:
              - "Node Attributes:"
              - " {{ __test_first_node }}: attr1=val1 attr2=val2"
          block:
            - name: Fetch node attributes configuration from the cluster
              command:
                cmd: pcs node attribute
              register: __test_pcs_node_attribute_config
              changed_when: false

            - name: Print real node attributes configuration
              debug:
                var: __test_pcs_node_attribute_config

            - name: Print expected node attributes configuration
              debug:
                var: __test_expected_lines | list

            - name: Check node attributes configuration
              assert:
                that:
                  - __test_pcs_node_attribute_config.stdout_lines
                    == __test_expected_lines | list

        - name: Verify node attributes
          when:
            - '"node.attributes.output-formats" in __test_pcs_capabilities'
          vars:
            # Only check the first node. The json output contains all nodes,
            # but the number of nodes depends on the test environment.
            __test_expected_lines: '
              {
                  "description": null,
                  "id": "1",
                  "instance_attributes": [
                      {
                          "id": "nodes-1",
                          "nvpairs": [
                              {
                                  "id": "nodes-1-attr1",
                                  "name": "attr1",
                                  "value": "val1"
                              },
                              {
                                  "id": "nodes-1-attr2",
                                  "name": "attr2",
                                  "value": "val2"
                              }
                          ],
                          "options": {},
                          "rule": null
                      }
                  ],
                  "score": null,
                  "type": null,
                  "uname": "{{ __test_first_node }}",
                  "utilization": []
              }'
          block:
            - name: Fetch node attributes configuration from the cluster
              command:
                cmd: pcs --output-format=json node attribute
              register: __test_pcs_node_attribute_config
              changed_when: false

            - name: Print real node attributes configuration
              debug:
                var: __test_pcs_node_attribute_config.stdout | from_json

            - name: Print expected node attributes configuration
              debug:
                var: __test_expected_lines | from_json

            # yamllint disable rule:line-length
            - name: Check node attributes configuration
              assert:
                that:
                  - (__test_pcs_node_attribute_config.stdout | from_json)["nodes"][0]
                    == __test_expected_lines | from_json
            # yamllint enable rule:line-length

        - name: Check firewall and selinux state
          include_tasks: tasks/check_firewall_selinux.yml
