2015-04-29 11:59:37 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
2018-05-28 21:13:12 +00:00
|
|
|
:codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
|
2015-04-29 11:59:37 +00:00
|
|
|
'''
|
|
|
|
# Import Python libs
|
2018-01-24 20:47:14 +00:00
|
|
|
from __future__ import absolute_import, unicode_literals, print_function
|
2017-03-22 16:42:17 +00:00
|
|
|
import os
|
2015-04-29 11:59:37 +00:00
|
|
|
|
|
|
|
# Import Salt Testing Libs
|
2017-03-22 16:42:17 +00:00
|
|
|
from tests.support.mixins import LoaderModuleMockMixin
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import skipIf, TestCase
|
|
|
|
from tests.support.mock import (
|
2015-04-29 11:59:37 +00:00
|
|
|
NO_MOCK,
|
|
|
|
NO_MOCK_REASON,
|
|
|
|
MagicMock,
|
|
|
|
patch)
|
|
|
|
|
|
|
|
# Import Salt Libs
|
2017-11-07 00:02:53 +00:00
|
|
|
from salt.ext.six.moves import range
|
2017-03-21 17:15:36 +00:00
|
|
|
import salt.modules.rh_ip as rh_ip
|
2015-04-29 11:59:37 +00:00
|
|
|
|
2017-03-22 16:42:17 +00:00
|
|
|
# Import 3rd-party libs
|
|
|
|
import jinja2.exceptions
|
2018-01-24 20:47:14 +00:00
|
|
|
from salt.ext import six
|
2015-04-29 11:59:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
2017-03-22 16:42:17 +00:00
|
|
|
class RhipTestCase(TestCase, LoaderModuleMockMixin):
|
2015-04-29 11:59:37 +00:00
|
|
|
'''
|
|
|
|
Test cases for salt.modules.rh_ip
|
|
|
|
'''
|
2017-03-22 16:42:17 +00:00
|
|
|
def setup_loader_modules(self):
|
|
|
|
return {rh_ip: {}}
|
|
|
|
|
2015-04-29 11:59:37 +00:00
|
|
|
def test_build_bond(self):
|
|
|
|
'''
|
|
|
|
Test to create a bond script in /etc/modprobe.d with the passed
|
|
|
|
settings and load the bonding kernel module.
|
|
|
|
'''
|
|
|
|
with patch.dict(rh_ip.__grains__, {'osrelease': 'osrelease'}):
|
|
|
|
with patch.object(rh_ip, '_parse_settings_bond', MagicMock()):
|
|
|
|
mock = jinja2.exceptions.TemplateNotFound('foo')
|
|
|
|
with patch.object(jinja2.Environment, 'get_template',
|
|
|
|
MagicMock(side_effect=mock)):
|
|
|
|
self.assertEqual(rh_ip.build_bond('iface'), '')
|
|
|
|
|
|
|
|
with patch.dict(rh_ip.__salt__, {'kmod.load':
|
|
|
|
MagicMock(return_value=None)}):
|
2015-05-11 18:31:24 +00:00
|
|
|
with patch.object(rh_ip, '_write_file_iface',
|
|
|
|
return_value=None):
|
|
|
|
with patch.object(rh_ip, '_read_temp', return_value='A'):
|
|
|
|
self.assertEqual(rh_ip.build_bond('iface', test='A'),
|
|
|
|
'A')
|
2015-04-29 11:59:37 +00:00
|
|
|
|
2015-05-11 18:31:24 +00:00
|
|
|
with patch.object(rh_ip, '_read_file', return_value='A'):
|
|
|
|
self.assertEqual(rh_ip.build_bond('iface', test=None),
|
|
|
|
'A')
|
2015-04-29 11:59:37 +00:00
|
|
|
|
|
|
|
def test_build_interface(self):
|
|
|
|
'''
|
|
|
|
Test to build an interface script for a network interface.
|
|
|
|
'''
|
2017-09-06 01:46:00 +00:00
|
|
|
with patch.dict(rh_ip.__grains__, {'os': 'Fedora', 'osmajorrelease': 26}):
|
2015-04-29 11:59:37 +00:00
|
|
|
with patch.object(rh_ip, '_raise_error_iface', return_value=None):
|
|
|
|
self.assertRaises(AttributeError,
|
|
|
|
rh_ip.build_interface,
|
|
|
|
'iface', 'slave', True)
|
|
|
|
|
2015-07-08 14:40:57 +00:00
|
|
|
with patch.dict(rh_ip.__salt__, {'network.interfaces': lambda: {'eth': True}}):
|
|
|
|
self.assertRaises(AttributeError,
|
|
|
|
rh_ip.build_interface,
|
|
|
|
'iface', 'eth', True, netmask='255.255.255.255', prefix=32,
|
|
|
|
test=True)
|
2017-11-06 22:04:22 +00:00
|
|
|
self.assertRaises(AttributeError,
|
|
|
|
rh_ip.build_interface,
|
|
|
|
'iface', 'eth', True, ipaddrs=['A'],
|
|
|
|
test=True)
|
|
|
|
self.assertRaises(AttributeError,
|
|
|
|
rh_ip.build_interface,
|
|
|
|
'iface', 'eth', True, ipv6addrs=['A'],
|
|
|
|
test=True)
|
2015-07-08 14:40:57 +00:00
|
|
|
|
2017-11-06 22:04:22 +00:00
|
|
|
for osrelease in range(5, 8):
|
2018-01-24 20:47:14 +00:00
|
|
|
with patch.dict(rh_ip.__grains__, {'os': 'RedHat', 'osrelease': six.text_type(osrelease)}):
|
2017-11-06 22:04:22 +00:00
|
|
|
with patch.object(rh_ip, '_raise_error_iface', return_value=None):
|
|
|
|
with patch.object(rh_ip, '_parse_settings_bond', MagicMock()):
|
|
|
|
mock = jinja2.exceptions.TemplateNotFound('foo')
|
2015-04-29 11:59:37 +00:00
|
|
|
with patch.object(jinja2.Environment,
|
2017-11-06 22:04:22 +00:00
|
|
|
'get_template',
|
|
|
|
MagicMock(side_effect=mock)):
|
2015-04-29 11:59:37 +00:00
|
|
|
self.assertEqual(rh_ip.build_interface('iface',
|
|
|
|
'vlan',
|
2017-11-06 22:04:22 +00:00
|
|
|
True), '')
|
2015-04-29 11:59:37 +00:00
|
|
|
|
2017-11-06 22:04:22 +00:00
|
|
|
with patch.object(rh_ip, '_read_temp', return_value='A'):
|
|
|
|
with patch.object(jinja2.Environment,
|
|
|
|
'get_template', MagicMock()):
|
|
|
|
self.assertEqual(rh_ip.build_interface('iface',
|
|
|
|
'vlan',
|
|
|
|
True,
|
|
|
|
test='A'),
|
|
|
|
'A')
|
|
|
|
|
|
|
|
with patch.object(rh_ip, '_write_file_iface',
|
|
|
|
return_value=None):
|
|
|
|
with patch.object(os.path, 'join',
|
2015-04-29 11:59:37 +00:00
|
|
|
return_value='A'):
|
2017-11-06 22:04:22 +00:00
|
|
|
with patch.object(rh_ip, '_read_file',
|
|
|
|
return_value='A'):
|
|
|
|
self.assertEqual(rh_ip.build_interface
|
|
|
|
('iface', 'vlan',
|
|
|
|
True), 'A')
|
|
|
|
if osrelease > 6:
|
|
|
|
with patch.dict(rh_ip.__salt__, {'network.interfaces': lambda: {'eth': True}}):
|
|
|
|
self.assertEqual(rh_ip.build_interface
|
|
|
|
('iface', 'eth', True,
|
|
|
|
ipaddrs=['127.0.0.1/8']), 'A')
|
|
|
|
self.assertEqual(rh_ip.build_interface
|
|
|
|
('iface', 'eth', True,
|
|
|
|
ipv6addrs=['fc00::1/128']), 'A')
|
2015-04-29 11:59:37 +00:00
|
|
|
|
|
|
|
def test_build_routes(self):
|
|
|
|
'''
|
|
|
|
Test to build a route script for a network interface.
|
|
|
|
'''
|
2015-06-08 22:14:35 +00:00
|
|
|
with patch.dict(rh_ip.__grains__, {'osrelease': '5.0'}):
|
2015-05-29 22:16:25 +00:00
|
|
|
with patch.object(rh_ip, '_parse_routes', MagicMock()):
|
|
|
|
mock = jinja2.exceptions.TemplateNotFound('foo')
|
|
|
|
with patch.object(jinja2.Environment,
|
|
|
|
'get_template', MagicMock(side_effect=mock)):
|
|
|
|
self.assertEqual(rh_ip.build_routes('iface'), '')
|
2015-05-30 00:04:49 +00:00
|
|
|
|
2015-05-29 22:16:25 +00:00
|
|
|
with patch.object(jinja2.Environment,
|
|
|
|
'get_template', MagicMock()):
|
2015-05-29 23:03:43 +00:00
|
|
|
with patch.object(rh_ip, '_read_temp', return_value=['A']):
|
|
|
|
self.assertEqual(rh_ip.build_routes('i', test='t'), ['A', 'A'])
|
2015-05-30 00:04:49 +00:00
|
|
|
|
2015-05-29 23:03:43 +00:00
|
|
|
with patch.object(rh_ip, '_read_file', return_value=['A']):
|
2015-05-29 22:16:25 +00:00
|
|
|
with patch.object(os.path, 'join', return_value='A'):
|
|
|
|
with patch.object(rh_ip, '_write_file_iface',
|
|
|
|
return_value=None):
|
|
|
|
self.assertEqual(rh_ip.build_routes('i',
|
|
|
|
test=None),
|
2015-05-29 23:03:43 +00:00
|
|
|
['A', 'A'])
|
2015-04-29 11:59:37 +00:00
|
|
|
|
|
|
|
def test_down(self):
|
|
|
|
'''
|
|
|
|
Test to shutdown a network interface
|
|
|
|
'''
|
|
|
|
with patch.dict(rh_ip.__salt__, {'cmd.run':
|
|
|
|
MagicMock(return_value='A')}):
|
|
|
|
self.assertEqual(rh_ip.down('iface', 'iface_type'), 'A')
|
|
|
|
|
|
|
|
self.assertEqual(rh_ip.down('iface', 'slave'), None)
|
|
|
|
|
|
|
|
def test_get_bond(self):
|
|
|
|
'''
|
|
|
|
Test to return the content of a bond script
|
|
|
|
'''
|
|
|
|
with patch.object(os.path, 'join', return_value='A'):
|
|
|
|
with patch.object(rh_ip, '_read_file', return_value='A'):
|
|
|
|
self.assertEqual(rh_ip.get_bond('iface'), 'A')
|
|
|
|
|
|
|
|
def test_get_interface(self):
|
|
|
|
'''
|
|
|
|
Test to return the contents of an interface script
|
|
|
|
'''
|
|
|
|
with patch.object(os.path, 'join', return_value='A'):
|
|
|
|
with patch.object(rh_ip, '_read_file', return_value='A'):
|
|
|
|
self.assertEqual(rh_ip.get_interface('iface'), 'A')
|
|
|
|
|
|
|
|
def test_up(self):
|
|
|
|
'''
|
|
|
|
Test to start up a network interface
|
|
|
|
'''
|
|
|
|
with patch.dict(rh_ip.__salt__, {'cmd.run':
|
|
|
|
MagicMock(return_value='A')}):
|
|
|
|
self.assertEqual(rh_ip.up('iface', 'iface_type'), 'A')
|
|
|
|
|
|
|
|
self.assertEqual(rh_ip.up('iface', 'slave'), None)
|
|
|
|
|
|
|
|
def test_get_routes(self):
|
|
|
|
'''
|
|
|
|
Test to return the contents of the interface routes script.
|
|
|
|
'''
|
|
|
|
with patch.object(os.path, 'join', return_value='A'):
|
2015-05-29 23:03:43 +00:00
|
|
|
with patch.object(rh_ip, '_read_file', return_value=['A']):
|
|
|
|
self.assertEqual(rh_ip.get_routes('iface'), ['A', 'A'])
|
2015-04-29 11:59:37 +00:00
|
|
|
|
|
|
|
def test_get_network_settings(self):
|
|
|
|
'''
|
|
|
|
Test to return the contents of the global network script.
|
|
|
|
'''
|
|
|
|
with patch.object(rh_ip, '_read_file', return_value='A'):
|
|
|
|
self.assertEqual(rh_ip.get_network_settings(), 'A')
|
|
|
|
|
|
|
|
def test_apply_network_settings(self):
|
|
|
|
'''
|
|
|
|
Test to apply global network configuration.
|
|
|
|
'''
|
|
|
|
with patch.dict(rh_ip.__salt__, {'service.restart':
|
|
|
|
MagicMock(return_value=True)}):
|
|
|
|
self.assertTrue(rh_ip.apply_network_settings())
|
|
|
|
|
|
|
|
def test_build_network_settings(self):
|
|
|
|
'''
|
|
|
|
Test to build the global network script.
|
|
|
|
'''
|
|
|
|
with patch.object(rh_ip, '_parse_rh_config', MagicMock()):
|
|
|
|
with patch.object(rh_ip, '_parse_network_settings', MagicMock()):
|
|
|
|
|
|
|
|
mock = jinja2.exceptions.TemplateNotFound('foo')
|
|
|
|
with patch.object(jinja2.Environment,
|
|
|
|
'get_template', MagicMock(side_effect=mock)):
|
|
|
|
self.assertEqual(rh_ip.build_network_settings(), '')
|
|
|
|
|
|
|
|
with patch.object(jinja2.Environment,
|
|
|
|
'get_template', MagicMock()):
|
|
|
|
with patch.object(rh_ip, '_read_temp', return_value='A'):
|
|
|
|
self.assertEqual(rh_ip.build_network_settings
|
|
|
|
(test='t'), 'A')
|
|
|
|
|
|
|
|
with patch.object(rh_ip, '_write_file_network',
|
|
|
|
return_value=None):
|
|
|
|
with patch.object(rh_ip, '_read_file',
|
|
|
|
return_value='A'):
|
|
|
|
self.assertEqual(rh_ip.build_network_settings
|
|
|
|
(test=None), 'A')
|