test_lvm.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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.lvm as lvm
  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 LvmTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Test cases for salt.states.lvm
  16. """
  17. def setup_loader_modules(self):
  18. return {lvm: {}}
  19. # 'pv_present' function tests: 1
  20. def test_pv_present(self):
  21. """
  22. Test to set a physical device to be used as an LVM physical volume
  23. """
  24. name = "/dev/sda5"
  25. comt = "Physical Volume {0} already present".format(name)
  26. ret = {"name": name, "changes": {}, "result": True, "comment": comt}
  27. mock = MagicMock(side_effect=[True, False])
  28. with patch.dict(lvm.__salt__, {"lvm.pvdisplay": mock}):
  29. self.assertDictEqual(lvm.pv_present(name), ret)
  30. comt = "Physical Volume {0} is set to be created".format(name)
  31. ret.update({"comment": comt, "result": None})
  32. with patch.dict(lvm.__opts__, {"test": True}):
  33. self.assertDictEqual(lvm.pv_present(name), ret)
  34. # 'pv_absent' function tests: 1
  35. def test_pv_absent(self):
  36. """
  37. Test to ensure that a Physical Device is not being used by lvm
  38. """
  39. name = "/dev/sda5"
  40. comt = "Physical Volume {0} does not exist".format(name)
  41. ret = {"name": name, "changes": {}, "result": True, "comment": comt}
  42. mock = MagicMock(side_effect=[False, True])
  43. with patch.dict(lvm.__salt__, {"lvm.pvdisplay": mock}):
  44. self.assertDictEqual(lvm.pv_absent(name), ret)
  45. comt = "Physical Volume {0} is set to be removed".format(name)
  46. ret.update({"comment": comt, "result": None})
  47. with patch.dict(lvm.__opts__, {"test": True}):
  48. self.assertDictEqual(lvm.pv_absent(name), ret)
  49. # 'vg_present' function tests: 1
  50. def test_vg_present(self):
  51. """
  52. Test to create an LVM volume group
  53. """
  54. name = "/dev/sda5"
  55. comt = "Failed to create Volume Group {0}".format(name)
  56. ret = {"name": name, "changes": {}, "result": False, "comment": comt}
  57. mock = MagicMock(return_value=False)
  58. with patch.dict(lvm.__salt__, {"lvm.vgdisplay": mock, "lvm.vgcreate": mock}):
  59. with patch.dict(lvm.__opts__, {"test": False}):
  60. self.assertDictEqual(lvm.vg_present(name), ret)
  61. comt = "Volume Group {0} is set to be created".format(name)
  62. ret.update({"comment": comt, "result": None})
  63. with patch.dict(lvm.__opts__, {"test": True}):
  64. self.assertDictEqual(lvm.vg_present(name), ret)
  65. # 'vg_absent' function tests: 1
  66. def test_vg_absent(self):
  67. """
  68. Test to remove an LVM volume group
  69. """
  70. name = "/dev/sda5"
  71. comt = "Volume Group {0} already absent".format(name)
  72. ret = {"name": name, "changes": {}, "result": True, "comment": comt}
  73. mock = MagicMock(side_effect=[False, True])
  74. with patch.dict(lvm.__salt__, {"lvm.vgdisplay": mock}):
  75. self.assertDictEqual(lvm.vg_absent(name), ret)
  76. comt = "Volume Group {0} is set to be removed".format(name)
  77. ret.update({"comment": comt, "result": None})
  78. with patch.dict(lvm.__opts__, {"test": True}):
  79. self.assertDictEqual(lvm.vg_absent(name), ret)
  80. # 'lv_present' function tests: 1
  81. def test_lv_present(self):
  82. """
  83. Test to create a new logical volume
  84. """
  85. name = "/dev/sda5"
  86. comt = "Logical Volume {0} already present".format(name)
  87. ret = {"name": name, "changes": {}, "result": True, "comment": comt}
  88. mock = MagicMock(side_effect=[True, False])
  89. with patch.dict(lvm.__salt__, {"lvm.lvdisplay": mock}):
  90. self.assertDictEqual(lvm.lv_present(name), ret)
  91. comt = "Logical Volume {0} is set to be created".format(name)
  92. ret.update({"comment": comt, "result": None})
  93. with patch.dict(lvm.__opts__, {"test": True}):
  94. self.assertDictEqual(lvm.lv_present(name), ret)
  95. def test_lv_present_with_force(self):
  96. """
  97. Test to create a new logical volume with force=True
  98. """
  99. name = "/dev/sda5"
  100. comt = "Logical Volume {0} already present".format(name)
  101. ret = {"name": name, "changes": {}, "result": True, "comment": comt}
  102. mock = MagicMock(side_effect=[True, False])
  103. with patch.dict(lvm.__salt__, {"lvm.lvdisplay": mock}):
  104. self.assertDictEqual(lvm.lv_present(name, force=True), ret)
  105. comt = "Logical Volume {0} is set to be created".format(name)
  106. ret.update({"comment": comt, "result": None})
  107. with patch.dict(lvm.__opts__, {"test": True}):
  108. self.assertDictEqual(lvm.lv_present(name, force=True), ret)
  109. # 'lv_absent' function tests: 1
  110. def test_lv_absent(self):
  111. """
  112. Test to remove a given existing logical volume
  113. from a named existing volume group
  114. """
  115. name = "/dev/sda5"
  116. comt = "Logical Volume {0} already absent".format(name)
  117. ret = {"name": name, "changes": {}, "result": True, "comment": comt}
  118. mock = MagicMock(side_effect=[False, True])
  119. with patch.dict(lvm.__salt__, {"lvm.lvdisplay": mock}):
  120. self.assertDictEqual(lvm.lv_absent(name), ret)
  121. comt = "Logical Volume {0} is set to be removed".format(name)
  122. ret.update({"comment": comt, "result": None})
  123. with patch.dict(lvm.__opts__, {"test": True}):
  124. self.assertDictEqual(lvm.lv_absent(name), ret)