Refactor iosxr_facts to use network_cli plugin (#20857)

This commit is contained in:
Ricardo Carrillo Cruz 2017-02-06 22:23:17 +01:00 committed by GitHub
commit 375846d764
10 changed files with 270 additions and 50 deletions

View file

@ -0,0 +1 @@
lollll

View file

@ -0,0 +1,41 @@
Loopback0 is up, line protocol is up
Interface state transitions: 1
Hardware is Loopback interface(s)
Description: Loopback
Internet address is 192.168.0.3/32
MTU 1500 bytes, BW 0 Kbit
reliability Unknown, txload Unknown, rxload Unknown
Encapsulation Loopback, loopback not set,
Last link flapped 12w1d
Last input Unknown, output Unknown
Last clearing of "show interface" counters Unknown
Input/output data rate is disabled.
GigabitEthernet0/0/0/0 is up, line protocol is up
Interface state transitions: 1
Hardware is GigabitEthernet, address is fa16.3e6c.20bd (bia fa16.3e6c.20bd)
Description: to nxos01
Internet address is 10.0.0.5/30
MTU 1514 bytes, BW 1000000 Kbit (Max: 1000000 Kbit)
reliability 255/255, txload 0/255, rxload 0/255
Encapsulation ARPA,
Full-duplex, 1000Mb/s, unknown, link type is force-up
output flow control is off, input flow control is off
Carrier delay (up) is 10 msec
loopback not set,
Last link flapped 12w1d
ARP type ARPA, ARP timeout 04:00:00
Last input 00:00:44, output 00:12:45
Last clearing of "show interface" counters never
5 minute input rate 0 bits/sec, 0 packets/sec
5 minute output rate 0 bits/sec, 0 packets/sec
150700 packets input, 36897055 bytes, 0 total input drops
0 drops for unrecognized upper-level protocol
Received 1 broadcast packets, 150445 multicast packets
0 runts, 0 giants, 0 throttles, 0 parity
0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored, 0 abort
11691 packets output, 2904632 bytes, 0 total output drops
Output 1 broadcast packets, 11436 multicast packets
0 output errors, 0 underruns, 0 applique, 0 resets
0 output buffer failures, 0 output buffers swapped out
1 carrier transitions

View file

@ -0,0 +1,5 @@
Loopback0 is Up, ipv6 protocol is Up, Vrfid is default (0x60000000)
IPv6 is disabled, link-local address unassigned
No global unicast address is configured
GigabitEthernet0/0/0/0 is Up, ipv6 protocol is Up, Vrfid is default (0x60000000)
IPv6 is disabled, link-local address unassigned

View file

@ -0,0 +1 @@
% LLDP is not enabled

View file

@ -0,0 +1 @@
% LLDP is not enabled

View file

@ -0,0 +1,5 @@
Physical Memory: 3095M total (1499M available)
Application Memory : 2893M (1499M available)
Image: 73M (bootram: 73M)
Reserved: 128M, IOMem: 0, flashfsys: 0
Total shared window: 23M

View file

@ -0,0 +1,43 @@
hostname iosxr01
service timestamps log datetime msec
service timestamps debug datetime msec
telnet vrf default ipv4 server max-servers 10
telnet vrf Mgmt-intf ipv4 server max-servers 10
domain name eng.ansible.com
domain lookup disable
vrf Mgmt-intf
address-family ipv4 unicast
!
address-family ipv6 unicast
!
!
line template vty
timestamp
exec-timeout 720 0
!
line console
exec-timeout 0 0
!
line default
exec-timeout 720 0
!
vty-pool default 0 50
control-plane
management-plane
inband
interface all
allow all
!
!
!
!
interface Loopback0
description Loopback
ipv4 address 192.168.0.1 255.255.255.255
!
interface GigabitEthernet0/0/0/0
description to nxos01
cdp
ipv4 address 10.0.0.1 255.255.255.252
!
end

View file

@ -0,0 +1,18 @@
Cisco IOS XR Software, Version 6.0.0[Default]
Copyright (c) 2015 by Cisco Systems, Inc.
ROM: GRUB, Version 1.99(0), DEV RELEASE
iosxr01 uptime is 11 weeks, 6 days, 2 hours, 2 minutes
System image file is "bootflash:disk0/xrvr-os-mbi-6.0.0/mbixrvr-rp.vm"
cisco IOS XRv Series (Pentium Celeron Stepping 3) processor with 3169911K bytes
of memory.
Pentium Celeron Stepping 3 processor at 3836MHz, Revision 2.174
IOS XRv Chassis
1 Management Ethernet
6 GigabitEthernet
97070k bytes of non-volatile configuration memory.
866M bytes of hard disk.
2321392k bytes of disk0: (Sector size 512 bytes).

View file

@ -0,0 +1,121 @@
# (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
import os
import json
from ansible.compat.tests import unittest
from ansible.compat.tests.mock import patch
from ansible.errors import AnsibleModuleExit
from ansible.modules.network.iosxr import iosxr_facts
from ansible.module_utils import basic
from ansible.module_utils._text import to_bytes
def set_module_args(args):
args = json.dumps({'ANSIBLE_MODULE_ARGS': args})
basic._ANSIBLE_ARGS = to_bytes(args)
fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures')
fixture_data = {}
def load_fixture(name):
path = os.path.join(fixture_path, name)
if path in fixture_data:
return fixture_data[path]
with open(path) as f:
data = f.read()
try:
data = json.loads(data)
except:
pass
fixture_data[path] = data
return data
class TestIosxrFacts(unittest.TestCase):
def setUp(self):
self.mock_run_commands = patch(
'ansible.modules.network.iosxr.iosxr_facts.run_commands')
self.run_commands = self.mock_run_commands.start()
def tearDown(self):
self.mock_run_commands.stop()
def execute_module(self, failed=False, changed=False):
def load_from_file(*args, **kwargs):
module, commands = args
output = list()
for item in commands:
try:
obj = json.loads(item)
command = obj['command']
except ValueError:
command = item
filename = str(command).replace(' ', '_')
filename = filename.replace('/', '7')
filename = filename.replace('|', '1')
output.append(load_fixture(filename))
return output
self.run_commands.side_effect = load_from_file
with self.assertRaises(AnsibleModuleExit) as exc:
iosxr_facts.main()
result = exc.exception.result
if failed:
self.assertTrue(result.get('failed'))
else:
self.assertEqual(result.get('changed'), changed, result)
return result
def test_iosxr_facts_gather_subset_default(self):
set_module_args(dict())
result = self.execute_module()
ansible_facts = result['ansible_facts']
self.assertIn('hardware', ansible_facts['ansible_net_gather_subset'])
self.assertIn('default', ansible_facts['ansible_net_gather_subset'])
self.assertIn('interfaces', ansible_facts['ansible_net_gather_subset'])
self.assertEquals('iosxr01', ansible_facts['ansible_net_hostname'])
self.assertEquals([], ansible_facts['ansible_net_filesystems'])
self.assertIn('GigabitEthernet0/0/0/0',
ansible_facts['ansible_net_interfaces'].keys())
def test_iosxr_facts_gather_subset_config(self):
set_module_args({'gather_subset': 'config'})
result = self.execute_module()
ansible_facts = result['ansible_facts']
self.assertIn('default', ansible_facts['ansible_net_gather_subset'])
self.assertIn('config', ansible_facts['ansible_net_gather_subset'])
self.assertEquals('iosxr01', ansible_facts['ansible_net_hostname'])
self.assertIn('ansible_net_config', ansible_facts)