test_napalm.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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
  10. from tests.support.mock import (
  11. MagicMock,
  12. patch
  13. )
  14. import tests.support.napalm as napalm_test_support
  15. import salt.grains.napalm as napalm_grains # NOQA
  16. import salt.proxy.napalm as napalm_proxy # NOQA
  17. napalm_grains.salt.utils.napalm.is_proxy = MagicMock(return_value=True)
  18. TEST_DEVICE_CACHE = {
  19. 'DRIVER': napalm_test_support.MockNapalmDevice(),
  20. 'DRIVER_NAME': 'cisco',
  21. 'OS_VERSION': '1.2.3',
  22. 'HOSTNAME': 'test-device.com',
  23. 'USERNAME': 'admin'
  24. }
  25. TEST_CACHE = {
  26. 'result': True,
  27. 'out': napalm_test_support.TEST_FACTS
  28. }
  29. @patch('salt.grains.napalm.DEVICE_CACHE', TEST_DEVICE_CACHE)
  30. @patch('salt.grains.napalm.GRAINS_CACHE', TEST_CACHE)
  31. class NapalmGrainsTestCase(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. 'file.file_exists': napalm_test_support.true,
  42. 'file.join': napalm_test_support.join,
  43. 'file.get_managed': napalm_test_support.get_managed_file,
  44. 'random.hash': napalm_test_support.random_hash,
  45. }
  46. }
  47. return {napalm_grains: module_globals}
  48. def test_os(self):
  49. ret = napalm_grains.getos(proxy=napalm_proxy)
  50. assert ret['os'] == 'cisco'
  51. def test_os_version(self):
  52. ret = napalm_grains.version(proxy=napalm_proxy)
  53. assert ret['version'] == '1.2.3'
  54. def test_model(self):
  55. ret = napalm_grains.model(proxy=napalm_proxy)
  56. assert ret['model'] == 'test_model'
  57. def test_serial(self):
  58. ret = napalm_grains.serial(proxy=napalm_proxy)
  59. assert ret['serial'] == '123456'
  60. def test_vendor(self):
  61. ret = napalm_grains.vendor(proxy=napalm_proxy)
  62. assert ret['vendor'] == 'cisco'
  63. def test_uptime(self):
  64. ret = napalm_grains.uptime(proxy=napalm_proxy)
  65. assert ret['uptime'] == 'Forever'
  66. def test_interfaces(self):
  67. ret = napalm_grains.interfaces(proxy=napalm_proxy)
  68. assert ret['interfaces'] == napalm_test_support.TEST_INTERFACES
  69. def test_username(self):
  70. ret = napalm_grains.username(proxy=napalm_proxy)
  71. assert ret['username'] == 'admin'
  72. def test_hostname(self):
  73. ret = napalm_grains.hostname(proxy=napalm_proxy)
  74. assert ret['hostname'] == 'test-device.com'
  75. def test_host(self):
  76. ret = napalm_grains.host(proxy=napalm_proxy)
  77. assert ret['host'] == 'test-device.com'