test_qemu_nbd.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.path
  8. import glob
  9. import tempfile
  10. # Import Salt Testing Libs
  11. from tests.support.mixins import LoaderModuleMockMixin
  12. from tests.support.unit import TestCase
  13. from tests.support.mock import (
  14. MagicMock,
  15. patch,
  16. )
  17. # Import Salt Libs
  18. import salt.modules.qemu_nbd as qemu_nbd
  19. class QemuNbdTestCase(TestCase, LoaderModuleMockMixin):
  20. '''
  21. Test cases for salt.modules.qemu_nbd
  22. '''
  23. def setup_loader_modules(self):
  24. return {qemu_nbd: {}}
  25. # 'connect' function tests: 1
  26. def test_connect(self):
  27. '''
  28. Test if it activate nbd for an image file.
  29. '''
  30. mock = MagicMock(return_value=True)
  31. with patch.dict(qemu_nbd.__salt__, {'cmd.run': mock}):
  32. with patch.object(os.path, 'isfile',
  33. MagicMock(return_value=False)):
  34. self.assertEqual(qemu_nbd.connect('/tmp/image.raw'), '')
  35. self.assertEqual(qemu_nbd.connect('/tmp/image.raw'), '')
  36. with patch.object(os.path, 'isfile', mock):
  37. with patch.object(glob, 'glob',
  38. MagicMock(return_value=['/dev/nbd0'])):
  39. with patch.dict(qemu_nbd.__salt__,
  40. {'cmd.run': mock,
  41. 'cmd.retcode': MagicMock(side_effect=[1, 0])}):
  42. self.assertEqual(qemu_nbd.connect('/tmp/image.raw'),
  43. '/dev/nbd0')
  44. with patch.dict(qemu_nbd.__salt__,
  45. {'cmd.run': mock,
  46. 'cmd.retcode': MagicMock(return_value=False)}):
  47. self.assertEqual(qemu_nbd.connect('/tmp/image.raw'), '')
  48. # 'mount' function tests: 1
  49. def test_mount(self):
  50. '''
  51. Test if it pass in the nbd connection device location,
  52. mount all partitions and return a dict of mount points.
  53. '''
  54. mock = MagicMock(return_value=True)
  55. with patch.dict(qemu_nbd.__salt__, {'cmd.run': mock}):
  56. self.assertDictEqual(qemu_nbd.mount('/dev/nbd0'), {})
  57. # 'init' function tests: 1
  58. def test_init(self):
  59. '''
  60. Test if it mount the named image via qemu-nbd
  61. and return the mounted roots
  62. '''
  63. mock = MagicMock(return_value=True)
  64. with patch.dict(qemu_nbd.__salt__, {'cmd.run': mock}):
  65. self.assertEqual(qemu_nbd.init('/srv/image.qcow2'), '')
  66. with patch.object(os.path, 'isfile', mock),\
  67. patch.object(glob, 'glob', MagicMock(return_value=['/dev/nbd0'])),\
  68. patch.dict(qemu_nbd.__salt__,
  69. {'cmd.run': mock,
  70. 'mount.mount': mock,
  71. 'cmd.retcode': MagicMock(side_effect=[1, 0])}):
  72. expected = {os.sep.join([tempfile.gettempdir(), 'nbd', 'nbd0', 'nbd0']): '/dev/nbd0'}
  73. self.assertDictEqual(qemu_nbd.init('/srv/image.qcow2'), expected)
  74. # 'clear' function tests: 1
  75. def test_clear(self):
  76. '''
  77. Test if it pass in the mnt dict returned from nbd_mount
  78. to unmount and disconnect the image from nbd.
  79. '''
  80. mock_run = MagicMock(return_value=True)
  81. with patch.dict(qemu_nbd.__salt__,
  82. {'cmd.run': mock_run,
  83. 'mount.umount': MagicMock(side_effect=[False, True])}):
  84. self.assertDictEqual(qemu_nbd.clear({"/mnt/foo": "/dev/nbd0p1"}),
  85. {'/mnt/foo': '/dev/nbd0p1'})
  86. self.assertDictEqual(qemu_nbd.clear({"/mnt/foo": "/dev/nbd0p1"}),
  87. {})