# SPDX-License-Identifier: BSD-3-Clause
---
- name: Play for testing dns support
  hosts: all
  vars:
    type: veth
    interface: ethtest0
  tasks:
    - name: Include the task 'show_interfaces.yml'
      include_tasks: tasks/show_interfaces.yml

    - name: Include the task 'manage_test_interface.yml'
      include_tasks: tasks/manage_test_interface.yml
      vars:
        state: present

    - name: Include the task 'assert_device_present.yml'
      include_tasks: tasks/assert_device_present.yml

    - name: Include network role
      include_role:
        name: linux-system-roles.network
      vars:
        network_connections:
          - name: "{{ interface }}"
            interface_name: "{{ interface }}"
            state: up
            type: ethernet
            autoconnect: true
            ip:
              route_metric4: 100
              dhcp4: false
              gateway4: 192.0.2.1
              dns_priority: 9999
              dns:
                - 192.0.2.2
                - 198.51.100.5
                - 2001:db8::20
              dns_search:
                - example.com
                - example.org
              dns_options:
                - no-aaaa
                - rotate
                - timeout:1

              route_metric6: -1
              auto6: false
              gateway6: 2001:db8::1

              address:
                - 192.0.2.3/24
                - 198.51.100.3/26
                - 2001:db8::80/7

              route:
                - network: 198.51.100.128
                  prefix: 26
                  gateway: 198.51.100.1
                  metric: 2
                - network: 198.51.100.64
                  prefix: 26
                  gateway: 198.51.100.6
                  metric: 4
              route_append_only: false
              rule_append_only: true

    - name: Verify nmcli connection DNS entry for IPv4
      shell: |
        set -euxo pipefail
        nmcli connection show {{ interface }} | grep ipv4.dns
      register: ipv4_dns
      ignore_errors: true
      changed_when: false

    - name: Verify nmcli connection DNS entry for IPv6
      shell: |
        set -euxo pipefail
        nmcli connection show {{ interface }} | grep ipv6.dns
      register: ipv6_dns
      ignore_errors: true
      changed_when: false

    - name: "Assert that DNS addresses are configured correctly"
      assert:
        that:
          - "'192.0.2.2' in ipv4_dns.stdout"
          - "'198.51.100.5' in ipv4_dns.stdout"
          - "'2001:db8::20' in ipv6_dns.stdout"
        msg: "DNS addresses are configured incorrectly"

    - name: "Assert that DNS search domains are configured correctly"
      assert:
        that:
          - "'example.com' in ipv4_dns.stdout"
          - "'example.org' in ipv4_dns.stdout"
          - "'example.com' in ipv6_dns.stdout"
          - "'example.org' in ipv6_dns.stdout"
        msg: "DNS search domains are configured incorrectly"

    - name: "Assert that DNS options are configured correctly"
      assert:
        that:
          - "'no-aaaa' in ipv4_dns.stdout"
          - "'rotate' in ipv4_dns.stdout"
          - "'timeout:1' in ipv4_dns.stdout"
          - "'no-aaaa' in ipv6_dns.stdout"
          - "'rotate' in ipv6_dns.stdout"
          - "'timeout:1' in ipv6_dns.stdout"
        msg: "DNS options are configured incorrectly"

    - name: "Assert that DNS priority is configured correctly"
      assert:
        that:
          - "'9999' in ipv4_dns.stdout"
          - "'9999' in ipv6_dns.stdout"
        msg: "DNS priority is configured incorrectly"

    - name: Include the tasks 'down_profile+delete_interface.yml'
      include_tasks: tasks/down_profile+delete_interface.yml
      vars:
        profile: "{{ interface }}"

    # FIXME: assert profile/device down
    - name: Include the task 'remove_profile.yml'
      include_tasks: tasks/remove_profile.yml
      vars:
        profile: "{{ interface }}"

    - name: Include the task 'assert_profile_absent.yml'
      include_tasks: tasks/assert_profile_absent.yml
      vars:
        profile: "{{ interface }}"

    - name: Include the task 'assert_device_absent.yml'
      include_tasks: tasks/assert_device_absent.yml

    - name: Verify network state restored to default
      include_tasks: tasks/check_network_dns.yml
