1
0

test_napalm.py 2.8 KB

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