Merge branch '2018.3' into 30367_orch_return_false_when_function_fails

This commit is contained in:
Nicole Thomas 2018-05-30 09:56:14 -04:00 committed by GitHub
commit 09242697b8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 211 additions and 18 deletions

View file

@ -451,7 +451,11 @@ def _bsd_memdata(osdata):
if osdata['kernel'] in ['OpenBSD', 'NetBSD']:
swapctl = salt.utils.path.which('swapctl')
swap_total = __salt__['cmd.run']('{0} -sk'.format(swapctl)).split(' ')[1]
swap_data = __salt__['cmd.run']('{0} -sk'.format(swapctl))
if swap_data == 'no swap devices configured':
swap_total = 0
else:
swap_total = swap_data.split(' ')[1]
else:
swap_total = __salt__['cmd.run']('{0} -n vm.swap_total'.format(sysctl))
grains['swap_total'] = int(swap_total) // 1024 // 1024

View file

@ -56,6 +56,7 @@ import salt.utils.data
import salt.utils.json
import salt.utils.versions
from salt.ext import six
from salt.ext.six.moves import map
from salt.exceptions import SaltInvocationError, CommandExecutionError
# Import third party libs
@ -65,6 +66,7 @@ try:
import boto.ec2
# pylint: enable=unused-import
from boto.ec2.blockdevicemapping import BlockDeviceMapping, BlockDeviceType
from boto.ec2.networkinterface import NetworkInterfaceSpecification, NetworkInterfaceCollection
HAS_BOTO = True
except ImportError:
HAS_BOTO = False
@ -1003,14 +1005,19 @@ def run(image_id, name=None, tags=None, key_name=None, security_groups=None,
return False
security_group_ids += [r]
if all((network_interface_id, network_interface_name)):
raise SaltInvocationError('Only one of network_interface_id or '
'network_interface_name may be provided.')
network_interface_args = list(map(int, [network_interface_id is not None,
network_interface_name is not None,
network_interfaces is not None]))
if sum(network_interface_args) > 1:
raise SaltInvocationError('Only one of network_interface_id, '
'network_interface_name or '
'network_interfaces may be provided.')
if network_interface_name:
result = get_network_interface_id(network_interface_name,
region=region, key=key,
keyid=keyid,
profile=profile)
region=region, key=key,
keyid=keyid,
profile=profile)
network_interface_id = result['result']
if not network_interface_id:
log.warning(
@ -1019,17 +1026,20 @@ def run(image_id, name=None, tags=None, key_name=None, security_groups=None,
)
if network_interface_id:
interface = boto.ec2.networkinterface.NetworkInterfaceSpecification(
interface = NetworkInterfaceSpecification(
network_interface_id=network_interface_id,
device_index=0
)
device_index=0)
else:
interface = boto.ec2.networkinterface.NetworkInterfaceSpecification(
interface = NetworkInterfaceSpecification(
subnet_id=subnet_id,
groups=security_group_ids,
device_index=0
)
interfaces = boto.ec2.networkinterface.NetworkInterfaceCollection(interface)
device_index=0)
if network_interfaces:
interfaces_specs = [NetworkInterfaceSpecification(**x) for x in network_interfaces]
interfaces = NetworkInterfaceCollection(*interfaces_specs)
else:
interfaces = NetworkInterfaceCollection(interface)
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)

View file

@ -181,7 +181,9 @@ def latest_version(*names, **kwargs):
out = __salt__['cmd.run'](cmd, output_loglevel='trace')
for line in out.splitlines():
p = line.split(',' if _supports_parsing() else None)
if line.startswith('No results found for'):
return pkglist
p = line.split(';' if _supports_parsing() else None)
if p and p[0] in ('=:', '<:', '>:', ''):
# These are explanation comments
@ -190,7 +192,7 @@ def latest_version(*names, **kwargs):
s = _splitpkg(p[0])
if s:
if not s[0] in pkglist:
if len(p) > 1 and p[1] == '<':
if len(p) > 1 and p[1] in ('<', '', '='):
pkglist[s[0]] = s[1]
else:
pkglist[s[0]] = ''
@ -669,7 +671,6 @@ def file_dict(*packages):
for package in packages:
cmd = ['pkg_info', '-qL', package]
ret = __salt__['cmd.run_all'](cmd, output_loglevel='trace')
files[package] = []
for line in ret['stderr'].splitlines():
errors.append(line)
@ -681,7 +682,7 @@ def file_dict(*packages):
continue # unexpected string
ret = {'errors': errors, 'files': files}
for field in ret:
for field in list(ret):
if not ret[field] or ret[field] == '':
del ret[field]
return ret

View file

