test_openstack_config.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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.openstack_config as openstack_config
  9. from salt.exceptions import CommandExecutionError
  10. # Import Salt Testing Libs
  11. from tests.support.mixins import LoaderModuleMockMixin
  12. from tests.support.mock import MagicMock, patch
  13. from tests.support.unit import TestCase
  14. class OpenstackConfigTestCase(TestCase, LoaderModuleMockMixin):
  15. """
  16. Test cases for salt.states.openstack_config
  17. """
  18. def setup_loader_modules(self):
  19. return {openstack_config: {"__opts__": {"test": False}}}
  20. # 'present' function tests: 1
  21. def test_present(self):
  22. """
  23. Test to ensure a value is set in an OpenStack configuration file.
  24. """
  25. name = "salt"
  26. filename = "/tmp/salt"
  27. section = "A"
  28. value = "SALT"
  29. ret = {"name": name, "result": False, "comment": "", "changes": {}}
  30. mock_lst = MagicMock(side_effect=[value, CommandExecutionError, "A"])
  31. mock_t = MagicMock(return_value=True)
  32. with patch.dict(
  33. openstack_config.__salt__,
  34. {"openstack_config.get": mock_lst, "openstack_config.set": mock_t},
  35. ):
  36. comt = "The value is already set to the correct value"
  37. ret.update({"comment": comt, "result": True})
  38. self.assertDictEqual(
  39. openstack_config.present(name, filename, section, value), ret
  40. )
  41. self.assertRaises(
  42. CommandExecutionError,
  43. openstack_config.present,
  44. name,
  45. filename,
  46. section,
  47. value,
  48. )
  49. comt = "The value has been updated"
  50. ret.update({"comment": comt, "changes": {"Value": "Updated"}})
  51. self.assertDictEqual(
  52. openstack_config.present(name, filename, section, value), ret
  53. )
  54. # 'absent' function tests: 1
  55. def test_absent(self):
  56. """
  57. Test to ensure a value is not set in an OpenStack configuration file.
  58. """
  59. name = "salt"
  60. filename = "/tmp/salt"
  61. section = "A"
  62. ret = {"name": name, "result": False, "comment": "", "changes": {}}
  63. mock_lst = MagicMock(
  64. side_effect=[
  65. CommandExecutionError("parameter not found:"),
  66. CommandExecutionError,
  67. "A",
  68. ]
  69. )
  70. mock_t = MagicMock(return_value=True)
  71. with patch.dict(
  72. openstack_config.__salt__,
  73. {"openstack_config.get": mock_lst, "openstack_config.delete": mock_t},
  74. ):
  75. comt = "The value is already absent"
  76. ret.update({"comment": comt, "result": True})
  77. self.assertDictEqual(openstack_config.absent(name, filename, section), ret)
  78. self.assertRaises(
  79. CommandExecutionError, openstack_config.absent, name, filename, section
  80. )
  81. comt = "The value has been deleted"
  82. ret.update({"comment": comt, "changes": {"Value": "Deleted"}})
  83. self.assertDictEqual(openstack_config.absent(name, filename, section), ret)