mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Merge pull request #41279 from Ch3LL/add_grain_tests
Add fqdn and dns core grain tests
This commit is contained in:
commit
6169b52749
1 changed files with 133 additions and 0 deletions
|
@ -28,6 +28,15 @@ from salt.grains import core
|
|||
|
||||
# Globals
|
||||
core.__salt__ = {}
|
||||
core.__opts__ = {}
|
||||
IPv4Address = salt.ext.ipaddress.IPv4Address
|
||||
IPv6Address = salt.ext.ipaddress.IPv6Address
|
||||
IP4_LOCAL = '127.0.0.1'
|
||||
IP4_ADD1 = '10.0.0.1'
|
||||
IP4_ADD2 = '10.0.0.2'
|
||||
IP6_LOCAL = '::1'
|
||||
IP6_ADD1 = '2001:4860:4860::8844'
|
||||
IP6_ADD2 = '2001:4860:4860::8888'
|
||||
|
||||
|
||||
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
||||
|
@ -459,6 +468,130 @@ PATCHLEVEL = 3
|
|||
self.assertListEqual(list(os_grains.get('osrelease_info')), os_release_map['osrelease_info'])
|
||||
self.assertEqual(os_grains.get('osmajorrelease'), os_release_map['osmajorrelease'])
|
||||
|
||||
def _check_ipaddress(self, value, ip_v):
|
||||
'''
|
||||
check if ip address in a list is valid
|
||||
'''
|
||||
for val in value:
|
||||
assert isinstance(val, six.string_types)
|
||||
ip_method = 'is_ipv{0}'.format(ip_v)
|
||||
self.assertTrue(getattr(salt.utils.network, ip_method)(val))
|
||||
|
||||
def _check_empty(self, key, value, empty):
|
||||
'''
|
||||
if empty is False and value does not exist assert error
|
||||
if empty is True and value exists assert error
|
||||
'''
|
||||
if not empty and not value:
|
||||
raise Exception("{0} is empty, expecting a value".format(key))
|
||||
elif empty and value:
|
||||
raise Exception("{0} is suppose to be empty. value: {1} \
|
||||
exists".format(key, value))
|
||||
|
||||
@skipIf(not salt.utils.is_linux(), 'System is not Linux')
|
||||
def test_fqdn_return(self):
|
||||
'''
|
||||
test ip4 and ip6 return values
|
||||
'''
|
||||
net_ip4_mock = [IP4_LOCAL, IP4_ADD1, IP4_ADD2]
|
||||
net_ip6_mock = [IP6_LOCAL, IP6_ADD1, IP6_ADD2]
|
||||
|
||||
self._run_fqdn_tests(net_ip4_mock, net_ip6_mock,
|
||||
ip4_empty=False, ip6_empty=False)
|
||||
|
||||
@skipIf(not salt.utils.is_linux(), 'System is not Linux')
|
||||
def test_fqdn6_empty(self):
|
||||
'''
|
||||
test when ip6 is empty
|
||||
'''
|
||||
net_ip4_mock = [IP4_LOCAL, IP4_ADD1, IP4_ADD2]
|
||||
net_ip6_mock = []
|
||||
|
||||
self._run_fqdn_tests(net_ip4_mock, net_ip6_mock,
|
||||
ip4_empty=False)
|
||||
|
||||
@skipIf(not salt.utils.is_linux(), 'System is not Linux')
|
||||
def test_fqdn4_empty(self):
|
||||
'''
|
||||
test when ip4 is empty
|
||||
'''
|
||||
net_ip4_mock = []
|
||||
net_ip6_mock = [IP6_LOCAL, IP6_ADD1, IP6_ADD2]
|
||||
|
||||
self._run_fqdn_tests(net_ip4_mock, net_ip6_mock,
|
||||
ip6_empty=False)
|
||||
|
||||
@skipIf(not salt.utils.is_linux(), 'System is not Linux')
|
||||
def test_fqdn_all_empty(self):
|
||||
'''
|
||||
test when both ip4 and ip6 are empty
|
||||
'''
|
||||
net_ip4_mock = []
|
||||
net_ip6_mock = []
|
||||
|
||||
self._run_fqdn_tests(net_ip4_mock, net_ip6_mock)
|
||||
|
||||
def _run_fqdn_tests(self, net_ip4_mock, net_ip6_mock,
|
||||
ip6_empty=True, ip4_empty=True):
|
||||
|
||||
def _check_type(key, value, ip4_empty, ip6_empty):
|
||||
'''
|
||||
check type and other checks
|
||||
'''
|
||||
assert isinstance(value, list)
|
||||
|
||||
if '4' in key:
|
||||
self._check_empty(key, value, ip4_empty)
|
||||
self._check_ipaddress(value, ip_v='4')
|
||||
elif '6' in key:
|
||||
self._check_empty(key, value, ip6_empty)
|
||||
self._check_ipaddress(value, ip_v='6')
|
||||
|
||||
ip4_mock = [(2, 1, 6, '', (IP4_ADD1, 0)),
|
||||
(2, 3, 0, '', (IP4_ADD2, 0))]
|
||||
ip6_mock = [(10, 1, 6, '', (IP6_ADD1, 0, 0, 0)),
|
||||
(10, 3, 0, '', (IP6_ADD2, 0, 0, 0))]
|
||||
|
||||
with patch.dict(core.__opts__, {'ipv6': False}):
|
||||
with patch.object(salt.utils.network, 'ip_addrs',
|
||||
MagicMock(return_value=net_ip4_mock)):
|
||||
with patch.object(salt.utils.network, 'ip_addrs6',
|
||||
MagicMock(return_value=net_ip6_mock)):
|
||||
with patch.object(core.socket, 'getaddrinfo', side_effect=[ip4_mock, ip6_mock]):
|
||||
get_fqdn = core.ip_fqdn()
|
||||
ret_keys = ['fqdn_ip4', 'fqdn_ip6', 'ipv4', 'ipv6']
|
||||
for key in ret_keys:
|
||||
value = get_fqdn[key]
|
||||
_check_type(key, value, ip4_empty, ip6_empty)
|
||||
|
||||
@skipIf(not salt.utils.is_linux(), 'System is not Linux')
|
||||
def test_dns_return(self):
|
||||
'''
|
||||
test the return for a dns grain. test for issue:
|
||||
https://github.com/saltstack/salt/issues/41230
|
||||
'''
|
||||
resolv_mock = {'domain': '', 'sortlist': [], 'nameservers':
|
||||
[IPv4Address(IP4_ADD1),
|
||||
IPv6Address(IP6_ADD1)], 'ip4_nameservers':
|
||||
[IPv4Address(IP4_ADD1)],
|
||||
'search': ['test.saltstack.com'], 'ip6_nameservers':
|
||||
[IPv6Address(IP6_ADD1)], 'options': []}
|
||||
ret = {'dns': {'domain': '', 'sortlist': [], 'nameservers':
|
||||
[IP4_ADD1, IP6_ADD1], 'ip4_nameservers':
|
||||
[IP4_ADD1], 'search': ['test.saltstack.com'],
|
||||
'ip6_nameservers': [IP6_ADD1], 'options':
|
||||
[]}}
|
||||
self._run_dns_test(resolv_mock, ret)
|
||||
|
||||
def _run_dns_test(self, resolv_mock, ret):
|
||||
with patch.object(salt.utils, 'is_windows',
|
||||
MagicMock(return_value=False)):
|
||||
with patch.dict(core.__opts__, {'ipv6': False}):
|
||||
with patch.object(salt.utils.dns, 'parse_resolv',
|
||||
MagicMock(return_value=resolv_mock)):
|
||||
get_dns = core.dns()
|
||||
self.assertEqual(get_dns, ret)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
from integration import run_tests
|
||||
|
|
Loading…
Add table
Reference in a new issue