test_napalm.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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.grains.napalm as napalm_grains # NOQA
  18. import salt.proxy.napalm as napalm_proxy # NOQA
  19. napalm_grains.salt.utils.napalm.is_proxy = MagicMock(return_value=True)
  20. TEST_DEVICE_CACHE = {
  21. 'DRIVER': napalm_test_support.MockNapalmDevice(),
  22. 'DRIVER_NAME': 'cisco',
  23. 'OS_VERSION': '1.2.3',
  24. 'HOSTNAME': 'test-device.com',
  25. 'USERNAME': 'admin'
  26. }
  27. TEST_CACHE = {
  28. 'result': True,
  29. 'out': napalm_test_support.TEST_FACTS
  30. }
  31. @skipIf(NO_MOCK, NO_MOCK_REASON)
  32. @patch('salt.grains.napalm.DEVICE_CACHE', TEST_DEVICE_CACHE)
  33. @patch('salt.grains.napalm.GRAINS_CACHE', TEST_CACHE)
  34. class NapalmGrainsTestCase(TestCase, LoaderModuleMockMixin):
  35. def setup_loader_modules(self):
  36. module_globals = {
  37. '__salt__': {
  38. 'config.option': MagicMock(return_value={
  39. 'test': {
  40. 'driver': 'test',
  41. 'key': '2orgk34kgk34g'
  42. }
  43. }),
  44. 'file.file_exists': napalm_test_support.true,
  45. 'file.join': napalm_test_support.join,
  46. 'file.get_managed': napalm_test_support.get_managed_file,
  47. 'random.hash': napalm_test_support.random_hash,
  48. }
  49. }
  50. return {napalm_grains: module_globals}
  51. def test_os(self):
  52. ret = napalm_grains.getos(proxy=napalm_proxy)
  53. assert ret['os'] == 'cisco'
  54. def test_os_version(self):
  55. ret = napalm_grains.version(proxy=napalm_proxy)
  56. assert ret['version'] == '1.2.3'
  57. def test_model(self):
  58. ret = napalm_grains.model(proxy=napalm_proxy)
  59. assert ret['model'] == 'test_model'
  60. def test_serial(self):
  61. ret = napalm_grains.serial(proxy=napalm_proxy)
  62. assert ret['serial'] == '123456'
  63. def test_vendor(self):
  64. ret = napalm_grains.vendor(proxy=napalm_proxy)
  65. assert ret['vendor'] == 'cisco'
  66. def test_uptime(self):
  67. ret = napalm_grains.uptime(proxy=napalm_proxy)
  68. assert ret['uptime'] == 'Forever'
  69. def test_interfaces(self):
  70. ret = napalm_grains.interfaces(proxy=napalm_proxy)
  71. assert ret['interfaces'] == napalm_test_support.TEST_INTERFACES
  72. def test_username(self):
  73. ret = napalm_grains.username(proxy=napalm_proxy)
  74. assert ret['username'] == 'admin'
  75. def test_hostname(self):
  76. ret = napalm_grains.hostname(proxy=napalm_proxy)
  77. assert ret['hostname'] == 'test-device.com'
  78. def test_host(self):
  79. ret = napalm_grains.host(proxy=napalm_proxy)
  80. assert ret['host'] == 'test-device.com'