test_nvme.py 2.2 KB

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