1
0

test_iscsi.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: :email:`Shane Lee <slee@saltstack.com>`
  4. """
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import errno
  8. import textwrap
  9. # Import Salt Libs
  10. import salt.grains.iscsi as iscsi
  11. from tests.support.mock import MagicMock, mock_open, patch
  12. # Import Salt Testing Libs
  13. from tests.support.unit import TestCase
  14. class IscsiGrainsTestCase(TestCase):
  15. """
  16. Test cases for iscsi grains
  17. """
  18. def test_windows_iscsi_iqn_grains(self):
  19. cmd_run_mock = MagicMock(
  20. return_value={
  21. "stdout": "iSCSINodeName\n" "iqn.1991-05.com.microsoft:simon-x1\n"
  22. }
  23. )
  24. _grains = {}
  25. with patch("salt.utils.path.which", MagicMock(return_value=True)):
  26. with patch("salt.modules.cmdmod.run_all", cmd_run_mock):
  27. _grains["iscsi_iqn"] = iscsi._windows_iqn()
  28. self.assertEqual(
  29. _grains.get("iscsi_iqn"), ["iqn.1991-05.com.microsoft:simon-x1"]
  30. )
  31. def test_aix_iscsi_iqn_grains(self):
  32. cmd_run_mock = MagicMock(
  33. return_value="initiator_name iqn.localhost.hostid.7f000001"
  34. )
  35. _grains = {}
  36. with patch("salt.modules.cmdmod.run", cmd_run_mock):
  37. _grains["iscsi_iqn"] = iscsi._aix_iqn()
  38. self.assertEqual(_grains.get("iscsi_iqn"), ["iqn.localhost.hostid.7f000001"])
  39. def test_linux_iscsi_iqn_grains(self):
  40. _iscsi_file = textwrap.dedent(
  41. """\
  42. ## DO NOT EDIT OR REMOVE THIS FILE!
  43. ## If you remove this file, the iSCSI daemon will not start.
  44. ## If you change the InitiatorName, existing access control lists
  45. ## may reject this initiator. The InitiatorName must be unique
  46. ## for each iSCSI initiator. Do NOT duplicate iSCSI InitiatorNames.
  47. InitiatorName=iqn.1993-08.org.debian:01:d12f7aba36
  48. """
  49. )
  50. with patch("salt.utils.files.fopen", mock_open(read_data=_iscsi_file)):
  51. iqn = iscsi._linux_iqn()
  52. assert isinstance(iqn, list)
  53. assert len(iqn) == 1
  54. assert iqn == ["iqn.1993-08.org.debian:01:d12f7aba36"]
  55. @patch(
  56. "salt.utils.files.fopen",
  57. MagicMock(
  58. side_effect=IOError(errno.EPERM, "The cables are not the same length.")
  59. ),
  60. )
  61. @patch("salt.grains.iscsi.log", MagicMock())
  62. def test_linux_iqn_non_root(self):
  63. """
  64. Test if linux_iqn is running on salt-master as non-root
  65. and handling access denial properly.
  66. :return:
  67. """
  68. assert iscsi._linux_iqn() == []
  69. iscsi.log.debug.assert_called()
  70. assert "Error while accessing" in iscsi.log.debug.call_args[0][0]
  71. assert "cables are not the same" in iscsi.log.debug.call_args[0][2].strerror
  72. assert iscsi.log.debug.call_args[0][2].errno == errno.EPERM
  73. assert iscsi.log.debug.call_args[0][1] == "/etc/iscsi/initiatorname.iscsi"
  74. @patch("salt.utils.files.fopen", MagicMock(side_effect=IOError(errno.ENOENT, "")))
  75. @patch("salt.grains.iscsi.log", MagicMock())
  76. def test_linux_iqn_no_iscsii_initiator(self):
  77. """
  78. Test if linux_iqn is running on salt-master as root.
  79. iscsii initiator is not there accessible or is not supported.
  80. :return:
  81. """
  82. assert iscsi._linux_iqn() == []
  83. iscsi.log.debug.assert_not_called()