test_clustershell.py 1.5 KB

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