test_diskusage.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 TestCase
  7. from tests.support.mock import 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. class DiskUsageBeaconTestCase(TestCase, LoaderModuleMockMixin):
  43. '''
  44. Test case for salt.beacons.adb
  45. '''
  46. def setup_loader_modules(self):
  47. return {}
  48. def test_non_list_config(self):
  49. config = {}
  50. ret = diskusage.validate(config)
  51. self.assertEqual(ret, (False, 'Configuration for diskusage beacon must'
  52. ' be a list.'))
  53. def test_empty_config(self):
  54. config = [{}]
  55. ret = diskusage.validate(config)
  56. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  57. def test_diskusage_match(self):
  58. disk_usage_mock = Mock(side_effect=STUB_DISK_USAGE)
  59. with patch('salt.utils.platform.is_windows',
  60. MagicMock(return_value=False)),\
  61. patch('psutil.disk_partitions',
  62. MagicMock(return_value=STUB_DISK_PARTITION)), \
  63. patch('psutil.disk_usage', disk_usage_mock):
  64. config = [{'/': '50%'}]
  65. ret = diskusage.validate(config)
  66. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  67. ret = diskusage.beacon(config)
  68. self.assertEqual(ret, [{'diskusage': 50, 'mount': '/'}])
  69. def test_diskusage_nomatch(self):
  70. disk_usage_mock = Mock(side_effect=STUB_DISK_USAGE)
  71. with patch('salt.utils.platform.is_windows',
  72. MagicMock(return_value=False)),\
  73. patch('psutil.disk_partitions',
  74. MagicMock(return_value=STUB_DISK_PARTITION)), \
  75. patch('psutil.disk_usage', disk_usage_mock):
  76. config = [{'/': '70%'}]
  77. ret = diskusage.validate(config)
  78. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  79. ret = diskusage.beacon(config)
  80. self.assertNotEqual(ret, [{'diskusage': 50, 'mount': '/'}])
  81. def test_diskusage_match_regex(self):
  82. disk_usage_mock = Mock(side_effect=STUB_DISK_USAGE)
  83. with patch('salt.utils.platform.is_windows',
  84. MagicMock(return_value=False)),\
  85. patch('psutil.disk_partitions',
  86. MagicMock(return_value=STUB_DISK_PARTITION)), \
  87. patch('psutil.disk_usage', disk_usage_mock):
  88. config = [{r'^\/': '50%'}]
  89. ret = diskusage.validate(config)
  90. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  91. ret = diskusage.beacon(config)
  92. self.assertEqual(ret, [{'diskusage': 50, 'mount': '/'}])
  93. def test_diskusage_windows_single_slash(self):
  94. r'''
  95. This tests new behavior (C:\)
  96. '''
  97. disk_usage_mock = Mock(return_value=WINDOWS_STUB_DISK_USAGE)
  98. with patch('salt.utils.platform.is_windows',
  99. MagicMock(return_value=True)):
  100. with patch('psutil.disk_partitions',
  101. MagicMock(return_value=WINDOWS_STUB_DISK_PARTITION)), \
  102. patch('psutil.disk_usage', disk_usage_mock):
  103. config = [{'C:\\': '50%'}]
  104. ret = diskusage.validate(config)
  105. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  106. ret = diskusage.beacon(config)
  107. self.assertEqual(ret, [{'diskusage': 50, 'mount': 'C:\\'}])
  108. def test_diskusage_windows_double_slash(self):
  109. '''
  110. This tests original behavior (C:\\)
  111. '''
  112. disk_usage_mock = Mock(return_value=WINDOWS_STUB_DISK_USAGE)
  113. with patch('salt.utils.platform.is_windows',
  114. MagicMock(return_value=True)):
  115. with patch('psutil.disk_partitions',
  116. MagicMock(return_value=WINDOWS_STUB_DISK_PARTITION)), \
  117. patch('psutil.disk_usage', disk_usage_mock):
  118. config = [{'C:\\\\': '50%'}]
  119. ret = diskusage.validate(config)
  120. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  121. ret = diskusage.beacon(config)
  122. self.assertEqual(ret, [{'diskusage': 50, 'mount': 'C:\\'}])
  123. def test_diskusage_windows_lowercase(self):
  124. r'''
  125. This tests lowercase drive letter (c:\)
  126. '''
  127. disk_usage_mock = Mock(return_value=WINDOWS_STUB_DISK_USAGE)
  128. with patch('salt.utils.platform.is_windows',
  129. MagicMock(return_value=True)):
  130. with patch('psutil.disk_partitions',
  131. MagicMock(return_value=WINDOWS_STUB_DISK_PARTITION)), \
  132. patch('psutil.disk_usage', disk_usage_mock):
  133. config = [{'c:\\': '50%'}]
  134. ret = diskusage.validate(config)
  135. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  136. ret = diskusage.beacon(config)
  137. self.assertEqual(ret, [{'diskusage': 50, 'mount': 'C:\\'}])
  138. def test_diskusage_windows_match_regex(self):
  139. disk_usage_mock = Mock(return_value=WINDOWS_STUB_DISK_USAGE)
  140. with patch('salt.utils.platform.is_windows',
  141. MagicMock(return_value=True)):
  142. with patch('psutil.disk_partitions',
  143. MagicMock(return_value=WINDOWS_STUB_DISK_PARTITION)), \
  144. patch('psutil.disk_usage', disk_usage_mock):
  145. config = [{'^[a-zA-Z]:\\': '50%'}]
  146. ret = diskusage.validate(config)
  147. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  148. ret = diskusage.beacon(config)
  149. _expected = [{u'diskusage': 50, u'mount': 'C:\\'},
  150. {u'diskusage': 50, u'mount': 'D:\\'}]
  151. self.assertEqual(ret, _expected)