1# (c) 2017 Red Hat Inc. 2# 3# This file is part of Ansible 4# 5# Ansible is free software: you can redistribute it and/or modify 6# it under the terms of the GNU General Public License as published by 7# the Free Software Foundation, either version 3 of the License, or 8# (at your option) any later version. 9# 10# Ansible is distributed in the hope that it will be useful, 11# but WITHOUT ANY WARRANTY; without even the implied warranty of 12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13# GNU General Public License for more details. 14# 15# You should have received a copy of the GNU General Public License 16# along with Ansible. If not, see <http://www.gnu.org/licenses/>. 17 18# Make coding more python3-ish 19from __future__ import (absolute_import, division, print_function) 20__metaclass__ = type 21 22try: 23 from lxml.etree import fromstring 24except ImportError: 25 from xml.etree.ElementTree import fromstring 26 27from units.compat.mock import patch 28from ansible.modules.network.junos import junos_facts 29from units.modules.utils import set_module_args 30from .junos_module import TestJunosModule, load_fixture 31 32RPC_CLI_MAP = { 33 'get-software-information': 'show version', 34 'get-interface-information': 'show interfaces details', 35 'get-system-memory-information': 'show system memory', 36 'get-chassis-inventory': 'show chassis hardware', 37 'get-route-engine-information': 'show chassis routing-engine', 38 'get-system-storage': 'show system storage' 39} 40 41 42class TestJunosCommandModule(TestJunosModule): 43 44 module = junos_facts 45 46 def setUp(self): 47 super(TestJunosCommandModule, self).setUp() 48 49 self.mock_get_config = patch('ansible.module_utils.network.junos.facts.legacy.base.get_configuration') 50 self.get_config = self.mock_get_config.start() 51 52 self.mock_netconf = patch('ansible.module_utils.network.junos.junos.NetconfConnection') 53 self.netconf_conn = self.mock_netconf.start() 54 55 self.mock_exec_rpc = patch('ansible.module_utils.network.junos.facts.legacy.base.exec_rpc') 56 self.exec_rpc = self.mock_exec_rpc.start() 57 58 self.mock_netconf_rpc = patch('ansible.module_utils.network.common.netconf.NetconfConnection') 59 self.netconf_rpc = self.mock_netconf_rpc.start() 60 61 self.mock_get_resource_connection = patch('ansible.module_utils.network.common.facts.facts.get_resource_connection') 62 self.get_resource_connection = self.mock_get_resource_connection.start() 63 64 self.mock_get_capabilities = patch('ansible.module_utils.network.junos.facts.legacy.base.get_capabilities') 65 self.get_capabilities = self.mock_get_capabilities.start() 66 67 self.get_capabilities.return_value = { 68 'device_info': { 69 'network_os': 'junos', 70 'network_os_hostname': 'vsrx01', 71 'network_os_model': 'vsrx', 72 'network_os_version': '17.3R1.10' 73 }, 74 'network_api': 'netconf' 75 } 76 77 def tearDown(self): 78 super(TestJunosCommandModule, self).tearDown() 79 self.mock_netconf.stop() 80 self.mock_exec_rpc.stop() 81 self.mock_netconf_rpc.stop() 82 self.mock_get_capabilities.stop() 83 self.mock_get_resource_connection.stop() 84 85 def load_fixtures(self, commands=None, format='text', changed=False): 86 def load_from_file(*args, **kwargs): 87 element = fromstring(args[1]) 88 89 if element.text: 90 path = str(element.text) 91 else: 92 path = RPC_CLI_MAP[str(element.tag)] 93 94 filename = path.replace(' ', '_') 95 filename = '%s_%s.txt' % (filename, format) 96 return load_fixture(filename) 97 98 self.exec_rpc.side_effect = load_from_file 99 100 def test_junos_get_facts(self): 101 set_module_args(dict()) 102 result = self.execute_module(format='xml') 103 facts = result['ansible_facts'] 104 105 self.assertEqual(facts['ansible_net_hostname'], 'vsrx01') 106 self.assertTrue('em0' in facts['ansible_net_interfaces']) 107 self.assertEqual(facts['ansible_net_interfaces']['em0']['type'], 'Ethernet') 108 self.assertEqual(facts['ansible_net_memtotal_mb'], 983500) 109 self.assertEqual(facts['ansible_net_filesystems'][0], '/dev/vtbd0s1a') 110 self.assertTrue('ansible_net_config' not in facts) 111 self.assertEqual(facts['ansible_net_routing_engines']["0"]['model'], 'RE-S-EX9200-1800X4') 112 self.assertEqual(facts['ansible_net_modules'][0]['name'], 'Midplane') 113 self.assertTrue(facts['ansible_net_has_2RE']) 114 115 def test_junos_get_facts_subset_config_set(self): 116 self.get_config.return_value = load_fixture('get_configuration_rpc_reply.txt') 117 set_module_args(dict(gather_subset='config', config_format='set')) 118 result = self.execute_module(format='xml') 119 facts = result['ansible_facts'] 120 121 self.assertTrue('ansible_net_config' in facts) 122 self.assertTrue(facts['ansible_net_config'].startswith('set')) 123 self.assertEqual(facts['ansible_net_hostname'], 'vsrx01') 124 self.assertTrue('ansible_net_interfaces' not in facts) 125 126 def test_junos_get_facts_subset_config_json(self): 127 self.get_config.return_value = load_fixture('get_configuration_rpc_reply_json.txt') 128 set_module_args(dict(gather_subset='config', config_format='json')) 129 result = self.execute_module(format='xml') 130 facts = result['ansible_facts'] 131 132 self.assertTrue('ansible_net_config' in facts) 133 self.assertTrue('configuration' in facts['ansible_net_config']) 134 self.assertEqual(facts['ansible_net_hostname'], 'vsrx01') 135 self.assertTrue('ansible_net_interfaces' not in facts) 136 137 def test_junos_get_facts_subset_list(self): 138 set_module_args(dict(gather_subset=['hardware', 'interfaces'])) 139 result = self.execute_module(format='xml') 140 facts = result['ansible_facts'] 141 142 self.assertTrue('ansible_net_config' not in facts) 143 self.assertEqual(facts['ansible_net_interfaces']['em0']['oper-status'], 'up') 144 self.assertEqual(facts['ansible_net_memfree_mb'], 200684) 145 146 def test_junos_get_facts_wrong_subset(self): 147 set_module_args(dict(gather_subset=['hardware', 'interfaces', 'test'])) 148 result = self.execute_module(format='xml', failed=True) 149 150 self.assertTrue(result['msg'].startswith('Subset must be one')) 151