test_disk.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 libs
  8. import salt.modules.disk as disk
  9. import salt.utils.path
  10. import salt.utils.platform
  11. # Import Salt Testing libs
  12. from tests.support.mixins import LoaderModuleMockMixin
  13. from tests.support.mock import MagicMock, patch
  14. from tests.support.unit import TestCase, skipIf
  15. STUB_DISK_USAGE = {
  16. "/": {
  17. "filesystem": None,
  18. "1K-blocks": 10000,
  19. "used": 10000,
  20. "available": 10000,
  21. "capacity": 10000,
  22. },
  23. "/dev": {
  24. "filesystem": None,
  25. "1K-blocks": 10000,
  26. "used": 10000,
  27. "available": 10000,
  28. "capacity": 10000,
  29. },
  30. "/run": {
  31. "filesystem": None,
  32. "1K-blocks": 10000,
  33. "used": 10000,
  34. "available": 10000,
  35. "capacity": 10000,
  36. },
  37. "/run/lock": {
  38. "filesystem": None,
  39. "1K-blocks": 10000,
  40. "used": 10000,
  41. "available": 10000,
  42. "capacity": 10000,
  43. },
  44. "/run/shm": {
  45. "filesystem": None,
  46. "1K-blocks": 10000,
  47. "used": 10000,
  48. "available": 10000,
  49. "capacity": 10000,
  50. },
  51. "/run/user": {
  52. "filesystem": None,
  53. "1K-blocks": 10000,
  54. "used": 10000,
  55. "available": 10000,
  56. "capacity": 10000,
  57. },
  58. "/sys/fs/cgroup": {
  59. "filesystem": None,
  60. "1K-blocks": 10000,
  61. "used": 10000,
  62. "available": 10000,
  63. "capacity": 10000,
  64. },
  65. }
  66. STUB_DISK_INODEUSAGE = {
  67. "/": {
  68. "inodes": 10000,
  69. "used": 10000,
  70. "free": 10000,
  71. "use": 10000,
  72. "filesystem": None,
  73. },
  74. "/dev": {
  75. "inodes": 10000,
  76. "used": 10000,
  77. "free": 10000,
  78. "use": 10000,
  79. "filesystem": None,
  80. },
  81. "/run": {
  82. "inodes": 10000,
  83. "used": 10000,
  84. "free": 10000,
  85. "use": 10000,
  86. "filesystem": None,
  87. },
  88. "/run/lock": {
  89. "inodes": 10000,
  90. "used": 10000,
  91. "free": 10000,
  92. "use": 10000,
  93. "filesystem": None,
  94. },
  95. "/run/shm": {
  96. "inodes": 10000,
  97. "used": 10000,
  98. "free": 10000,
  99. "use": 10000,
  100. "filesystem": None,
  101. },
  102. "/run/user": {
  103. "inodes": 10000,
  104. "used": 10000,
  105. "free": 10000,
  106. "use": 10000,
  107. "filesystem": None,
  108. },
  109. "/sys/fs/cgroup": {
  110. "inodes": 10000,
  111. "used": 10000,
  112. "free": 10000,
  113. "use": 10000,
  114. "filesystem": None,
  115. },
  116. }
  117. STUB_DISK_PERCENT = {
  118. "/": 50,
  119. "/dev": 10,
  120. "/run": 10,
  121. "/run/lock": 10,
  122. "/run/shm": 10,
  123. "/run/user": 10,
  124. "/sys/fs/cgroup": 10,
  125. }
  126. STUB_DISK_BLKID = {"/dev/sda": {"TYPE": "ext4", "UUID": None}}
  127. class DiskTestCase(TestCase, LoaderModuleMockMixin):
  128. """
  129. TestCase for salt.modules.disk module
  130. """
  131. def setup_loader_modules(self):
  132. return {disk: {}}
  133. def test_usage_dict(self):
  134. with patch.dict(disk.__grains__, {"kernel": "Linux"}), patch(
  135. "salt.modules.disk.usage", MagicMock(return_value=STUB_DISK_USAGE)
  136. ):
  137. mock_cmd = MagicMock(return_value=1)
  138. with patch.dict(disk.__salt__, {"cmd.run": mock_cmd}):
  139. self.assertDictEqual(STUB_DISK_USAGE, disk.usage(args=None))
  140. def test_usage_none(self):
  141. with patch.dict(disk.__grains__, {"kernel": "Linux"}), patch(
  142. "salt.modules.disk.usage", MagicMock(return_value="")
  143. ):
  144. mock_cmd = MagicMock(return_value=1)
  145. with patch.dict(disk.__salt__, {"cmd.run": mock_cmd}):
  146. self.assertEqual("", disk.usage(args=None))
  147. def test_inodeusage(self):
  148. with patch.dict(disk.__grains__, {"kernel": "OpenBSD"}), patch(
  149. "salt.modules.disk.inodeusage", MagicMock(return_value=STUB_DISK_INODEUSAGE)
  150. ):
  151. mock = MagicMock()
  152. with patch.dict(disk.__salt__, {"cmd.run": mock}):
  153. self.assertDictEqual(STUB_DISK_INODEUSAGE, disk.inodeusage(args=None))
  154. def test_percent(self):
  155. with patch.dict(disk.__grains__, {"kernel": "Linux"}), patch(
  156. "salt.modules.disk.percent", MagicMock(return_value=STUB_DISK_PERCENT)
  157. ):
  158. mock = MagicMock()
  159. with patch.dict(disk.__salt__, {"cmd.run": mock}):
  160. self.assertDictEqual(STUB_DISK_PERCENT, disk.percent(args=None))
  161. def test_percent_args(self):
  162. with patch.dict(disk.__grains__, {"kernel": "Linux"}), patch(
  163. "salt.modules.disk.percent", MagicMock(return_value="/")
  164. ):
  165. mock = MagicMock()
  166. with patch.dict(disk.__salt__, {"cmd.run": mock}):
  167. self.assertEqual("/", disk.percent("/"))
  168. def test_blkid(self):
  169. with patch.dict(
  170. disk.__salt__, {"cmd.run_stdout": MagicMock(return_value=1)}
  171. ), patch("salt.modules.disk.blkid", MagicMock(return_value=STUB_DISK_BLKID)):
  172. self.assertDictEqual(STUB_DISK_BLKID, disk.blkid())
  173. def test_dump(self):
  174. mock = MagicMock(return_value={"retcode": 0, "stdout": ""})
  175. with patch.dict(disk.__salt__, {"cmd.run_all": mock}):
  176. disk.dump("/dev/sda")
  177. mock.assert_called_once_with(
  178. "blockdev --getro --getsz --getss --getpbsz --getiomin "
  179. "--getioopt --getalignoff --getmaxsect --getsize "
  180. "--getsize64 --getra --getfra /dev/sda",
  181. python_shell=False,
  182. )
  183. def test_wipe(self):
  184. mock = MagicMock(return_value={"retcode": 0, "stdout": ""})
  185. with patch.dict(disk.__salt__, {"cmd.run_all": mock}):
  186. disk.wipe("/dev/sda")
  187. mock.assert_called_once_with("wipefs -a /dev/sda", python_shell=False)
  188. def test_tune(self):
  189. mock = MagicMock(
  190. return_value="712971264\n512\n512\n512\n0\n0\n88\n712971264\n365041287168\n512\n512"
  191. )
  192. with patch.dict(disk.__salt__, {"cmd.run": mock}):
  193. mock_dump = MagicMock(return_value={"retcode": 0, "stdout": ""})
  194. with patch("salt.modules.disk.dump", mock_dump):
  195. kwargs = {"read-ahead": 512, "filesystem-read-ahead": 1024}
  196. disk.tune("/dev/sda", **kwargs)
  197. self.assert_called_once(mock)
  198. args, kwargs = mock.call_args
  199. # Assert called once with either 'blockdev --setra 512 --setfra 512 /dev/sda' or
  200. # 'blockdev --setfra 512 --setra 512 /dev/sda' and python_shell=False kwarg.
  201. self.assertEqual(len(args), 1)
  202. self.assertTrue(args[0].startswith("blockdev "))
  203. self.assertTrue(args[0].endswith(" /dev/sda"))
  204. self.assertIn(" --setra 512 ", args[0])
  205. self.assertIn(" --setfra 1024 ", args[0])
  206. self.assertEqual(len(args[0].split()), 6)
  207. self.assertEqual(kwargs, {"python_shell": False})
  208. def test_format(self):
  209. """
  210. unit tests for disk.format
  211. """
  212. device = "/dev/sdX1"
  213. mock = MagicMock(return_value=0)
  214. with patch.dict(disk.__salt__, {"cmd.retcode": mock}), patch(
  215. "salt.utils.path.which", MagicMock(return_value=True)
  216. ):
  217. self.assertEqual(disk.format_(device), True)
  218. def test_fat_format(self):
  219. """
  220. unit tests for disk.format when using fat argument
  221. """
  222. device = "/dev/sdX1"
  223. expected = ["mkfs", "-t", "fat", "-F", 12, "/dev/sdX1"]
  224. mock = MagicMock(return_value=0)
  225. with patch.dict(disk.__salt__, {"cmd.retcode": mock}), patch(
  226. "salt.utils.path.which", MagicMock(return_value=True)
  227. ):
  228. self.assertEqual(disk.format_(device, fs_type="fat", fat=12), True)
  229. args, kwargs = mock.call_args_list[0]
  230. assert expected == args[0]
  231. @skipIf(
  232. not salt.utils.path.which("lsblk") and not salt.utils.path.which("df"),
  233. "lsblk or df not found",
  234. )
  235. def test_fstype(self):
  236. """
  237. unit tests for disk.fstype
  238. """
  239. device = "/dev/sdX1"
  240. fs_type = "ext4"
  241. mock = MagicMock(return_value="FSTYPE\n{0}".format(fs_type))
  242. with patch.dict(disk.__grains__, {"kernel": "Linux"}), patch.dict(
  243. disk.__salt__, {"cmd.run": mock}
  244. ), patch("salt.utils.path.which", MagicMock(return_value=True)):
  245. self.assertEqual(disk.fstype(device), fs_type)
  246. def test_resize2fs(self):
  247. """
  248. unit tests for disk.resize2fs
  249. """
  250. device = "/dev/sdX1"
  251. mock = MagicMock()
  252. with patch.dict(disk.__salt__, {"cmd.run_all": mock}), patch(
  253. "salt.utils.path.which", MagicMock(return_value=True)
  254. ):
  255. disk.resize2fs(device)
  256. mock.assert_called_once_with(
  257. "resize2fs {0}".format(device), python_shell=False
  258. )
  259. @skipIf(salt.utils.platform.is_windows(), "Skip on Windows")
  260. @skipIf(not salt.utils.path.which("mkfs"), "mkfs not found")
  261. def test_format_(self):
  262. """
  263. unit tests for disk.format_
  264. """
  265. device = "/dev/sdX1"
  266. mock = MagicMock(return_value=0)
  267. with patch.dict(disk.__salt__, {"cmd.retcode": mock}):
  268. disk.format_(device=device)
  269. mock.assert_any_call(["mkfs", "-t", "ext4", device], ignore_retcode=True)
  270. @skipIf(salt.utils.platform.is_windows(), "Skip on Windows")
  271. @skipIf(not salt.utils.path.which("mkfs"), "mkfs not found")
  272. def test_format__fat(self):
  273. """
  274. unit tests for disk.format_ with FAT parameter
  275. """
  276. device = "/dev/sdX1"
  277. mock = MagicMock(return_value=0)
  278. with patch.dict(disk.__salt__, {"cmd.retcode": mock}):
  279. disk.format_(device=device, fs_type="fat", fat=12)
  280. mock.assert_any_call(
  281. ["mkfs", "-t", "fat", "-F", 12, device], ignore_retcode=True
  282. )