test_blockdev.py 4.7 KB

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