napalm.py 13 KB

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