1
0

napalm.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. """
  2. Base classes for napalm unit tests
  3. :codeauthor: :email:`Anthony Shaw <anthonyshaw@apache.org>`
  4. """
  5. from functools import wraps
  6. from salt.utils.immutabletypes import freeze
  7. TEST_INTERFACES = freeze(
  8. {
  9. "Management1": {
  10. "is_up": False,
  11. "is_enabled": False,
  12. "description": "",
  13. "last_flapped": -1,
  14. "speed": 1000,
  15. "mac_address": "dead:beef:dead",
  16. }
  17. }
  18. )
  19. # Test data
  20. TEST_FACTS = freeze(
  21. {
  22. "__opts__": {},
  23. "OPTIONAL_ARGS": {},
  24. "uptime": "Forever",
  25. "UP": True,
  26. "HOSTNAME": "test-device.com",
  27. "hostname": "test-device.com",
  28. "username": "admin",
  29. "os_version": "1.2.3",
  30. "model": "test_model",
  31. "serial_number": "123456",
  32. "vendor": "cisco",
  33. "interface_list": TEST_INTERFACES,
  34. }
  35. )
  36. TEST_ENVIRONMENT = freeze({"hot": "yes"})
  37. TEST_COMMAND_RESPONSE = freeze({"show run": "all the command output"})
  38. TEST_TRACEROUTE_RESPONSE = freeze(
  39. {
  40. "success": {
  41. 1: {
  42. "probes": {
  43. 1: {
  44. "rtt": 1.123,
  45. "ip_address": "206.223.116.21",
  46. "host_name": "eqixsj-google-gige.google.com",
  47. }
  48. }
  49. }
  50. }
  51. }
  52. )
  53. TEST_PING_RESPONSE = freeze(
  54. {
  55. "success": {
  56. "probes_sent": 5,
  57. "packet_loss": 0,
  58. "rtt_min": 72.158,
  59. "rtt_max": 72.433,
  60. "rtt_avg": 72.268,
  61. "rtt_stddev": 0.094,
  62. "results": [{"ip_address": "1.1.1.1", "rtt": 72.248}],
  63. }
  64. }
  65. )
  66. TEST_ARP_TABLE = freeze(
  67. [
  68. {
  69. "interface": "MgmtEth0/RSP0/CPU0/0",
  70. "mac": "5C:5E:AB:DA:3C:F0",
  71. "ip": "172.17.17.1",
  72. "age": 1454496274.84,
  73. }
  74. ]
  75. )
  76. TEST_IPADDRS = freeze(
  77. {"FastEthernet8": {"ipv4": {"10.66.43.169": {"prefix_length": 22}}}}
  78. )
  79. TEST_INTERFACES = freeze(
  80. {
  81. "Management1": {
  82. "is_up": False,
  83. "is_enabled": False,
  84. "description": "",
  85. "last_flapped": -1,
  86. "speed": 1000,
  87. "mac_address": "dead:beef:dead",
  88. }
  89. }
  90. )
  91. TEST_LLDP_NEIGHBORS = freeze(
  92. {"Ethernet2": [{"hostname": "junos-unittest", "port": "520"}]}
  93. )
  94. TEST_MAC_TABLE = freeze(
  95. [
  96. {
  97. "mac": "00:1C:58:29:4A:71",
  98. "interface": "Ethernet47",
  99. "vlan": 100,
  100. "static": False,
  101. "active": True,
  102. "moves": 1,
  103. "last_move": 1454417742.58,
  104. }
  105. ]
  106. )
  107. TEST_RUNNING_CONFIG = freeze({"one": "two"})
  108. TEST_OPTICS = freeze(
  109. {
  110. "et1": {
  111. "physical_channels": {
  112. "channel": [
  113. {
  114. "index": 0,
  115. "state": {
  116. "input_power": {
  117. "instant": 0.0,
  118. "avg": 0.0,
  119. "min": 0.0,
  120. "max": 0.0,
  121. },
  122. "output_power": {
  123. "instant": 0.0,
  124. "avg": 0.0,
  125. "min": 0.0,
  126. "max": 0.0,
  127. },
  128. "laser_bias_current": {
  129. "instant": 0.0,
  130. "avg": 0.0,
  131. "min": 0.0,
  132. "max": 0.0,
  133. },
  134. },
  135. }
  136. ]
  137. }
  138. }
  139. }
  140. )
  141. TEST_BGP_CONFIG = freeze({"test": "value"})
  142. TEST_BGP_NEIGHBORS = freeze(
  143. {
  144. "default": {
  145. 8121: [
  146. {
  147. "up": True,
  148. "local_as": 13335,
  149. "remote_as": 8121,
  150. "local_address": "172.101.76.1",
  151. "local_address_configured": True,
  152. "local_port": 179,
  153. "remote_address": "192.247.78.0",
  154. "router_id": "192.168.0.1",
  155. "remote_port": 58380,
  156. "multihop": False,
  157. "import_policy": "4-NTT-TRANSIT-IN",
  158. "export_policy": "4-NTT-TRANSIT-OUT",
  159. "input_messages": 123,
  160. "output_messages": 13,
  161. "input_updates": 123,
  162. "output_updates": 5,
  163. "messages_queued_out": 23,
  164. "connection_state": "Established",
  165. "previous_connection_state": "EstabSync",
  166. "last_event": "RecvKeepAlive",
  167. "suppress_4byte_as": False,
  168. "local_as_prepend": False,
  169. "holdtime": 90,
  170. "configured_holdtime": 90,
  171. "keepalive": 30,
  172. "configured_keepalive": 30,
  173. "active_prefix_count": 132808,
  174. "received_prefix_count": 566739,
  175. "accepted_prefix_count": 566479,
  176. "suppressed_prefix_count": 0,
  177. "advertise_prefix_count": 0,
  178. "flap_count": 27,
  179. }
  180. ]
  181. }
  182. }
  183. )
  184. TEST_TERM_CONFIG = freeze({"result": True, "already_configured": False})
  185. TEST_NTP_PEERS = freeze(
  186. {
  187. "192.168.0.1": 1,
  188. "172.17.17.1": 2,
  189. "172.17.17.2": 3,
  190. "2400:cb00:6:1024::c71b:840a": 4,
  191. }
  192. )
  193. TEST_NTP_SERVERS = freeze(
  194. {
  195. "192.168.0.1": 1,
  196. "172.17.17.1": 2,
  197. "172.17.17.2": 3,
  198. "2400:cb00:6:1024::c71b:840a": 4,
  199. }
  200. )
  201. TEST_NTP_STATS = freeze(
  202. [
  203. {
  204. "remote": "188.114.101.4",
  205. "referenceid": "188.114.100.1",
  206. "synchronized": True,
  207. "stratum": 4,
  208. "type": "-",
  209. "when": "107",
  210. "hostpoll": 256,
  211. "reachability": 377,
  212. "delay": 164.228,
  213. "offset": -13.866,
  214. "jitter": 2.695,
  215. }
  216. ]
  217. )
  218. TEST_PROBES_CONFIG = freeze(
  219. {
  220. "probe1": {
  221. "test1": {
  222. "probe_type": "icmp-ping",
  223. "target": "192.168.0.1",
  224. "source": "192.168.0.2",
  225. "probe_count": 13,
  226. "test_interval": 3,
  227. },
  228. "test2": {
  229. "probe_type": "http-ping",
  230. "target": "172.17.17.1",
  231. "source": "192.17.17.2",
  232. "probe_count": 5,
  233. "test_interval": 60,
  234. },
  235. }
  236. }
  237. )
  238. TEST_PROBES_RESULTS = freeze(
  239. {
  240. "probe1": {
  241. "test1": {
  242. "last_test_min_delay": 63.120,
  243. "global_test_min_delay": 62.912,
  244. "current_test_avg_delay": 63.190,
  245. "global_test_max_delay": 177.349,
  246. "current_test_max_delay": 63.302,
  247. "global_test_avg_delay": 63.802,
  248. "last_test_avg_delay": 63.438,
  249. "last_test_max_delay": 65.356,
  250. "probe_type": "icmp-ping",
  251. "rtt": 63.138,
  252. "last_test_loss": 0,
  253. "round_trip_jitter": -59.0,
  254. "target": "192.168.0.1",
  255. "source": "192.168.0.2",
  256. "probe_count": 15,
  257. "current_test_min_delay": 63.138,
  258. },
  259. "test2": {
  260. "last_test_min_delay": 176.384,
  261. "global_test_min_delay": 169.226,
  262. "current_test_avg_delay": 177.098,
  263. "global_test_max_delay": 292.628,
  264. "current_test_max_delay": 180.055,
  265. "global_test_avg_delay": 177.959,
  266. "last_test_avg_delay": 177.178,
  267. "last_test_max_delay": 184.671,
  268. "probe_type": "icmp-ping",
  269. "rtt": 176.449,
  270. "last_test_loss": 0,
  271. "round_trip_jitter": -34.0,
  272. "target": "172.17.17.1",
  273. "source": "172.17.17.2",
  274. "probe_count": 15,
  275. "current_test_min_delay": 176.402,
  276. },
  277. }
  278. }
  279. )
  280. TEST_ROUTE = freeze(
  281. {
  282. "172.16.0.0/25": [
  283. {
  284. "protocol": "BGP",
  285. "last_active": True,
  286. "current_active": True,
  287. "age": 1178693,
  288. "routing_table": "inet.0",
  289. "next_hop": "192.168.0.11",
  290. "outgoing_interface": "xe-1/1/1.100",
  291. "preference": 170,
  292. "selected_next_hop": False,
  293. "protocol_attributes": {
  294. "remote_as": 65001,
  295. "metric": 5,
  296. "local_as": 13335,
  297. "as_path": "",
  298. "remote_address": "192.168.0.11",
  299. "metric2": 0,
  300. "local_preference": 0,
  301. "communities": ["0:2", "no-export"],
  302. "preference2": -1,
  303. },
  304. "inactive_reason": "",
  305. },
  306. {
  307. "protocol": "BGP",
  308. "last_active": False,
  309. "current_active": False,
  310. "age": 2359429,
  311. "routing_table": "inet.0",
  312. "next_hop": "192.168.0.17",
  313. "outgoing_interface": "xe-1/1/1.100",
  314. "preference": 170,
  315. "selected_next_hop": True,
  316. "protocol_attributes": {
  317. "remote_as": 65001,
  318. "metric": 5,
  319. "local_as": 13335,
  320. "as_path": "",
  321. "remote_address": "192.168.0.17",
  322. "metric2": 0,
  323. "local_preference": 0,
  324. "communities": ["0:3", "no-export"],
  325. "preference2": -1,
  326. },
  327. "inactive_reason": "Not Best in its group - Router ID",
  328. },
  329. ]
  330. }
  331. )
  332. TEST_SNMP_INFO = freeze({"test_": "value"})
  333. TEST_USERS = freeze(
  334. {
  335. "mircea": {
  336. "level": 15,
  337. "password": "$1$0P70xKPa$4jt5/10cBTckk6I/w/",
  338. "sshkeys": [
  339. "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC4pFn+shPwTb2yELO4L7NtQrKOJXNeCl1je\
  340. l9STXVaGnRAnuc2PXl35vnWmcUq6YbUEcgUTRzzXfmelJKuVJTJIlMXii7h2xkbQp0YZIEs4P\
  341. 8ipwnRBAxFfk/ZcDsdfsdfsdfsdN56ejk345jhk345jk345jk341p3A/9LIL7l6YewLBCwJj6\
  342. D+fWSJ0/YW+7oH17Fk2HH+tw0L5PcWLHkwA4t60iXn16qDbIk/ze6jv2hDGdCdz7oYQeCE55C\
  343. CHOHMJWYfN3jcL4s0qv8/u6Ka1FVkV7iMmro7ChThoV/5snI4Ljf2wKqgHH7TfNaCfpU0WvHA\
  344. nTs8zhOrGScSrtb mircea@master-roshi"
  345. ],
  346. }
  347. }
  348. )
  349. class MockNapalmDevice:
  350. """Setup a mock device for our tests"""
  351. def __init__(self):
  352. self._d = {}
  353. def __getitem__(self, key):
  354. return self._d[key]
  355. def __setitem__(self, key, value):
  356. self._d[key] = value
  357. def get_facts(self):
  358. return TEST_FACTS.copy()
  359. def get_environment(self):
  360. return TEST_ENVIRONMENT.copy()
  361. def get_arp_table(self):
  362. return TEST_ARP_TABLE.copy()
  363. def get(self, key, default=None, *args, **kwargs):
  364. try:
  365. if key == "DRIVER":
  366. return self
  367. return TEST_FACTS.copy()[key]
  368. except KeyError:
  369. return default
  370. def cli(self, commands, *args, **kwargs):
  371. assert commands[0] == "show run"
  372. return TEST_COMMAND_RESPONSE.copy()
  373. def traceroute(self, destination, **kwargs):
  374. assert destination == "destination.com"
  375. return TEST_TRACEROUTE_RESPONSE.copy()
  376. def ping(self, destination, **kwargs):
  377. assert destination == "destination.com"
  378. return TEST_PING_RESPONSE.copy()
  379. def get_config(self, retrieve="all"):
  380. assert retrieve == "running"
  381. return TEST_RUNNING_CONFIG.copy()
  382. def get_interfaces_ip(self, **kwargs):
  383. return TEST_IPADDRS.copy()
  384. def get_interfaces(self, **kwargs):
  385. return TEST_INTERFACES.copy()
  386. def get_lldp_neighbors_detail(self, **kwargs):
  387. return TEST_LLDP_NEIGHBORS.copy()
  388. def get_mac_address_table(self, **kwargs):
  389. return TEST_MAC_TABLE.copy()
  390. def get_optics(self, **kwargs):
  391. return TEST_OPTICS.copy()
  392. def load_merge_candidate(self, filename=None, config=None):
  393. assert config == "new config"
  394. return TEST_RUNNING_CONFIG.copy()
  395. def load_replace_candidate(self, filename=None, config=None):
  396. assert config == "new config"
  397. return TEST_RUNNING_CONFIG.copy()
  398. def commit_config(self, **kwargs):
  399. return TEST_RUNNING_CONFIG.copy()
  400. def discard_config(self, **kwargs):
  401. return TEST_RUNNING_CONFIG.copy()
  402. def compare_config(self, **kwargs):
  403. return TEST_RUNNING_CONFIG.copy()
  404. def rollback(self, **kwargs):
  405. return TEST_RUNNING_CONFIG.copy()
  406. def get_bgp_config(self, **kwargs):
  407. return TEST_BGP_CONFIG.copy()
  408. def get_bgp_neighbors_detail(self, neighbor_address=None, **kwargs):
  409. assert neighbor_address is None or "test_address"
  410. return TEST_BGP_NEIGHBORS.copy()
  411. def get_ntp_peers(self, **kwargs):
  412. return TEST_NTP_PEERS.copy()
  413. def get_ntp_servers(self, **kwargs):
  414. return TEST_NTP_SERVERS.copy()
  415. def get_ntp_stats(self, **kwargs):
  416. return TEST_NTP_STATS.copy()
  417. def get_probes_config(self, **kwargs):
  418. return TEST_PROBES_CONFIG.copy()
  419. def get_probes_results(self, **kwargs):
  420. return TEST_PROBES_RESULTS.copy()
  421. def get_route_to(self, destination, protocol=None, **kwargs):
  422. assert destination == "1.2.3.4"
  423. return TEST_ROUTE.copy()
  424. def get_snmp_information(self, **kwargs):
  425. return TEST_SNMP_INFO.copy()
  426. def get_users(self, **kwargs):
  427. return TEST_USERS.copy()
  428. def mock_proxy_napalm_wrap(func):
  429. """
  430. The proper decorator checks for proxy minions. We don't care
  431. so just pass back to the origination function
  432. """
  433. @wraps(func)
  434. def func_wrapper(*args, **kwargs):
  435. func.__globals__["napalm_device"] = MockNapalmDevice()
  436. return func(*args, **kwargs)
  437. return func_wrapper
  438. def true(name):
  439. return True
  440. def random_hash(source, method):
  441. return 12346789
  442. def join(*files):
  443. return True
  444. def get_managed_file(*args, **kwargs):
  445. return "True"