1# 2# (c) 2019, Ansible by Red Hat, inc 3# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) 4# 5 6from __future__ import absolute_import, division, print_function 7 8__metaclass__ = type 9 10from ansible_collections.cisco.iosxr.tests.unit.compat.mock import patch 11from ansible_collections.cisco.iosxr.plugins.modules import iosxr_ospfv2 12from ansible_collections.cisco.iosxr.tests.unit.modules.utils import ( 13 set_module_args, 14) 15from .iosxr_module import TestIosxrModule, load_fixture 16 17 18class TestIosxrOspfV2Module(TestIosxrModule): 19 module = iosxr_ospfv2 20 21 def setUp(self): 22 super(TestIosxrOspfV2Module, self).setUp() 23 24 self.mock_get_config = patch( 25 "ansible_collections.ansible.netcommon.plugins.module_utils.network.common.network.Config.get_config" 26 ) 27 self.get_config = self.mock_get_config.start() 28 29 self.mock_load_config = patch( 30 "ansible_collections.ansible.netcommon.plugins.module_utils.network.common.network.Config.load_config" 31 ) 32 self.load_config = self.mock_load_config.start() 33 34 self.mock_get_resource_connection_config = patch( 35 "ansible_collections.ansible.netcommon.plugins.module_utils.network.common.cfg.base." 36 "get_resource_connection" 37 ) 38 self.get_resource_connection_config = ( 39 self.mock_get_resource_connection_config.start() 40 ) 41 42 self.mock_get_resource_connection_facts = patch( 43 "ansible_collections.ansible.netcommon.plugins.module_utils.network.common.rm_base.resource_module_base." 44 "get_resource_connection" 45 ) 46 self.get_resource_connection_facts = ( 47 self.mock_get_resource_connection_facts.start() 48 ) 49 50 self.mock_edit_config = patch( 51 "ansible_collections.cisco.iosxr.plugins.module_utils.network.iosxr.providers.providers.CliProvider.edit_config" 52 ) 53 self.edit_config = self.mock_edit_config.start() 54 55 self.mock_execute_show_command = patch( 56 "ansible_collections.cisco.iosxr.plugins.module_utils.network.iosxr.facts.ospfv2.ospfv2." 57 "Ospfv2Facts.get_ospfv2_data" 58 ) 59 self.execute_show_command = self.mock_execute_show_command.start() 60 61 def tearDown(self): 62 super(TestIosxrOspfV2Module, self).tearDown() 63 self.mock_get_resource_connection_config.stop() 64 self.mock_get_resource_connection_facts.stop() 65 self.mock_edit_config.stop() 66 self.mock_get_config.stop() 67 self.mock_load_config.stop() 68 self.mock_execute_show_command.stop() 69 70 def load_fixtures(self, commands=None): 71 def load_from_file(*args, **kwargs): 72 return load_fixture("iosxr_ospfv2.cfg") 73 74 self.execute_show_command.side_effect = load_from_file 75 76 def test_iosxr_ospfv2_merged(self): 77 set_module_args( 78 dict( 79 config=dict( 80 processes=[ 81 dict( 82 process_id="300", 83 default_metric=10, 84 cost=2, 85 areas=[dict(area_id="11", default_cost=5)], 86 ) 87 ] 88 ), 89 state="merged", 90 ) 91 ) 92 commands = [ 93 "router ospf 300", 94 "cost 2", 95 "default-metric 10", 96 "area 11 default-cost 5", 97 ] 98 result = self.execute_module(changed=True) 99 self.assertEqual(sorted(result["commands"]), sorted(commands)) 100 101 def test_iosxr_ospfv2_merged_idempotent(self): 102 set_module_args( 103 dict( 104 config=dict( 105 processes=[ 106 dict( 107 process_id="30", 108 default_metric=10, 109 cost=2, 110 areas=[dict(area_id="11", default_cost=5)], 111 ) 112 ] 113 ), 114 state="merged", 115 ) 116 ) 117 self.execute_module(changed=False, commands=[]) 118 119 def test_iosxr_ospfv2_replaced(self): 120 set_module_args( 121 dict( 122 config=dict( 123 processes=[ 124 dict( 125 process_id="30", 126 cost=2, 127 areas=[dict(area_id="11", default_cost=5)], 128 ), 129 dict( 130 process_id="40", 131 default_metric=10, 132 cost=2, 133 areas=[dict(area_id="11", default_cost=5)], 134 ), 135 ] 136 ), 137 state="replaced", 138 ) 139 ) 140 commands = [ 141 "router ospf 30", 142 "no default-metric 10", 143 "router ospf 40", 144 "cost 2", 145 "default-metric 10", 146 "area 11 default-cost 5", 147 ] 148 result = self.execute_module(changed=True) 149 self.assertEqual(sorted(result["commands"]), sorted(commands)) 150 151 def test_iosxr_ospfv2_replaced_idempotent(self): 152 set_module_args( 153 dict( 154 config=dict( 155 processes=[ 156 dict( 157 process_id="30", 158 default_metric=10, 159 cost=2, 160 areas=[dict(area_id="11", default_cost=5)], 161 ) 162 ] 163 ), 164 state="replaced", 165 ) 166 ) 167 self.execute_module(changed=False, commands=[]) 168 169 def test_iosxr_ospfv2_overridden(self): 170 set_module_args( 171 dict( 172 config=dict( 173 processes=[ 174 dict( 175 process_id="40", 176 default_metric=10, 177 cost=2, 178 areas=[dict(area_id="11", default_cost=5)], 179 ) 180 ] 181 ), 182 state="overridden", 183 ) 184 ) 185 186 commands = [ 187 "router ospf 30", 188 "no cost 2", 189 "no default-metric 10", 190 "no area 11 default-cost 5", 191 "router ospf 40", 192 "cost 2", 193 "default-metric 10", 194 "area 11 default-cost 5", 195 ] 196 result = self.execute_module(changed=True) 197 self.assertEqual(sorted(result["commands"]), sorted(commands)) 198 199 def test_iosxr_ospfv2_overridden_idempotent(self): 200 set_module_args( 201 dict( 202 config=dict( 203 processes=[ 204 dict( 205 process_id="30", 206 default_metric=10, 207 cost=2, 208 areas=[dict(area_id="11", default_cost=5)], 209 ) 210 ] 211 ), 212 state="overridden", 213 ) 214 ) 215 self.execute_module(changed=False, commands=[]) 216 217 def test_iosxr_ospfv2_deleted(self): 218 set_module_args( 219 dict( 220 config=dict(processes=[dict(process_id="30", cost=2)]), 221 state="deleted", 222 ) 223 ) 224 commands = [ 225 "router ospf 30", 226 "no cost 2", 227 "no default-metric 10", 228 "no area 11 default-cost 5", 229 ] 230 result = self.execute_module(changed=True) 231 self.assertEqual(sorted(result["commands"]), sorted(commands)) 232 233 def test_iosxr_ospfv2_parsed(self): 234 set_module_args( 235 dict( 236 running_config="router ospf 50\n cost 2\n area 11\n default-cost 5\n !\n!", 237 state="parsed", 238 ) 239 ) 240 result = self.execute_module(changed=False) 241 parsed_list = { 242 "processes": [ 243 dict( 244 process_id="50", 245 cost=2, 246 areas=[dict(area_id="11", default_cost=5)], 247 ) 248 ] 249 } 250 self.assertEqual(parsed_list, result["parsed"]) 251 252 def test_iosxr_ospfv2_rendered(self): 253 set_module_args( 254 dict( 255 config=dict( 256 processes=[ 257 dict( 258 process_id="60", 259 default_metric=10, 260 cost=2, 261 areas=[dict(area_id="11", default_cost=5)], 262 ) 263 ] 264 ), 265 state="rendered", 266 ) 267 ) 268 commands = [ 269 "area 11 default-cost 5", 270 "cost 2", 271 "default-metric 10", 272 "router ospf 60", 273 ] 274 result = self.execute_module(changed=False) 275 self.assertEqual(sorted(result["rendered"]), commands) 276