test_lvm.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. """
  2. :codeauthor: :email:`Shane Lee <slee@saltstack.com>`
  3. """
  4. # Import Python libs
  5. # Import Salt Libs
  6. import salt.grains.lvm as lvm
  7. # Import Salt Testing Libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.mock import MagicMock, patch
  10. from tests.support.unit import TestCase
  11. class LvmGrainsTestCase(TestCase, LoaderModuleMockMixin):
  12. """
  13. Test cases for LVM grains
  14. """
  15. def setup_loader_modules(self):
  16. return {
  17. lvm: {"__salt__": {}},
  18. }
  19. def test__linux_lvm(self):
  20. """
  21. Test grains._linux_lvm, normal return
  22. Should return a populated dictionary
  23. """
  24. vgs_out = " vg00\n vg01"
  25. lvs_out_vg00 = " root\n swap\n tmp \n usr \n var"
  26. lvs_out_vg01 = " opt \n"
  27. cmd_out = MagicMock(
  28. autospec=True, side_effect=[vgs_out, lvs_out_vg00, lvs_out_vg01]
  29. )
  30. patch_which = patch(
  31. "salt.utils.path.which", autospec=True, return_value="/usr/sbin/lvm"
  32. )
  33. patch_cmd_lvm = patch.dict(lvm.__salt__, {"cmd.run": cmd_out})
  34. with patch_which, patch_cmd_lvm:
  35. ret = lvm._linux_lvm()
  36. assert ret == {
  37. "lvm": {"vg00": ["root", "swap", "tmp", "usr", "var"], "vg01": ["opt"]}
  38. }, ret
  39. def test__linux_lvm_no_lvm(self):
  40. """
  41. Test grains._linux_lvm, no lvm installed
  42. Should return nothing
  43. """
  44. vgs_out = " vg00\n vg01"
  45. lvs_out_vg00 = " root\n swap\n tmp \n usr \n var"
  46. lvs_out_vg01 = " opt \n"
  47. cmd_out = MagicMock(
  48. autospec=True, side_effect=[vgs_out, lvs_out_vg00, lvs_out_vg01]
  49. )
  50. patch_which = patch("salt.utils.path.which", autospec=True, return_value="")
  51. patch_cmd_lvm = patch.dict(lvm.__salt__, {"cmd.run": cmd_out})
  52. with patch_which, patch_cmd_lvm:
  53. ret = lvm._linux_lvm()
  54. assert ret is None, ret
  55. def test__linux_lvm_no_logical_volumes(self):
  56. """
  57. Test grains._linux_lvm, lvm is installed but no volumes
  58. Should return a dictionary only with the header
  59. """
  60. vgs_out = ""
  61. cmd_out = MagicMock(autospec=True, side_effect=[vgs_out])
  62. patch_which = patch(
  63. "salt.utils.path.which", autospec=True, return_value="/usr/sbin/lvm"
  64. )
  65. patch_cmd_lvm = patch.dict(lvm.__salt__, {"cmd.run": cmd_out})
  66. with patch_which, patch_cmd_lvm:
  67. ret = lvm._linux_lvm()
  68. assert ret == {"lvm": {}}, ret
  69. def test__aix_lvm(self):
  70. """
  71. Test grains._aix_lvm, normal return
  72. Should return a populated dictionary
  73. """
  74. lsvg_out = "rootvg\nothervg"
  75. lsvg_out_rootvg = "rootvg:\n\
  76. LV NAME TYPE LPs PPs PVs LV STATE MOUNT POINT\n\
  77. hd5 boot 1 1 1 closed/syncd N/A\n\
  78. hd6 paging 32 32 1 open/syncd N/A\n\
  79. hd8 jfs2log 1 1 1 open/syncd N/A\n\
  80. hd4 jfs2 32 32 1 open/syncd /\n\
  81. hd2 jfs2 16 16 1 open/syncd /usr\n\
  82. hd9var jfs2 32 32 1 open/syncd /var\n\
  83. hd3 jfs2 32 32 1 open/syncd /tmp\n\
  84. hd1 jfs2 16 16 1 open/syncd /home\n\
  85. hd10opt jfs2 16 16 1 open/syncd /opt"
  86. lsvg_out_othervg = "othervg:\n\
  87. LV NAME TYPE LPs PPs PVs LV STATE MOUNT POINT\n\
  88. loglv01 jfs2log 1 1 1 open/syncd N/A\n\
  89. datalv jfs2 16 16 1 open/syncd /data"
  90. cmd_out = MagicMock(
  91. autospec=True, side_effect=[lsvg_out, lsvg_out_rootvg, lsvg_out_othervg]
  92. )
  93. patch_which = patch(
  94. "salt.utils.path.which", autospec=True, return_value="/usr/sbin/lsvg"
  95. )
  96. patch_cmd_lvm = patch.dict(lvm.__salt__, {"cmd.run": cmd_out})
  97. with patch_which, patch_cmd_lvm:
  98. ret = lvm._aix_lvm()
  99. assert ret == {
  100. "lvm": {
  101. "rootvg": [
  102. "hd5",
  103. "hd6",
  104. "hd8",
  105. "hd4",
  106. "hd2",
  107. "hd9var",
  108. "hd3",
  109. "hd1",
  110. "hd10opt",
  111. ],
  112. "othervg": ["loglv01", "datalv"],
  113. }
  114. }, ret