# SPDX-License-Identifier: BSD-3-Clause
---
- name: Play for testing wireless connection
  hosts: all
  vars:
    interface: wlan0
  tasks:
    - name: "INIT: wireless tests"
      debug:
        msg: "##################################################"
    - name: Include the task 'setup_mock_wifi.yml'
      include_tasks: tasks/setup_mock_wifi.yml
    - name: Copy client certs
      copy:
        src: "{{ item }}"
        dest: "/etc/pki/tls/{{ item }}"
        mode: "0644"
      with_items:
        - client.key
        - client.pem
        - cacert.pem
    - name: Test wireless connection with WPA-PSK
      block:
        - name: "TEST: wireless connection with WPA-PSK"
          debug:
            msg: "##################################################"
        - name: Import network role
          import_role:
            name: linux-system-roles.network
          vars:
            network_allow_restart: true
            network_connections:
              - name: "{{ interface }}"
                state: up
                type: wireless
                ip:
                  address:
                    - 203.0.113.2/24
                  dhcp4: "no"
                  auto6: "no"
                wireless:
                  ssid: "mock_wifi"
                  key_mgmt: "wpa-psk"
                  password: "p@55w0rD"
        - name: Import network role
          import_role:
            name: linux-system-roles.network
          vars:
            network_connections:
              - name: "{{ interface }}"
                persistent_state: absent
                state: down
        - name: "TEST: wireless connection with 802.1x TLS-EAP"
          debug:
            msg: "##################################################"
        - name: Import network role
          import_role:
            name: linux-system-roles.network
          vars:
            network_allow_restart: true
            network_connections:
              - name: "{{ interface }}"
                state: up
                type: wireless
                ip:
                  address:
                    - 203.0.113.2/24
                  dhcp4: "no"
                  auto6: "no"
                wireless:
                  ssid: "mock_wifi"
                  key_mgmt: "wpa-eap"
                ieee802_1x:
                  identity: myhost
                  eap: tls
                  private_key: /etc/pki/tls/client.key
                  private_key_password: test
                  private_key_password_flags:
                    - none
                  client_cert: /etc/pki/tls/client.pem
                  ca_cert: /etc/pki/tls/cacert.pem
      always:
        - name: Clean up the test device and the connection profile
          tags:
            - "tests::cleanup"
          block:
            - name: Import network role
              import_role:
                name: linux-system-roles.network
              vars:
                network_connections:
                  - name: "{{ interface }}"
                    persistent_state: absent
                    state: down
              failed_when: false
            - name: Include the task 'cleanup_mock_wifi.yml'
              include_tasks: tasks/cleanup_mock_wifi.yml
            - name: Verify network state restored to default
              include_tasks: tasks/check_network_dns.yml
