test_napalm.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: :email:`Anthony Shaw <anthonyshaw@apache.org>`
  4. '''
  5. # Import Python Libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Testing Libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.unit import TestCase, skipIf
  10. from tests.support.mock import (
  11. MagicMock,
  12. NO_MOCK,
  13. NO_MOCK_REASON,
  14. patch
  15. )
  16. import tests.support.napalm as napalm_test_support
  17. import salt.proxy.napalm as napalm_proxy # NOQA
  18. TEST_OPTS = {
  19. 'proxytype': 'napalm',
  20. 'driver': 'junos',
  21. 'host': 'core05.nrt02'
  22. }
  23. def mock_get_device(opts, *args, **kwargs):
  24. assert opts == TEST_OPTS
  25. return {
  26. 'DRIVER': napalm_test_support.MockNapalmDevice(),
  27. 'UP': True
  28. }
  29. @skipIf(NO_MOCK, NO_MOCK_REASON)
  30. @patch('salt.utils.napalm.get_device', mock_get_device)
  31. class NapalmProxyTestCase(TestCase, LoaderModuleMockMixin):
  32. def setup_loader_modules(self):
  33. module_globals = {
  34. '__salt__': {
  35. 'config.option': MagicMock(return_value={
  36. 'test': {
  37. 'driver': 'test',
  38. 'key': '2orgk34kgk34g'
  39. }
  40. })
  41. }
  42. }
  43. module_globals['napalm_base'] = MagicMock()
  44. return {napalm_proxy: module_globals}
  45. def test_init(self):
  46. ret = napalm_proxy.init(TEST_OPTS)
  47. assert ret is True
  48. def test_alive(self):
  49. ret = napalm_proxy.alive(TEST_OPTS)
  50. assert ret is True
  51. def test_ping(self):
  52. napalm_proxy.init(TEST_OPTS)
  53. ret = napalm_proxy.ping()
  54. assert ret is True
  55. def test_initialized(self):
  56. napalm_proxy.init(TEST_OPTS)
  57. ret = napalm_proxy.initialized()
  58. assert ret is True
  59. def test_get_device(self):
  60. napalm_proxy.init(TEST_OPTS)
  61. ret = napalm_proxy.get_device()
  62. assert ret['UP'] is True
  63. def test_get_grains(self):
  64. napalm_proxy.init(TEST_OPTS)
  65. ret = napalm_proxy.get_grains()
  66. assert ret['out'] == napalm_test_support.TEST_FACTS
  67. def test_grains_refresh(self):
  68. napalm_proxy.init(TEST_OPTS)
  69. ret = napalm_proxy.grains_refresh()
  70. assert ret['out'] == napalm_test_support.TEST_FACTS
  71. def test_fns(self):
  72. ret = napalm_proxy.fns()
  73. assert 'details' in ret.keys()
  74. def test_shutdown(self):
  75. ret = napalm_proxy.shutdown(TEST_OPTS)
  76. assert ret is True
  77. def test_call(self):
  78. napalm_proxy.init(TEST_OPTS)
  79. ret = napalm_proxy.call('get_arp_table')
  80. assert ret['out'] == napalm_test_support.TEST_ARP_TABLE