2017-10-16 18:31:06 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
|
|
|
unit tests for clustershell roster
|
|
|
|
'''
|
|
|
|
# Import Python libs
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
# Import Salt Testing libraries
|
|
|
|
from tests.support.unit import TestCase, skipIf
|
|
|
|
from tests.support.mock import (
|
|
|
|
NO_MOCK,
|
|
|
|
NO_MOCK_REASON,
|
|
|
|
MagicMock,
|
|
|
|
patch)
|
|
|
|
|
2017-12-22 20:00:56 +00:00
|
|
|
# Import third-party libs
|
|
|
|
try:
|
2017-12-27 21:23:08 +00:00
|
|
|
from ClusterShell.NodeSet import NodeSet # pylint: disable=unused-import
|
2017-12-22 20:00:56 +00:00
|
|
|
HAS_CLUSTERSHELL = True
|
|
|
|
except (ImportError, OSError) as e:
|
|
|
|
HAS_CLUSTERSHELL = False
|
|
|
|
|
2017-10-16 18:31:06 +00:00
|
|
|
|
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
2017-12-22 20:00:56 +00:00
|
|
|
@skipIf(HAS_CLUSTERSHELL is False, 'Install Python Clustershell bindings before running these tests.')
|
2017-10-16 18:31:06 +00:00
|
|
|
class ClusterShellTestCase(TestCase):
|
|
|
|
'''
|
|
|
|
Test cases for clustershell roster
|
|
|
|
'''
|
|
|
|
def test_targets(self):
|
|
|
|
mock_socket = MagicMock()
|
|
|
|
mock_nodeset = MagicMock()
|
|
|
|
mock_nodeset.NodeSet.return_value = ['foo']
|
|
|
|
with patch.dict('sys.modules', **{'socket': mock_socket, 'ClusterShell.NodeSet': mock_nodeset}):
|
|
|
|
import salt.roster.clustershell
|
|
|
|
salt.roster.clustershell.__opts__ = {}
|
|
|
|
with patch.dict(salt.roster.clustershell.__opts__, {'ssh_scan_ports': [1, 2, 3],
|
|
|
|
'ssh_scan_timeout': 30}):
|
|
|
|
# Reimports are necessary to re-init the namespace.
|
|
|
|
import socket
|
|
|
|
from ClusterShell.NodeSet import NodeSet
|
|
|
|
ret = salt.roster.clustershell.targets('foo')
|
|
|
|
mock_socket.gethostbyname.assert_any_call('foo')
|
|
|
|
self.assertTrue('foo' in ret)
|
|
|
|
self.assertTrue(ret['foo']['port'] == 3)
|