| 1 |
__author__ = "Stacy Smith" |
| 2 |
__credits__ = "Jeremy Schulman, Nitin Kumar" |
| 3 |
|
| 4 |
import unittest |
| 5 |
import nose2 |
| 6 |
from unittest.mock import patch, MagicMock |
| 7 |
import os |
| 8 |
from lxml import etree |
| 9 |
|
| 10 |
from jnpr.junos import Device |
| 11 |
from jnpr.junos.exception import RpcError |
| 12 |
|
| 13 |
from ncclient.manager import Manager, make_device_handler |
| 14 |
from ncclient.transport import SSHSession |
| 15 |
|
| 16 |
|
| 17 |
class TestCurrentRe(unittest.TestCase): |
| 18 |
@patch("ncclient.manager.connect") |
| 19 |
def setUp(self, mock_connect): |
| 20 |
mock_connect.side_effect = self._mock_manager_setup |
| 21 |
self.dev = Device( |
| 22 |
host="1.1.1.1", user="rick", password="password123", gather_facts=False |
| 23 |
) |
| 24 |
self.dev.open() |
| 25 |
|
| 26 |
@patch("jnpr.junos.Device.execute") |
| 27 |
def test_current_re_fact(self, mock_execute): |
| 28 |
mock_execute.side_effect = self._mock_manager_current_re |
| 29 |
self.assertEqual( |
| 30 |
self.dev.facts["current_re"], |
| 31 |
["re0", "master", "node", "fwdd", "member", "pfem"], |
| 32 |
) |
| 33 |
|
| 34 |
@patch("jnpr.junos.Device.execute") |
| 35 |
def test_current_re_fact_empty(self, mock_execute): |
| 36 |
mock_execute.side_effect = self._mock_manager_current_re_empty |
| 37 |
self.assertEqual(self.dev.facts["current_re"], None) |
| 38 |
|
| 39 |
@patch("jnpr.junos.Device.execute") |
| 40 |
def test_current_re_fact_srx_cluster_primary(self, mock_execute): |
| 41 |
mock_execute.side_effect = self._mock_manager_current_re_srx_primary |
| 42 |
self.dev.facts._cache["srx_cluster_id"] = "15" |
| 43 |
self.assertEqual(self.dev.facts["current_re"], ["node0", "primary"]) |
| 44 |
|
| 45 |
@patch("jnpr.junos.Device.execute") |
| 46 |
def test_current_re_fact_srx_cluster_primary_id_255(self, mock_execute): |
| 47 |
mock_execute.side_effect = self._mock_manager_current_re_srx_primary |
| 48 |
self.dev.facts._cache["srx_cluster_id"] = "255" |
| 49 |
self.assertEqual(self.dev.facts["current_re"], ["node0", "primary"]) |
| 50 |
|
| 51 |
@patch("jnpr.junos.Device.execute") |
| 52 |
def test_current_re_fact_srx_cluster_primary_id_16(self, mock_execute): |
| 53 |
mock_execute.side_effect = self._mock_manager_current_re_srx_primary |
| 54 |
self.dev.facts._cache["srx_cluster_id"] = "16" |
| 55 |
print(self.dev.facts._cache["srx_cluster_id"]) |
| 56 |
self.assertEqual(self.dev.facts["current_re"], ["node0"]) |
| 57 |
|
| 58 |
@patch("jnpr.junos.Device.execute") |
| 59 |
def test_current_re_fact_srx_cluster_primary_id_31(self, mock_execute): |
| 60 |
mock_execute.side_effect = self._mock_manager_current_re_srx_primary |
| 61 |
self.dev.facts._cache["srx_cluster_id"] = "31" |
| 62 |
self.assertEqual(self.dev.facts["current_re"], ["node0", "primary"]) |
| 63 |
|
| 64 |
@patch("jnpr.junos.Device.execute") |
| 65 |
def test_current_re_fact_srx_cluster_secondary(self, mock_execute): |
| 66 |
mock_execute.side_effect = self._mock_manager_current_re_srx_secondary |
| 67 |
self.dev.facts._cache["srx_cluster_id"] = "15" |
| 68 |
self.assertEqual(self.dev.facts["current_re"], ["node1"]) |
| 69 |
|
| 70 |
@patch("jnpr.junos.Device.execute") |
| 71 |
def test_current_re_fact_srx_cluster_index_error(self, mock_execute): |
| 72 |
mock_execute.side_effect = self._mock_manager_current_re_srx_index_err |
| 73 |
self.dev.facts._cache["srx_cluster_id"] = "15" |
| 74 |
self.assertEqual(self.dev.facts["current_re"], None) |
| 75 |
|
| 76 |
@patch("jnpr.junos.Device.execute") |
| 77 |
def test_current_re_fact_jdm(self, mock_execute): |
| 78 |
mock_execute.side_effect = self._mock_manager_current_re_jdm |
| 79 |
self.assertEqual(self.dev.facts["current_re"], ["server0"]) |
| 80 |
|
| 81 |
@patch("jnpr.junos.Device.execute") |
| 82 |
def test_current_re_fact_rpc_error(self, mock_execute): |
| 83 |
mock_execute.side_effect = self._mock_manager_current_re_rpc_error |
| 84 |
self.assertEqual(self.dev.facts["current_re"], None) |
| 85 |
|
| 86 |
def _read_file(self, fname): |
| 87 |
from ncclient.xml_ import NCElement |
| 88 |
|
| 89 |
fpath = os.path.join(os.path.dirname(__file__), "rpc-reply", fname) |
| 90 |
foo = open(fpath).read() |
| 91 |
|
| 92 |
rpc_reply = NCElement( |
| 93 |
foo, self.dev._conn._device_handler.transform_reply() |
| 94 |
)._NCElement__doc[0] |
| 95 |
return rpc_reply |
| 96 |
|
| 97 |
def _mock_manager_setup(self, *args, **kwargs): |
| 98 |
if kwargs: |
| 99 |
device_params = kwargs["device_params"] |
| 100 |
device_handler = make_device_handler(device_params) |
| 101 |
session = SSHSession(device_handler) |
| 102 |
return Manager(session, device_handler) |
| 103 |
|
| 104 |
def _mock_manager_current_re(self, *args, **kwargs): |
| 105 |
if args: |
| 106 |
if ( |
| 107 |
args[0].tag == "file-show" |
| 108 |
and args[0].xpath("filename")[0].text == "/usr/share/cevo/cevo_version" |
| 109 |
): |
| 110 |
raise RpcError |
| 111 |
else: |
| 112 |
return self._read_file("current_re_" + args[0].tag + ".xml") |
| 113 |
|
| 114 |
def _mock_manager_current_re_empty(self, *args, **kwargs): |
| 115 |
if args: |
| 116 |
if ( |
| 117 |
args[0].tag == "file-show" |
| 118 |
and args[0].xpath("filename")[0].text == "/usr/share/cevo/cevo_version" |
| 119 |
): |
| 120 |
raise RpcError |
| 121 |
else: |
| 122 |
return self._read_file("current_re_empty_" + args[0].tag + ".xml") |
| 123 |
|
| 124 |
def _mock_manager_current_re_srx_primary(self, *args, **kwargs): |
| 125 |
if args: |
| 126 |
if ( |
| 127 |
args[0].tag == "file-show" |
| 128 |
and args[0].xpath("filename")[0].text == "/usr/share/cevo/cevo_version" |
| 129 |
): |
| 130 |
raise RpcError |
| 131 |
else: |
| 132 |
return self._read_file("current_re_srx_primary_" + args[0].tag + ".xml") |
| 133 |
|
| 134 |
def _mock_manager_current_re_srx_secondary(self, *args, **kwargs): |
| 135 |
if args: |
| 136 |
if ( |
| 137 |
args[0].tag == "file-show" |
| 138 |
and args[0].xpath("filename")[0].text == "/usr/share/cevo/cevo_version" |
| 139 |
): |
| 140 |
raise RpcError |
| 141 |
else: |
| 142 |
return self._read_file( |
| 143 |
"current_re_srx_secondary_" + args[0].tag + ".xml" |
| 144 |
) |
| 145 |
|
| 146 |
def _mock_manager_current_re_srx_index_err(self, *args, **kwargs): |
| 147 |
if args: |
| 148 |
if ( |
| 149 |
args[0].tag == "file-show" |
| 150 |
and args[0].xpath("filename")[0].text == "/usr/share/cevo/cevo_version" |
| 151 |
): |
| 152 |
raise RpcError |
| 153 |
else: |
| 154 |
return self._read_file( |
| 155 |
"current_re_srx_index_err_" + args[0].tag + ".xml" |
| 156 |
) |
| 157 |
|
| 158 |
def _mock_manager_current_re_jdm(self, *args, **kwargs): |
| 159 |
if args: |
| 160 |
if ( |
| 161 |
args[0].tag == "file-show" |
| 162 |
and args[0].xpath("filename")[0].text == "/usr/share/cevo/cevo_version" |
| 163 |
): |
| 164 |
raise RpcError |
| 165 |
elif args[0].tag == "get-interface-information": |
| 166 |
raise RpcError() |
| 167 |
else: |
| 168 |
return self._read_file("current_re_jdm_" + args[0].tag + ".xml") |
| 169 |
|
| 170 |
def _mock_manager_current_re_rpc_error(self, *args, **kwargs): |
| 171 |
if args: |
| 172 |
raise RpcError() |