From e972ebdf1a6e6fb74d241a7c5ee40282b88e5c62 Mon Sep 17 00:00:00 2001 From: Ch3LL Date: Tue, 13 Mar 2018 16:27:02 -0400 Subject: [PATCH] Add for new source_* minion options --- salt/minion.py | 4 +- tests/unit/test_minion.py | 72 +++++++++++++++++++++++++++++ tests/unit/transport/test_zeromq.py | 31 +++++++++++++ 3 files changed, 105 insertions(+), 2 deletions(-) diff --git a/salt/minion.py b/salt/minion.py index b366c4cc18..249d53bd02 100644 --- a/salt/minion.py +++ b/salt/minion.py @@ -144,10 +144,10 @@ def resolve_dns(opts, fallback=True): if (opts.get('file_client', 'remote') == 'local' and not opts.get('use_master_when_local', False)): check_dns = False + # Because I import salt.log below I need to re-import salt.utils here + import salt.utils if check_dns is True: - # Because I import salt.log below I need to re-import salt.utils here - import salt.utils try: if opts['master'] == '': raise SaltSystemExit diff --git a/tests/unit/test_minion.py b/tests/unit/test_minion.py index 8ccd214012..aef01e93ed 100644 --- a/tests/unit/test_minion.py +++ b/tests/unit/test_minion.py @@ -29,6 +29,78 @@ class MinionTestCase(TestCase): with patch.dict(__opts__, {'ipv6': False, 'master': float('127.0'), 'master_port': '4555', 'retry_dns': False}): self.assertRaises(SaltSystemExit, salt.minion.resolve_dns, __opts__) + def test_source_int_name_local(self): + ''' + test when file_client local and + source_interface_name is set + ''' + interfaces = {'bond0.1234': {'hwaddr': '01:01:01:d0:d0:d0', + 'up': True, 'inet': + [{'broadcast': '111.1.111.255', + 'netmask': '111.1.0.0', + 'label': 'bond0', + 'address': '111.1.0.1'}]}} + with patch.dict(__opts__, {'ipv6': False, 'master': '127.0.0.1', + 'master_port': '4555', 'file_client': 'local', + 'source_interface_name': 'bond0.1234', + 'source_ret_port': 49017, + 'source_publish_port': 49018}), \ + patch('salt.utils.network.interfaces', + MagicMock(return_value=interfaces)): + assert salt.minion.resolve_dns(__opts__) == {'master_ip': '127.0.0.1', + 'source_ip': '111.1.0.1', + 'source_ret_port': 49017, + 'source_publish_port': 49018, + 'master_uri': 'tcp://127.0.0.1:4555'} + + def test_source_int_name_remote(self): + ''' + test when file_client remote and + source_interface_name is set and + interface is down + ''' + interfaces = {'bond0.1234': {'hwaddr': '01:01:01:d0:d0:d0', + 'up': False, 'inet': + [{'broadcast': '111.1.111.255', + 'netmask': '111.1.0.0', + 'label': 'bond0', + 'address': '111.1.0.1'}]}} + with patch.dict(__opts__, {'ipv6': False, 'master': '127.0.0.1', + 'master_port': '4555', 'file_client': 'remote', + 'source_interface_name': 'bond0.1234', + 'source_ret_port': 49017, + 'source_publish_port': 49018}), \ + patch('salt.utils.network.interfaces', + MagicMock(return_value=interfaces)): + assert salt.minion.resolve_dns(__opts__) == {'master_ip': '127.0.0.1', + 'source_ret_port': 49017, + 'source_publish_port': 49018, + 'master_uri': 'tcp://127.0.0.1:4555'} + + def test_source_address(self): + ''' + test when source_address is set + ''' + interfaces = {'bond0.1234': {'hwaddr': '01:01:01:d0:d0:d0', + 'up': False, 'inet': + [{'broadcast': '111.1.111.255', + 'netmask': '111.1.0.0', + 'label': 'bond0', + 'address': '111.1.0.1'}]}} + with patch.dict(__opts__, {'ipv6': False, 'master': '127.0.0.1', + 'master_port': '4555', 'file_client': 'local', + 'source_interface_name': '', + 'source_address': '111.1.0.1', + 'source_ret_port': 49017, + 'source_publish_port': 49018}), \ + patch('salt.utils.network.interfaces', + MagicMock(return_value=interfaces)): + assert salt.minion.resolve_dns(__opts__) == {'source_publish_port': 49018, + 'source_ret_port': 49017, + 'master_uri': 'tcp://127.0.0.1:4555', + 'source_ip': '111.1.0.1', + 'master_ip': '127.0.0.1'} + @skip_if_not_root def test_sock_path_len(self): ''' diff --git a/tests/unit/transport/test_zeromq.py b/tests/unit/transport/test_zeromq.py index 4a7d8b1cd2..2b000baca4 100644 --- a/tests/unit/transport/test_zeromq.py +++ b/tests/unit/transport/test_zeromq.py @@ -305,3 +305,34 @@ class AsyncReqMessageClientPoolTest(TestCase): def test_destroy(self): self.message_client_pool.destroy() self.assertEqual([], self.message_client_pool.message_clients) + + +class ZMQConfigTest(TestCase): + def test_master_uri(self): + ''' + test _get_master_uri method + ''' + m_ip = '127.0.0.1' + m_port = 4505 + s_ip = '111.1.0.1' + s_port = 4058 + + # source ip and source_port empty + assert salt.transport.zeromq._get_master_uri(master_ip=m_ip, + master_port=m_port) == 'tcp://{0}:{1}'.format(m_ip, m_port) + + # pass in both source_ip and source_port + assert salt.transport.zeromq._get_master_uri(master_ip=m_ip, + master_port=m_port, + source_ip=s_ip, + source_port=s_port) == 'tcp://{0}:{1};{2}:{3}'.format(s_ip, s_port, m_ip, m_port) + + # pass in only source_ip + assert salt.transport.zeromq._get_master_uri(master_ip=m_ip, + master_port=m_port, + source_ip=s_ip) == 'tcp://{0}:0;{1}:{2}'.format(s_ip, m_ip, m_port) + + # pass in only source_port + assert salt.transport.zeromq._get_master_uri(master_ip=m_ip, + master_port=m_port, + source_port=s_port) == 'tcp://0.0.0.0:{0};{1}:{2}'.format(s_port, m_ip, m_port)