test_lvm.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, unicode_literals, print_function
  7. # Import Salt Testing Libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.unit import TestCase
  10. from tests.support.mock import (
  11. MagicMock,
  12. patch
  13. )
  14. # Import Salt Libs
  15. import salt.states.lvm as lvm
  16. class LvmTestCase(TestCase, LoaderModuleMockMixin):
  17. '''
  18. Test cases for salt.states.lvm
  19. '''
  20. def setup_loader_modules(self):
  21. return {lvm: {}}
  22. # 'pv_present' function tests: 1
  23. def test_pv_present(self):
  24. '''
  25. Test to set a physical device to be used as an LVM physical volume
  26. '''
  27. name = '/dev/sda5'
  28. comt = ('Physical Volume {0} already present'.format(name))
  29. ret = {'name': name,
  30. 'changes': {},
  31. 'result': True,
  32. 'comment': comt}
  33. mock = MagicMock(side_effect=[True, False])
  34. with patch.dict(lvm.__salt__, {'lvm.pvdisplay': mock}):
  35. self.assertDictEqual(lvm.pv_present(name), ret)
  36. comt = ('Physical Volume {0} is set to be created'.format(name))
  37. ret.update({'comment': comt, 'result': None})
  38. with patch.dict(lvm.__opts__, {'test': True}):
  39. self.assertDictEqual(lvm.pv_present(name), ret)
  40. # 'pv_absent' function tests: 1
  41. def test_pv_absent(self):
  42. '''
  43. Test to ensure that a Physical Device is not being used by lvm
  44. '''
  45. name = '/dev/sda5'
  46. comt = ('Physical Volume {0} does not exist'.format(name))
  47. ret = {'name': name,
  48. 'changes': {},
  49. 'result': True,
  50. 'comment': comt}
  51. mock = MagicMock(side_effect=[False, True])
  52. with patch.dict(lvm.__salt__, {'lvm.pvdisplay': mock}):
  53. self.assertDictEqual(lvm.pv_absent(name), ret)
  54. comt = ('Physical Volume {0} is set to be removed'.format(name))
  55. ret.update({'comment': comt, 'result': None})
  56. with patch.dict(lvm.__opts__, {'test': True}):
  57. self.assertDictEqual(lvm.pv_absent(name), ret)
  58. # 'vg_present' function tests: 1
  59. def test_vg_present(self):
  60. '''
  61. Test to create an LVM volume group
  62. '''
  63. name = '/dev/sda5'
  64. comt = ('Failed to create Volume Group {0}'.format(name))
  65. ret = {'name': name,
  66. 'changes': {},
  67. 'result': False,
  68. 'comment': comt}
  69. mock = MagicMock(return_value=False)
  70. with patch.dict(lvm.__salt__, {'lvm.vgdisplay': mock,
  71. 'lvm.vgcreate': mock}):
  72. with patch.dict(lvm.__opts__, {'test': False}):
  73. self.assertDictEqual(lvm.vg_present(name), ret)
  74. comt = ('Volume Group {0} is set to be created'.format(name))
  75. ret.update({'comment': comt, 'result': None})
  76. with patch.dict(lvm.__opts__, {'test': True}):
  77. self.assertDictEqual(lvm.vg_present(name), ret)
  78. # 'vg_absent' function tests: 1
  79. def test_vg_absent(self):
  80. '''
  81. Test to remove an LVM volume group
  82. '''
  83. name = '/dev/sda5'
  84. comt = ('Volume Group {0} already absent'.format(name))
  85. ret = {'name': name,
  86. 'changes': {},
  87. 'result': True,
  88. 'comment': comt}
  89. mock = MagicMock(side_effect=[False, True])
  90. with patch.dict(lvm.__salt__, {'lvm.vgdisplay': mock}):
  91. self.assertDictEqual(lvm.vg_absent(name), ret)
  92. comt = ('Volume Group {0} is set to be removed'.format(name))
  93. ret.update({'comment': comt, 'result': None})
  94. with patch.dict(lvm.__opts__, {'test': True}):
  95. self.assertDictEqual(lvm.vg_absent(name), ret)
  96. # 'lv_present' function tests: 1
  97. def test_lv_present(self):
  98. '''
  99. Test to create a new logical volume
  100. '''
  101. name = '/dev/sda5'
  102. comt = ('Logical Volume {0} already present'.format(name))
  103. ret = {'name': name,
  104. 'changes': {},
  105. 'result': True,
  106. 'comment': comt}
  107. mock = MagicMock(side_effect=[True, False])
  108. with patch.dict(lvm.__salt__, {'lvm.lvdisplay': mock}):
  109. self.assertDictEqual(lvm.lv_present(name), ret)
  110. comt = ('Logical Volume {0} is set to be created'.format(name))
  111. ret.update({'comment': comt, 'result': None})
  112. with patch.dict(lvm.__opts__, {'test': True}):
  113. self.assertDictEqual(lvm.lv_present(name), ret)
  114. def test_lv_present_with_force(self):
  115. '''
  116. Test to create a new logical volume with force=True
  117. '''
  118. name = '/dev/sda5'
  119. comt = ('Logical Volume {0} already present'.format(name))
  120. ret = {'name': name,
  121. 'changes': {},
  122. 'result': True,
  123. 'comment': comt}
  124. mock = MagicMock(side_effect=[True, False])
  125. with patch.dict(lvm.__salt__, {'lvm.lvdisplay': mock}):
  126. self.assertDictEqual(lvm.lv_present(name, force=True), ret)
  127. comt = ('Logical Volume {0} is set to be created'.format(name))
  128. ret.update({'comment': comt, 'result': None})
  129. with patch.dict(lvm.__opts__, {'test': True}):
  130. self.assertDictEqual(lvm.lv_present(name, force=True), ret)
  131. # 'lv_absent' function tests: 1
  132. def test_lv_absent(self):
  133. '''
  134. Test to remove a given existing logical volume
  135. from a named existing volume group
  136. '''
  137. name = '/dev/sda5'
  138. comt = ('Logical Volume {0} already absent'.format(name))
  139. ret = {'name': name,
  140. 'changes': {},
  141. 'result': True,
  142. 'comment': comt}
  143. mock = MagicMock(side_effect=[False, True])
  144. with patch.dict(lvm.__salt__, {'lvm.lvdisplay': mock}):
  145. self.assertDictEqual(lvm.lv_absent(name), ret)
  146. comt = ('Logical Volume {0} is set to be removed'.format(name))
  147. ret.update({'comment': comt, 'result': None})
  148. with patch.dict(lvm.__opts__, {'test': True}):
  149. self.assertDictEqual(lvm.lv_absent(name), ret)