test_parted_partition.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Dave Rawks (dave@pandora.com)
  4. tests.unit.modules.parted_test
  5. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  6. '''
  7. # Import Python libs
  8. from __future__ import absolute_import, unicode_literals, print_function
  9. # Import Salt Testing libs
  10. from tests.support.mixins import LoaderModuleMockMixin
  11. from tests.support.unit import TestCase
  12. from tests.support.mock import MagicMock, patch
  13. # Import Salt libs
  14. from salt.exceptions import CommandExecutionError
  15. import salt.modules.parted_partition as parted
  16. class PartedTestCase(TestCase, LoaderModuleMockMixin):
  17. def setup_loader_modules(self):
  18. self.cmdrun = MagicMock()
  19. self.cmdrun_stdout = MagicMock()
  20. self.addCleanup(delattr, self, 'cmdrun')
  21. self.addCleanup(delattr, self, 'cmdrun_stdout')
  22. return {
  23. parted: {
  24. '__salt__': {
  25. 'cmd.run': self.cmdrun,
  26. 'cmd.run_stdout': self.cmdrun_stdout
  27. }
  28. }
  29. }
  30. # Test __virtual__ function for module registration
  31. def test_virtual_bails_on_windows(self):
  32. '''
  33. If running windows, __virtual__ shouldn't register module
  34. '''
  35. with patch('salt.utils.platform.is_windows', lambda: True):
  36. ret = parted.__virtual__()
  37. err = (False, 'The parted execution module failed to load Windows systems are not supported.')
  38. self.assertEqual(err, ret)
  39. def test_virtual_bails_without_parted(self):
  40. '''
  41. If parted not in PATH, __virtual__ shouldn't register module
  42. '''
  43. with patch('salt.utils.path.which', lambda exe: not exe == "parted"),\
  44. patch('salt.utils.platform.is_windows', return_value=False):
  45. ret = parted.__virtual__()
  46. err = (False, 'The parted execution module failed to load parted binary is not in the path.')
  47. self.assertEqual(err, ret)
  48. def test_virtual_bails_without_lsblk(self):
  49. '''
  50. If lsblk not in PATH, __virtual__ shouldn't register module
  51. '''
  52. with patch('salt.utils.path.which', lambda exe: not exe == "lsblk"),\
  53. patch('salt.utils.platform.is_windows', return_value=False):
  54. ret = parted.__virtual__()
  55. err = (False, 'The parted execution module failed to load lsblk binary is not in the path.')
  56. self.assertEqual(err, ret)
  57. def test_virtual_bails_without_partprobe(self):
  58. '''
  59. If partprobe not in PATH, __virtual__ shouldn't register module
  60. '''
  61. with patch('salt.utils.path.which', lambda exe: not exe == "partprobe"),\
  62. patch('salt.utils.platform.is_windows', return_value=False):
  63. ret = parted.__virtual__()
  64. err = (False, 'The parted execution module failed to load partprobe binary is not in the path.')
  65. self.assertEqual(err, ret)
  66. def test_virtual(self):
  67. '''
  68. On expected platform with correct utils in PATH, register "partition" module
  69. '''
  70. with patch('salt.utils.platform.is_windows', lambda: False), \
  71. patch('salt.utils.path.which', lambda exe: exe in ('parted', 'lsblk', 'partprobe')):
  72. ret = parted.__virtual__()
  73. expect = 'partition'
  74. self.assertEqual(ret, expect)
  75. # Test probe function
  76. def test_probe_wo_args(self):
  77. parted.probe()
  78. self.cmdrun.assert_called_once_with('partprobe -- ')
  79. def test_probe_w_single_arg(self):
  80. with patch('salt.modules.parted_partition._validate_device', MagicMock()):
  81. parted.probe('/dev/sda')
  82. self.cmdrun.assert_called_once_with('partprobe -- /dev/sda')
  83. def test_probe_w_multiple_args(self):
  84. with patch('salt.modules.parted_partition._validate_device', MagicMock()):
  85. parted.probe('/dev/sda', '/dev/sdb')
  86. self.cmdrun.assert_called_once_with('partprobe -- /dev/sda /dev/sdb')
  87. # Test _list function
  88. @staticmethod
  89. def parted_print_output(k):
  90. output = {
  91. "valid": (
  92. '''BYT;\n'''
  93. '''/dev/sda:4000GB:scsi:512:512:gpt:AMCC 9650SE-24M DISK:;\n'''
  94. '''1:17.4kB:150MB:150MB:ext3::boot;\n'''
  95. '''2:3921GB:4000GB:79.3GB:linux-swap(v1)::;\n'''
  96. ),
  97. "valid chs": (
  98. '''CHS;\n'''
  99. '''/dev/sda:3133,0,2:scsi:512:512:gpt:AMCC 9650SE-24M DISK:;\n'''
  100. '''1:0,0,34:2431,134,43:ext3::boot;\n'''
  101. '''2:2431,134,44:2492,80,42:linux-swap(v1)::;\n'''
  102. ),
  103. "valid_legacy": (
  104. '''BYT;\n'''
  105. '''/dev/sda:4000GB:scsi:512:512:gpt:AMCC 9650SE-24M DISK;\n'''
  106. '''1:17.4kB:150MB:150MB:ext3::boot;\n'''
  107. '''2:3921GB:4000GB:79.3GB:linux-swap(v1)::;\n'''
  108. ),
  109. "empty": '',
  110. "bad_label_info": (
  111. '''BYT;\n'''
  112. '''badbadbadbad\n'''
  113. '''1:17.4kB:150MB:150MB:ext3::boot;\n'''
  114. '''2:3921GB:4000GB:79.3GB:linux-swap(v1)::;\n'''
  115. ),
  116. "bad_header": (
  117. '''badbadbadbad\n'''
  118. '''/dev/sda:4000GB:scsi:512:512:gpt:AMCC 9650SE-24M DISK:;\n'''
  119. '''1:17.4kB:150MB:150MB:ext3::boot;\n'''
  120. '''2:3921GB:4000GB:79.3GB:linux-swap(v1)::;\n'''
  121. ),
  122. "bad_partition": (
  123. '''BYT;\n'''
  124. '''/dev/sda:4000GB:scsi:512:512:gpt:AMCC 9650SE-24M DISK:;\n'''
  125. '''badbadbadbad\n'''
  126. '''2:3921GB:4000GB:79.3GB:linux-swap(v1)::;\n'''
  127. ),
  128. }
  129. return output[k]
  130. def test_list__without_device(self):
  131. self.assertRaises(TypeError, parted.list_)
  132. def test_list__empty_cmd_output(self):
  133. with patch('salt.modules.parted_partition._validate_device', MagicMock()):
  134. self.cmdrun_stdout.return_value = self.parted_print_output('empty')
  135. output = parted.list_('/dev/sda')
  136. self.cmdrun_stdout.assert_called_once_with('parted -m -s /dev/sda print')
  137. expected = {'info': {}, 'partitions': {}}
  138. self.assertEqual(output, expected)
  139. def test_list__valid_unit_empty_cmd_output(self):
  140. with patch('salt.modules.parted_partition._validate_device', MagicMock()):
  141. self.cmdrun_stdout.return_value = self.parted_print_output('empty')
  142. output = parted.list_('/dev/sda', unit='s')
  143. self.cmdrun_stdout.assert_called_once_with('parted -m -s /dev/sda unit s print')
  144. expected = {'info': {}, 'partitions': {}}
  145. self.assertEqual(output, expected)
  146. def test_list__invalid_unit(self):
  147. self.assertRaises(CommandExecutionError, parted.list_, '/dev/sda',
  148. unit='badbadbad')
  149. self.assertFalse(self.cmdrun.called)
  150. def test_list__bad_header(self):
  151. with patch('salt.modules.parted_partition._validate_device', MagicMock()):
  152. self.cmdrun_stdout.return_value = self.parted_print_output('bad_header')
  153. self.assertRaises(CommandExecutionError, parted.list_, '/dev/sda')
  154. self.cmdrun_stdout.assert_called_once_with('parted -m -s /dev/sda print')
  155. def test_list__bad_label_info(self):
  156. with patch('salt.modules.parted_partition._validate_device', MagicMock()):
  157. self.cmdrun_stdout.return_value = self.parted_print_output('bad_label_info')
  158. self.assertRaises(CommandExecutionError, parted.list_, '/dev/sda')
  159. self.cmdrun_stdout.assert_called_once_with('parted -m -s /dev/sda print')
  160. def test_list__bad_partition(self):
  161. with patch('salt.modules.parted_partition._validate_device', MagicMock()):
  162. self.cmdrun_stdout.return_value = self.parted_print_output('bad_partition')
  163. self.assertRaises(CommandExecutionError, parted.list_, '/dev/sda')
  164. self.cmdrun_stdout.assert_called_once_with('parted -m -s /dev/sda print')
  165. def test_list__valid_cmd_output(self):
  166. with patch('salt.modules.parted_partition._validate_device', MagicMock()):
  167. self.cmdrun_stdout.return_value = self.parted_print_output('valid')
  168. output = parted.list_('/dev/sda')
  169. self.cmdrun_stdout.assert_called_once_with('parted -m -s /dev/sda print')
  170. expected = {
  171. 'info': {
  172. 'logical sector': '512',
  173. 'physical sector': '512',
  174. 'interface': 'scsi',
  175. 'model': 'AMCC 9650SE-24M DISK',
  176. 'disk': '/dev/sda',
  177. 'disk flags': '',
  178. 'partition table': 'gpt',
  179. 'size': '4000GB'
  180. },
  181. 'partitions': {
  182. '1': {
  183. 'end': '150MB',
  184. 'number': '1',
  185. 'start': '17.4kB',
  186. 'file system': 'ext3',
  187. 'flags': 'boot',
  188. 'name': '',
  189. 'size': '150MB'},
  190. '2': {
  191. 'end': '4000GB',
  192. 'number': '2',
  193. 'start': '3921GB',
  194. 'file system': 'linux-swap(v1)',
  195. 'flags': '',
  196. 'name': '',
  197. 'size': '79.3GB'
  198. }
  199. }
  200. }
  201. self.assertEqual(output, expected)
  202. def test_list__valid_unit_valid_cmd_output(self):
  203. with patch('salt.modules.parted_partition._validate_device', MagicMock()):
  204. self.cmdrun_stdout.return_value = self.parted_print_output('valid')
  205. output = parted.list_('/dev/sda', unit='s')
  206. self.cmdrun_stdout.assert_called_once_with('parted -m -s /dev/sda unit s print')
  207. expected = {
  208. 'info': {
  209. 'logical sector': '512',
  210. 'physical sector': '512',
  211. 'interface': 'scsi',
  212. 'model': 'AMCC 9650SE-24M DISK',
  213. 'disk': '/dev/sda',
  214. 'disk flags': '',
  215. 'partition table': 'gpt',
  216. 'size': '4000GB'
  217. },
  218. 'partitions': {
  219. '1': {
  220. 'end': '150MB',
  221. 'number': '1',
  222. 'start': '17.4kB',
  223. 'file system': 'ext3',
  224. 'flags': 'boot',
  225. 'name': '',
  226. 'size': '150MB'},
  227. '2': {
  228. 'end': '4000GB',
  229. 'number': '2',
  230. 'start': '3921GB',
  231. 'file system': 'linux-swap(v1)',
  232. 'flags': '',
  233. 'name': '',
  234. 'size': '79.3GB'
  235. }
  236. }
  237. }
  238. self.assertEqual(output, expected)
  239. def test_list__valid_unit_chs_valid_cmd_output(self):
  240. with patch('salt.modules.parted_partition._validate_device', MagicMock()):
  241. self.cmdrun_stdout.return_value = self.parted_print_output('valid chs')
  242. output = parted.list_('/dev/sda', unit='chs')
  243. self.cmdrun_stdout.assert_called_once_with('parted -m -s /dev/sda unit chs print')
  244. expected = {
  245. 'info': {
  246. 'logical sector': '512',
  247. 'physical sector': '512',
  248. 'interface': 'scsi',
  249. 'model': 'AMCC 9650SE-24M DISK',
  250. 'disk': '/dev/sda',
  251. 'disk flags': '',
  252. 'partition table': 'gpt',
  253. 'size': '3133,0,2'
  254. },
  255. 'partitions': {
  256. '1': {
  257. 'end': '2431,134,43',
  258. 'number': '1',
  259. 'start': '0,0,34',
  260. 'file system': 'ext3',
  261. 'flags': 'boot',
  262. 'name': ''},
  263. '2': {
  264. 'end': '2492,80,42',
  265. 'number': '2',
  266. 'start': '2431,134,44',
  267. 'file system': 'linux-swap(v1)',
  268. 'flags': '',
  269. 'name': ''}
  270. }
  271. }
  272. self.assertEqual(output, expected)
  273. def test_list__valid_legacy_cmd_output(self):
  274. with patch('salt.modules.parted_partition._validate_device', MagicMock()):
  275. self.cmdrun_stdout.return_value = self.parted_print_output('valid_legacy')
  276. output = parted.list_('/dev/sda')
  277. self.cmdrun_stdout.assert_called_once_with('parted -m -s /dev/sda print')
  278. expected = {
  279. 'info': {
  280. 'logical sector': '512',
  281. 'physical sector': '512',
  282. 'interface': 'scsi',
  283. 'model': 'AMCC 9650SE-24M DISK',
  284. 'disk': '/dev/sda',
  285. 'partition table': 'gpt',
  286. 'size': '4000GB'
  287. },
  288. 'partitions': {
  289. '1': {
  290. 'end': '150MB',
  291. 'number': '1',
  292. 'start': '17.4kB',
  293. 'file system': 'ext3',
  294. 'flags': 'boot',
  295. 'name': '',
  296. 'size': '150MB'},
  297. '2': {
  298. 'end': '4000GB',
  299. 'number': '2',
  300. 'start': '3921GB',
  301. 'file system': 'linux-swap(v1)',
  302. 'flags': '',
  303. 'name': '',
  304. 'size': '79.3GB'
  305. }
  306. }
  307. }
  308. self.assertEqual(output, expected)
  309. def test_list__valid_unit_valid_legacy_cmd_output(self):
  310. with patch('salt.modules.parted_partition._validate_device', MagicMock()):
  311. self.cmdrun_stdout.return_value = self.parted_print_output('valid_legacy')
  312. output = parted.list_('/dev/sda', unit='s')
  313. self.cmdrun_stdout.assert_called_once_with('parted -m -s /dev/sda unit s print')
  314. expected = {
  315. 'info': {
  316. 'logical sector': '512',
  317. 'physical sector': '512',
  318. 'interface': 'scsi',
  319. 'model': 'AMCC 9650SE-24M DISK',
  320. 'disk': '/dev/sda',
  321. 'partition table': 'gpt',
  322. 'size': '4000GB'
  323. },
  324. 'partitions': {
  325. '1': {
  326. 'end': '150MB',
  327. 'number': '1',
  328. 'start': '17.4kB',
  329. 'file system': 'ext3',
  330. 'flags': 'boot',
  331. 'name': '',
  332. 'size': '150MB'},
  333. '2': {
  334. 'end': '4000GB',
  335. 'number': '2',
  336. 'start': '3921GB',
  337. 'file system': 'linux-swap(v1)',
  338. 'flags': '',
  339. 'name': '',
  340. 'size': '79.3GB'
  341. }
  342. }
  343. }
  344. self.assertEqual(output, expected)