Merge pull request #36139 from meaksh/tests-fixes-for-2016.3

Fixing unit tests for 2016.3
This commit is contained in:
Mike Place 2016-09-08 22:20:21 +09:00 committed by GitHub
commit 1f909038f0
21 changed files with 129 additions and 100 deletions

View file

@ -41,8 +41,11 @@ def _check_systemd_salt_config():
sysctl_dir = os.path.split(conf)[0]
if not os.path.exists(sysctl_dir):
os.makedirs(sysctl_dir)
with salt.utils.fopen(conf, 'w'):
pass
try:
salt.utils.fopen(conf, 'w').close()
except (IOError, OSError):
msg = 'Could not create file: {0}'
raise CommandExecutionError(msg.format(conf))
return conf

View file

@ -8,7 +8,13 @@
# Import Python libs
from __future__ import absolute_import
import libcloud.security
try:
import libcloud.security
HAS_LIBCLOUD = True
except ImportError:
HAS_LIBCLOUD = False
import platform
import os
@ -44,7 +50,7 @@ ON_SUSE = True if 'SuSE' in platform.dist() else False
ON_MAC = True if 'Darwin' in platform.system() else False
if not os.path.exists('/etc/ssl/certs/YaST-CA.pem') and ON_SUSE:
if os.path.isfile('/etc/ssl/ca-bundle.pem'):
if os.path.isfile('/etc/ssl/ca-bundle.pem') and HAS_LIBCLOUD:
libcloud.security.CA_CERTS_PATH.append('/etc/ssl/ca-bundle.pem')
else:
HAS_CERTS = False

View file

@ -8,7 +8,13 @@
# Import Python libs
from __future__ import absolute_import
import libcloud.security
try:
import libcloud.security
HAS_LIBCLOUD = True
except ImportError:
HAS_LIBCLOUD = False
import platform
import os
@ -51,7 +57,7 @@ ON_SUSE = True if 'SuSE' in platform.dist() else False
ON_MAC = True if 'Darwin' in platform.system() else False
if not os.path.exists('/etc/ssl/certs/YaST-CA.pem') and ON_SUSE:
if os.path.isfile('/etc/ssl/ca-bundle.pem'):
if os.path.isfile('/etc/ssl/ca-bundle.pem') and HAS_LIBCLOUD:
libcloud.security.CA_CERTS_PATH.append('/etc/ssl/ca-bundle.pem')
else:
HAS_CERTS = False

View file

@ -103,6 +103,11 @@ if _has_required_boto():
StopLoggingTime=None)
@skipIf(HAS_BOTO is False, 'The boto module must be installed.')
@skipIf(_has_required_boto() is False, 'The boto3 module must be greater than'
' or equal to version {0}'
.format(required_boto3_version))
@skipIf(NO_MOCK, NO_MOCK_REASON)
class BotoCloudTrailTestCaseBase(TestCase):
conn = None
@ -128,11 +133,6 @@ class BotoCloudTrailTestCaseMixin(object):
pass
@skipIf(HAS_BOTO is False, 'The boto module must be installed.')
@skipIf(_has_required_boto() is False, 'The boto3 module must be greater than'
' or equal to version {0}'
.format(required_boto3_version))
@skipIf(NO_MOCK, NO_MOCK_REASON)
class BotoCloudTrailTestCase(BotoCloudTrailTestCaseBase, BotoCloudTrailTestCaseMixin):
'''
TestCase for salt.modules.boto_cloudtrail module

View file

@ -103,6 +103,11 @@ if _has_required_boto():
ruleDisabled=True)
@skipIf(HAS_BOTO is False, 'The boto module must be installed.')
@skipIf(_has_required_boto() is False, 'The boto3 module must be greater than'
' or equal to version {0}'
.format(required_boto3_version))
@skipIf(NO_MOCK, NO_MOCK_REASON)
class BotoIoTTestCaseBase(TestCase):
conn = None
@ -128,11 +133,6 @@ class BotoIoTTestCaseMixin(object):
pass
@skipIf(HAS_BOTO is False, 'The boto module must be installed.')
@skipIf(_has_required_boto() is False, 'The boto3 module must be greater than'
' or equal to version {0}'
.format(required_boto3_version))
@skipIf(NO_MOCK, NO_MOCK_REASON)
class BotoIoTPolicyTestCase(BotoIoTTestCaseBase, BotoIoTTestCaseMixin):
'''
TestCase for salt.modules.boto_iot module

