1
0

test_iscsi.py 3.4 KB

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