New Module: vyos_ping (#50193)
* New module for vyos ping functionality Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com> * Add some new parameters Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com> * Need spaces Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com> * Fix pylint errors Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com> * Fix pylint errors Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com> * Fix docs Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com> * Fix docs Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com> * Fix docs Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com>
This commit is contained in:
parent
1e09e21ba1
commit
7845373f96
5 changed files with 379 additions and 0 deletions
246
lib/ansible/modules/network/vyos/vyos_ping.py
Normal file
246
lib/ansible/modules/network/vyos/vyos_ping.py
Normal file
|
@ -0,0 +1,246 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# (c) 2017, Ansible by Red Hat, inc
|
||||
#
|
||||
# This file is part of Ansible by Red Hat
|
||||
#
|
||||
# Ansible is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Ansible is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
__metaclass__ = type
|
||||
|
||||
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
||||
'status': ['preview'],
|
||||
'supported_by': 'community'}
|
||||
|
||||
DOCUMENTATION = """
|
||||
---
|
||||
module: vyos_ping
|
||||
short_description: Tests reachability using ping from VyOS network devices
|
||||
description:
|
||||
- Tests reachability using ping from a VyOS device to a remote destination.
|
||||
- Tested against VyOS 1.1.8 (helium)
|
||||
- For a general purpose network module, see the M(net_ping) module.
|
||||
- For Windows targets, use the M(win_ping) module instead.
|
||||
- For targets running Python, use the M(ping) module instead.
|
||||
author:
|
||||
- Nilashish Chakraborty (@nilashishc)
|
||||
version_added: '2.8'
|
||||
options:
|
||||
dest:
|
||||
description:
|
||||
- The IP Address or hostname (resolvable by the device) of the remote node.
|
||||
required: true
|
||||
count:
|
||||
description:
|
||||
- Number of packets to send to check reachability.
|
||||
type: int
|
||||
default: 5
|
||||
source:
|
||||
description:
|
||||
- The source interface or IP Address to use while sending the ping packet(s).
|
||||
ttl:
|
||||
description:
|
||||
- The time-to-live value for the ICMP packet(s).
|
||||
type: int
|
||||
size:
|
||||
description:
|
||||
- Determines the size (in bytes) of the ping packet(s).
|
||||
type: int
|
||||
interval:
|
||||
description:
|
||||
- Determines the interval (in seconds) between consecutive pings.
|
||||
type: int
|
||||
state:
|
||||
description:
|
||||
- Determines if the expected result is success or fail.
|
||||
choices: [ absent, present ]
|
||||
default: present
|
||||
notes:
|
||||
- For a general purpose network module, see the M(net_ping) module.
|
||||
- For Windows targets, use the M(win_ping) module instead.
|
||||
- For targets running Python, use the M(ping) module instead.
|
||||
extends_documentation_fragment: vyos
|
||||
"""
|
||||
|
||||
EXAMPLES = """
|
||||
- name: Test reachability to 10.10.10.10
|
||||
vyos_ping:
|
||||
dest: 10.10.10.10
|
||||
|
||||
- name: Test reachability to 10.20.20.20 using source and ttl set
|
||||
vyos_ping:
|
||||
dest: 10.20.20.20
|
||||
source: eth0
|
||||
ttl: 128
|
||||
|
||||
- name: Test unreachability to 10.30.30.30 using interval
|
||||
vyos_ping:
|
||||
dest: 10.30.30.30
|
||||
interval: 3
|
||||
state: absent
|
||||
|
||||
- name: Test reachability to 10.40.40.40 setting count and source
|
||||
vyos_ping:
|
||||
dest: 10.40.40.40
|
||||
source: eth1
|
||||
count: 20
|
||||
size: 512
|
||||
"""
|
||||
|
||||
RETURN = """
|
||||
commands:
|
||||
description: List of commands sent.
|
||||
returned: always
|
||||
type: list
|
||||
sample: ["ping 10.8.38.44 count 10 interface eth0 ttl 128"]
|
||||
packet_loss:
|
||||
description: Percentage of packets lost.
|
||||
returned: always
|
||||
type: str
|
||||
sample: "0%"
|
||||
packets_rx:
|
||||
description: Packets successfully received.
|
||||
returned: always
|
||||
type: int
|
||||
sample: 20
|
||||
packets_tx:
|
||||
description: Packets successfully transmitted.
|
||||
returned: always
|
||||
type: int
|
||||
sample: 20
|
||||
rtt:
|
||||
description: The round trip time (RTT) stats.
|
||||
returned: when ping succeeds
|
||||
type: dict
|
||||
sample: {"avg": 2, "max": 8, "min": 1, "mdev": 24}
|
||||
"""
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.network.vyos.vyos import run_commands
|
||||
from ansible.module_utils.network.vyos.vyos import vyos_argument_spec
|
||||
import re
|
||||
|
||||
|
||||
def main():
|
||||
""" main entry point for module execution
|
||||
"""
|
||||
argument_spec = dict(
|
||||
count=dict(type="int", default=5),
|
||||
dest=dict(type="str", required=True),
|
||||
source=dict(type="str"),
|
||||
ttl=dict(type='int'),
|
||||
size=dict(type='int'),
|
||||
interval=dict(type='int'),
|
||||
state=dict(type="str", choices=["absent", "present"], default="present"),
|
||||
)
|
||||
|
||||
argument_spec.update(vyos_argument_spec)
|
||||
|
||||
module = AnsibleModule(argument_spec=argument_spec)
|
||||
|
||||
count = module.params["count"]
|
||||
dest = module.params["dest"]
|
||||
source = module.params["source"]
|
||||
size = module.params["size"]
|
||||
ttl = module.params["ttl"]
|
||||
interval = module.params["interval"]
|
||||
|
||||
warnings = list()
|
||||
|
||||
results = {}
|
||||
if warnings:
|
||||
results["warnings"] = warnings
|
||||
|
||||
results["commands"] = [build_ping(dest, count, size, interval, source, ttl)]
|
||||
|
||||
ping_results = run_commands(module, commands=results["commands"])
|
||||
ping_results_list = ping_results[0].split("\n")
|
||||
|
||||
rtt_info, rate_info = None, None
|
||||
for line in ping_results_list:
|
||||
if line.startswith('rtt'):
|
||||
rtt_info = line
|
||||
if line.startswith('%s packets transmitted' % count):
|
||||
rate_info = line
|
||||
|
||||
if rtt_info:
|
||||
rtt = parse_rtt(rtt_info)
|
||||
for k, v in rtt.items():
|
||||
if rtt[k] is not None:
|
||||
rtt[k] = int(v)
|
||||
results["rtt"] = rtt
|
||||
|
||||
pkt_loss, rx, tx = parse_rate(rate_info)
|
||||
results["packet_loss"] = str(pkt_loss) + "%"
|
||||
results["packets_rx"] = int(rx)
|
||||
results["packets_tx"] = int(tx)
|
||||
|
||||
validate_results(module, pkt_loss, results)
|
||||
|
||||
module.exit_json(**results)
|
||||
|
||||
|
||||
def build_ping(dest, count, size=None, interval=None, source=None, ttl=None):
|
||||
cmd = "ping {0} count {1}".format(dest, str(count))
|
||||
|
||||
if source:
|
||||
cmd += " interface {0}".format(source)
|
||||
|
||||
if ttl:
|
||||
cmd += " ttl {0}".format(str(ttl))
|
||||
|
||||
if size:
|
||||
cmd += " size {0}".format(str(size))
|
||||
|
||||
if interval:
|
||||
cmd += " interval {0}".format(str(interval))
|
||||
|
||||
return cmd
|
||||
|
||||
|
||||
def parse_rate(rate_info):
|
||||
rate_re = re.compile(
|
||||
r"(?P<tx>\d+) (?:\w+) (?:\w+), (?P<rx>\d+) (?:\w+), (?P<pkt_loss>\d+)% (?:\w+) (?:\w+), (?:\w+) (?P<time>\d+)")
|
||||
rate_err_re = re.compile(
|
||||
r"(?P<tx>\d+) (?:\w+) (?:\w+), (?P<rx>\d+) (?:\w+), (?:[+-])(?P<err>\d+) (?:\w+), (?P<pkt_loss>\d+)% (?:\w+) (?:\w+), (?:\w+) (?P<time>\d+)")
|
||||
|
||||
if rate_re.match(rate_info):
|
||||
rate = rate_re.match(rate_info)
|
||||
elif rate_err_re.match(rate_info):
|
||||
rate = rate_err_re.match(rate_info)
|
||||
|
||||
return rate.group("pkt_loss"), rate.group("rx"), rate.group("tx")
|
||||
|
||||
|
||||
def parse_rtt(rtt_info):
|
||||
rtt_re = re.compile(
|
||||
r"rtt (?:.*)=(?:\s*)(?P<min>\d*).(?:\d*)/(?P<avg>\d*).(?:\d*)/(?P<max>\d+).(?:\d*)/(?P<mdev>\d*)")
|
||||
rtt = rtt_re.match(rtt_info)
|
||||
|
||||
return rtt.groupdict()
|
||||
|
||||
|
||||
def validate_results(module, loss, results):
|
||||
state = module.params["state"]
|
||||
if state == "present" and int(loss) == 100:
|
||||
module.fail_json(msg="Ping failed unexpectedly", **results)
|
||||
elif state == "absent" and int(loss) < 100:
|
||||
module.fail_json(msg="Ping succeeded unexpectedly", **results)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
|
@ -0,0 +1,7 @@
|
|||
PING 10.10.10.10 (10.10.10.10) 56(84) bytes of data.
|
||||
64 bytes from 10.10.10.10: icmp_req=1 ttl=255 time=1.27 ms
|
||||
64 bytes from 10.10.10.10: icmp_req=2 ttl=255 time=2.28 ms
|
||||
|
||||
--- 10.8.38.66 ping statistics ---
|
||||
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
|
||||
rtt min/avg/max/mdev = 12.1222/17.124/22.225/10.143 ms
|
|
@ -0,0 +1,15 @@
|
|||
PING 10.10.10.11 (10.8.38.65) 512(540) bytes of data.
|
||||
520 bytes from 10.10.10.11: icmp_req=1 ttl=255 time=1.17 ms
|
||||
520 bytes from 10.10.10.11: icmp_req=2 ttl=255 time=1.32 ms
|
||||
520 bytes from 10.10.10.11: icmp_req=3 ttl=255 time=1.21 ms
|
||||
520 bytes from 10.10.10.11: icmp_req=4 ttl=255 time=1.46 ms
|
||||
520 bytes from 10.10.10.11: icmp_req=5 ttl=255 time=1.32 ms
|
||||
520 bytes from 10.10.10.11: icmp_req=6 ttl=255 time=1.28 ms
|
||||
520 bytes from 10.10.10.11: icmp_req=7 ttl=255 time=1.25 ms
|
||||
520 bytes from 10.10.10.11: icmp_req=8 ttl=255 time=1.23 ms
|
||||
520 bytes from 10.10.10.11: icmp_req=9 ttl=255 time=1.34 ms
|
||||
520 bytes from 10.10.10.11: icmp_req=10 ttl=255 time=21.0 ms
|
||||
|
||||
--- 10.10.10.11 ping statistics ---
|
||||
10 packets transmitted, 10 received, 0% packet loss, time 9012ms
|
||||
rtt min/avg/max/mdev = 1.170/3.262/21.002/5.913 ms
|
|
@ -0,0 +1,9 @@
|
|||
PING 10.10.10.20 (10.10.10.20) 56(84) bytes of data.
|
||||
From 10.10.10.20 icmp_seq=1 Destination Host Unreachable
|
||||
From 10.10.10.20 icmp_seq=2 Destination Host Unreachable
|
||||
From 10.10.10.20 icmp_seq=3 Destination Host Unreachable
|
||||
From 10.10.10.20 icmp_seq=4 Destination Host Unreachable
|
||||
|
||||
--- 10.10.10.20 ping statistics ---
|
||||
4 packets transmitted, 0 received, +4 errors, 100% packet loss, time 3053ms
|
||||
pipe 3
|
102
test/units/modules/network/vyos/test_vyos_ping.py
Normal file
102
test/units/modules/network/vyos/test_vyos_ping.py
Normal file
|
@ -0,0 +1,102 @@
|
|||
# (c) 2016 Red Hat Inc.
|
||||
#
|
||||
# This file is part of Ansible
|
||||
#
|
||||
# Ansible is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Ansible is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
# Make coding more python3-ish
|
||||
from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
from units.compat.mock import patch
|
||||
from ansible.modules.network.vyos import vyos_ping
|
||||
from units.modules.utils import set_module_args
|
||||
from .vyos_module import TestVyosModule, load_fixture
|
||||
|
||||
|
||||
class TestVyosPingModule(TestVyosModule):
|
||||
|
||||
module = vyos_ping
|
||||
|
||||
def setUp(self):
|
||||
super(TestVyosPingModule, self).setUp()
|
||||
self.mock_run_commands = patch('ansible.modules.network.vyos.vyos_ping.run_commands')
|
||||
self.run_commands = self.mock_run_commands.start()
|
||||
|
||||
def tearDown(self):
|
||||
super(TestVyosPingModule, self).tearDown()
|
||||
self.mock_run_commands.stop()
|
||||
|
||||
def load_fixtures(self, commands=None):
|
||||
def load_from_file(*args, **kwargs):
|
||||
commands = kwargs['commands']
|
||||
output = list()
|
||||
|
||||
for command in commands:
|
||||
filename = str(command).split(' | ')[0].replace(' ', '_')
|
||||
output.append(load_fixture('vyos_ping_%s' % filename))
|
||||
return output
|
||||
|
||||
self.run_commands.side_effect = load_from_file
|
||||
|
||||
def test_vyos_ping_expected_success(self):
|
||||
''' Test for successful pings when destination should be reachable '''
|
||||
set_module_args(dict(count=2, dest="10.10.10.10"))
|
||||
self.execute_module()
|
||||
|
||||
def test_vyos_ping_expected_failure(self):
|
||||
''' Test for unsuccessful pings when destination should not be reachable '''
|
||||
set_module_args(dict(count=4, dest="10.10.10.20", state="absent"))
|
||||
self.execute_module()
|
||||
|
||||
def test_vyos_ping_unexpected_success(self):
|
||||
''' Test for successful pings when destination should not be reachable - FAIL. '''
|
||||
set_module_args(dict(count=2, dest="10.10.10.10", state="absent"))
|
||||
self.execute_module(failed=True)
|
||||
|
||||
def test_vyos_ping_unexpected_failure(self):
|
||||
''' Test for unsuccessful pings when destination should be reachable - FAIL. '''
|
||||
set_module_args(dict(count=4, dest="10.10.10.20"))
|
||||
self.execute_module(failed=True)
|
||||
|
||||
def test_vyos_ping_failure_stats(self):
|
||||
'''Test for asserting stats when ping fails'''
|
||||
set_module_args(dict(count=4, dest="10.10.10.20"))
|
||||
result = self.execute_module(failed=True)
|
||||
self.assertEqual(result['packet_loss'], '100%')
|
||||
self.assertEqual(result['packets_rx'], 0)
|
||||
self.assertEqual(result['packets_tx'], 4)
|
||||
|
||||
def test_vyos_ping_success_stats(self):
|
||||
'''Test for asserting stats when ping passes'''
|
||||
set_module_args(dict(count=2, dest="10.10.10.10"))
|
||||
result = self.execute_module()
|
||||
self.assertEqual(result['packet_loss'], '0%')
|
||||
self.assertEqual(result['packets_rx'], 2)
|
||||
self.assertEqual(result['packets_tx'], 2)
|
||||
self.assertEqual(result['rtt']['min'], 12)
|
||||
self.assertEqual(result['rtt']['avg'], 17)
|
||||
self.assertEqual(result['rtt']['max'], 22)
|
||||
self.assertEqual(result['rtt']['mdev'], 10)
|
||||
|
||||
def test_vyos_ping_success_stats_with_options(self):
|
||||
set_module_args(dict(count=10, ttl=128, size=512, dest="10.10.10.11"))
|
||||
result = self.execute_module()
|
||||
self.assertEqual(result['packet_loss'], '0%')
|
||||
self.assertEqual(result['packets_rx'], 10)
|
||||
self.assertEqual(result['packets_tx'], 10)
|
||||
self.assertEqual(result['rtt']['min'], 1)
|
||||
self.assertEqual(result['rtt']['avg'], 3)
|
||||
self.assertEqual(result['rtt']['max'], 21)
|
||||
self.assertEqual(result['rtt']['mdev'], 5)
|
Loading…
Reference in a new issue