test_scsi.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 copy
  8. import os
  9. # Import Salt Libs
  10. import salt.modules.scsi as scsi
  11. import salt.utils.path
  12. # Import Salt Testing Libs
  13. from tests.support.mixins import LoaderModuleMockMixin
  14. from tests.support.mock import MagicMock, patch
  15. from tests.support.unit import TestCase
  16. class ScsiTestCase(TestCase, LoaderModuleMockMixin):
  17. """
  18. Test cases for salt.modules.scsi
  19. """
  20. def setup_loader_modules(self):
  21. return {scsi: {}}
  22. def test_ls_(self):
  23. """
  24. Test for list SCSI devices, with details
  25. """
  26. lsscsi = {
  27. "stdout": "[0:0:0:0] disk HP LOGICAL VOLUME 6.68 /dev/sda [8:0]",
  28. "stderr": "",
  29. "retcode": 0,
  30. }
  31. lsscsi_size = {
  32. "stdout": "[0:0:0:0] disk HP LOGICAL VOLUME 6.68 /dev/sda [8:0] 1.20TB",
  33. "stderr": "",
  34. "retcode": 0,
  35. }
  36. result = {
  37. "[0:0:0:0]": {
  38. "major": "8",
  39. "lun": "0:0:0:0",
  40. "device": "/dev/sda",
  41. "model": "LOGICAL VOLUME 6.68",
  42. "minor": "0",
  43. "size": None,
  44. }
  45. }
  46. result_size = copy.deepcopy(result)
  47. result_size["[0:0:0:0]"]["size"] = "1.20TB"
  48. mock = MagicMock(return_value="/usr/bin/lsscsi")
  49. with patch.object(salt.utils.path, "which", mock):
  50. # get_size = True
  51. cmd_mock = MagicMock(return_value=lsscsi_size)
  52. with patch.dict(scsi.__salt__, {"cmd.run_all": cmd_mock}):
  53. self.assertDictEqual(scsi.ls_(), result_size)
  54. with patch.dict(
  55. lsscsi_size, {"retcode": 1, "stderr": "An error occurred"}
  56. ):
  57. self.assertEqual(scsi.ls_(), "An error occurred")
  58. with patch.dict(
  59. lsscsi_size,
  60. {"retcode": 1, "stderr": "lsscsi: invalid option -- 's'\nUsage:"},
  61. ):
  62. self.assertEqual(
  63. scsi.ls_(), "lsscsi: invalid option -- 's' - try get_size=False"
  64. )
  65. # get_size = False
  66. cmd_mock = MagicMock(return_value=lsscsi)
  67. with patch.dict(scsi.__salt__, {"cmd.run_all": cmd_mock}):
  68. self.assertDictEqual(scsi.ls_(get_size=False), result)
  69. mock = MagicMock(return_value=None)
  70. with patch.object(salt.utils.path, "which", mock):
  71. self.assertEqual(
  72. scsi.ls_(), "scsi.ls not available - lsscsi command not found"
  73. )
  74. def test_rescan_all(self):
  75. """
  76. Test for list scsi devices
  77. """
  78. mock = MagicMock(side_effect=[False, True])
  79. with patch.object(os.path, "isdir", mock):
  80. self.assertEqual(scsi.rescan_all("host"), "Host host does not exist")
  81. with patch.dict(scsi.__salt__, {"cmd.run": MagicMock(return_value="A")}):
  82. self.assertListEqual(scsi.rescan_all("host"), ["A"])