1
0

napalm.py 13 KB

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