View file

@ -109,6 +109,11 @@ def _has_required_boto():
return True
@skipIf(HAS_BOTO is False, 'The boto module must be installed.')
@skipIf(_has_required_boto() is False, 'The boto3 module must be greater than'
' or equal to version {0}'
.format(required_boto3_version))
@skipIf(NO_MOCK, NO_MOCK_REASON)
class BotoLambdaTestCaseBase(TestCase):
conn = None
@ -145,11 +150,6 @@ class BotoLambdaTestCaseMixin(object):
pass
@skipIf(HAS_BOTO is False, 'The boto module must be installed.')
@skipIf(_has_required_boto() is False, 'The boto3 module must be greater than'
' or equal to version {0}'
.format(required_boto3_version))
@skipIf(NO_MOCK, NO_MOCK_REASON)
class BotoLambdaFunctionTestCase(BotoLambdaTestCaseBase, BotoLambdaTestCaseMixin):
'''
TestCase for salt.modules.boto_lambda module

View file

@ -205,6 +205,11 @@ if _has_required_boto():
}
@skipIf(HAS_BOTO is False, 'The boto module must be installed.')
@skipIf(_has_required_boto() is False, 'The boto3 module must be greater than'
' or equal to version {0}'
.format(required_boto3_version))
@skipIf(NO_MOCK, NO_MOCK_REASON)
class BotoS3BucketTestCaseBase(TestCase):
conn = None
@ -230,11 +235,6 @@ class BotoS3BucketTestCaseMixin(object):
pass
@skipIf(HAS_BOTO is False, 'The boto module must be installed.')
@skipIf(_has_required_boto() is False, 'The boto3 module must be greater than'
' or equal to version {0}'
.format(required_boto3_version))
@skipIf(NO_MOCK, NO_MOCK_REASON)
class BotoS3BucketTestCase(BotoS3BucketTestCaseBase, BotoS3BucketTestCaseMixin):
'''
TestCase for salt.modules.boto_s3_bucket module

View file

@ -124,6 +124,13 @@ def _has_required_moto():
context = {}
@skipIf(NO_MOCK, NO_MOCK_REASON)
@skipIf(HAS_BOTO is False, 'The boto module must be installed.')
@skipIf(HAS_MOTO is False, 'The moto module must be installed.')
@skipIf(_has_required_boto() is False, 'The boto module must be greater than'
' or equal to version {0}'
.format(required_boto_version))
@skipIf(_has_required_moto() is False, 'The moto version must be >= to version {0}'.format(required_moto_version))
class BotoVpcTestCaseBase(TestCase):
def setUp(self):
boto_vpc.__context__ = {}
@ -249,13 +256,6 @@ class BotoVpcTestCaseMixin(object):
return rtbl
@skipIf(NO_MOCK, NO_MOCK_REASON)
@skipIf(HAS_BOTO is False, 'The boto module must be installed.')
@skipIf(HAS_MOTO is False, 'The moto module must be installed.')
@skipIf(_has_required_boto() is False, 'The boto module must be greater than'
' or equal to version {0}'
.format(required_boto_version))
@skipIf(_has_required_moto() is False, 'The moto version must be >= to version {0}'.format(required_moto_version))
class BotoVpcTestCase(BotoVpcTestCaseBase, BotoVpcTestCaseMixin):
'''
TestCase for salt.modules.boto_vpc module

View file

@ -84,17 +84,22 @@ class LinuxSysctlTestCase(TestCase):
self.assertEqual(linux_sysctl.assign(
'net.ipv4.ip_forward', 1), ret)
@patch('os.path.isfile', MagicMock(return_value=False))
def test_persist_no_conf_failure(self):
'''
Tests adding of config file failure
'''
with patch('salt.utils.fopen', mock_open()) as m_open:
helper_open = m_open()
helper_open.write.assertRaises(CommandExecutionError,
linux_sysctl.persist,
'net.ipv4.ip_forward',
1, config=None)
asn_cmd = {'pid': 1337, 'retcode': 0,
'stderr': "sysctl: permission denied", 'stdout': ''}
mock_asn_cmd = MagicMock(return_value=asn_cmd)
cmd = "sysctl -w net.ipv4.ip_forward=1"
mock_cmd = MagicMock(return_value=cmd)
with patch.dict(linux_sysctl.__salt__, {'cmd.run_stdout': mock_cmd,
'cmd.run_all': mock_asn_cmd}):
with patch('salt.utils.fopen', mock_open()) as m_open:
self.assertRaises(CommandExecutionError,
linux_sysctl.persist,
'net.ipv4.ip_forward',
1, config=None)
@patch('os.path.isfile', MagicMock(return_value=False))
@patch('os.path.exists', MagicMock(return_value=True))

View file

@ -72,11 +72,11 @@ class DarwinSysctlTestCase(TestCase):
Tests adding of config file failure
'''
with patch('salt.utils.fopen', mock_open()) as m_open:
helper_open = m_open()
helper_open.write.assertRaises(CommandExecutionError,
mac_sysctl.persist,
'net.inet.icmp.icmplim',
50, config=None)
m_open.side_effect = IOError(13, 'Permission denied', '/file')
self.assertRaises(CommandExecutionError,
mac_sysctl.persist,
'net.inet.icmp.icmplim',
50, config=None)
@patch('os.path.isfile', MagicMock(return_value=False))
def test_persist_no_conf_success(self):

