test_blockdev.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 os
  8. # Import Salt Libs
  9. import salt.states.blockdev as blockdev
  10. import salt.utils.path
  11. # Import Salt Testing Libs
  12. from tests.support.mixins import LoaderModuleMockMixin
  13. from tests.support.mock import MagicMock, Mock, patch
  14. from tests.support.unit import TestCase
  15. class BlockdevTestCase(TestCase, LoaderModuleMockMixin):
  16. """
  17. Test cases for salt.states.blockdev
  18. """
  19. def setup_loader_modules(self):
  20. return {blockdev: {}}
  21. # 'tuned' function tests: 1
  22. def test_tuned(self):
  23. """
  24. Test to manage options of block device
  25. """
  26. name = "/dev/vg/master-data"
  27. ret = {"name": name, "result": True, "changes": {}, "comment": ""}
  28. comt = ("Changes to {0} cannot be applied. " "Not a block device. ").format(
  29. name
  30. )
  31. with patch.dict(blockdev.__salt__, {"file.is_blkdev": False}):
  32. ret.update({"comment": comt})
  33. self.assertDictEqual(blockdev.tuned(name), ret)
  34. comt = "Changes to {0} will be applied ".format(name)
  35. with patch.dict(blockdev.__salt__, {"file.is_blkdev": True}):
  36. ret.update({"comment": comt, "result": None})
  37. with patch.dict(blockdev.__opts__, {"test": True}):
  38. self.assertDictEqual(blockdev.tuned(name), ret)
  39. # 'formatted' function tests: 1
  40. def test_formatted(self):
  41. """
  42. Test to manage filesystems of partitions.
  43. """
  44. name = "/dev/vg/master-data"
  45. ret = {"name": name, "result": False, "changes": {}, "comment": ""}
  46. with patch.object(
  47. os.path, "exists", MagicMock(side_effect=[False, True, True, True, True])
  48. ):
  49. comt = "{0} does not exist".format(name)
  50. ret.update({"comment": comt})
  51. self.assertDictEqual(blockdev.formatted(name), ret)
  52. mock_ext4 = MagicMock(return_value="ext4")
  53. # Test state return when block device is already in the correct state
  54. with patch.dict(blockdev.__salt__, {"cmd.run": mock_ext4}):
  55. comt = "{0} already formatted with ext4".format(name)
  56. ret.update({"comment": comt, "result": True})
  57. self.assertDictEqual(blockdev.formatted(name), ret)
  58. # Test state return when provided block device is an invalid fs_type
  59. with patch.dict(blockdev.__salt__, {"cmd.run": MagicMock(return_value="")}):
  60. ret.update({"comment": "Invalid fs_type: foo-bar", "result": False})
  61. with patch.object(
  62. salt.utils.path, "which", MagicMock(return_value=False)
  63. ):
  64. self.assertDictEqual(
  65. blockdev.formatted(name, fs_type="foo-bar"), ret
  66. )
  67. # Test state return when provided block device state will change and test=True
  68. with patch.dict(
  69. blockdev.__salt__, {"cmd.run": MagicMock(return_value="new-thing")}
  70. ):
  71. comt = "Changes to {0} will be applied ".format(name)
  72. ret.update({"comment": comt, "result": None})
  73. with patch.object(
  74. salt.utils.path, "which", MagicMock(return_value=True)
  75. ):
  76. with patch.dict(blockdev.__opts__, {"test": True}):
  77. self.assertDictEqual(blockdev.formatted(name), ret)
  78. # Test state return when block device format fails
  79. with patch.dict(
  80. blockdev.__salt__,
  81. {
  82. "cmd.run": MagicMock(return_value=mock_ext4),
  83. "disk.format": MagicMock(return_value=True),
  84. },
  85. ):
  86. comt = "Failed to format {0}".format(name)
  87. ret.update({"comment": comt, "result": False})
  88. with patch.object(
  89. salt.utils.path, "which", MagicMock(return_value=True)
  90. ):
  91. with patch.dict(blockdev.__opts__, {"test": False}):
  92. self.assertDictEqual(blockdev.formatted(name), ret)
  93. def test__checkblk(self):
  94. """
  95. Confirm that we call cmd.run with ignore_retcode=True
  96. """
  97. cmd_mock = Mock()
  98. with patch.dict(blockdev.__salt__, {"cmd.run": cmd_mock}):
  99. blockdev._checkblk("/dev/foo")
  100. cmd_mock.assert_called_once_with(
  101. ["blkid", "-o", "value", "-s", "TYPE", "/dev/foo"], ignore_retcode=True
  102. )