test_clustershell.py 1.6 KB

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