View file

@ -141,10 +141,10 @@ class MountTestCase(TestCase):
with patch.dict(mount.__grains__, {'kernel': ''}):
with patch.object(mount, 'fstab', mock_fstab):
with patch('salt.utils.fopen', mock_open()) as m_open:
helper_open = m_open()
helper_open.write.assertRaises(CommandExecutionError,
mount.rm_fstab,
config=None)
m_open.side_effect = IOError(13, 'Permission denied:', '/file')
self.assertRaises(CommandExecutionError,
mount.rm_fstab,
'name', 'device')
def test_set_fstab(self):
'''
@ -180,11 +180,7 @@ class MountTestCase(TestCase):
mock = MagicMock(return_value={'name': 'name'})
with patch.object(mount, 'fstab', mock):
with patch('salt.utils.fopen', mock_open()) as m_open:
helper_open = m_open()
helper_open.write.assertRaises(CommandExecutionError,
mount.rm_automaster,
'name', 'device')
self.assertTrue(mount.rm_automaster('name', 'device'))
def test_set_automaster(self):
'''

View file

@ -11,7 +11,7 @@ from __future__ import absolute_import
# Import Salt Testing libs
from salttesting import skipIf, TestCase
from salttesting.helpers import ensure_in_syspath
from salttesting.mock import NO_MOCK, NO_MOCK_REASON
from salttesting.mock import NO_MOCK, NO_MOCK_REASON, MagicMock
ensure_in_syspath('../../')
# Import salt libs
@ -20,6 +20,10 @@ from salt.modules import portage_config
@skipIf(NO_MOCK, NO_MOCK_REASON)
class PortageConfigTestCase(TestCase):
class DummyAtom(object):
def __init__(self, atom):
self.cp, self.repo = atom.split("::") if "::" in atom else (atom, None)
def test_get_config_file_wildcards(self):
pairs = [
('*/*::repo', '/etc/portage/package.mask/repo'),
@ -29,7 +33,11 @@ class PortageConfigTestCase(TestCase):
('cat/pkg::repo', '/etc/portage/package.mask/cat/pkg'),
]
portage_config.portage = MagicMock()
for (atom, expected) in pairs:
dummy_atom = self.DummyAtom(atom)
portage_config.portage.dep.Atom = MagicMock(return_value=dummy_atom)
portage_config._p_to_cp = MagicMock(return_value=dummy_atom.cp)
self.assertEqual(portage_config._get_config_file('mask', atom), expected)
if __name__ == '__main__':

View file

@ -85,10 +85,12 @@ class PuppetTestCase(TestCase):
with patch('salt.utils.fopen', mock_open()):
self.assertTrue(puppet.disable())
with patch('salt.utils.fopen', mock_open()) as m_open:
helper_open = m_open()
helper_open.write.assertRaises(CommandExecutionError,
puppet.disable)
try:
with patch('salt.utils.fopen', mock_open()) as m_open:
m_open.side_effect = IOError(13, 'Permission denied:', '/file')
self.assertRaises(CommandExecutionError, puppet.disable)
except StopIteration:
pass
def test_status(self):
'''
@ -145,9 +147,8 @@ class PuppetTestCase(TestCase):
self.assertDictEqual(puppet.summary(), {'resources': 1})
with patch('salt.utils.fopen', mock_open()) as m_open:
helper_open = m_open()
helper_open.write.assertRaises(CommandExecutionError,
puppet.summary)
m_open.side_effect = IOError(13, 'Permission denied:', '/file')
self.assertRaises(CommandExecutionError, puppet.summary)
def test_plugin_sync(self):
'''

