Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Windows] Fix ping result is true when host unreachable issue #302

Merged
merged 2 commits into from May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 21 additions & 24 deletions windows/network_device_ops/network_adapter_deviceops.yml
Expand Up @@ -6,9 +6,6 @@
#
# Get network adapter number before hotadd
- include_tasks: network_adapter_number_check.yml
- debug:
msg: "The number of '{{ nic_type }}' got from VM/guest before hotadd: {{ vm_nic_num_get }}/{{ guest_nic_num_get }}"

- name: Set fact of the NIC info before hotadd
set_fact:
nic_num_before_hotadd: "{{ vm_nic_num_get }}"
Expand All @@ -22,18 +19,15 @@
- name: Verify new network adapter hotadd operation succeed
assert:
that:
- "{{ not add_adapter.failed }}"
- "{{ 'changed' in add_adapter }}"
- "{{ add_adapter.changed }}"
fail_msg: "Hotadd new network adapter task result is not changed."
- name: Wait 10 seconds after network adapter hotadd
pause:
seconds: 10

# Get network adapter number after hotadd
- include_tasks: network_adapter_number_check.yml
- debug:
msg: "The number of '{{ nic_type }}' got from VM/guest after hotadd: {{ vm_nic_num_get }}/{{ guest_nic_num_get }}"

- name: Set fact of the NIC info after hotadd
set_fact:
nic_num_after_hotadd: "{{ vm_nic_num_get }}"
Expand All @@ -42,18 +36,7 @@

- name: Get MAC address of new added network adapter
set_fact:
new_nic_mac_address: "{{ nic_mac_addr_list_after_hotadd | difference(nic_mac_addr_list_before_hotadd) }}"
- name: Set fact of MAC address of new added network adapter
set_fact:
new_nic_mac_address: "{{ new_nic_mac_address[0] if new_nic_mac_address | length == 1 else '' }}"
- debug:
msg: "The new added '{{ nic_type }}' MAC address: {{ new_nic_mac_address }}"
- name: Check MAC address of new added NIC
assert:
that:
- new_nic_mac_address | ansible.netcommon.hwaddr
fail_msg: "MAC address got is not valid."
success_msg: "Got valid MAC address."
new_nic_mac_address: "{{ (nic_mac_addr_list_after_hotadd | difference(nic_mac_addr_list_before_hotadd))[0] | default('') }}"

# Check disconnect and reconnect of newwork adapter
- include_tasks: network_adapter_status_check.yml
Expand All @@ -65,26 +48,40 @@
- name: Check remove network adapter to VM succeed
assert:
that:
- "{{ not remove_result.failed }}"
- "{{ 'changed' in remove_result }}"
- "{{ remove_result.changed }}"
fail_msg: "Hot remove network adapter task result is not changed."
- name: Wait 10 seconds after network adapter hot remove
pause:
seconds: 10

# Get the number of VM network adapter after hot remove
- include_tasks: network_adapter_number_check.yml
- debug:
msg: "The number of '{{ nic_type }}' got from VM/guest after hot remove: {{ vm_nic_num_get }}/{{ guest_nic_num_get }}"

- name: Set fact of the NIC info after hot remove
set_fact:
nic_num_after_hotremove: "{{ vm_nic_num_get }}"
nic_num_after_hotremove_guest: "{{ guest_nic_num_get }}"

- name: "Display test results of '{{ nic_type }}' in check conditions"
debug:
msg:
- "New network adapter MAC address: {{ new_nic_mac_address }}"
- "Network adapter number on VM before hotadd: {{ nic_num_before_hotadd }}"
- "Network adapter number on VM after hotadd: {{ nic_num_after_hotadd }}"
- "Network adapter number in guest before hotadd: {{ nic_num_before_hotadd_guest }}"
- "Network adapter number in guest after hotadd: {{ nic_num_after_hotadd_guest }}"
- "Network adapter number on VM after hot remove: {{ nic_num_after_hotremove }}"
- "Network adapter number in guest after hot remove: {{ nic_num_after_hotremove_guest }}"
- "Ping vlan gateway result after hotadd: {{ ping_result_after_hotadd }}"
- "Ping vlan gateway result after disable in guest: {{ ping_result_after_disable }}"
- "Ping vlan gateway result after re-enable in guest: {{ ping_result_after_enable }}"
- "Ping vlan gateway result after disconnect from VM: {{ ping_result_after_disconnect }}"
- "Ping vlan gateway result after re-connect from VM: {{ ping_result_after_reconnect }}"

