1
0

test_nvme.py 2.2 KB

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