# SPDX-License-Identifier: BSD-3-Clause
---
- name: Play for testing ipv6 disabled
  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: Initialize the connection_failed flag
      set_fact:
        connection_failed: false

    - name: Try configuration
      block:
        - name: Include network role
          include_role:
            name: linux-system-roles.network
          vars:
            network_connections:
              - name: "{{ interface }}"
                interface_name: "{{ interface }}"
                type: ethernet
                ip:
                  ipv6_disabled: true
            __header: "# Ansible managed test header"

      rescue:
        - name: Assert that configuring `ipv6_disabled` will only fail when the
                running version of NetworkManager does not support it
          assert:
            that: ansible_failed_result.stderr is search(errmsg)
            msg: Reconfiguring network connection is not failed with the error {{ errmsg }}
          vars:
            errmsg: ip.ipv6_disabled is not supported by the running version of NetworkManager

    - name: Verify nmcli connection ipv6.method
      shell: |
        set -euxo pipefail
        nmcli connection show {{ interface }} | grep ipv6.method
      register: ipv6_method
      ignore_errors: true
      changed_when: false
      when: not __network_connections_result.failed

    - name: "Assert that ipv6.method disabled is configured correctly"
      assert:
        that:
          - "'disabled' in ipv6_method.stdout"
        msg: "ipv6.method disabled is configured incorrectly"
      when: not __network_connections_result.failed

    - name: Set the connection_failed flag
      set_fact:
        connection_failed: true
      when: __network_connections_result.failed

    - name: Include the task 'down_profile.yml'
      include_tasks: tasks/down_profile.yml
      vars:
        profile: "{{ interface }}"
      when: not connection_failed

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

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

    - 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