- name: Verify network adapter numbers and connect status to VLAN gateway
assert:
that:
- new_nic_mac_address | ansible.netcommon.hwaddr
- "{{ nic_num_before_hotadd | int == nic_num_before_hotadd_guest | int }}"
- "{{ nic_num_after_hotadd | int == nic_num_after_hotadd_guest | int }}"
- "{{ nic_num_before_hotadd | int == nic_num_after_hotadd | int - 1 }}"
Expand All @@ -96,4 +93,4 @@
- "{{ ping_result_after_enable }}"
- "{{ not ping_result_after_disconnect }}"
- "{{ ping_result_after_reconnect }}"
fail_msg: "One or more check conditions failed, please check the network adapter numbers before/after hotadd or before/after hot remve, and ping vlan results."
fail_msg: "One or more check conditions failed, please check above displayed results."
6 changes: 4 additions & 2 deletions windows/network_device_ops/network_adapter_status_check.yml
Expand Up @@ -61,8 +61,9 @@
- name: Check disconnect network adapter to VM succeed
assert:
that:
- "{{ not reconfig_result.failed }}"
- "{{ 'changed' in reconfig_result }}"
- "{{ reconfig_result.changed }}"
fail_msg: "Disconnect VM network adapter task result is not changed."
- include_tasks: ../utils/win_ping_target.yml
vars:
win_ping_target_ip: "{{ vlan_gateway }}"
Expand All @@ -79,8 +80,9 @@
- name: Check reconnect network adapter to VM succeed
assert:
that:
- "{{ not reconfig_result.failed }}"
- "{{ 'changed' in reconfig_result }}"
- "{{ reconfig_result.changed }}"
fail_msg: "Re-connect VM network adapter task result is not changed."
- include_tasks: ../utils/win_ping_target.yml
vars:
win_ping_target_ip: "{{ vlan_gateway }}"
Expand Down
24 changes: 19 additions & 5 deletions windows/utils/win_ping_target.yml
Expand Up @@ -8,18 +8,20 @@
# Return:
# win_ping_target_result
#
- name: Check ping target IP address is defined
- name: "Check ping target IP address is specified"
fail:
msg: "Please specify ping target IP address"
when: win_ping_target_ip is undefined or not win_ping_target_ip
- debug:
msg: "Will ping this target IP address: {{ win_ping_target_ip }}"

- name: "Initialize the ping target result"
set_fact:
win_ping_target_result: False
- name: "Set fact of the ping command"
set_fact:
win_ping_command: "ping -n 3 {{ win_ping_target_ip }}"
when: win_ping_src_ip is undefined or not win_ping_src_ip

- name: "Set fact of the ping command"
set_fact:
win_ping_command: "ping -n 3 -S {{ win_ping_src_ip }} {{ win_ping_target_ip }}"
Expand All @@ -33,8 +35,20 @@
win_powershell_cmd: "{{ win_ping_command }}"
win_execute_cmd_ignore_error: True

- name: Set fact of the ping result
set_fact:
win_ping_target_result: "{{ True if (win_powershell_cmd_output.rc is defined and win_powershell_cmd_output.rc == 0) else False }}"
- block:
- name: "Set ping result to True"
set_fact:
win_ping_target_result: True
when: win_powershell_cmd_output.rc == 0
- name: "Set ping result to False"
set_fact:
win_ping_target_result: False
when: >
(win_powershell_cmd_output.rc != 0) or
(win_powershell_cmd_output.stdout | regex_findall('unreachable') | length == 3)
when:
- win_powershell_cmd_output is defined
- win_powershell_cmd_output.rc is defined
- win_powershell_cmd_output.stdout is defined
- debug:
msg: "Ping target IP address result: {{ win_ping_target_result }}"