@ -0,0 +1,178 @@
# -*- coding: utf-8 -*-
# Import Python Libs
from __future__ import absolute_import
import os
# Import Salt Testing Libs
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.unit import TestCase, skipIf
from tests.support.mock import (
MagicMock,
patch,
NO_MOCK,
NO_MOCK_REASON
)
# Import Salt Libs
import salt.modules.pkgin as pkgin
@skipIf(NO_MOCK, NO_MOCK_REASON)
class PkginTestCase(TestCase, LoaderModuleMockMixin):
'''
Test cases for salt.modules.pkgin
'''
def setup_loader_modules(self):
return {
pkgin: {
'__opts__': {
'cachedir': '/tmp'
}
}
}
def test_search(self):
'''
Test searching for a package
'''
# Test searching for an available and uninstalled package
pkgin_out = [
'somepkg-1.0 Some package description here',
'',
'=: package is installed and up-to-date',
'<: package is installed but newer version is available',
'>: installed package has a greater version than available package'
]
pkgin__get_version_mock = MagicMock(return_value=['0', '9', '0'])
pkgin__check_pkgin_mock = MagicMock(return_value='/opt/pkg/bin/pkgin')
pkgin_search_cmd = MagicMock(return_value=os.linesep.join(pkgin_out))
with patch('salt.modules.pkgin._get_version', pkgin__get_version_mock), \
patch('salt.modules.pkgin._check_pkgin', pkgin__check_pkgin_mock), \
patch.dict(pkgin.__salt__, {'cmd.run': pkgin_search_cmd}):
self.assertDictEqual(pkgin.search('somepkg'), {'somepkg': '1.0'})
# Test searching for an available and installed package
pkgin_out = [
'somepkg-1.0 = Some package description here',
'',
'=: package is installed and up-to-date',
'<: package is installed but newer version is available',
'>: installed package has a greater version than available package'
]
pkgin_search_cmd = MagicMock(return_value=os.linesep.join(pkgin_out))
with patch('salt.modules.pkgin._get_version', pkgin__get_version_mock), \
patch('salt.modules.pkgin._check_pkgin', pkgin__check_pkgin_mock), \
patch.dict(pkgin.__salt__, {'cmd.run': pkgin_search_cmd}):
self.assertDictEqual(pkgin.search('somepkg'), {'somepkg': '1.0'})
def test_latest_version(self):
'''
Test getting the latest version of a package
'''
# Test getting the latest version of an uninstalled package
pkgin_out = [
'somepkg-1.0;;Some package description here',
'',
'=: package is installed and up-to-date',
'<: package is installed but newer version is available',
'>: installed package has a greater version than available package'
]
pkgin__get_version_mock = MagicMock(return_value=['0', '9', '0'])
pkgin__check_pkgin_mock = MagicMock(return_value='/opt/pkg/bin/pkgin')
pkgin_refresh_db_mock = MagicMock(return_value=True)
pkgin_search_cmd = MagicMock(return_value=os.linesep.join(pkgin_out))
with patch('salt.modules.pkgin.refresh_db', pkgin_refresh_db_mock), \
patch('salt.modules.pkgin._get_version', pkgin__get_version_mock), \
patch('salt.modules.pkgin._check_pkgin', pkgin__check_pkgin_mock), \
patch.dict(pkgin.__salt__, {'cmd.run': pkgin_search_cmd}):
self.assertEqual(pkgin.latest_version('somepkg'), '1.0')
# Test getting the latest version of an installed package
pkgin_out = [
'somepkg-1.1;<;Some package description here',
'',
'=: package is installed and up-to-date',
'<: package is installed but newer version is available',
'>: installed package has a greater version than available package'
]
pkgin_refresh_db_mock = MagicMock(return_value=True)
pkgin_search_cmd = MagicMock(return_value=os.linesep.join(pkgin_out))
with patch('salt.modules.pkgin.refresh_db', pkgin_refresh_db_mock), \
patch('salt.modules.pkgin._get_version', pkgin__get_version_mock), \
patch('salt.modules.pkgin._check_pkgin', pkgin__check_pkgin_mock), \
patch.dict(pkgin.__salt__, {'cmd.run': pkgin_search_cmd}):
self.assertEqual(pkgin.latest_version('somepkg'), '1.1')
# Test getting the latest version of a package that is already installed
# and is already at the latest version
pkgin_out = [
'somepkg-1.2;=;Some package description here',
'',
'=: package is installed and up-to-date',
'<: package is installed but newer version is available',
'>: installed package has a greater version than available package'
]
pkgin_refresh_db_mock = MagicMock(return_value=True)
pkgin_search_cmd = MagicMock(return_value=os.linesep.join(pkgin_out))
with patch('salt.modules.pkgin.refresh_db', pkgin_refresh_db_mock), \
patch('salt.modules.pkgin._get_version', pkgin__get_version_mock), \
patch('salt.modules.pkgin._check_pkgin', pkgin__check_pkgin_mock), \
patch.dict(pkgin.__salt__, {'cmd.run': pkgin_search_cmd}):
self.assertEqual(pkgin.latest_version('somepkg'), '1.2')
# Test getting the latest version of a bogus package
pkgin_out = 'No results found for ^boguspkg$'
pkgin_refresh_db_mock = MagicMock(return_value=True)
pkgin_search_cmd = MagicMock(return_value=pkgin_out)
with patch('salt.modules.pkgin.refresh_db', pkgin_refresh_db_mock), \
patch('salt.modules.pkgin._get_version', pkgin__get_version_mock), \
patch('salt.modules.pkgin._check_pkgin', pkgin__check_pkgin_mock), \
patch.dict(pkgin.__salt__, {'cmd.run': pkgin_search_cmd}):
self.assertEqual(pkgin.latest_version('boguspkg'), {})
def test_file_dict(self):
'''
Test that file_dict doesn't crash
'''
pkg_info_stdout = [
'/opt/pkg/bin/pkgin',
'/opt/pkg/man/man1/pkgin.1',
'/opt/pkg/share/examples/pkgin/preferred.conf.example',
'/opt/pkg/share/examples/pkgin/repositories.conf.example'
]
pkg_info_out = {
'pid': 1234,
'retcode': 0,
'stderr': '',
'stdout': os.linesep.join(pkg_info_stdout)
}
pkg_info_cmd = MagicMock(return_value=pkg_info_out)
with patch.dict(pkgin.__salt__, {'cmd.run_all': pkg_info_cmd}):
self.assertDictEqual(pkgin.file_dict('pkgin'), {
'files': {
'pkgin': [
'/opt/pkg/bin/pkgin',
'/opt/pkg/man/man1/pkgin.1',
'/opt/pkg/share/examples/pkgin/preferred.conf.example',
'/opt/pkg/share/examples/pkgin/repositories.conf.example'
]
}
})