# SPDX-License-Identifier: BSD-3-Clause
---
- name: Play for testing route table
  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: Configure connection profile and specify the numeric table in
        static routes
      include_role:
        name: linux-system-roles.network
      vars:
        network_connections:
          - name: "{{ interface }}"
            interface_name: "{{ interface }}"
            state: up
            type: ethernet
            autoconnect: true
            ip:
              dhcp4: false
              address:
                - 198.51.100.3/26
              route:
                - network: 198.51.100.128
                  prefix: 26
                  gateway: 198.51.100.1
                  metric: 2
                  table: 30400
                - network: 198.51.100.64
                  prefix: 26
                  gateway: 198.51.100.6
                  metric: 4
                  table: 30200
                - network: 192.0.2.64
                  prefix: 26
                  gateway: 198.51.100.8
                  metric: 50
                  table: 30200
                  src: 198.51.100.3

    - name: Get the routes from the route table 30200
      command: ip route show table 30200
      register: route_table_30200
      ignore_errors: true
      changed_when: false

    - name: Get the routes from the route table 30400
      command: ip route show table 30400
      register: route_table_30400
      ignore_errors: true
      changed_when: false

    - name: Assert that the route table 30200 contains the specified route
      assert:
        that:
          - route_table_30200.stdout is search("198.51.100.64/26 via
            198.51.100.6 dev ethtest0 proto static metric 4")
          - route_table_30200.stdout is search("192.0.2.64/26 via
            198.51.100.8 dev ethtest0 proto static src 198.51.100.3
            metric 50")
        msg: "the route table 30200 does not exist or does not contain the
          specified route"

    - name: Assert that the route table 30400 contains the specified route
      assert:
        that:
          - route_table_30400.stdout is search("198.51.100.128/26 via
            198.51.100.1 dev ethtest0 proto static metric 2")
        msg: "the route table 30400 does not exist or does not contain the
          specified route"

    - name: Create a dedicated test file in `/etc/iproute2/rt_tables.d/` and
            add a new routing table
      lineinfile:
        path: /etc/iproute2/rt_tables.d/table.conf
        line: "200 custom"
        mode: "0644"
        create: true

    - name: Reconfigure connection profile and specify the named table in
        static routes
      include_role:
        name: linux-system-roles.network
      vars:
        network_connections:
          - name: "{{ interface }}"
            interface_name: "{{ interface }}"
            state: up
            type: ethernet
            autoconnect: true
            ip:
              dhcp4: false
              address:
                - 198.51.100.3/26
              route:
                - network: 198.51.100.128
                  prefix: 26
                  gateway: 198.51.100.1
                  metric: 2
                  table: custom
                - network: 198.51.100.64
                  prefix: 26
                  gateway: 198.51.100.6
                  metric: 4
                  table: custom
                - network: 192.0.2.64
                  prefix: 26
                  gateway: 198.51.100.8
                  metric: 50
                  table: custom
                  src: 198.51.100.3

    - name: Get the routes from the named route table 'custom'
      command: ip route show table custom
      register: route_table_custom
      ignore_errors: true
      changed_when: false

    - name: Assert that the named route table 'custom' contains the
        specified route
      assert:
        that:
          - route_table_custom.stdout is search("198.51.100.128/26 via
            198.51.100.1 dev ethtest0 proto static metric 2")
          - route_table_custom.stdout is search("198.51.100.64/26 via
            198.51.100.6 dev ethtest0 proto static metric 4")
          - route_table_custom.stdout is search("192.0.2.64/26 via
            198.51.100.8 dev ethtest0 proto static src 198.51.100.3
            metric 50")
        msg: "the named route table 'custom' does not exist or does not contain
          the specified route"

    - name: Remove the dedicated test file in `/etc/iproute2/rt_tables.d/`
      file:
        state: absent
        path: /etc/iproute2/rt_tables.d/table.conf

    - 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
