test_powerpath.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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.powerpath as powerpath
  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 PowerpathTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Test cases for salt.states.powerpath
  16. """
  17. def setup_loader_modules(self):
  18. return {powerpath: {}}
  19. # 'license_present' function tests: 1
  20. def test_license_present(self):
  21. """
  22. Test to ensures that the specified PowerPath license key is present
  23. on the host.
  24. """
  25. name = "mylic"
  26. ret = {"name": name, "changes": {}, "result": True, "comment": ""}
  27. mock_t = MagicMock(
  28. side_effect=[
  29. {"result": True, "output": name},
  30. {"result": False, "output": name},
  31. ]
  32. )
  33. mock = MagicMock(side_effect=[False, True, True, True, True])
  34. mock_l = MagicMock(return_value=[{"key": name}])
  35. with patch.dict(
  36. powerpath.__salt__,
  37. {
  38. "powerpath.has_powerpath": mock,
  39. "powerpath.list_licenses": mock_l,
  40. "powerpath.add_license": mock_t,
  41. },
  42. ):
  43. comt = "PowerPath is not installed."
  44. ret.update({"comment": comt, "result": False})
  45. self.assertDictEqual(powerpath.license_present(name), ret)
  46. comt = "License key {0} already present".format(name)
  47. ret.update({"comment": comt, "result": True})
  48. self.assertDictEqual(powerpath.license_present(name), ret)
  49. with patch.dict(powerpath.__opts__, {"test": True}):
  50. comt = "License key Mylic is set to be added"
  51. ret.update({"comment": comt, "result": None, "name": "Mylic"})
  52. self.assertDictEqual(powerpath.license_present("Mylic"), ret)
  53. with patch.dict(powerpath.__opts__, {"test": False}):
  54. ret.update(
  55. {"comment": name, "result": True, "changes": {"Mylic": "added"}}
  56. )
  57. self.assertDictEqual(powerpath.license_present("Mylic"), ret)
  58. ret.update({"result": False, "changes": {}})
  59. self.assertDictEqual(powerpath.license_present("Mylic"), ret)
  60. # 'license_absent' function tests: 1
  61. def test_license_absent(self):
  62. """
  63. Test to ensures that the specified PowerPath license key is absent
  64. on the host.
  65. """
  66. name = "mylic"
  67. ret = {"name": name, "changes": {}, "result": True, "comment": ""}
  68. mock_t = MagicMock(
  69. side_effect=[
  70. {"result": True, "output": name},
  71. {"result": False, "output": name},
  72. ]
  73. )
  74. mock = MagicMock(side_effect=[False, True, True, True, True])
  75. mock_l = MagicMock(return_value=[{"key": "salt"}])
  76. with patch.dict(
  77. powerpath.__salt__,
  78. {
  79. "powerpath.has_powerpath": mock,
  80. "powerpath.list_licenses": mock_l,
  81. "powerpath.remove_license": mock_t,
  82. },
  83. ):
  84. comt = "PowerPath is not installed."
  85. ret.update({"comment": comt, "result": False})
  86. self.assertDictEqual(powerpath.license_absent(name), ret)
  87. comt = "License key {0} not present".format(name)
  88. ret.update({"comment": comt, "result": True})
  89. self.assertDictEqual(powerpath.license_absent(name), ret)
  90. with patch.dict(powerpath.__opts__, {"test": True}):
  91. comt = "License key salt is set to be removed"
  92. ret.update({"comment": comt, "result": None, "name": "salt"})
  93. self.assertDictEqual(powerpath.license_absent("salt"), ret)
  94. with patch.dict(powerpath.__opts__, {"test": False}):
  95. ret.update(
  96. {"comment": name, "result": True, "changes": {"salt": "removed"}}
  97. )
  98. self.assertDictEqual(powerpath.license_absent("salt"), ret)
  99. ret.update({"result": False, "changes": {}})
  100. self.assertDictEqual(powerpath.license_absent("salt"), ret)