test_netbox.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: :email:`Zach Moody <zmoody@do.co>`
  4. """
  5. # Import Python Libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import salt.modules.netbox as netbox
  8. # Import Salt Testing Libs
  9. from tests.support.mixins import LoaderModuleMockMixin
  10. from tests.support.mock import MagicMock, call, patch
  11. from tests.support.unit import TestCase, skipIf
  12. try:
  13. import pynetbox # pylint: disable=unused-import
  14. HAS_PYNETBOX = True
  15. except ImportError:
  16. HAS_PYNETBOX = False
  17. NETBOX_RESPONSE_STUB = {
  18. "device_name": "test1-router1",
  19. "url": "http://test/",
  20. "device_role": {"name": "router", "url": "http://test/"},
  21. }
  22. def mocked_clean_kwargs_filter(**kwargs):
  23. """
  24. Mocked args.clean_kwargs for filter tests
  25. """
  26. return {"site": "test"}
  27. def mocked_clean_kwargs_get(**kwargs):
  28. """
  29. Mocked args.clean_kwargs for get tests
  30. """
  31. return {"name": "test"}
  32. @skipIf(HAS_PYNETBOX is False, "pynetbox lib not installed")
  33. @patch("salt.modules.netbox._config", MagicMock())
  34. class NetBoxTestCase(TestCase, LoaderModuleMockMixin):
  35. def setup_loader_modules(self):
  36. return {
  37. netbox: {},
  38. }
  39. def test_get_by_id(self):
  40. with patch("pynetbox.api", MagicMock()) as mock:
  41. with patch.dict(
  42. netbox.__utils__, {"args.clean_kwargs": mocked_clean_kwargs_get}
  43. ):
  44. netbox.get_("dcim", "devices", id=1)
  45. self.assertEqual(mock.mock_calls[1], call().dcim.devices.get(1))
  46. def test_get_by_name(self):
  47. with patch("pynetbox.api", MagicMock()) as mock:
  48. with patch.dict(
  49. netbox.__utils__, {"args.clean_kwargs": mocked_clean_kwargs_get}
  50. ):
  51. netbox.get_("dcim", "devices", name="test")
  52. self.assertEqual(
  53. mock.mock_calls[1], call().dcim.devices.get(name="test")
  54. )
  55. def test_filter_by_site(self):
  56. with patch("pynetbox.api", MagicMock()) as mock:
  57. with patch.dict(
  58. netbox.__utils__, {"args.clean_kwargs": mocked_clean_kwargs_filter}
  59. ):
  60. netbox.filter_("dcim", "devices", site="test")
  61. self.assertEqual(
  62. mock.mock_calls[1], call().dcim.devices.filter(site="test")
  63. )
  64. def test_filter_url(self):
  65. strip_url = netbox._strip_url_field(NETBOX_RESPONSE_STUB)
  66. self.assertTrue(
  67. "url" not in strip_url and "url" not in strip_url["device_role"]
  68. )
  69. def test_get_secret(self):
  70. with patch("pynetbox.api", MagicMock()) as mock:
  71. with patch.dict(
  72. netbox.__utils__, {"args.clean_kwargs": mocked_clean_kwargs_get}
  73. ):
  74. netbox.get_("secrets", "secrets", name="test")
  75. self.assertTrue("token" and "private_key_file" in mock.call_args[1])
  76. def test_token_present(self):
  77. with patch("pynetbox.api", MagicMock()) as mock:
  78. with patch.dict(
  79. netbox.__utils__, {"args.clean_kwargs": mocked_clean_kwargs_get}
  80. ):
  81. netbox.get_("dcim", "devices", name="test")
  82. self.assertTrue("token" in mock.call_args[1])