test_napalm.py 2.9 KB

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