test_diskusage.py 7.2 KB

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