1
0

test_diskusage.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. # coding: utf-8
  2. # Python libs
  3. from __future__ import absolute_import
  4. from collections import namedtuple
  5. # Salt libs
  6. import salt.beacons.diskusage as diskusage
  7. from tests.support.mixins import LoaderModuleMockMixin
  8. from tests.support.mock import MagicMock, Mock, patch
  9. # Salt testing libs
  10. from tests.support.unit import TestCase
  11. STUB_DISK_PARTITION = [
  12. namedtuple("partition", "device mountpoint fstype, opts")(
  13. "tmpfs", "/mnt/tmp", "tmpfs", "rw,nosuid,nodev,relatime,size=10240k"
  14. ),
  15. namedtuple("partition", "device mountpoint fstype, opts")(
  16. "/dev/disk0s2", "/", "hfs", "rw,local,rootfs,dovolfs,journaled,multilabel"
  17. ),
  18. ]
  19. WINDOWS_STUB_DISK_PARTITION = [
  20. namedtuple("partition", "device mountpoint fstype, opts")(
  21. "C:\\", "C:\\", "NTFS", "rw,fixed"
  22. ),
  23. namedtuple("partition", "device mountpoint fstype, opts")(
  24. "D:\\", "D:\\", "CDFS", "ro,cdrom"
  25. ),
  26. ]
  27. STUB_DISK_USAGE = [
  28. namedtuple("usage", "total used free percent")(1000, 500, 500, 50),
  29. namedtuple("usage", "total used free percent")(100, 75, 25, 25),
  30. ]
  31. WINDOWS_STUB_DISK_USAGE = namedtuple("usage", "total used free percent")(
  32. 1000, 500, 500, 50
  33. )
  34. class DiskUsageBeaconTestCase(TestCase, LoaderModuleMockMixin):
  35. """
  36. Test case for salt.beacons.adb
  37. """
  38. def setup_loader_modules(self):
  39. return {}
  40. def test_non_list_config(self):
  41. config = {}
  42. ret = diskusage.validate(config)
  43. self.assertEqual(
  44. ret, (False, "Configuration for diskusage beacon must be a list.")
  45. )
  46. def test_empty_config(self):
  47. config = [{}]
  48. ret = diskusage.validate(config)
  49. self.assertEqual(ret, (True, "Valid beacon configuration"))
  50. def test_diskusage_match(self):
  51. disk_usage_mock = Mock(side_effect=STUB_DISK_USAGE)
  52. with patch(
  53. "salt.utils.platform.is_windows", MagicMock(return_value=False)
  54. ), patch(
  55. "psutil.disk_partitions", MagicMock(return_value=STUB_DISK_PARTITION)
  56. ), patch(
  57. "psutil.disk_usage", disk_usage_mock
  58. ):
  59. config = [{"/": "50%"}]
  60. ret = diskusage.validate(config)
  61. self.assertEqual(ret, (True, "Valid beacon configuration"))
  62. ret = diskusage.beacon(config)
  63. self.assertEqual(ret, [{"diskusage": 50, "mount": "/"}])
  64. def test_diskusage_nomatch(self):
  65. disk_usage_mock = Mock(side_effect=STUB_DISK_USAGE)
  66. with patch(
  67. "salt.utils.platform.is_windows", MagicMock(return_value=False)
  68. ), patch(
  69. "psutil.disk_partitions", MagicMock(return_value=STUB_DISK_PARTITION)
  70. ), patch(
  71. "psutil.disk_usage", disk_usage_mock
  72. ):
  73. config = [{"/": "70%"}]
  74. ret = diskusage.validate(config)
  75. self.assertEqual(ret, (True, "Valid beacon configuration"))
  76. ret = diskusage.beacon(config)
  77. self.assertNotEqual(ret, [{"diskusage": 50, "mount": "/"}])
  78. def test_diskusage_match_regex(self):
  79. disk_usage_mock = Mock(side_effect=STUB_DISK_USAGE)
  80. with patch(
  81. "salt.utils.platform.is_windows", MagicMock(return_value=False)
  82. ), patch(
  83. "psutil.disk_partitions", MagicMock(return_value=STUB_DISK_PARTITION)
  84. ), patch(
  85. "psutil.disk_usage", disk_usage_mock
  86. ):
  87. config = [{r"^\/": "50%"}]
  88. ret = diskusage.validate(config)
  89. self.assertEqual(ret, (True, "Valid beacon configuration"))
  90. ret = diskusage.beacon(config)
  91. self.assertEqual(ret, [{"diskusage": 50, "mount": "/"}])
  92. def test_diskusage_windows_single_slash(self):
  93. r"""
  94. This tests new behavior (C:\)
  95. """
  96. disk_usage_mock = Mock(return_value=WINDOWS_STUB_DISK_USAGE)
  97. with patch("salt.utils.platform.is_windows", MagicMock(return_value=True)):
  98. with patch(
  99. "psutil.disk_partitions",
  100. MagicMock(return_value=WINDOWS_STUB_DISK_PARTITION),
  101. ), patch("psutil.disk_usage", disk_usage_mock):
  102. config = [{"C:\\": "50%"}]
  103. ret = diskusage.validate(config)
  104. self.assertEqual(ret, (True, "Valid beacon configuration"))
  105. ret = diskusage.beacon(config)
  106. self.assertEqual(ret, [{"diskusage": 50, "mount": "C:\\"}])
  107. def test_diskusage_windows_double_slash(self):
  108. """
  109. This tests original behavior (C:\\)
  110. """
  111. disk_usage_mock = Mock(return_value=WINDOWS_STUB_DISK_USAGE)
  112. with patch("salt.utils.platform.is_windows", MagicMock(return_value=True)):
  113. with patch(
  114. "psutil.disk_partitions",
  115. MagicMock(return_value=WINDOWS_STUB_DISK_PARTITION),
  116. ), patch("psutil.disk_usage", disk_usage_mock):
  117. config = [{"C:\\\\": "50%"}]
  118. ret = diskusage.validate(config)
  119. self.assertEqual(ret, (True, "Valid beacon configuration"))
  120. ret = diskusage.beacon(config)
  121. self.assertEqual(ret, [{"diskusage": 50, "mount": "C:\\"}])
  122. def test_diskusage_windows_lowercase(self):
  123. r"""
  124. This tests lowercase drive letter (c:\)
  125. """
  126. disk_usage_mock = Mock(return_value=WINDOWS_STUB_DISK_USAGE)
  127. with patch("salt.utils.platform.is_windows", MagicMock(return_value=True)):
  128. with patch(
  129. "psutil.disk_partitions",
  130. MagicMock(return_value=WINDOWS_STUB_DISK_PARTITION),
  131. ), patch("psutil.disk_usage", disk_usage_mock):
  132. config = [{"c:\\": "50%"}]
  133. ret = diskusage.validate(config)
  134. self.assertEqual(ret, (True, "Valid beacon configuration"))
  135. ret = diskusage.beacon(config)
  136. self.assertEqual(ret, [{"diskusage": 50, "mount": "C:\\"}])
  137. def test_diskusage_windows_match_regex(self):
  138. disk_usage_mock = Mock(return_value=WINDOWS_STUB_DISK_USAGE)
  139. with patch("salt.utils.platform.is_windows", MagicMock(return_value=True)):
  140. with patch(
  141. "psutil.disk_partitions",
  142. MagicMock(return_value=WINDOWS_STUB_DISK_PARTITION),
  143. ), patch("psutil.disk_usage", disk_usage_mock):
  144. config = [{"^[a-zA-Z]:\\": "50%"}]
  145. ret = diskusage.validate(config)
  146. self.assertEqual(ret, (True, "Valid beacon configuration"))
  147. ret = diskusage.beacon(config)
  148. _expected = [
  149. {u"diskusage": 50, u"mount": "C:\\"},
  150. {u"diskusage": 50, u"mount": "D:\\"},
  151. ]
  152. self.assertEqual(ret, _expected)