test_btrfs.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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
  10. from tests.support.mock import (
  11. mock_open,
  12. MagicMock,
  13. patch,
  14. )
  15. # Import Salt Libs
  16. import salt.utils.files
  17. import salt.utils.fsutils
  18. import salt.modules.btrfs as btrfs
  19. from salt.exceptions import CommandExecutionError
  20. class BtrfsTestCase(TestCase, LoaderModuleMockMixin):
  21. '''
  22. Test cases for salt.modules.btrfs
  23. '''
  24. def setup_loader_modules(self):
  25. return {btrfs: {}}
  26. # 'version' function tests: 1
  27. def test_version(self):
  28. '''
  29. Test if it return BTRFS version.
  30. '''
  31. mock = MagicMock(return_value={'retcode': 1,
  32. 'stderr': '',
  33. 'stdout': 'Salt'})
  34. with patch.dict(btrfs.__salt__, {'cmd.run_all': mock}):
  35. self.assertDictEqual(btrfs.version(), {'version': 'Salt'})
  36. # 'info' function tests: 1
  37. def test_info(self):
  38. '''
  39. Test if it get BTRFS filesystem information.
  40. '''
  41. with patch('salt.utils.fsutils._verify_run', MagicMock(return_value=True)):
  42. mock = MagicMock(return_value={'retcode': 1,
  43. 'stderr': '',
  44. 'stdout': 'Salt'})
  45. with patch.dict(btrfs.__salt__, {'cmd.run_all': mock}):
  46. mock = MagicMock(return_value={'Salt': 'salt'})
  47. with patch.object(btrfs, '_parse_btrfs_info', mock):
  48. self.assertDictEqual(btrfs.info('/dev/sda1'),
  49. {'Salt': 'salt'})
  50. # 'devices' function tests: 1
  51. def test_devices(self):
  52. '''
  53. Test if it get known BTRFS formatted devices on the system.
  54. '''
  55. with patch('salt.utils.fsutils._blkid_output',
  56. MagicMock(return_value='Salt')):
  57. mock = MagicMock(return_value={'retcode': 1,
  58. 'stderr': '',
  59. 'stdout': 'Salt'})
  60. with patch.dict(btrfs.__salt__, {'cmd.run_all': mock}):
  61. self.assertEqual(btrfs.devices(), 'Salt')
  62. # 'defragment' function tests: 2
  63. def test_defragment(self):
  64. '''
  65. Test if it defragment mounted BTRFS filesystem.
  66. '''
  67. with patch('salt.utils.fsutils._is_device', MagicMock(return_value=False)):
  68. with patch('os.path.exists', MagicMock(return_value=True)):
  69. ret = [{'range': '/dev/sda1',
  70. 'mount_point': False,
  71. 'log': False, 'passed': True}]
  72. mock_run = MagicMock(return_value={'retcode': 1,
  73. 'stderr': '',
  74. 'stdout': 'Salt'})
  75. with patch.dict(btrfs.__salt__, {'cmd.run_all': mock_run}):
  76. mock_file = mock_open(read_data='/dev/sda1 / ext4 rw,data=ordered 0 0')
  77. with patch.object(salt.utils.files, 'fopen', mock_file):
  78. self.assertListEqual(btrfs.defragment('/dev/sda1'), ret)
  79. def test_defragment_error(self):
  80. '''
  81. Test if it gives device not mount error
  82. '''
  83. with patch('salt.utils.fsutils._is_device', MagicMock(return_value=True)):
  84. mock_run = MagicMock(return_value={'retcode': 1,
  85. 'stderr': '',
  86. 'stdout': 'Salt'})
  87. with patch.dict(btrfs.__salt__, {'cmd.run_all': mock_run}):
  88. mock_file = mock_open(read_data='/dev/sda1 / ext4 rw,data=ordered 0 0')
  89. with patch.object(salt.utils.files, 'fopen', mock_file):
  90. self.assertRaises(CommandExecutionError, btrfs.defragment,
  91. '/dev/sda1')
  92. # 'features' function tests: 1
  93. def test_features(self):
  94. '''
  95. Test if it list currently available BTRFS features.
  96. '''
  97. with patch('salt.utils.fsutils._verify_run', MagicMock(return_value=True)):
  98. mock = MagicMock(return_value={'retcode': 1,
  99. 'stderr': '',
  100. 'stdout': 'Salt'})
  101. with patch.dict(btrfs.__salt__, {'cmd.run_all': mock}):
  102. self.assertDictEqual(btrfs.features(), {})
  103. # 'usage' function tests: 1
  104. def test_usage(self):
  105. '''
  106. Test if it shows in which disk the chunks are allocated.
  107. '''
  108. with patch('salt.utils.fsutils._verify_run', MagicMock(return_value=True)):
  109. mock = MagicMock(return_value={'retcode': 1,
  110. 'stderr': '',
  111. 'stdout': 'Salt'})
  112. with patch.dict(btrfs.__salt__, {'cmd.run_all': mock}):
  113. mock = MagicMock(return_value={'Salt': 'salt'})
  114. with patch.object(btrfs, '_usage_specific', mock):
  115. self.assertDictEqual(btrfs.usage('/dev/sda1'),
  116. {'Salt': 'salt'})
  117. mock = MagicMock(return_value={'retcode': 1,
  118. 'stderr': '',
  119. 'stdout': 'Unallocated:\n'})
  120. with patch.dict(btrfs.__salt__, {'cmd.run_all': mock}):
  121. mock = MagicMock(return_value={'/dev/sda1': True})
  122. with patch.object(btrfs, '_usage_unallocated', mock):
  123. self.assertDictEqual(btrfs.usage('/dev/sda1'),
  124. {'unallocated': {'/dev/sda1': True}})
  125. mock = MagicMock(return_value={'retcode': 1,
  126. 'stderr': '',
  127. 'stdout': 'Overall:\n'})
  128. with patch.dict(btrfs.__salt__, {'cmd.run_all': mock}):
  129. mock = MagicMock(return_value={'/dev/sda1': True})
  130. with patch.object(btrfs, '_usage_overall', mock):
  131. self.assertDictEqual(btrfs.usage('/dev/sda1'),
  132. {'overall': {'/dev/sda1': True}})
  133. # 'mkfs' function tests: 3
  134. def test_mkfs(self):
  135. '''
  136. Test if it create a file system on the specified device.
  137. '''
  138. mock_cmd = MagicMock(return_value={'retcode': 1,
  139. 'stderr': '',
  140. 'stdout': 'Salt'})
  141. mock_info = MagicMock(return_value=[])
  142. with patch.dict(btrfs.__salt__, {'cmd.run_all': mock_cmd,
  143. 'btrfs.info': mock_info}):
  144. mock_file = mock_open(read_data='/dev/sda1 / ext4 rw,data=ordered 0 0')
  145. with patch.object(salt.utils.files, 'fopen', mock_file):
  146. self.assertDictEqual(btrfs.mkfs('/dev/sda1'), {'log': 'Salt'})
  147. def test_mkfs_error(self):
  148. '''
  149. Test if it No devices specified error
  150. '''
  151. self.assertRaises(CommandExecutionError, btrfs.mkfs)
  152. def test_mkfs_mount_error(self):
  153. '''
  154. Test if it device mount error
  155. '''
  156. mock = MagicMock(return_value={'/dev/sda1': True})
  157. with patch.object(salt.utils.fsutils, '_get_mounts', mock):
  158. self.assertRaises(CommandExecutionError, btrfs.mkfs, '/dev/sda1')
  159. # 'resize' function tests: 4
  160. def test_resize(self):
  161. '''
  162. Test if it resize filesystem.
  163. '''
  164. with patch('salt.utils.fsutils._is_device', MagicMock(return_value=True)):
  165. mock = MagicMock(return_value={'retcode': 1,
  166. 'stderr': '',
  167. 'stdout': 'Salt'})
  168. mock_info = MagicMock(return_value=[])
  169. with patch.dict(btrfs.__salt__, {'cmd.run_all': mock,
  170. 'btrfs.info': mock_info}):
  171. mock = MagicMock(return_value={'/dev/sda1': True})
  172. with patch.object(salt.utils.fsutils, '_get_mounts', mock):
  173. self.assertDictEqual(btrfs.resize('/dev/sda1', 'max'),
  174. {'log': 'Salt'})
  175. def test_resize_valid_error(self):
  176. '''
  177. Test if it gives device should be mounted error
  178. '''
  179. with patch('salt.utils.fsutils._is_device', MagicMock(return_value=False)):
  180. mock = MagicMock(return_value={'retcode': 1,
  181. 'stderr': '',
  182. 'stdout': 'Salt'})
  183. with patch.dict(btrfs.__salt__, {'cmd.run_all': mock}):
  184. self.assertRaises(CommandExecutionError, btrfs.resize,
  185. '/dev/sda1', 'max')
  186. def test_resize_mount_error(self):
  187. '''
  188. Test if it gives mount point error
  189. '''
  190. with patch('salt.utils.fsutils._is_device', MagicMock(return_value=True)):
  191. mock = MagicMock(return_value={'/dev/sda1': False})
  192. with patch.object(salt.utils.fsutils, '_get_mounts', mock):
  193. self.assertRaises(CommandExecutionError, btrfs.resize,
  194. '/dev/sda1', 'max')
  195. def test_resize_size_error(self):
  196. '''
  197. Test if it gives unknown size error
  198. '''
  199. self.assertRaises(CommandExecutionError, btrfs.resize,
  200. '/dev/sda1', '250m')
  201. # 'convert' function tests: 5
  202. def test_convert(self):
  203. '''
  204. Test if it convert ext2/3/4 to BTRFS
  205. '''
  206. with patch('os.path.exists', MagicMock(return_value=True)):
  207. ret = {'after': {'balance_log': 'Salt',
  208. 'ext4_image': 'removed',
  209. 'ext4_image_info': 'N/A',
  210. 'fsck_status': 'N/A',
  211. 'mount_point': None,
  212. 'type': 'ext4'},
  213. 'before': {'fsck_status': 'Filesystem errors corrected',
  214. 'mount_point': None,
  215. 'type': 'ext4'}}
  216. mock = MagicMock(return_value={'retcode': 1,
  217. 'stderr': '',
  218. 'stdout': 'Salt'})
  219. with patch.dict(btrfs.__salt__, {'cmd.run_all': mock}):
  220. mock = MagicMock(return_value={'/dev/sda3': {'type': 'ext4'}})
  221. with patch.object(salt.utils.fsutils, '_blkid_output', mock):
  222. mock = MagicMock(return_value={'/dev/sda3': [{'mount_point': None}]})
  223. with patch.object(salt.utils.fsutils, '_get_mounts', mock):
  224. self.assertDictEqual(btrfs.convert('/dev/sda3', permanent=True),
  225. ret)
  226. def test_convert_device_error(self):
  227. '''
  228. Test if it gives device not found error
  229. '''
  230. mock = MagicMock(return_value={'retcode': 1,
  231. 'stderr': '',
  232. 'stdout': 'Salt'})
  233. with patch.dict(btrfs.__salt__, {'cmd.run_all': mock}):
  234. mock = MagicMock(return_value={'/dev/sda1': False})
  235. with patch.object(salt.utils.fsutils, '_blkid_output', mock):
  236. self.assertRaises(CommandExecutionError, btrfs.convert,
  237. '/dev/sda1')
  238. def test_convert_filesystem_error(self):
  239. '''
  240. Test if it gives file system error
  241. '''
  242. with patch('salt.utils.fsutils._is_device', MagicMock(return_value=True)):
  243. mock = MagicMock(return_value={'retcode': 1,
  244. 'stderr': '',
  245. 'stdout': 'Salt'})
  246. with patch.dict(btrfs.__salt__, {'cmd.run_all': mock}):
  247. mock = MagicMock(return_value={'/dev/sda1': {'type': 'ext'}})
  248. with patch.object(salt.utils.fsutils, '_blkid_output', mock):
  249. self.assertRaises(CommandExecutionError, btrfs.convert,
  250. '/dev/sda1')
  251. def test_convert_error(self):
  252. '''
  253. Test if it gives error cannot convert root
  254. '''
  255. with patch('salt.utils.fsutils._is_device', MagicMock(return_value=True)):
  256. mock = MagicMock(return_value={'retcode': 1,
  257. 'stderr': '',
  258. 'stdout': 'Salt'})
  259. with patch.dict(btrfs.__salt__, {'cmd.run_all': mock}):
  260. mock = MagicMock(return_value={'/dev/sda1': {'type': 'ext4',
  261. 'mount_point': '/'}})
  262. with patch.object(salt.utils.fsutils, '_blkid_output', mock):
  263. mock = MagicMock(return_value={'/dev/sda1':
  264. [{'mount_point': '/'}]})
  265. with patch.object(salt.utils.fsutils, '_get_mounts', mock):
  266. self.assertRaises(CommandExecutionError, btrfs.convert,
  267. '/dev/sda1')
  268. def test_convert_migration_error(self):
  269. '''
  270. Test if it gives migration error
  271. '''
  272. with patch('salt.utils.fsutils._is_device', MagicMock(return_value=True)):
  273. mock_run = MagicMock(return_value={'retcode': 1,
  274. 'stderr': '',
  275. 'stdout': 'Salt'})
  276. with patch.dict(btrfs.__salt__, {'cmd.run_all': mock_run}):
  277. mock_blk = MagicMock(return_value={'/dev/sda1': {'type': 'ext4'}})
  278. with patch.object(salt.utils.fsutils, '_blkid_output', mock_blk):
  279. mock_file = mock_open(read_data='/dev/sda1 / ext4 rw,data=ordered 0 0')
  280. with patch.object(salt.utils.files, 'fopen', mock_file):
  281. self.assertRaises(CommandExecutionError, btrfs.convert,
  282. '/dev/sda1')
  283. # 'add' function tests: 1
  284. def test_add(self):
  285. '''
  286. Test if it add a devices to a BTRFS filesystem.
  287. '''
  288. with patch('salt.modules.btrfs._restripe', MagicMock(return_value={})):
  289. self.assertDictEqual(btrfs.add('/mountpoint', '/dev/sda1', '/dev/sda2'), {})
  290. # 'delete' function tests: 1
  291. def test_delete(self):
  292. '''
  293. Test if it delete a devices to a BTRFS filesystem.
  294. '''
  295. with patch('salt.modules.btrfs._restripe', MagicMock(return_value={})):
  296. self.assertDictEqual(btrfs.delete('/mountpoint', '/dev/sda1',
  297. '/dev/sda2'), {})
  298. # 'properties' function tests: 1
  299. def test_properties(self):
  300. '''
  301. Test if list properties for given btrfs object
  302. '''
  303. with patch('salt.utils.fsutils._verify_run', MagicMock(return_value=True)):
  304. mock = MagicMock(return_value={'retcode': 1,
  305. 'stderr': '',
  306. 'stdout': 'Salt'})
  307. with patch.dict(btrfs.__salt__, {'cmd.run_all': mock}):
  308. self.assertDictEqual(btrfs.properties('/dev/sda1', 'subvol'), {})
  309. def test_properties_unknown_error(self):
  310. '''
  311. Test if it gives unknown property error
  312. '''
  313. self.assertRaises(CommandExecutionError, btrfs.properties,
  314. '/dev/sda1', 'a')
  315. def test_properties_error(self):
  316. '''
  317. Test if it gives exception error
  318. '''
  319. self.assertRaises(CommandExecutionError, btrfs.properties,
  320. '/dev/sda1', 'subvol', True)