1
0

test_iscsi.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 Testing Libs
  10. from tests.support.unit import TestCase, skipIf
  11. from tests.support.mock import (
  12. patch,
  13. mock_open,
  14. MagicMock,
  15. NO_MOCK,
  16. NO_MOCK_REASON
  17. )
  18. # Import Salt Libs
  19. import salt.grains.iscsi as iscsi
  20. @skipIf(NO_MOCK, NO_MOCK_REASON)
  21. class IscsiGrainsTestCase(TestCase):
  22. '''
  23. Test cases for iscsi grains
  24. '''
  25. def test_windows_iscsi_iqn_grains(self):
  26. cmd_run_mock = MagicMock(
  27. return_value={'stdout': 'iSCSINodeName\n'
  28. 'iqn.1991-05.com.microsoft:simon-x1\n'}
  29. )
  30. _grains = {}
  31. with patch('salt.utils.path.which', MagicMock(return_value=True)):
  32. with patch('salt.modules.cmdmod.run_all', cmd_run_mock):
  33. _grains['iscsi_iqn'] = iscsi._windows_iqn()
  34. self.assertEqual(_grains.get('iscsi_iqn'),
  35. ['iqn.1991-05.com.microsoft:simon-x1'])
  36. def test_aix_iscsi_iqn_grains(self):
  37. cmd_run_mock = MagicMock(
  38. return_value='initiator_name iqn.localhost.hostid.7f000001'
  39. )
  40. _grains = {}
  41. with patch('salt.modules.cmdmod.run', cmd_run_mock):
  42. _grains['iscsi_iqn'] = iscsi._aix_iqn()
  43. self.assertEqual(_grains.get('iscsi_iqn'),
  44. ['iqn.localhost.hostid.7f000001'])
  45. def test_linux_iscsi_iqn_grains(self):
  46. _iscsi_file = textwrap.dedent('''\
  47. ## DO NOT EDIT OR REMOVE THIS FILE!
  48. ## If you remove this file, the iSCSI daemon will not start.
  49. ## If you change the InitiatorName, existing access control lists
  50. ## may reject this initiator. The InitiatorName must be unique
  51. ## for each iSCSI initiator. Do NOT duplicate iSCSI InitiatorNames.
  52. InitiatorName=iqn.1993-08.org.debian:01:d12f7aba36
  53. ''')
  54. with patch('salt.utils.files.fopen', mock_open(read_data=_iscsi_file)):
  55. iqn = iscsi._linux_iqn()
  56. assert isinstance(iqn, list)
  57. assert len(iqn) == 1
  58. assert iqn == ['iqn.1993-08.org.debian:01:d12f7aba36']
  59. @patch('salt.utils.files.fopen', MagicMock(side_effect=IOError(errno.EPERM,
  60. 'The cables are not the same length.')))
  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()