test_debconfmod.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. """
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Libs
  8. import salt.states.debconfmod as debconfmod
  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. class DebconfmodTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Test cases for salt.states.debconfmod
  16. """
  17. def setup_loader_modules(self):
  18. return {debconfmod: {}}
  19. # 'set_file' function tests: 1
  20. def test_set_file(self):
  21. """
  22. Test to set debconf selections from a file or a template
  23. """
  24. name = "nullmailer"
  25. source = "salt://pathto/pkg.selections"
  26. ret = {"name": name, "result": False, "comment": "", "changes": {}}
  27. comt = "Context must be formed as a dict"
  28. ret.update({"comment": comt})
  29. self.assertDictEqual(debconfmod.set_file(name, source, context="salt"), ret)
  30. comt = "Defaults must be formed as a dict"
  31. ret.update({"comment": comt})
  32. self.assertDictEqual(debconfmod.set_file(name, source, defaults="salt"), ret)
  33. with patch.dict(debconfmod.__opts__, {"test": True}):
  34. comt = "Debconf selections would have been set."
  35. ret.update({"comment": comt, "result": None})
  36. self.assertDictEqual(debconfmod.set_file(name, source), ret)
  37. with patch.dict(debconfmod.__opts__, {"test": False}):
  38. mock = MagicMock(return_value=True)
  39. with patch.dict(debconfmod.__salt__, {"debconf.set_file": mock}):
  40. comt = "Debconf selections were set."
  41. ret.update({"comment": comt, "result": True})
  42. self.assertDictEqual(debconfmod.set_file(name, source), ret)
  43. # 'set' function tests: 1
  44. def test_set(self):
  45. """
  46. Test to set debconf selections
  47. """
  48. name = "nullmailer"
  49. data = {
  50. "shared/mailname": {"type": "string", "value": "server.domain.tld"},
  51. "nullmailer/relayhost": {"type": "string", "value": "mail.domain.tld"},
  52. }
  53. ret = {"name": name, "result": None, "comment": "", "changes": {}}
  54. changes = {
  55. "nullmailer/relayhost": "New value: mail.domain.tld",
  56. "shared/mailname": "New value: server.domain.tld",
  57. }
  58. mock = MagicMock(return_value=None)
  59. with patch.dict(debconfmod.__salt__, {"debconf.show": mock}):
  60. with patch.dict(debconfmod.__opts__, {"test": True}):
  61. ret.update({"changes": changes})
  62. self.assertDictEqual(debconfmod.set(name, data), ret)