View file

@ -326,7 +326,7 @@ class UserAddTestCase(TestCase):
'''
Test the user information
'''
self.assertEqual(useradd.info('salt'), {})
self.assertEqual(useradd.info('username-that-doesnt-exist'), {})
mock = MagicMock(return_value=pwd.struct_passwd(('_TEST_GROUP',
'*',
@ -336,9 +336,7 @@ class UserAddTestCase(TestCase):
'/var/virusmails',
'/usr/bin/false')))
with patch.object(pwd, 'getpwnam', mock):
mock = MagicMock(return_value='Group Name')
with patch.object(useradd, 'list_groups', mock):
self.assertEqual(useradd.info('salt')['name'], '_TEST_GROUP')
self.assertEqual(useradd.info('username-that-doesnt-exist')['name'], '_TEST_GROUP')
# 'list_groups' function tests: 1

View file

@ -54,10 +54,18 @@ include('http')
extend_template = '''#!pyobjects
include('http')
from salt.utils.pyobjects import StateFactory
Service = StateFactory('service')
Service.running(extend('apache'), watch=[{'file': '/etc/file'}])
'''
map_template = '''#!pyobjects
from salt.utils.pyobjects import StateFactory
Service = StateFactory('service')
class Samba(Map):
__merge__ = 'samba:lookup'
@ -127,6 +135,9 @@ from salt://password.sls import password
'''
requisite_implicit_list_template = '''#!pyobjects
from salt.utils.pyobjects import StateFactory
Service = StateFactory('service')
with Pkg.installed("pkg"):
Service.running("service", watch=File("file"), require=Cmd("cmd"))
'''

View file

@ -104,6 +104,11 @@ if _has_required_boto():
StopLoggingTime=None)
@skipIf(HAS_BOTO is False, 'The boto module must be installed.')
@skipIf(_has_required_boto() is False, 'The boto3 module must be greater than'
' or equal to version {0}'
.format(required_boto3_version))
@skipIf(NO_MOCK, NO_MOCK_REASON)
class BotoCloudTrailStateTestCaseBase(TestCase):
conn = None
@ -124,11 +129,6 @@ class BotoCloudTrailStateTestCaseBase(TestCase):
session_instance.client.return_value = self.conn
@skipIf(HAS_BOTO is False, 'The boto module must be installed.')
@skipIf(_has_required_boto() is False, 'The boto3 module must be greater than'
' or equal to version {0}'
.format(required_boto3_version))
@skipIf(NO_MOCK, NO_MOCK_REASON)
class BotoCloudTrailTestCase(BotoCloudTrailStateTestCaseBase, BotoCloudTrailTestCaseMixin):
'''
TestCase for salt.modules.boto_cloudtrail state.module

View file

@ -103,6 +103,11 @@ if _has_required_boto():
principal = 'arn:aws:iot:us-east-1:1234:cert/21fc104aaaf6043f5756c1b57bda84ea8395904c43f28517799b19e4c42514'
@skipIf(HAS_BOTO is False, 'The boto module must be installed.')
@skipIf(_has_required_boto() is False, 'The boto3 module must be greater than'
' or equal to version {0}'
.format(required_boto3_version))
@skipIf(NO_MOCK, NO_MOCK_REASON)
class BotoIoTStateTestCaseBase(TestCase):
conn = None
@ -123,11 +128,6 @@ class BotoIoTStateTestCaseBase(TestCase):
session_instance.client.return_value = self.conn
@skipIf(HAS_BOTO is False, 'The boto module must be installed.')
@skipIf(_has_required_boto() is False, 'The boto3 module must be greater than'
' or equal to version {0}'
.format(required_boto3_version))
@skipIf(NO_MOCK, NO_MOCK_REASON)
class BotoIoTPolicyTestCase(BotoIoTStateTestCaseBase, BotoIoTTestCaseMixin):
'''
TestCase for salt.modules.boto_iot state.module

