test_napalm_yang_mod.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: :email:`Anthony Shaw <anthonyshaw@apache.org>`
  4. '''
  5. # Import Python Libs
  6. from __future__ import absolute_import, unicode_literals, print_function
  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. )
  13. import tests.support.napalm as napalm_test_support
  14. import salt.modules.napalm_yang_mod as napalm_yang_mod # NOQA
  15. import salt.modules.napalm_network as napalm_network # NOQA
  16. TEST_DIFF = {
  17. 'diff1': 'value'
  18. }
  19. class MockNapalmYangModel(object):
  20. def Root(self):
  21. return MagicMock()
  22. class MockNapalmYangModels(object):
  23. openconfig_interfaces = MockNapalmYangModel()
  24. class MockUtils(object):
  25. def diff(self, *args):
  26. return TEST_DIFF
  27. class MockNapalmYangModule(object):
  28. base = MockNapalmYangModel()
  29. models = MockNapalmYangModels()
  30. utils = MockUtils()
  31. TEST_CONFIG = {
  32. 'comment': 'Configuration discarded.',
  33. 'already_configured': False,
  34. 'result': True,
  35. 'diff': '[edit interfaces xe-0/0/5]+ description "Adding a description";'
  36. }
  37. def mock_net_load_config(**kwargs):
  38. return TEST_CONFIG
  39. class NapalmYangModModuleTestCase(TestCase, LoaderModuleMockMixin):
  40. def setup_loader_modules(self):
  41. module_globals = {
  42. '__salt__': {
  43. 'config.option': MagicMock(return_value={
  44. 'test': {
  45. 'driver': 'test',
  46. 'key': '2orgk34kgk34g'
  47. }
  48. }),
  49. 'file.file_exists': napalm_test_support.true,
  50. 'file.join': napalm_test_support.join,
  51. 'file.get_managed': napalm_test_support.get_managed_file,
  52. 'random.hash': napalm_test_support.random_hash,
  53. 'net.load_template': napalm_network.load_template,
  54. 'net.load_config': mock_net_load_config
  55. }
  56. }
  57. module_globals['napalm_yang'] = MockNapalmYangModule()
  58. return {napalm_yang_mod: module_globals, napalm_network: module_globals}
  59. def test_diff(self):
  60. ret = napalm_yang_mod.diff({}, {'test': True}, 'models.openconfig_interfaces')
  61. assert ret == TEST_DIFF
  62. def test_diff_list(self):
  63. '''
  64. Test it with an actual list
  65. '''
  66. ret = napalm_yang_mod.diff({}, {'test': True}, ['models.openconfig_interfaces'])
  67. assert ret == TEST_DIFF
  68. def test_parse(self):
  69. ret = napalm_yang_mod.parse('models.openconfig_interfaces')
  70. assert ret is not None
  71. def test_get_config(self):
  72. ret = napalm_yang_mod.get_config({}, 'models.openconfig_interfaces')
  73. assert ret is not None
  74. def test_load_config(self):
  75. ret = napalm_yang_mod.load_config({}, 'models.openconfig_interfaces')
  76. assert ret is TEST_CONFIG
  77. def test_compliance_report(self):
  78. ret = napalm_yang_mod.compliance_report({}, 'models.openconfig_interfaces')
  79. assert ret is not None