test_disk.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Testing libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.unit import TestCase, skipIf
  10. from tests.support.mock import MagicMock, patch
  11. # Import Salt libs
  12. import salt.modules.disk as disk
  13. import salt.utils.path
  14. STUB_DISK_USAGE = {
  15. '/': {'filesystem': None, '1K-blocks': 10000, 'used': 10000, 'available': 10000, 'capacity': 10000},
  16. '/dev': {'filesystem': None, '1K-blocks': 10000, 'used': 10000, 'available': 10000, 'capacity': 10000},
  17. '/run': {'filesystem': None, '1K-blocks': 10000, 'used': 10000, 'available': 10000, 'capacity': 10000},
  18. '/run/lock': {'filesystem': None, '1K-blocks': 10000, 'used': 10000, 'available': 10000, 'capacity': 10000},
  19. '/run/shm': {'filesystem': None, '1K-blocks': 10000, 'used': 10000, 'available': 10000, 'capacity': 10000},
  20. '/run/user': {'filesystem': None, '1K-blocks': 10000, 'used': 10000, 'available': 10000, 'capacity': 10000},
  21. '/sys/fs/cgroup': {'filesystem': None, '1K-blocks': 10000, 'used': 10000, 'available': 10000, 'capacity': 10000}
  22. }
  23. STUB_DISK_INODEUSAGE = {
  24. '/': {'inodes': 10000, 'used': 10000, 'free': 10000, 'use': 10000, 'filesystem': None},
  25. '/dev': {'inodes': 10000, 'used': 10000, 'free': 10000, 'use': 10000, 'filesystem': None},
  26. '/run': {'inodes': 10000, 'used': 10000, 'free': 10000, 'use': 10000, 'filesystem': None},
  27. '/run/lock': {'inodes': 10000, 'used': 10000, 'free': 10000, 'use': 10000, 'filesystem': None},
  28. '/run/shm': {'inodes': 10000, 'used': 10000, 'free': 10000, 'use': 10000, 'filesystem': None},
  29. '/run/user': {'inodes': 10000, 'used': 10000, 'free': 10000, 'use': 10000, 'filesystem': None},
  30. '/sys/fs/cgroup': {'inodes': 10000, 'used': 10000, 'free': 10000, 'use': 10000, 'filesystem': None}
  31. }
  32. STUB_DISK_PERCENT = {
  33. '/': 50,
  34. '/dev': 10,
  35. '/run': 10,
  36. '/run/lock': 10,
  37. '/run/shm': 10,
  38. '/run/user': 10,
  39. '/sys/fs/cgroup': 10
  40. }
  41. STUB_DISK_BLKID = {'/dev/sda': {'TYPE': 'ext4', 'UUID': None}}
  42. class DiskTestCase(TestCase, LoaderModuleMockMixin):
  43. '''
  44. TestCase for salt.modules.disk module
  45. '''
  46. def setup_loader_modules(self):
  47. return {disk: {}}
  48. def test_usage_dict(self):
  49. with patch.dict(disk.__grains__, {'kernel': 'Linux'}), \
  50. patch('salt.modules.disk.usage',
  51. MagicMock(return_value=STUB_DISK_USAGE)):
  52. mock_cmd = MagicMock(return_value=1)
  53. with patch.dict(disk.__salt__, {'cmd.run': mock_cmd}):
  54. self.assertDictEqual(STUB_DISK_USAGE, disk.usage(args=None))
  55. def test_usage_none(self):
  56. with patch.dict(disk.__grains__, {'kernel': 'Linux'}), \
  57. patch('salt.modules.disk.usage', MagicMock(return_value='')):
  58. mock_cmd = MagicMock(return_value=1)
  59. with patch.dict(disk.__salt__, {'cmd.run': mock_cmd}):
  60. self.assertEqual('', disk.usage(args=None))
  61. def test_inodeusage(self):
  62. with patch.dict(disk.__grains__, {'kernel': 'OpenBSD'}), \
  63. patch('salt.modules.disk.inodeusage',
  64. MagicMock(return_value=STUB_DISK_INODEUSAGE)):
  65. mock = MagicMock()
  66. with patch.dict(disk.__salt__, {'cmd.run': mock}):
  67. self.assertDictEqual(STUB_DISK_INODEUSAGE, disk.inodeusage(args=None))
  68. def test_percent(self):
  69. with patch.dict(disk.__grains__, {'kernel': 'Linux'}), \
  70. patch('salt.modules.disk.percent',
  71. MagicMock(return_value=STUB_DISK_PERCENT)):
  72. mock = MagicMock()
  73. with patch.dict(disk.__salt__, {'cmd.run': mock}):
  74. self.assertDictEqual(STUB_DISK_PERCENT, disk.percent(args=None))
  75. def test_percent_args(self):
  76. with patch.dict(disk.__grains__, {'kernel': 'Linux'}), \
  77. patch('salt.modules.disk.percent', MagicMock(return_value='/')):
  78. mock = MagicMock()
  79. with patch.dict(disk.__salt__, {'cmd.run': mock}):
  80. self.assertEqual('/', disk.percent('/'))
  81. def test_blkid(self):
  82. with patch.dict(disk.__salt__, {'cmd.run_stdout': MagicMock(return_value=1)}), \
  83. patch('salt.modules.disk.blkid', MagicMock(return_value=STUB_DISK_BLKID)):
  84. self.assertDictEqual(STUB_DISK_BLKID, disk.blkid())
  85. def test_dump(self):
  86. mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
  87. with patch.dict(disk.__salt__, {'cmd.run_all': mock}):
  88. disk.dump('/dev/sda')
  89. mock.assert_called_once_with(
  90. 'blockdev --getro --getsz --getss --getpbsz --getiomin '
  91. '--getioopt --getalignoff --getmaxsect --getsize '
  92. '--getsize64 --getra --getfra /dev/sda',
  93. python_shell=False
  94. )
  95. def test_wipe(self):
  96. mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
  97. with patch.dict(disk.__salt__, {'cmd.run_all': mock}):
  98. disk.wipe('/dev/sda')
  99. mock.assert_called_once_with(
  100. 'wipefs -a /dev/sda',
  101. python_shell=False
  102. )
  103. def test_tune(self):
  104. mock = MagicMock(return_value='712971264\n512\n512\n512\n0\n0\n88\n712971264\n365041287168\n512\n512')
  105. with patch.dict(disk.__salt__, {'cmd.run': mock}):
  106. mock_dump = MagicMock(return_value={'retcode': 0, 'stdout': ''})
  107. with patch('salt.modules.disk.dump', mock_dump):
  108. kwargs = {'read-ahead': 512, 'filesystem-read-ahead': 1024}
  109. disk.tune('/dev/sda', **kwargs)
  110. self.assert_called_once(mock)
  111. args, kwargs = mock.call_args
  112. # Assert called once with either 'blockdev --setra 512 --setfra 512 /dev/sda' or
  113. # 'blockdev --setfra 512 --setra 512 /dev/sda' and python_shell=False kwarg.
  114. self.assertEqual(len(args), 1)
  115. self.assertTrue(args[0].startswith('blockdev '))
  116. self.assertTrue(args[0].endswith(' /dev/sda'))
  117. self.assertIn(' --setra 512 ', args[0])
  118. self.assertIn(' --setfra 1024 ', args[0])
  119. self.assertEqual(len(args[0].split()), 6)
  120. self.assertEqual(kwargs, {'python_shell': False})
  121. def test_format(self):
  122. '''
  123. unit tests for disk.format
  124. '''
  125. device = '/dev/sdX1'
  126. mock = MagicMock(return_value=0)
  127. with patch.dict(disk.__salt__, {'cmd.retcode': mock}),\
  128. patch('salt.utils.path.which', MagicMock(return_value=True)):
  129. self.assertEqual(disk.format_(device), True)
  130. @skipIf(not salt.utils.path.which('lsblk') and not salt.utils.path.which('df'),
  131. 'lsblk or df not found')
  132. def test_fstype(self):
  133. '''
  134. unit tests for disk.fstype
  135. '''
  136. device = '/dev/sdX1'
  137. fs_type = 'ext4'
  138. mock = MagicMock(return_value='FSTYPE\n{0}'.format(fs_type))
  139. with patch.dict(disk.__grains__, {'kernel': 'Linux'}), \
  140. patch.dict(disk.__salt__, {'cmd.run': mock}), \
  141. patch('salt.utils.path.which', MagicMock(return_value=True)):
  142. self.assertEqual(disk.fstype(device), fs_type)
  143. def test_resize2fs(self):
  144. '''
  145. unit tests for disk.resize2fs
  146. '''
  147. device = '/dev/sdX1'
  148. mock = MagicMock()
  149. with patch.dict(disk.__salt__, {'cmd.run_all': mock}), \
  150. patch('salt.utils.path.which', MagicMock(return_value=True)):
  151. disk.resize2fs(device)
  152. mock.assert_called_once_with('resize2fs {0}'.format(device), python_shell=False)