| 1 |
try: |
| 2 |
import unittest2 as unittest |
| 3 |
except ImportError: |
| 4 |
import unittest |
| 5 |
import nose2 |
| 6 |
from unittest.mock import patch, MagicMock, call |
| 7 |
from jnpr.junos.exception import FactLoopError |
| 8 |
|
| 9 |
from jnpr.junos import Device |
| 10 |
|
| 11 |
from ncclient.manager import Manager, make_device_handler |
| 12 |
from ncclient.transport import SSHSession |
| 13 |
|
| 14 |
__author__ = "Stacy Smith" |
| 15 |
__credits__ = "Jeremy Schulman, Nitin Kumar" |
| 16 |
|
| 17 |
|
| 18 |
class TestFactCache(unittest.TestCase): |
| 19 |
@patch("ncclient.manager.connect") |
| 20 |
def setUp(self, mock_connect): |
| 21 |
mock_connect.side_effect = self._mock_manager_setup |
| 22 |
self.dev = Device(host="1.1.1.1", user="rick", password="password123") |
| 23 |
self.dev.open() |
| 24 |
|
| 25 |
def test_factcache_unknown_fact(self): |
| 26 |
with self.assertRaises(KeyError): |
| 27 |
unknown = self.dev.facts["unknown"] |
| 28 |
|
| 29 |
def test_factcache_fact_loop(self): |
| 30 |
# The personality fact calls the |
| 31 |
# model fact. |
| 32 |
# Change the callback for the model |
| 33 |
# fact to be the same as the personality fact |
| 34 |
# in order to induce a fact loop. |
| 35 |
tmp = self.dev.facts._callbacks["model"] |
| 36 |
self.dev.facts._callbacks["model"] = self.dev.facts._callbacks["personality"] |
| 37 |
# Now, trying to fetch the personality |
| 38 |
# fact should cause a FactLoopError |
| 39 |
with self.assertRaises(FactLoopError): |
| 40 |
personality = self.dev.facts["personality"] |
| 41 |
self.dev.facts._callbacks["model"] = tmp # To clear FactLoopError |
| 42 |
|
| 43 |
def test_factcache_return_unexpected_fact(self): |
| 44 |
# Create a callback for the foo fact. |
| 45 |
self.dev.facts._callbacks["foo"] = get_foo_bar_fact |
| 46 |
# Now, trying to access the foo fact should cause a |
| 47 |
# RunTimeError because the bar fact is also unexpectedly provided |
| 48 |
with self.assertRaises(RuntimeError): |
| 49 |
foo = self.dev.facts["foo"] |
| 50 |
|
| 51 |
@patch("jnpr.junos.factcache.warnings") |
| 52 |
def test_factcache_nonmatching_old_and_new_fact(self, mock_warn): |
| 53 |
# Set fact style to 'both' |
| 54 |
self.dev._fact_style = "both" |
| 55 |
# Create a callback for the foo fact. |
| 56 |
self.dev.facts._callbacks["foo"] = get_foo_fact |
| 57 |
# Cache the new-style foo fact |
| 58 |
self.dev.facts._cache["foo"] = "foo" |
| 59 |
# Set the old-style foo fact to a different value |
| 60 |
self.dev._ofacts["foo"] = "bar" |
| 61 |
# Now, trying to access the foo fact should cause a |
| 62 |
# RunTimeWarning because the values of the new and old-style facts |
| 63 |
# do not match |
| 64 |
foo = self.dev.facts["foo"] |
| 65 |
mock_warn.assert_has_calls( |
| 66 |
[ |
| 67 |
call.warn( |
| 68 |
"New and old-style facts do not match for the foo fact.\n" |
| 69 |
" New-style value: foo\n Old-style value: bar\n", |
| 70 |
RuntimeWarning, |
| 71 |
) |
| 72 |
] |
| 73 |
) |
| 74 |
|
| 75 |
def test_factcache_fail_to_return_expected_fact(self): |
| 76 |
# Create a callback for the foo fact. |
| 77 |
self.dev.facts._callbacks["foo"] = get_bar_fact |
| 78 |
self.dev.facts._callbacks["bar"] = get_bar_fact |
| 79 |
# Now, trying to access the foo fact should cause a |
| 80 |
# RunTimeError because the foo fact is not provided |
| 81 |
with self.assertRaises(RuntimeError): |
| 82 |
foo = self.dev.facts["foo"] |
| 83 |
|
| 84 |
def test_factcache_delete_fact(self): |
| 85 |
# Create a callback for the foo fact. |
| 86 |
self.dev.facts._callbacks["foo"] = get_foo_fact |
| 87 |
foo = self.dev.facts["foo"] |
| 88 |
# Now, trying to delete the foo fact should cause a |
| 89 |
# RunTimeError |
| 90 |
with self.assertRaises(RuntimeError): |
| 91 |
self.dev.facts.pop("foo", None) |
| 92 |
|
| 93 |
def test_factcache_set_fact(self): |
| 94 |
# Create a callback for the foo fact. |
| 95 |
self.dev.facts._callbacks["foo"] = get_foo_fact |
| 96 |
foo = self.dev.facts["foo"] |
| 97 |
# Now, trying to set the foo fact should cause a |
| 98 |
# RunTimeError |
| 99 |
with self.assertRaises(RuntimeError): |
| 100 |
self.dev.facts["foo"] = "bar" |
| 101 |
|
| 102 |
def test_factcache_iter_facts(self): |
| 103 |
# Override the callbacks |
| 104 |
self.dev.facts._callbacks = { |
| 105 |
"foo": get_foo_fact, |
| 106 |
"bar": get_bar_fact, |
| 107 |
"_hidden": get_foo_bar_fact, |
| 108 |
} |
| 109 |
# Now, get the length of the facts |
| 110 |
self.assertEqual(len(list(self.dev.facts)), 2) |
| 111 |
|
| 112 |
def test_factcache_len_facts(self): |
| 113 |
# Override the callbacks |
| 114 |
self.dev.facts._callbacks = {"foo": get_foo_fact, "bar": get_bar_fact} |
| 115 |
# Now, get the length of the facts |
| 116 |
self.assertEqual(len(self.dev.facts), 2) |
| 117 |
|
| 118 |
def test_factcache_string_repr(self): |
| 119 |
# Override the callbacks to only support foo and bar facts. |
| 120 |
self.dev.facts._callbacks = {"foo": get_foo_fact, "bar": get_bar_fact} |
| 121 |
# Set values for foo and bar facts |
| 122 |
self.dev.facts._cache["foo"] = "foo" |
| 123 |
self.dev.facts._cache["bar"] = {"bar": "bar"} |
| 124 |
# Now, get the string (pretty) representation of the facts |
| 125 |
self.assertEqual( |
| 126 |
str(self.dev.facts), "{'bar': {'bar': 'bar'}, " "'foo': 'foo'}" |
| 127 |
) |
| 128 |
|
| 129 |
def test_factcache_repr_facts(self): |
| 130 |
# Override the callbacks |
| 131 |
self.dev.facts._callbacks = {"foo": get_foo_fact, "bar": get_bar_fact} |
| 132 |
# Now, get the length of the facts |
| 133 |
self.assertEqual(str(self.dev.facts), "{'bar': 'bar', 'foo': 'foo'}") |
| 134 |
|
| 135 |
def test_factcache_refresh_single_key(self): |
| 136 |
# Override the callbacks |
| 137 |
self.dev.facts._callbacks = { |
| 138 |
"foo": get_foo_fact, |
| 139 |
"bar": get_bar_fact, |
| 140 |
"_hidden": get_hidden_fact, |
| 141 |
} |
| 142 |
# Populate the cache |
| 143 |
self.dev.facts._cache["foo"] = "before" |
| 144 |
self.dev.facts._cache["bar"] = "before" |
| 145 |
self.dev.facts._cache["_hidden"] = "before" |
| 146 |
# Confirm the cached values |
| 147 |
self.assertEqual(self.dev.facts["foo"], "before") |
| 148 |
self.assertEqual(self.dev.facts["bar"], "before") |
| 149 |
self.assertEqual(self.dev.facts["_hidden"], "before") |
| 150 |
# Refresh just the foo fact |
| 151 |
self.dev.facts._refresh(keys="foo") |
| 152 |
# Confirm the values now |
| 153 |
self.assertEqual(self.dev.facts["foo"], "foo") |
| 154 |
self.assertEqual(self.dev.facts["bar"], "before") |
| 155 |
self.assertEqual(self.dev.facts["_hidden"], "before") |
| 156 |
|
| 157 |
def test_factcache_refresh_two_keys(self): |
| 158 |
# Override the callbacks |
| 159 |
self.dev.facts._callbacks = { |
| 160 |
"foo": get_foo_fact, |
| 161 |
"bar": get_bar_fact, |
| 162 |
"_hidden": get_hidden_fact, |
| 163 |
} |
| 164 |
# Populate the cache |
| 165 |
self.dev.facts._cache["foo"] = "before" |
| 166 |
self.dev.facts._cache["bar"] = "before" |
| 167 |
self.dev.facts._cache["_hidden"] = "before" |
| 168 |
# Confirm the cached values |
| 169 |
self.assertEqual(self.dev.facts["foo"], "before") |
| 170 |
self.assertEqual(self.dev.facts["bar"], "before") |
| 171 |
self.assertEqual(self.dev.facts["_hidden"], "before") |
| 172 |
# Refresh the foo and _hidden facts |
| 173 |
self.dev.facts._refresh(keys=("foo", "_hidden")) |
| 174 |
# Confirm the values now |
| 175 |
self.assertEqual(self.dev.facts["foo"], "foo") |
| 176 |
self.assertEqual(self.dev.facts["bar"], "before") |
| 177 |
self.assertEqual(self.dev.facts["_hidden"], True) |
| 178 |
|
| 179 |
def test_factcache_refresh_unknown_fact(self): |
| 180 |
# Override the callbacks |
| 181 |
self.dev.facts._callbacks = {"foo": get_foo_fact, "_hidden": get_hidden_fact} |
| 182 |
# Populate the cache |
| 183 |
self.dev.facts._cache["foo"] = "before" |
| 184 |
self.dev.facts._cache["_hidden"] = "before" |
| 185 |
# Confirm the cached values |
| 186 |
self.assertEqual(self.dev.facts["foo"], "before") |
| 187 |
self.assertEqual(self.dev.facts["_hidden"], "before") |
| 188 |
# Refresh just the unknown bar fact which should raise a RuntimeError |
| 189 |
with self.assertRaises(RuntimeError): |
| 190 |
self.dev.facts._refresh(keys=("bar")) |
| 191 |
|
| 192 |
def test_factcache_refresh_all_facts(self): |
| 193 |
# Override the callbacks |
| 194 |
self.dev.facts._callbacks = { |
| 195 |
"foo": get_foo_fact, |
| 196 |
"bar": get_bar_fact, |
| 197 |
"_hidden": get_hidden_fact, |
| 198 |
} |
| 199 |
# Populate the cache |
| 200 |
self.dev.facts._cache["foo"] = "before" |
| 201 |
self.dev.facts._cache["bar"] = "before" |
| 202 |
self.dev.facts._cache["_hidden"] = "before" |
| 203 |
# Confirm the cached values |
| 204 |
self.assertEqual(self.dev.facts["foo"], "before") |
| 205 |
self.assertEqual(self.dev.facts["bar"], "before") |
| 206 |
self.assertEqual(self.dev.facts["_hidden"], "before") |
| 207 |
# Refresh all facts |
| 208 |
self.dev.facts._refresh() |
| 209 |
# Confirm the values now |
| 210 |
self.assertEqual(self.dev.facts["foo"], "foo") |
| 211 |
self.assertEqual(self.dev.facts["bar"], "bar") |
| 212 |
self.assertEqual(self.dev.facts["_hidden"], True) |
| 213 |
|
| 214 |
@patch("jnpr.junos.device.warnings") |
| 215 |
def test_factcache_refresh_exception_on_failure(self, mock_warn): |
| 216 |
# Override the callbacks |
| 217 |
self.dev.facts._callbacks = { |
| 218 |
"foo": get_foo_raise_error, |
| 219 |
} |
| 220 |
# Populate the cache |
| 221 |
self.dev.facts._cache["foo"] = "before" |
| 222 |
# Confirm the cached values |
| 223 |
self.assertEqual(self.dev.facts["foo"], "before") |
| 224 |
with self.assertRaises(ValueError): |
| 225 |
self.dev.facts._refresh(exception_on_failure=True, keys="foo") |
| 226 |
|
| 227 |
@patch("jnpr.junos.device.warnings") |
| 228 |
def test_factcache_refresh_no_exception_on_failure(self, mock_warn): |
| 229 |
# Override the callbacks |
| 230 |
self.dev.facts._callbacks = { |
| 231 |
"foo": get_foo_raise_error, |
| 232 |
} |
| 233 |
# Populate the cache |
| 234 |
self.dev.facts._cache["foo"] = "before" |
| 235 |
# Confirm the cached values |
| 236 |
self.assertEqual(self.dev.facts["foo"], "before") |
| 237 |
self.dev.facts._refresh(exception_on_failure=False, keys="foo") |
| 238 |
self.assertEqual(self.dev.facts["foo"], None) |
| 239 |
|
| 240 |
@patch("jnpr.junos.device.warnings") |
| 241 |
@patch("jnpr.junos.factcache.warnings") |
| 242 |
def test_factcache_refresh_warnings_on_failure(self, mock_warn, mock_device_warn): |
| 243 |
# Refresh all facts with warnings on failure |
| 244 |
self.dev.facts._refresh(warnings_on_failure=True) |
| 245 |
mock_warn.assert_has_calls( |
| 246 |
[ |
| 247 |
call.warn( |
| 248 |
"Facts gathering is incomplete. To know the reason call " |
| 249 |
'"dev.facts_refresh(exception_on_failure=True)"', |
| 250 |
RuntimeWarning, |
| 251 |
) |
| 252 |
] |
| 253 |
) |
| 254 |
# mock_warn.assert_called_once('Facts gathering is incomplete. ' |
| 255 |
# 'To know the reason call ' |
| 256 |
# '"dev.facts_refresh(' |
| 257 |
# 'exception_on_failure=True)"', |
| 258 |
# RuntimeWarning) |
| 259 |
|
| 260 |
def _mock_manager_setup(self, *args, **kwargs): |
| 261 |
if kwargs: |
| 262 |
device_params = kwargs["device_params"] |
| 263 |
device_handler = make_device_handler(device_params) |
| 264 |
session = SSHSession(device_handler) |
| 265 |
return Manager(session, device_handler) |
| 266 |
|
| 267 |
|
| 268 |
def get_foo_fact(device): |
| 269 |
return {"foo": "foo"} |
| 270 |
|
| 271 |
|
| 272 |
def get_foo_bar_fact(device): |
| 273 |
return { |
| 274 |
"foo": "foo", |
| 275 |
"bar": "bar", |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
def get_bar_fact(device): |
| 280 |
return { |
| 281 |
"bar": "bar", |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
def get_hidden_fact(device): |
| 286 |
return { |
| 287 |
"_hidden": True, |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
def get_foo_raise_error(device): |
| 292 |
raise ValueError("Error") |