test_nvme.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: :email:`Simon Dodsley <simon@purestorage.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.nvme as nvme
  20. @skipIf(NO_MOCK, NO_MOCK_REASON)
  21. class NvmeGrainsTestCase(TestCase):
  22. '''
  23. Test cases for nvme grains
  24. '''
  25. def test_linux_nvme_nqn_grains(self):
  26. _nvme_file = textwrap.dedent('''\
  27. nqn.2014-08.org.nvmexpress:fc_lif:uuid:2cd61a74-17f9-4c22-b350-3020020c458d
  28. ''')
  29. with patch('salt.utils.files.fopen', mock_open(read_data=_nvme_file)):
  30. nqn = nvme._linux_nqn()
  31. assert isinstance(nqn, list)
  32. assert len(nqn) == 1
  33. assert nqn == ['nqn.2014-08.org.nvmexpress:fc_lif:uuid:2cd61a74-17f9-4c22-b350-3020020c458d']
  34. @patch('salt.utils.files.fopen', MagicMock(side_effect=IOError(errno.EPERM,
  35. 'The cables are not the same length.')))
  36. @patch('salt.grains.nvme.log', MagicMock())
  37. def test_linux_nqn_non_root(self):
  38. '''
  39. Test if linux_nqn is running on salt-master as non-root
  40. and handling access denial properly.
  41. :return:
  42. '''
  43. assert nvme._linux_nqn() == []
  44. nvme.log.debug.assert_called()
  45. assert 'Error while accessing' in nvme.log.debug.call_args[0][0]
  46. assert 'cables are not the same' in nvme.log.debug.call_args[0][2].strerror
  47. assert nvme.log.debug.call_args[0][2].errno == errno.EPERM
  48. assert nvme.log.debug.call_args[0][1] == '/etc/nvme/hostnqn'
  49. @patch('salt.utils.files.fopen', MagicMock(side_effect=IOError(errno.ENOENT, '')))
  50. @patch('salt.grains.nvme.log', MagicMock())
  51. def test_linux_nqn_no_nvme_initiator(self):
  52. '''
  53. Test if linux_nqn is running on salt-master as root.
  54. nvme initiator is not there accessible or is not supported.
  55. :return:
  56. '''
  57. assert nvme._linux_nqn() == []
  58. nvme.log.debug.assert_not_called()