| 1 |
import unittest |
| 2 |
import sys |
| 3 |
import nose2 |
| 4 |
|
| 5 |
from jnpr.junos import Device |
| 6 |
from jnpr.junos.utils.config import Config |
| 7 |
from jnpr.junos.exception import ( |
| 8 |
RpcError, |
| 9 |
LockError, |
| 10 |
UnlockError, |
| 11 |
CommitError, |
| 12 |
RpcTimeoutError, |
| 13 |
ConfigLoadError, |
| 14 |
ConnectClosedError, |
| 15 |
) |
| 16 |
|
| 17 |
import ncclient |
| 18 |
from ncclient.manager import Manager, make_device_handler |
| 19 |
from ncclient.transport import SSHSession |
| 20 |
from ncclient.operations import RPCError, RPCReply |
| 21 |
|
| 22 |
from unittest.mock import MagicMock, patch |
| 23 |
from lxml import etree |
| 24 |
import os |
| 25 |
|
| 26 |
__author__ = "Nitin Kumar, Rick Sherman" |
| 27 |
__credits__ = "Jeremy Schulman" |
| 28 |
|
| 29 |
if sys.version < "3": |
| 30 |
builtin_string = "__builtin__" |
| 31 |
else: |
| 32 |
builtin_string = "builtins" |
| 33 |
|
| 34 |
|
| 35 |
class TestConfig(unittest.TestCase): |
| 36 |
@patch("ncclient.manager.connect") |
| 37 |
def setUp(self, mock_connect): |
| 38 |
mock_connect.side_effect = self._mock_manager |
| 39 |
|
| 40 |
self.dev = Device( |
| 41 |
host="1.1.1.1", user="test", password="test123", gather_facts=False |
| 42 |
) |
| 43 |
self.dev.open() |
| 44 |
self.conf = Config(self.dev) |
| 45 |
|
| 46 |
@patch("ncclient.operations.session.CloseSession.request") |
| 47 |
def tearDown(self, mock_session): |
| 48 |
self.dev.close() |
| 49 |
|
| 50 |
def test_config_constructor(self): |
| 51 |
self.assertTrue(isinstance(self.conf._dev, Device)) |
| 52 |
|
| 53 |
def test_config_confirm_true(self): |
| 54 |
self.conf.rpc.commit_configuration = MagicMock() |
| 55 |
self.conf.commit(confirm=True) |
| 56 |
self.conf.rpc.commit_configuration.assert_called_with(confirmed=True) |
| 57 |
|
| 58 |
def test_config_commit_confirm(self): |
| 59 |
self.conf.rpc.commit_configuration = MagicMock() |
| 60 |
self.conf.commit(confirm=10) |
| 61 |
self.conf.rpc.commit_configuration.assert_called_with( |
| 62 |
**{"confirm-timeout": "10", "confirmed": True} |
| 63 |
) |
| 64 |
|
| 65 |
def test_config_commit_comment(self): |
| 66 |
self.conf.rpc.commit_configuration = MagicMock() |
| 67 |
self.conf.commit(comment="Test") |
| 68 |
self.conf.rpc.commit_configuration.assert_called_with(log="Test") |
| 69 |
|
| 70 |
def test_config_commit_sync(self): |
| 71 |
self.conf.rpc.commit_configuration = MagicMock() |
| 72 |
self.conf.commit(sync=True) |
| 73 |
self.conf.rpc.commit_configuration.assert_called_with(synchronize=True) |
| 74 |
|
| 75 |
def test_config_commit_force_sync(self): |
| 76 |
self.conf.rpc.commit_configuration = MagicMock() |
| 77 |
self.conf.commit(force_sync=True) |
| 78 |
self.conf.rpc.commit_configuration.assert_called_with( |
| 79 |
**{"synchronize": True, "force-synchronize": True} |
| 80 |
) |
| 81 |
|
| 82 |
def test_config_commit_timeout(self): |
| 83 |
self.conf.rpc.commit_configuration = MagicMock() |
| 84 |
self.conf.commit(timeout=60) |
| 85 |
self.conf.rpc.commit_configuration.assert_called_with(dev_timeout=60) |
| 86 |
|
| 87 |
def test_config_commit_full(self): |
| 88 |
self.conf.rpc.commit_configuration = MagicMock() |
| 89 |
self.conf.commit(full=True) |
| 90 |
self.conf.rpc.commit_configuration.assert_called_with(full=True) |
| 91 |
|
| 92 |
def test_config_commit_detail(self): |
| 93 |
self.conf.rpc.commit_configuration = MagicMock() |
| 94 |
self.conf.rpc.commit_configuration.return_value = "<mockdetail/>" |
| 95 |
self.assertEqual("<mockdetail/>", self.conf.commit(detail=True)) |
| 96 |
self.conf.rpc.commit_configuration.assert_called_with({"detail": "detail"}) |
| 97 |
|
| 98 |
def test_config_commit_combination(self): |
| 99 |
self.conf.rpc.commit_configuration = MagicMock() |
| 100 |
self.conf.rpc.commit_configuration.return_value = "<moredetail/>" |
| 101 |
self.assertEqual( |
| 102 |
"<moredetail/>", self.conf.commit(detail=True, force_sync=True, full=True) |
| 103 |
) |
| 104 |
self.conf.rpc.commit_configuration.assert_called_with( |
| 105 |
{"detail": "detail"}, |
| 106 |
**{"synchronize": True, "full": True, "force-synchronize": True} |
| 107 |
) |
| 108 |
|
| 109 |
@patch("jnpr.junos.utils.config.JXML.remove_namespaces") |
| 110 |
def test_config_commit_xml_exception(self, mock_jxml): |
| 111 |
class MyException(Exception): |
| 112 |
xml = etree.fromstring("<test/>") |
| 113 |
|
| 114 |
self.conf.rpc.commit_configuration = MagicMock(side_effect=MyException) |
| 115 |
self.assertRaises(CommitError, self.conf.commit) |
| 116 |
|
| 117 |
def test_config_commit_exception(self): |
| 118 |
class MyException(Exception): |
| 119 |
pass |
| 120 |
|
| 121 |
self.conf.rpc.commit_configuration = MagicMock(side_effect=MyException) |
| 122 |
self.assertRaises(MyException, self.conf.commit) |
| 123 |
|
| 124 |
def test_config_commit_exception_RpcError(self): |
| 125 |
ex = RpcError(rsp="ok") |
| 126 |
self.conf.rpc.commit_configuration = MagicMock(side_effect=ex) |
| 127 |
self.assertTrue(self.conf.commit()) |
| 128 |
import xml.etree.ElementTree as ET |
| 129 |
|
| 130 |
xmldata = """<data><company name="Juniper"> |
| 131 |
<code>pyez</code> |
| 132 |
<year>2013</year> |
| 133 |
</company></data>""" |
| 134 |
root = ET.fromstring(xmldata) |
| 135 |
el = root.find("company") |
| 136 |
ex = RpcError(rsp=el) |
| 137 |
self.conf.rpc.commit_configuration = MagicMock(side_effect=ex) |
| 138 |
self.assertRaises(CommitError, self.conf.commit) |
| 139 |
|
| 140 |
def test_commit_check(self): |
| 141 |
self.conf.rpc.commit_configuration = MagicMock() |
| 142 |
self.assertTrue(self.conf.commit_check()) |
| 143 |
|
| 144 |
# @patch('jnpr.junos.utils.config.JXML.rpc_error') |
| 145 |
def test_commit_check_exception(self): |
| 146 |
class MyException(Exception): |
| 147 |
xml = etree.fromstring( |
| 148 |
""" |
| 149 |
<rpc-reply> |
| 150 |
<rpc-error> |
| 151 |
<error-type>protocol</error-type> |
| 152 |
<error-tag>operation-failed</error-tag> |
| 153 |
<error-severity>error</error-severity> |
| 154 |
<error-message>permission denied</error-message> |
| 155 |
<error-info> |
| 156 |
<bad-element>system</bad-element> |
| 157 |
</error-info> |
| 158 |
</rpc-error> |
| 159 |
</rpc-reply> |
| 160 |
""" |
| 161 |
) |
| 162 |
|
| 163 |
self.conf.rpc.commit_configuration = MagicMock(side_effect=MyException) |
| 164 |
# with self.assertRaises(AttributeError): |
| 165 |
self.assertDictEqual( |
| 166 |
self.conf.commit_check(), |
| 167 |
{ |
| 168 |
"source": None, |
| 169 |
"message": "permission denied", |
| 170 |
"bad_element": "system", |
| 171 |
"severity": "error", |
| 172 |
"edit_path": None, |
| 173 |
}, |
| 174 |
) |
| 175 |
|
| 176 |
def test_config_commit_check_exception_RpcError(self): |
| 177 |
ex = RpcError(rsp="ok") |
| 178 |
self.conf.rpc.commit_configuration = MagicMock(side_effect=ex) |
| 179 |
self.assertTrue(self.conf.commit_check()) |
| 180 |
import xml.etree.ElementTree as ET |
| 181 |
|
| 182 |
xmldata = """<data><company name="Juniper"> |
| 183 |
<code>pyez</code> |
| 184 |
<year>2013</year> |
| 185 |
</company></data>""" |
| 186 |
root = ET.fromstring(xmldata) |
| 187 |
el = root.find("company") |
| 188 |
ex = RpcError(rsp=el) |
| 189 |
self.conf.rpc.commit_configuration = MagicMock(side_effect=ex) |
| 190 |
self.assertRaises(CommitError, self.conf.commit_check) |
| 191 |
|
| 192 |
def test_config_diff(self): |
| 193 |
self.conf.rpc.get_configuration = MagicMock() |
| 194 |
self.conf.diff() |
| 195 |
self.conf.rpc.get_configuration.assert_called_with( |
| 196 |
{"compare": "rollback", "rollback": "0", "format": "text"}, |
| 197 |
ignore_warning=False, |
| 198 |
) |
| 199 |
|
| 200 |
def test_config_diff_use_fast_diff(self): |
| 201 |
self.conf.rpc.get_configuration = MagicMock() |
| 202 |
self.conf.diff(use_fast_diff=True) |
| 203 |
self.conf.rpc.get_configuration.assert_called_with( |
| 204 |
{ |
| 205 |
"compare": "rollback", |
| 206 |
"rollback": "0", |
| 207 |
"format": "text", |
| 208 |
"use-fast-diff": "yes", |
| 209 |
}, |
| 210 |
ignore_warning=False, |
| 211 |
) |
| 212 |
|
| 213 |
def test_config_diff_use_fast_diff_rb_id_gt_0(self): |
| 214 |
self.conf.rpc.get_configuration = MagicMock() |
| 215 |
with self.assertRaises(ValueError): |
| 216 |
self.conf.diff(use_fast_diff=True, rb_id=1) |
| 217 |
|
| 218 |
def test_config_diff_exception_severity_warning(self): |
| 219 |
rpc_xml = """ |
| 220 |
<rpc-error> |
| 221 |
<error-severity>warning</error-severity> |
| 222 |
<error-info><bad-element>bgp</bad-element></error-info> |
| 223 |
<error-message>mgd: statement must contain additional statements</error-message> |
| 224 |
</rpc-error> |
| 225 |
""" |
| 226 |
rsp = etree.XML(rpc_xml) |
| 227 |
self.conf.rpc.get_configuration = MagicMock(side_effect=RpcError(rsp=rsp)) |
| 228 |
self.assertEqual(self.conf.diff(), "Unable to parse diff from response!") |
| 229 |
|
| 230 |
def test_config_diff_exception_severity_warning_still_raise(self): |
| 231 |
rpc_xml = """ |
| 232 |
<rpc-error> |
| 233 |
<error-severity>warning</error-severity> |
| 234 |
<error-info><bad-element>bgp</bad-element></error-info> |
| 235 |
<error-message>statement not found</error-message> |
| 236 |
</rpc-error> |
| 237 |
""" |
| 238 |
rsp = etree.XML(rpc_xml) |
| 239 |
self.conf.rpc.get_configuration = MagicMock(side_effect=RpcError(rsp=rsp)) |
| 240 |
self.assertRaises(RpcError, self.conf.diff) |
| 241 |
|
| 242 |
def test_config_pdiff(self): |
| 243 |
self.conf.diff = MagicMock(return_value="Stuff") |
| 244 |
self.conf.pdiff() |
| 245 |
self.conf.diff.assert_called_once_with(0, False, False) |
| 246 |
|
| 247 |
def test_config_diff_rpc_timeout(self): |
| 248 |
ex = RpcTimeoutError(self.dev, None, 10) |
| 249 |
self.conf.rpc.get_configuration = MagicMock(side_effect=ex) |
| 250 |
self.assertRaises(RpcTimeoutError, self.conf.diff) |
| 251 |
|
| 252 |
def test_config_load(self): |
| 253 |
self.assertRaises(RuntimeError, self.conf.load) |
| 254 |
|
| 255 |
def test_config_load_vargs_len(self): |
| 256 |
self.assertRaises(RuntimeError, self.conf.load, "test.xml") |
| 257 |
|
| 258 |
def test_config_load_len_with_format_set(self): |
| 259 |
self.conf.rpc.load_config = MagicMock(return_value="rpc_contents") |
| 260 |
self.assertEqual(self.conf.load("test.xml", format="set"), "rpc_contents") |
| 261 |
|
| 262 |
def test_config_load_len_with_format_xml(self): |
| 263 |
self.conf.rpc.load_config = MagicMock(return_value="rpc_contents") |
| 264 |
xmldata = """<snmp> |
| 265 |
<community> |
| 266 |
<name>iBGP</name> |
| 267 |
</community> |
| 268 |
</snmp>""" |
| 269 |
|
| 270 |
self.assertEqual(self.conf.load(xmldata, format="xml"), "rpc_contents") |
| 271 |
|
| 272 |
def test_config_load_len_with_format_text(self): |
| 273 |
self.conf.rpc.load_config = MagicMock(return_value="rpc_contents") |
| 274 |
textdata = """policy-options { |
| 275 |
prefix-list TEST1-NETS { |
| 276 |
100.0.0.0/24; |
| 277 |
} |
| 278 |
policy-statement TEST1-NETS { |
| 279 |
term TEST1 { |
| 280 |
from { |
| 281 |
prefix-list TEST1-NETS; |
| 282 |
} |
| 283 |
then accept; |
| 284 |
} |
| 285 |
term REJECT { |
| 286 |
then reject; |
| 287 |
} |
| 288 |
} |
| 289 |
}""" |
| 290 |
|
| 291 |
self.assertEqual(self.conf.load(textdata), "rpc_contents") |
| 292 |
|
| 293 |
def test_config_load_with_format_json(self): |
| 294 |
self.conf.rpc.load_config = MagicMock( |
| 295 |
return_value=etree.fromstring( |
| 296 |
"""<load-configuration-results> |
| 297 |
<ok/> |
| 298 |
</load-configuration-results>""" |
| 299 |
) |
| 300 |
) |
| 301 |
op = self.conf.load("test.json", format="json") |
| 302 |
self.assertEqual(op.tag, "load-configuration-results") |
| 303 |
self.assertEqual(self.conf.rpc.load_config.call_args[1]["format"], "json") |
| 304 |
|
| 305 |
@patch(builtin_string + ".open") |
| 306 |
def test_config_load_with_format_json_from_file_ext(self, mock_open): |
| 307 |
self.conf.rpc.load_config = MagicMock( |
| 308 |
return_value=etree.fromstring( |
| 309 |
"""<load-configuration-results> |
| 310 |
<ok/> |
| 311 |
</load-configuration-results>""" |
| 312 |
) |
| 313 |
) |
| 314 |
op = self.conf.load(path="test.json") |
| 315 |
self.assertEqual(op.tag, "load-configuration-results") |
| 316 |
self.assertEqual(self.conf.rpc.load_config.call_args[1]["format"], "json") |
| 317 |
|
| 318 |
@patch(builtin_string + ".open") |
| 319 |
def test_config_load_update(self, mock_open): |
| 320 |
self.conf.rpc.load_config = MagicMock( |
| 321 |
return_value=etree.fromstring( |
| 322 |
"""<load-configuration-results> |
| 323 |
<ok/> |
| 324 |
</load-configuration-results>""" |
| 325 |
) |
| 326 |
) |
| 327 |
op = self.conf.load(path="test.conf", update=True) |
| 328 |
self.assertEqual(op.tag, "load-configuration-results") |
| 329 |
self.assertEqual(self.conf.rpc.load_config.call_args[1]["format"], "text") |
| 330 |
|
| 331 |
def test_config_load_update_merge_overwrite(self): |
| 332 |
self.assertRaises( |
| 333 |
ValueError, |
| 334 |
self.conf.load, |
| 335 |
path="test.jnpr", |
| 336 |
update=True, |
| 337 |
merge=True, |
| 338 |
overwrite=True, |
| 339 |
) |
| 340 |
|
| 341 |
@patch(builtin_string + ".open") |
| 342 |
def test_config_load_lformat_byext_ValueError(self, mock_open): |
| 343 |
self.conf.rpc.load_config = MagicMock(return_value="rpc_contents") |
| 344 |
self.assertRaises(ValueError, self.conf.load, path="test.jnpr") |
| 345 |
|
| 346 |
def test_config_load_lset_format_ValueError(self): |
| 347 |
self.conf.rpc.load_config = MagicMock(return_value="rpc_contents") |
| 348 |
self.assertRaises( |
| 349 |
ValueError, self.conf.load, "test.xml", format="set", overwrite=True |
| 350 |
) |
| 351 |
|
| 352 |
@patch(builtin_string + ".open") |
| 353 |
@patch("jnpr.junos.utils.config.etree.XML") |
| 354 |
def test_config_load_path_xml(self, mock_etree, mock_open): |
| 355 |
self.conf.dev.Template = MagicMock() |
| 356 |
mock_etree.return_value = "rpc_contents" |
| 357 |
self.conf.rpc.load_config = MagicMock(return_value=mock_etree.return_value) |
| 358 |
self.assertEqual(self.conf.load(path="test.xml"), "rpc_contents") |
| 359 |
|
| 360 |
@patch(builtin_string + ".open") |
| 361 |
def test_config_load_path_text(self, mock_open): |
| 362 |
self.conf.rpc.load_config = MagicMock() |
| 363 |
self.conf.load(path="test.conf") |
| 364 |
self.assertEqual(self.conf.rpc.load_config.call_args[1]["format"], "text") |
| 365 |
|
| 366 |
@patch(builtin_string + ".open") |
| 367 |
def test_config_load_path_set(self, mock_open): |
| 368 |
self.conf.rpc.load_config = MagicMock() |
| 369 |
self.conf.load(path="test.set") |
| 370 |
self.assertEqual(self.conf.rpc.load_config.call_args[1]["action"], "set") |
| 371 |
|
| 372 |
@patch(builtin_string + ".open") |
| 373 |
def test_config_load_try_load_rpcerror(self, mock_open): |
| 374 |
ex = ConfigLoadError( |
| 375 |
rsp=etree.fromstring( |
| 376 |
( |
| 377 |
"""<load-configuration-results> |
| 378 |
<rpc-error> |
| 379 |
<error-severity>error</error-severity> |
| 380 |
<error-message>syntax error</error-message> |
| 381 |
</rpc-error> |
| 382 |
</load-configuration-results>""" |
| 383 |
) |
| 384 |
) |
| 385 |
) |
| 386 |
self.conf.rpc.load_config = MagicMock(side_effect=ex) |
| 387 |
self.assertRaises(ConfigLoadError, self.conf.load, path="config.conf") |
| 388 |
|
| 389 |
@patch(builtin_string + ".open") |
| 390 |
def test_config_load_try_load_rpctimeouterror(self, mock_open): |
| 391 |
ex = RpcTimeoutError(self.dev, None, 10) |
| 392 |
self.conf.rpc.load_config = MagicMock(side_effect=ex) |
| 393 |
self.assertRaises(RpcTimeoutError, self.conf.load, path="config.conf") |
| 394 |
|
| 395 |
@patch(builtin_string + ".open") |
| 396 |
def test_config_try_load_exception(self, mock_open): |
| 397 |
class OtherException(Exception): |
| 398 |
pass |
| 399 |
|
| 400 |
self.conf.rpc.load_config = MagicMock(side_effect=OtherException()) |
| 401 |
self.assertRaises(OtherException, self.conf.load, path="config.conf") |
| 402 |
|
| 403 |
@patch("jnpr.junos.utils.config.etree.XML") |
| 404 |
def test_config_load_template_path(self, mock_etree): |
| 405 |
self.conf.rpc.load_config = MagicMock() |
| 406 |
self.conf.dev.Template = MagicMock() |
| 407 |
self.conf.load(template_path="test.xml") |
| 408 |
self.assertEqual(self.conf.rpc.load_config.call_args[1]["format"], "xml") |
| 409 |
|
| 410 |
def test_config_load_template(self): |
| 411 |
class Temp: |
| 412 |
filename = "abc.xml" |
| 413 |
render = MagicMock(return_value="<test/>") |
| 414 |
|
| 415 |
self.conf.rpc.load_config = MagicMock() |
| 416 |
self.conf.load(template=Temp) |
| 417 |
self.assertEqual(self.conf.rpc.load_config.call_args[1]["format"], "xml") |
| 418 |
|
| 419 |
def test_config_diff_exception(self): |
| 420 |
self.conf.rpc.get_configuration = MagicMock() |
| 421 |
self.assertRaises(ValueError, self.conf.diff, 51) |
| 422 |
self.assertRaises(ValueError, self.conf.diff, -1) |
| 423 |
|
| 424 |
def test_config_lock(self): |
| 425 |
self.conf.rpc.lock_configuration = MagicMock() |
| 426 |
self.assertTrue(self.conf.lock()) |
| 427 |
|
| 428 |
@patch("jnpr.junos.utils.config.JXML.rpc_error") |
| 429 |
def test_config_lock_LockError(self, mock_jxml): |
| 430 |
ex = RpcError(rsp="ok") |
| 431 |
self.conf.rpc.lock_configuration = MagicMock(side_effect=ex) |
| 432 |
self.assertRaises(LockError, self.conf.lock) |
| 433 |
|
| 434 |
@patch("jnpr.junos.utils.config.JXML.rpc_error") |
| 435 |
def test_config_lock_ConnectClosedError(self, mock_jxml): |
| 436 |
ex = ConnectClosedError(dev=self) |
| 437 |
self.conf.rpc.lock_configuration = MagicMock(side_effect=ex) |
| 438 |
self.assertRaises(ConnectClosedError, self.conf.lock) |
| 439 |
|
| 440 |
@patch("jnpr.junos.utils.config.JXML.remove_namespaces") |
| 441 |
def test_config_lock_exception(self, mock_jxml): |
| 442 |
class MyException(Exception): |
| 443 |
xml = "test" |
| 444 |
|
| 445 |
self.conf.rpc.lock_configuration = MagicMock(side_effect=MyException) |
| 446 |
self.assertRaises(LockError, self.conf.lock) |
| 447 |
|
| 448 |
def test_config_unlock(self): |
| 449 |
self.conf.rpc.unlock_configuration = MagicMock() |
| 450 |
self.assertTrue(self.conf.unlock()) |
| 451 |
|
| 452 |
@patch("jnpr.junos.utils.config.JXML.rpc_error") |
| 453 |
def test_config_unlock_LockError(self, mock_jxml): |
| 454 |
ex = RpcError(rsp="ok") |
| 455 |
self.conf.rpc.unlock_configuration = MagicMock(side_effect=ex) |
| 456 |
self.assertRaises(UnlockError, self.conf.unlock) |
| 457 |
|
| 458 |
@patch("jnpr.junos.utils.config.JXML.rpc_error") |
| 459 |
def test_config_unlock_ConnectClosedError(self, mock_jxml): |
| 460 |
ex = ConnectClosedError(dev=self) |
| 461 |
self.conf.rpc.unlock_configuration = MagicMock(side_effect=ex) |
| 462 |
self.assertRaises(ConnectClosedError, self.conf.unlock) |
| 463 |
|
| 464 |
@patch("jnpr.junos.utils.config.JXML.remove_namespaces") |
| 465 |
def test_config_unlock_exception(self, mock_jxml): |
| 466 |
class MyException(Exception): |
| 467 |
xml = "test" |
| 468 |
|
| 469 |
self.conf.rpc.unlock_configuration = MagicMock(side_effect=MyException) |
| 470 |
self.assertRaises(UnlockError, self.conf.unlock) |
| 471 |
|
| 472 |
def test_config_rollback(self): |
| 473 |
self.conf.rpc.load_configuration = MagicMock() |
| 474 |
self.assertTrue(self.conf.rollback()) |
| 475 |
|
| 476 |
def test_config_rollback_exception(self): |
| 477 |
self.conf.rpc.load_configuration = MagicMock() |
| 478 |
self.assertRaises(ValueError, self.conf.rollback, 51) |
| 479 |
self.assertRaises(ValueError, self.conf.rollback, -1) |
| 480 |
|
| 481 |
@patch("jnpr.junos.Device.execute") |
| 482 |
def test_rescue_action_save(self, mock_exec): |
| 483 |
self.dev.request_save_rescue_configuration = MagicMock() |
| 484 |
self.assertTrue(self.conf.rescue("save")) |
| 485 |
|
| 486 |
@patch("jnpr.junos.Device.execute") |
| 487 |
def test_rescue_action_get_exception(self, mock_exec): |
| 488 |
self.dev.rpc.get_rescue_information = MagicMock(side_effect=Exception) |
| 489 |
self.assertTrue(self.conf.rescue("get") is None) |
| 490 |
|
| 491 |
@patch("jnpr.junos.Device.execute") |
| 492 |
def test_rescue_action_get(self, mock_exec): |
| 493 |
self.dev.rpc.get_rescue_information = MagicMock() |
| 494 |
self.dev.rpc.get_rescue_information.return_value = 1 |
| 495 |
self.assertEqual(self.conf.rescue("get", format="xml"), 1) |
| 496 |
|
| 497 |
@patch("jnpr.junos.Device.execute") |
| 498 |
def test_rescue_action_delete(self, mock_exec): |
| 499 |
self.dev.rpc.request_delete_rescue_configuration = MagicMock() |
| 500 |
self.assertTrue(self.conf.rescue("delete")) |
| 501 |
|
| 502 |
@patch("jnpr.junos.Device.execute") |
| 503 |
def test_rescue_action_reload(self, mock_exec): |
| 504 |
self.dev.rpc.load_configuration = MagicMock() |
| 505 |
self.dev.rpc.load_configuration.return_value = True |
| 506 |
self.assertTrue(self.conf.rescue("reload")) |
| 507 |
|
| 508 |
@patch("jnpr.junos.Device.execute") |
| 509 |
def test_rescue_action_reload_exception(self, mock_exec): |
| 510 |
self.dev.rpc.load_configuration = MagicMock(side_effect=Exception) |
| 511 |
self.assertFalse(self.conf.rescue("reload")) |
| 512 |
|
| 513 |
@patch("jnpr.junos.Device.execute") |
| 514 |
def test_rescue_action_unsupported_action(self, mock_exec): |
| 515 |
self.assertRaises(ValueError, self.conf.rescue, "abc") |
| 516 |
|
| 517 |
def test_config_load_lset_from_rexp_xml(self): |
| 518 |
self.conf.rpc.load_config = MagicMock() |
| 519 |
conf = """<snmp><name>iBGP</name></snmp>""" |
| 520 |
self.conf.load(conf) |
| 521 |
self.assertEqual(self.conf.rpc.load_config.call_args[1]["format"], "xml") |
| 522 |
|
| 523 |
def test_config_load_lset_from_rexp_json(self): |
| 524 |
self.conf.rpc.load_config = MagicMock() |
| 525 |
conf = """{ |
| 526 |
"configuration" : { |
| 527 |
"system" : { |
| 528 |
"services" : { |
| 529 |
"telnet" : [null] |
| 530 |
} |
| 531 |
} |
| 532 |
} |
| 533 |
}""" |
| 534 |
self.conf.load(conf) |
| 535 |
self.assertEqual(self.conf.rpc.load_config.call_args[1]["format"], "json") |
| 536 |
|
| 537 |
def test_config_load_lset_from_rexp_set(self): |
| 538 |
self.conf.rpc.load_config = MagicMock() |
| 539 |
conf = """set system domain-name englab.nitin.net""" |
| 540 |
self.conf.load(conf) |
| 541 |
self.assertEqual(self.conf.rpc.load_config.call_args[1]["action"], "set") |
| 542 |
|
| 543 |
def test_config_load_lset_from_rexp_set_delete(self): |
| 544 |
self.conf.rpc.load_config = MagicMock() |
| 545 |
conf = """delete snmp""" |
| 546 |
self.conf.load(conf) |
| 547 |
self.assertEqual(self.conf.rpc.load_config.call_args[1]["action"], "set") |
| 548 |
|
| 549 |
def test_config_load_lset_from_rexp_set_rename(self): |
| 550 |
self.conf.rpc.load_config = MagicMock() |
| 551 |
conf = """rename firewall family inet filter f1 to filter f2""" |
| 552 |
self.conf.load(conf) |
| 553 |
self.assertEqual(self.conf.rpc.load_config.call_args[1]["action"], "set") |
| 554 |
|
| 555 |
def test_config_load_lset_from_rexp_insert(self): |
| 556 |
self.conf.rpc.load_config = MagicMock() |
| 557 |
conf = """insert policy-options policy-statement hop term 10 after 9""" |
| 558 |
self.conf.load(conf) |
| 559 |
self.assertEqual(self.conf.rpc.load_config.call_args[1]["action"], "set") |
| 560 |
|
| 561 |
def test_config_load_lset_from_rexp_set_activate(self): |
| 562 |
self.conf.rpc.load_config = MagicMock() |
| 563 |
conf = """activate system services ftp""" |
| 564 |
self.conf.load(conf) |
| 565 |
self.assertEqual(self.conf.rpc.load_config.call_args[1]["action"], "set") |
| 566 |
|
| 567 |
def test_config_load_lset_from_rexp_set_deactivate(self): |
| 568 |
self.conf.rpc.load_config = MagicMock() |
| 569 |
conf = """deactivate system services ftp""" |
| 570 |
self.conf.load(conf) |
| 571 |
self.assertEqual(self.conf.rpc.load_config.call_args[1]["action"], "set") |
| 572 |
|
| 573 |
def test_config_load_lset_from_rexp_set_annotate(self): |
| 574 |
self.conf.rpc.load_config = MagicMock() |
| 575 |
conf = """annotate system \"Annotation test\"""" |
| 576 |
self.conf.load(conf) |
| 577 |
self.assertEqual(self.conf.rpc.load_config.call_args[1]["action"], "set") |
| 578 |
|
| 579 |
def test_config_load_lset_from_rexp_set_copy(self): |
| 580 |
self.conf.rpc.load_config = MagicMock() |
| 581 |
conf = """copy firewall family inet filter f1 to filter f2""" |
| 582 |
self.conf.load(conf) |
| 583 |
self.assertEqual(self.conf.rpc.load_config.call_args[1]["action"], "set") |
| 584 |
|
| 585 |
def test_config_load_lset_from_rexp_set_protect(self): |
| 586 |
self.conf.rpc.load_config = MagicMock() |
| 587 |
conf = """protect system services""" |
| 588 |
self.conf.load(conf) |
| 589 |
self.assertEqual(self.conf.rpc.load_config.call_args[1]["action"], "set") |
| 590 |
|
| 591 |
def test_config_load_lset_from_rexp_set_unprotect(self): |
| 592 |
self.conf.rpc.load_config = MagicMock() |
| 593 |
conf = """unprotect system services""" |
| 594 |
self.conf.load(conf) |
| 595 |
self.assertEqual(self.conf.rpc.load_config.call_args[1]["action"], "set") |
| 596 |
|
| 597 |
def test_config_load_lset_from_rexp_conf(self): |
| 598 |
self.conf.rpc.load_config = MagicMock() |
| 599 |
conf = """ |
| 600 |
snmp { |
| 601 |
location USA; |
| 602 |
community iBGP { |
| 603 |
authorization read-only; |
| 604 |
} |
| 605 |
}""" |
| 606 |
self.conf.load(conf) |
| 607 |
self.assertEqual(self.conf.rpc.load_config.call_args[1]["format"], "text") |
| 608 |
|
| 609 |
def test_config_load_lset_from_rexp_conf_replace_tag(self): |
| 610 |
self.conf.rpc.load_config = MagicMock() |
| 611 |
conf = """replace: |
| 612 |
snmp { |
| 613 |
location USA; |
| 614 |
community iBGP { |
| 615 |
authorization read-only; |
| 616 |
} |
| 617 |
}""" |
| 618 |
self.conf.load(conf) |
| 619 |
self.assertEqual(self.conf.rpc.load_config.call_args[1]["format"], "text") |
| 620 |
self.assertEqual(self.conf.rpc.load_config.call_args[1]["action"], "replace") |
| 621 |
|
| 622 |
def test_config_load_lset_from_rexp_error(self): |
| 623 |
self.conf.rpc.load_config = MagicMock() |
| 624 |
conf = """test>""" |
| 625 |
self.assertRaises(RuntimeError, self.conf.load, conf) |
| 626 |
|
| 627 |
def test_load_merge_true(self): |
| 628 |
self.conf.rpc.load_config = MagicMock() |
| 629 |
conf = """ |
| 630 |
snmp { |
| 631 |
location USA; |
| 632 |
community iBGP { |
| 633 |
authorization read-only; |
| 634 |
} |
| 635 |
}""" |
| 636 |
self.conf.load(conf, merge=True) |
| 637 |
self.assertFalse("action" in self.conf.rpc.load_config.call_args[1]) |
| 638 |
|
| 639 |
def test_commit_RpcTimeoutError(self): |
| 640 |
ex = RpcTimeoutError(self.dev, None, 10) |
| 641 |
self.dev.rpc.commit_configuration = MagicMock(side_effect=ex) |
| 642 |
self.assertRaises(RpcTimeoutError, self.conf.commit) |
| 643 |
|
| 644 |
def test_commit_check_RpcTimeoutError(self): |
| 645 |
ex = RpcTimeoutError(self.dev, None, 10) |
| 646 |
self.dev.rpc.commit_configuration = MagicMock(side_effect=ex) |
| 647 |
self.assertRaises(RpcTimeoutError, self.conf.commit_check) |
| 648 |
|
| 649 |
def test_commit_configuration_multi_rpc_error(self): |
| 650 |
self.dev._conn.rpc = MagicMock(side_effect=self._mock_manager) |
| 651 |
try: |
| 652 |
self.dev.rpc.commit_configuration() |
| 653 |
except Exception as ex: |
| 654 |
self.assertTrue(isinstance(ex, RpcError)) |
| 655 |
self.assertEqual( |
| 656 |
ex.message, |
| 657 |
"error: interface-range 'axp' is not defined\n" |
| 658 |
"error: interface-ranges expansion failed", |
| 659 |
) |
| 660 |
self.assertEqual( |
| 661 |
ex.errs, |
| 662 |
[ |
| 663 |
{ |
| 664 |
"source": None, |
| 665 |
"message": "interface-range 'axp' is not defined", |
| 666 |
"bad_element": None, |
| 667 |
"severity": "error", |
| 668 |
"edit_path": None, |
| 669 |
}, |
| 670 |
{ |
| 671 |
"source": None, |
| 672 |
"message": "interface-ranges expansion failed", |
| 673 |
"bad_element": None, |
| 674 |
"severity": "error", |
| 675 |
"edit_path": None, |
| 676 |
}, |
| 677 |
], |
| 678 |
) |
| 679 |
|
| 680 |
@patch("jnpr.junos.utils.config.Config.lock") |
| 681 |
@patch("jnpr.junos.utils.config.Config.unlock") |
| 682 |
def test_config_mode_exclusive(self, mock_unlock, mock_lock): |
| 683 |
with Config(self.dev, mode="exclusive") as conf: |
| 684 |
conf.rpc.load_config = MagicMock() |
| 685 |
conf.load("conf", format="set") |
| 686 |
self.assertTrue(mock_lock.called and mock_unlock.called) |
| 687 |
|
| 688 |
@patch("jnpr.junos.Device.execute") |
| 689 |
def test_config_mode_batch(self, mock_exec): |
| 690 |
self.dev.rpc.open_configuration = MagicMock() |
| 691 |
with Config(self.dev, mode="batch") as conf: |
| 692 |
conf.load("conf", format="set") |
| 693 |
self.dev.rpc.open_configuration.assert_called_with(batch=True) |
| 694 |
|
| 695 |
@patch("jnpr.junos.Device.execute") |
| 696 |
def test_config_mode_private(self, mock_exec): |
| 697 |
self.dev.rpc.open_configuration = MagicMock() |
| 698 |
with Config(self.dev, mode="private") as conf: |
| 699 |
conf.load("conf", format="set") |
| 700 |
self.dev.rpc.open_configuration.assert_called_with(private=True) |
| 701 |
|
| 702 |
@patch("jnpr.junos.Device.execute") |
| 703 |
def test_config_mode_dynamic(self, mock_exec): |
| 704 |
self.dev.rpc.open_configuration = MagicMock() |
| 705 |
with Config(self.dev, mode="dynamic") as conf: |
| 706 |
conf.load("conf", format="set") |
| 707 |
self.dev.rpc.open_configuration.assert_called_with(dynamic=True) |
| 708 |
|
| 709 |
@patch("jnpr.junos.Device.execute") |
| 710 |
def test_config_mode_ephemeral_default(self, mock_exec): |
| 711 |
self.dev.rpc.open_configuration = MagicMock() |
| 712 |
with Config(self.dev, mode="ephemeral") as conf: |
| 713 |
conf.load("conf", format="set") |
| 714 |
self.dev.rpc.open_configuration.assert_called_with(ephemeral=True) |
| 715 |
|
| 716 |
@patch("jnpr.junos.Device.execute") |
| 717 |
def test_config_mode_ephemeral_instance(self, mock_exec): |
| 718 |
self.dev.rpc.open_configuration = MagicMock() |
| 719 |
with Config(self.dev, mode="ephemeral", ephemeral_instance="xyz") as conf: |
| 720 |
conf.load("conf", format="set") |
| 721 |
self.dev.rpc.open_configuration.assert_called_with(ephemeral_instance="xyz") |
| 722 |
|
| 723 |
def test_config_unsupported_kwargs(self): |
| 724 |
self.dev.rpc.open_configuration = MagicMock() |
| 725 |
try: |
| 726 |
with Config(self.dev, mode="ephemeral", xyz="xyz") as conf: |
| 727 |
conf.load("conf", format="set") |
| 728 |
except Exception as ex: |
| 729 |
self.assertEqual(str(ex), "Unsupported argument provided to Config class") |
| 730 |
|
| 731 |
@patch("jnpr.junos.Device.execute") |
| 732 |
def test_config_mode_close_configuration_ex(self, mock_exec): |
| 733 |
self.dev.rpc.open_configuration = MagicMock() |
| 734 |
ex = RpcError(rsp="ok") |
| 735 |
ex.message = "Configuration database is not open" |
| 736 |
self.dev.rpc.close_configuration = MagicMock(side_effect=ex) |
| 737 |
try: |
| 738 |
with Config(self.dev, mode="batch") as conf: |
| 739 |
conf.load("conf", format="set") |
| 740 |
except Exception as ex: |
| 741 |
self.assertTrue(isinstance(ex, RpcError)) |
| 742 |
self.assertTrue(self.dev.rpc.close_configuration.called) |
| 743 |
|
| 744 |
def test_config_mode_undefined(self): |
| 745 |
try: |
| 746 |
with Config(self.dev, mode="unknown") as conf: |
| 747 |
conf.load("conf", format="set") |
| 748 |
except Exception as ex: |
| 749 |
self.assertTrue(isinstance(ex, ValueError)) |
| 750 |
|
| 751 |
@patch("jnpr.junos.Device.execute") |
| 752 |
@patch("jnpr.junos.utils.config.warnings") |
| 753 |
def test_config_mode_batch_open_configuration_ex(self, mock_warnings, mock_exec): |
| 754 |
rpc_xml = """ |
| 755 |
<rpc-error> |
| 756 |
<error-severity>warning</error-severity> |
| 757 |
<error-info><bad-element>bgp</bad-element></error-info> |
| 758 |
<error-message>syntax error</error-message> |
| 759 |
</rpc-error> |
| 760 |
""" |
| 761 |
rsp = etree.XML(rpc_xml) |
| 762 |
obj = RpcError(rsp=rsp) |
| 763 |
self.dev.rpc.open_configuration = MagicMock(side_effect=obj) |
| 764 |
with Config(self.dev, mode="batch") as conf: |
| 765 |
conf.load("conf", format="set") |
| 766 |
self.dev.rpc.open_configuration.assert_called_with(batch=True) |
| 767 |
|
| 768 |
@patch("jnpr.junos.Device.execute") |
| 769 |
@patch("jnpr.junos.utils.config.warnings") |
| 770 |
def test_config_mode_private_open_configuration_ex(self, mock_warnings, mock_exec): |
| 771 |
rpc_xml = """ |
| 772 |
<rpc-error> |
| 773 |
<error-severity>warning</error-severity> |
| 774 |
<error-info><bad-element>bgp</bad-element></error-info> |
| 775 |
<error-message>syntax error</error-message> |
| 776 |
</rpc-error> |
| 777 |
""" |
| 778 |
rsp = etree.XML(rpc_xml) |
| 779 |
obj = RpcError(rsp=rsp) |
| 780 |
self.dev.rpc.open_configuration = MagicMock(side_effect=obj) |
| 781 |
with Config(self.dev, mode="private") as conf: |
| 782 |
conf.load("conf", format="set") |
| 783 |
self.dev.rpc.open_configuration.assert_called_with(private=True) |
| 784 |
|
| 785 |
def test__enter__private_exception_RpcTimeoutError(self): |
| 786 |
ex = RpcTimeoutError(self.dev, None, 10) |
| 787 |
self.conf.rpc.open_configuration = MagicMock(side_effect=ex) |
| 788 |
self.assertRaises( |
| 789 |
RpcTimeoutError, Config.__enter__, Config(self.dev, mode="private") |
| 790 |
) |
| 791 |
|
| 792 |
def test__enter__private_exception_RpcError(self): |
| 793 |
rpc_xml = """<rpc-error> |
| 794 |
<error-severity>error</error-severity> |
| 795 |
<error-message>syntax error</error-message> |
| 796 |
</rpc-error>""" |
| 797 |
rsp = etree.XML(rpc_xml) |
| 798 |
self.conf.rpc.open_configuration = MagicMock(side_effect=RpcError(rsp=rsp)) |
| 799 |
self.assertRaises(RpcError, Config.__enter__, Config(self.dev, mode="private")) |
| 800 |
|
| 801 |
def test__enter__dyanamic_exception_RpcError(self): |
| 802 |
rpc_xml = """<rpc-error> |
| 803 |
<error-severity>error</error-severity> |
| 804 |
<error-message>syntax error</error-message> |
| 805 |
</rpc-error>""" |
| 806 |
rsp = etree.XML(rpc_xml) |
| 807 |
self.conf.rpc.open_configuration = MagicMock(side_effect=RpcError(rsp=rsp)) |
| 808 |
self.assertRaises(RpcError, Config.__enter__, Config(self.dev, mode="dynamic")) |
| 809 |
|
| 810 |
def test__enter__batch_exception_RpcTimeoutError(self): |
| 811 |
ex = RpcTimeoutError(self.dev, None, 10) |
| 812 |
self.conf.rpc.open_configuration = MagicMock(side_effect=ex) |
| 813 |
self.assertRaises( |
| 814 |
RpcTimeoutError, Config.__enter__, Config(self.dev, mode="batch") |
| 815 |
) |
| 816 |
|
| 817 |
def test__enter__batch_exception_RpcError(self): |
| 818 |
rpc_xml = """<rpc-error> |
| 819 |
<error-severity>error</error-severity> |
| 820 |
<error-message>syntax error</error-message> |
| 821 |
</rpc-error>""" |
| 822 |
rsp = etree.XML(rpc_xml) |
| 823 |
self.conf.rpc.open_configuration = MagicMock(side_effect=RpcError(rsp=rsp)) |
| 824 |
self.assertRaises(RpcError, Config.__enter__, Config(self.dev, mode="batch")) |
| 825 |
|
| 826 |
def test_config_load_url(self): |
| 827 |
self.conf.rpc.load_config = MagicMock() |
| 828 |
self.conf.load(url="/var/home/user/golden.conf") |
| 829 |
self.assertEqual( |
| 830 |
self.conf.rpc.load_config.call_args[1]["url"], "/var/home/user/golden.conf" |
| 831 |
) |
| 832 |
|
| 833 |
@patch("jnpr.junos.Device.execute") |
| 834 |
def test_load_config_patch(self, mock_exec): |
| 835 |
conf = """[edit system] |
| 836 |
- host-name pakzds904; |
| 837 |
+ host-name pakzds904_set; |
| 838 |
""" |
| 839 |
self.conf.load(conf, format="text", patch=True) |
| 840 |
self.assertEqual(mock_exec.call_args[0][0].tag, "load-configuration") |
| 841 |
self.assertEqual( |
| 842 |
mock_exec.call_args[0][0].attrib, {"format": "text", "action": "patch"} |
| 843 |
) |
| 844 |
|
| 845 |
@patch("jnpr.junos.Device.execute") |
| 846 |
def test_load_config_text(self, mock_exec): |
| 847 |
textdata = """policy-options { |
| 848 |
prefix-list TEST1-NETS { |
| 849 |
100.0.0.0/24; |
| 850 |
} |
| 851 |
policy-statement TEST1-NETS { |
| 852 |
term TEST1 { |
| 853 |
from { |
| 854 |
prefix-list TEST1-NETS; |
| 855 |
} |
| 856 |
then accept; |
| 857 |
} |
| 858 |
term REJECT { |
| 859 |
then reject; |
| 860 |
} |
| 861 |
} |
| 862 |
}""" |
| 863 |
self.conf.load(textdata, overwrite=True) |
| 864 |
self.assertEqual(mock_exec.call_args[0][0].tag, "load-configuration") |
| 865 |
self.assertEqual( |
| 866 |
mock_exec.call_args[0][0].getchildren()[0].tag, "configuration-text" |
| 867 |
) |
| 868 |
self.assertEqual( |
| 869 |
mock_exec.call_args[0][0].attrib, {"format": "text", "action": "override"} |
| 870 |
) |
| 871 |
|
| 872 |
def _read_file(self, fname): |
| 873 |
fpath = os.path.join(os.path.dirname(__file__), "rpc-reply", fname) |
| 874 |
foo = open(fpath).read() |
| 875 |
|
| 876 |
# specific to multi rpc error |
| 877 |
if fname == "commit-configuration.xml": |
| 878 |
raw = etree.XML(foo) |
| 879 |
obj = RPCReply(raw) |
| 880 |
obj.parse() |
| 881 |
raise RPCError(etree.XML(foo), errs=obj._errors) |
| 882 |
|
| 883 |
def _mock_manager(self, *args, **kwargs): |
| 884 |
if kwargs: |
| 885 |
device_params = kwargs["device_params"] |
| 886 |
device_handler = make_device_handler(device_params) |
| 887 |
session = SSHSession(device_handler) |
| 888 |
return Manager(session, device_handler) |
| 889 |
|
| 890 |
elif args: |
| 891 |
return self._read_file(args[0].tag + ".xml") |