在测试link_flap测试用例的时候出错了,所以了解一下link_flap是在测试什么。

目前的理解就是轮着down掉Leaf交换机的端口,然后确认DUT交换机的端口是不是也是down的状态。

首先查看ansible/roles/test/tasks/link_flap.yml

# 通过调用ansible/library/conn_graph_facts.py获取交换机的信息
# 主要靠解析文件files/lab_connection_graph.xml
- name: Gathering lab graph facts about the device
  conn_graph_facts: host={{ inventory_hostname }}
  connection: local
  tags: always

# 上面会得到device_conn信息
- set_fact:
    neighbors: "{{device_conn}}"

- include: link_flap/link_flap_helper.yml
  with_items: "{{ device_conn.keys() }}"

接着查看ansible/roles/test/tasks/link_flap/link_flap_helper.yml

- block:
    # 获取端口
    - set_fact:
        interface: "{{item}}"

    - debug: msg={{interface}}

    # 获取当前端口连接的设备以及端口
    - set_fact:
        peer_device: "{{neighbors[interface]['peerdevice']}}"
        neighbor_interface: "{{neighbors[interface]['peerport']}}"

    # 类似上面获取对端交换机的信息
    - conn_graph_facts: host={{ peer_device }}
      connection: local

    # 设置一些需要的信息
    - set_fact:
        peer_host: "{{device_info['mgmtip']}}"
        peer_hwsku: "{{device_info['HwSku']}}"
        peer_type: "{{device_info['Type']}}"

    - set_fact:
        intfs_to_exclude: "{{interface}}"

    # 如果对端是leaf的话,down掉指定的端口
    # 这块容易出问题,因为交换机支持不全,命令也不一定匹配所有交换机
    - name: Shut down neighbor interface {{neighbor_interface}} on {{peer_host}}
      action: apswitch template=neighbor_interface_shut_single.j2
      args:
        host: "{{peer_host}}"
        login: "{{switch_login[hwsku_map[peer_hwsku]]}}"
      connection: switch
      when: peer_type == "FanoutLeaf"

    - name: find interface name mapping
      port_alias: hwsku="{{peer_hwsku}}"
      delegate_to: "{{peer_host}}"
      when: peer_type == "FanoutLeafSonic"

    - name: Shutting down neighbor interface {{neighbor_interface}} on {{peer_host}}
      become: true
      shell: ip link set {{port_alias_map[neighbor_interface]}} down
      delegate_to: "{{peer_host}}"
      when: peer_type == "FanoutLeafSonic"

    - pause:
        seconds: 20

    - interface_facts: up_ports={{minigraph_ports | difference(intfs_to_exclude)}}

    - debug: msg="Found link down ports {{ansible_interface_link_down_ports}} "
      when: ansible_interface_link_down_ports | length > 0

    # 确定所有其他的端口都是up
    - name: Verify interfaces are up correctly
      assert: { that: "ansible_interface_link_down_ports | length == 0" }

    # 确定刚才down掉的端口对应的端口也是down的
    - name: Verify {{intfs_to_exclude}} is down correctly
      assert: { that: "ansible_interface_facts[intfs_to_exclude]['active'] == False" }

  always:
    # up之前down掉的端口
    - name: Bring up neighbor interface {{neighbor_interface}} on {{peer_host}}
      action: apswitch template=neighbor_interface_no_shut_single.j2
      args:
        host: "{{peer_host}}"
        login: "{{switch_login[hwsku_map[peer_hwsku]]}}"
      connection: switch
      when: peer_type == "FanoutLeaf"

    - name: Bring up neighbor interface {{neighbor_interface}} on {{peer_host}}
      become: true
      shell: ip link set {{port_alias_map[neighbor_interface]}} up
      delegate_to: "{{peer_host}}"
      when: peer_type == "FanoutLeafSonic"

    - pause:
        seconds: 20

    - interface_facts: up_ports={{minigraph_ports}}

    - debug: msg="Found link down ports {{ansible_interface_link_down_ports}} "
      when: ansible_interface_link_down_ports | length > 0

    - name: Verify all interfaces are up
      assert: { that: "ansible_interface_link_down_ports | length == 0" }
最后修改:2021 年 08 月 18 日 08 : 56 PM