test_disks.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: :email:`Shane Lee <slee@saltstack.com>`
  4. """
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import textwrap
  8. # Import Salt Libs
  9. import salt.grains.disks as disks
  10. # Import Salt Testing Libs
  11. from tests.support.mixins import LoaderModuleMockMixin
  12. from tests.support.mock import MagicMock, mock_open, patch
  13. from tests.support.unit import TestCase
  14. class DisksGrainsTestCase(TestCase, LoaderModuleMockMixin):
  15. """
  16. Test cases for disks grains
  17. """
  18. def setup_loader_modules(self):
  19. return {
  20. disks: {"__salt__": {}},
  21. }
  22. def test__windows_disks(self):
  23. """
  24. Test grains._windows_disks, normal return
  25. Should return a populated dictionary
  26. """
  27. mock_which = MagicMock(return_value="C:\\Windows\\System32\\wbem\\WMIC.exe")
  28. wmic_result = textwrap.dedent(
  29. """
  30. DeviceId MediaType
  31. 0 4
  32. 1 0
  33. 2 3
  34. 3 5
  35. """
  36. )
  37. mock_run_all = MagicMock(return_value={"stdout": wmic_result, "retcode": 0})
  38. with patch("salt.utils.path.which", mock_which), patch.dict(
  39. disks.__salt__, {"cmd.run_all": mock_run_all}
  40. ):
  41. result = disks._windows_disks()
  42. expected = {
  43. "ssds": ["\\\\.\\PhysicalDrive0"],
  44. "disks": [
  45. "\\\\.\\PhysicalDrive0",
  46. "\\\\.\\PhysicalDrive1",
  47. "\\\\.\\PhysicalDrive2",
  48. "\\\\.\\PhysicalDrive3",
  49. ],
  50. }
  51. self.assertDictEqual(result, expected)
  52. cmd = " ".join(
  53. [
  54. "C:\\Windows\\System32\\wbem\\WMIC.exe",
  55. "/namespace:\\\\root\\microsoft\\windows\\storage",
  56. "path",
  57. "MSFT_PhysicalDisk",
  58. "get",
  59. "DeviceID,MediaType",
  60. "/format:table",
  61. ]
  62. )
  63. mock_run_all.assert_called_once_with(cmd)
  64. def test__windows_disks_retcode(self):
  65. """
  66. Test grains._windows_disks, retcode 1
  67. Should return empty lists
  68. """
  69. mock_which = MagicMock(return_value="C:\\Windows\\System32\\wbem\\WMIC.exe")
  70. mock_run_all = MagicMock(return_value={"stdout": "", "retcode": 1})
  71. with patch("salt.utils.path.which", mock_which), patch.dict(
  72. disks.__salt__, {"cmd.run_all": mock_run_all}
  73. ):
  74. result = disks._windows_disks()
  75. expected = {"ssds": [], "disks": []}
  76. self.assertDictEqual(result, expected)
  77. def test__linux_disks(self):
  78. """
  79. Test grains._linux_disks, normal return
  80. Should return a populated dictionary
  81. """
  82. files = [
  83. "/sys/block/asm!.asm_ctl_vbg0",
  84. "/sys/block/dm-0",
  85. "/sys/block/loop0",
  86. "/sys/block/ram0",
  87. "/sys/block/sda",
  88. "/sys/block/sdb",
  89. "/sys/block/vda",
  90. ]
  91. links = [
  92. "../devices/virtual/block/asm!.asm_ctl_vbg0",
  93. "../devices/virtual/block/dm-0",
  94. "../devices/virtual/block/loop0",
  95. "../devices/virtual/block/ram0",
  96. "../devices/pci0000:00/0000:00:1f.2/ata1/host0/target0:0:0/0:0:0:0/block/sda",
  97. "../devices/pci0000:35/0000:35:00.0/0000:36:00.0/host2/target2:1:0/2:1:0:0/block/sdb",
  98. "../devices/pci0000L00:0000:00:05.0/virtio2/block/vda",
  99. ]
  100. contents = [
  101. "1",
  102. "1",
  103. "1",
  104. "0",
  105. "1",
  106. "1",
  107. "1",
  108. ]
  109. patch_glob = patch("glob.glob", autospec=True, return_value=files)
  110. patch_readlink = patch(
  111. "salt.utils.path.readlink", autospec=True, side_effect=links
  112. )
  113. patch_fopen = patch("salt.utils.files.fopen", mock_open(read_data=contents))
  114. with patch_glob, patch_readlink, patch_fopen:
  115. ret = disks._linux_disks()
  116. assert ret == {"disks": ["sda", "sdb", "vda"], "ssds": []}, ret