test_lvs_service.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.lvs_service as lvs_service
  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 LvsServiceTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Test cases for salt.states.lvs_service
  16. """
  17. def setup_loader_modules(self):
  18. return {lvs_service: {}}
  19. # 'present' function tests: 1
  20. def test_present(self):
  21. """
  22. Test to ensure that the named service is present.
  23. """
  24. name = "lvsrs"
  25. ret = {"name": name, "result": True, "comment": "", "changes": {}}
  26. mock_check = MagicMock(
  27. side_effect=[
  28. True,
  29. True,
  30. True,
  31. False,
  32. True,
  33. False,
  34. True,
  35. False,
  36. False,
  37. False,
  38. False,
  39. ]
  40. )
  41. mock_edit = MagicMock(side_effect=[True, False])
  42. mock_add = MagicMock(side_effect=[True, False])
  43. with patch.dict(
  44. lvs_service.__salt__,
  45. {
  46. "lvs.check_service": mock_check,
  47. "lvs.edit_service": mock_edit,
  48. "lvs.add_service": mock_add,
  49. },
  50. ):
  51. with patch.dict(lvs_service.__opts__, {"test": True}):
  52. comt = "LVS Service lvsrs is present"
  53. ret.update({"comment": comt})
  54. self.assertDictEqual(lvs_service.present(name), ret)
  55. comt = "LVS Service lvsrs is present but some " "options should update"
  56. ret.update({"comment": comt, "result": None})
  57. self.assertDictEqual(lvs_service.present(name), ret)
  58. with patch.dict(lvs_service.__opts__, {"test": False}):
  59. comt = "LVS Service lvsrs has been updated"
  60. ret.update(
  61. {"comment": comt, "result": True, "changes": {"lvsrs": "Update"}}
  62. )
  63. self.assertDictEqual(lvs_service.present(name), ret)
  64. comt = "LVS Service lvsrs update failed"
  65. ret.update({"comment": comt, "result": False, "changes": {}})
  66. self.assertDictEqual(lvs_service.present(name), ret)
  67. with patch.dict(lvs_service.__opts__, {"test": True}):
  68. comt = "LVS Service lvsrs is not present and needs" " to be created"
  69. ret.update({"comment": comt, "result": None})
  70. self.assertDictEqual(lvs_service.present(name), ret)
  71. with patch.dict(lvs_service.__opts__, {"test": False}):
  72. comt = "LVS Service lvsrs has been created"
  73. ret.update(
  74. {"comment": comt, "result": True, "changes": {"lvsrs": "Present"}}
  75. )
  76. self.assertDictEqual(lvs_service.present(name), ret)
  77. comt = "LVS Service lvsrs create failed(False)"
  78. ret.update({"comment": comt, "result": False, "changes": {}})
  79. self.assertDictEqual(lvs_service.present(name), ret)
  80. # 'absent' function tests: 1
  81. def test_absent(self):
  82. """
  83. Test to ensure the LVS Real Server in specified service is absent.
  84. """
  85. name = "lvsrs"
  86. ret = {"name": name, "result": None, "comment": "", "changes": {}}
  87. mock_check = MagicMock(side_effect=[True, True, True, False])
  88. mock_delete = MagicMock(side_effect=[True, False])
  89. with patch.dict(
  90. lvs_service.__salt__,
  91. {"lvs.check_service": mock_check, "lvs.delete_service": mock_delete},
  92. ):
  93. with patch.dict(lvs_service.__opts__, {"test": True}):
  94. comt = "LVS Service lvsrs is present and needs to be removed"
  95. ret.update({"comment": comt})
  96. self.assertDictEqual(lvs_service.absent(name), ret)
  97. with patch.dict(lvs_service.__opts__, {"test": False}):
  98. comt = "LVS Service lvsrs has been removed"
  99. ret.update(
  100. {"comment": comt, "result": True, "changes": {"lvsrs": "Absent"}}
  101. )
  102. self.assertDictEqual(lvs_service.absent(name), ret)
  103. comt = "LVS Service lvsrs removed failed(False)"
  104. ret.update({"comment": comt, "result": False, "changes": {}})
  105. self.assertDictEqual(lvs_service.absent(name), ret)
  106. comt = "LVS Service lvsrs is not present, so it cannot be removed"
  107. ret.update({"comment": comt, "result": True})
  108. self.assertDictEqual(lvs_service.absent(name), ret)