test_netbox.py 3.3 KB

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