test_net_napalm_yang.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Anthony Shaw <anthonyshaw@apache.org>
  4. """
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Libs
  8. import salt.states.net_napalm_yang as netyang
  9. # Import Salt Testing Libs
  10. from tests.support.mixins import LoaderModuleMockMixin
  11. from tests.support.mock import MagicMock, patch
  12. from tests.support.unit import TestCase
  13. TEST_DATA = {"foo": "bar"}
  14. class NetyangTestCase(TestCase, LoaderModuleMockMixin):
  15. def setup_loader_modules(self):
  16. return {netyang: {}}
  17. def test_managed(self):
  18. ret = {"changes": {}, "comment": "Loaded.", "name": "test", "result": False}
  19. parse = MagicMock(return_value="abcdef")
  20. temp_file = MagicMock(return_value="")
  21. compliance_report = MagicMock(return_value={"complies": False})
  22. load_config = MagicMock(return_value={"comment": "Loaded."})
  23. file_remove = MagicMock()
  24. with patch("salt.utils.files.fopen"):
  25. with patch.dict(
  26. netyang.__salt__,
  27. {
  28. "temp.file": temp_file,
  29. "napalm_yang.parse": parse,
  30. "napalm_yang.load_config": load_config,
  31. "napalm_yang.compliance_report": compliance_report,
  32. "file.remove": file_remove,
  33. },
  34. ):
  35. with patch.dict(netyang.__opts__, {"test": False}):
  36. self.assertDictEqual(
  37. netyang.managed("test", "test", models=("model1",)), ret
  38. )
  39. assert parse.called
  40. assert temp_file.called
  41. assert compliance_report.called
  42. assert load_config.called
  43. assert file_remove.called
  44. def test_configured(self):
  45. ret = {"changes": {}, "comment": "Loaded.", "name": "test", "result": False}
  46. load_config = MagicMock(return_value={"comment": "Loaded."})
  47. with patch("salt.utils.files.fopen"):
  48. with patch.dict(netyang.__salt__, {"napalm_yang.load_config": load_config}):
  49. with patch.dict(netyang.__opts__, {"test": False}):
  50. self.assertDictEqual(
  51. netyang.configured("test", "test", models=("model1",)), ret
  52. )
  53. assert load_config.called