test_qemu_nbd.py 3.6 KB

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