test_clustershell.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # -*- coding: utf-8 -*-
  2. """
  3. unit tests for clustershell roster
  4. """
  5. # Import Python libs
  6. from __future__ import absolute_import
  7. from tests.support.mock import MagicMock, patch
  8. # Import Salt Testing libraries
  9. from tests.support.unit import TestCase, skipIf
  10. # Import third-party libs
  11. try:
  12. from ClusterShell.NodeSet import NodeSet # pylint: disable=unused-import
  13. HAS_CLUSTERSHELL = True
  14. except (ImportError, OSError) as e:
  15. HAS_CLUSTERSHELL = False
  16. @skipIf(
  17. HAS_CLUSTERSHELL is False,
  18. "Install Python Clustershell bindings before running these tests.",
  19. )
  20. class ClusterShellTestCase(TestCase):
  21. """
  22. Test cases for clustershell roster
  23. """
  24. def test_targets(self):
  25. mock_socket = MagicMock()
  26. mock_nodeset = MagicMock()
  27. mock_nodeset.NodeSet.return_value = ["foo"]
  28. with patch.dict(
  29. "sys.modules",
  30. **{"socket": mock_socket, "ClusterShell.NodeSet": mock_nodeset}
  31. ):
  32. import salt.roster.clustershell
  33. salt.roster.clustershell.__opts__ = {}
  34. with patch.dict(
  35. salt.roster.clustershell.__opts__,
  36. {"ssh_scan_ports": [1, 2, 3], "ssh_scan_timeout": 30},
  37. ):
  38. # Reimports are necessary to re-init the namespace.
  39. # pylint: disable=unused-import
  40. import socket
  41. from ClusterShell.NodeSet import NodeSet
  42. # pylint: enable=unused-import
  43. ret = salt.roster.clustershell.targets("foo")
  44. mock_socket.gethostbyname.assert_any_call("foo")
  45. self.assertTrue("foo" in ret)
  46. self.assertTrue(ret["foo"]["port"] == 3)