View file

@ -101,6 +101,11 @@ def _has_required_boto():
return True
@skipIf(HAS_BOTO is False, 'The boto module must be installed.')
@skipIf(_has_required_boto() is False, 'The boto3 module must be greater than'
' or equal to version {0}'
.format(required_boto3_version))
@skipIf(NO_MOCK, NO_MOCK_REASON)
class BotoLambdaStateTestCaseBase(TestCase):
conn = None
@ -121,11 +126,6 @@ class BotoLambdaStateTestCaseBase(TestCase):
session_instance.client.return_value = self.conn
@skipIf(HAS_BOTO is False, 'The boto module must be installed.')
@skipIf(_has_required_boto() is False, 'The boto3 module must be greater than'
' or equal to version {0}'
.format(required_boto3_version))
@skipIf(NO_MOCK, NO_MOCK_REASON)
class BotoLambdaFunctionTestCase(BotoLambdaStateTestCaseBase, BotoLambdaTestCaseMixin):
'''
TestCase for salt.modules.boto_lambda state.module

View file

@ -277,6 +277,11 @@ if _has_required_boto():
}
@skipIf(HAS_BOTO is False, 'The boto module must be installed.')
@skipIf(_has_required_boto() is False, 'The boto3 module must be greater than'
' or equal to version {0}'
.format(required_boto3_version))
@skipIf(NO_MOCK, NO_MOCK_REASON)
class BotoS3BucketStateTestCaseBase(TestCase):
conn = None
@ -297,11 +302,6 @@ class BotoS3BucketStateTestCaseBase(TestCase):
session_instance.client.return_value = self.conn
@skipIf(HAS_BOTO is False, 'The boto module must be installed.')
@skipIf(_has_required_boto() is False, 'The boto3 module must be greater than'
' or equal to version {0}'
.format(required_boto3_version))
@skipIf(NO_MOCK, NO_MOCK_REASON)
class BotoS3BucketTestCase(BotoS3BucketStateTestCaseBase, BotoS3BucketTestCaseMixin):
'''
TestCase for salt.modules.boto_s3_bucket state.module

View file

@ -150,16 +150,17 @@ class NetworkTestCase(TestCase):
interfaces = network._interfaces_ifconfig(SOLARIS)
self.assertEqual(interfaces,
{'ilbext0': {'inet': [{'address': '10.10.11.11',
'broadcast': '10.10.11.31',
'netmask': '255.255.255.224'},
{'address': '10.10.11.12',
'broadcast': '10.10.11.31',
'netmask': '255.255.255.224'}],
'inet6': [{'address': '::',
'prefixlen': '0'}],
'inet6': [],
'up': True},
'ilbint0': {'inet': [{'address': '10.6.0.11',
'broadcast': '10.6.0.255',
'netmask': '255.255.255.0'}],
'inet6': [{'address': '::',
'prefixlen': '0'}],
'inet6': [],
'up': True},
'lo0': {'inet': [{'address': '127.0.0.1',
'netmask': '255.0.0.0'}],
@ -174,8 +175,7 @@ class NetworkTestCase(TestCase):
'up': True},
'vpn0': {'inet': [{'address': '10.6.0.14',
'netmask': '255.0.0.0'}],
'inet6': [{'address': '::',
'prefixlen': '0'}],
'inet6': [],
'up': True}}
)

View file

@ -527,14 +527,9 @@ class UtilsTestCase(TestCase):
ret = utils.date_cast('Mon Dec 23 10:19:15 MST 2013')
expected_ret = datetime.datetime(2013, 12, 23, 10, 19, 15)
self.assertEqual(ret, expected_ret)
except ImportError:
try:
ret = utils.date_cast('Mon Dec 23 10:19:15 MST 2013')
expected_ret = datetime.datetime(2013, 12, 23, 10, 19, 15)
self.assertEqual(ret, expected_ret)
except RuntimeError:
# Unparseable without timelib installed
self.skipTest('\'timelib\' is not installed')
except RuntimeError:
# Unparseable without timelib installed
self.skipTest('\'timelib\' is not installed')
@skipIf(not HAS_TIMELIB, '\'timelib\' is not installed')
def test_date_format(self):