From e5fc36b5fad0683f57022bf2f3c63f453cda5e8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Su=C3=A1rez=20Hern=C3=A1ndez?= Date: Tue, 6 Sep 2016 11:21:05 +0100 Subject: [PATCH 18/19] Unit tests fixes for 2016.3.2 * Fixing skipped boto tests to prevent errors if boto3 does not exists. * Fix tests that assert CommandExecutionError (#32485) Trying to assert that an exception was raised using helper_open.write.assertRaises() is bogus--there is no such method. Use standard unittest.assertRaises() instead. * Skip utils_test if timelib is not installed (#32699) date_cast() throws a RuntimeError, not an ImportError * Fix tests (#35693) Fix tests/unit/modules/useradd_test.py::UserAddTestCase::test_info Fix unit/pyobjects_test.py::MapTests::test_map Fix tests/unit/pyobjects_test.py::RendererTests::test_extend Fix tests/unit/pyobjects_test.py::RendererTests::test_requisite_implicit_list * Fix tests to prevent errors when libcloud is not present * Fixed _interfaces_ifconfig output for SunOS test * Fix PortageConfigTestCase in case of portage is not present * Rename dockerio.py unit tests to dockerio_test.py These tests have never run automatically because of an incorrect file name. Added a skipIf on these tests as they are currently non-functioning and the module they're testing has been deprecated. * Prevent tests failures if boto does not exists --- salt/modules/boto_elb.py | 2 +- salt/modules/linux_sysctl.py | 6 +- tests/unit/cloud/clouds/dimensiondata_test.py | 10 ++- tests/unit/cloud/clouds/gce_test.py | 10 ++- tests/unit/modules/boto_cloudtrail_test.py | 10 +-- tests/unit/modules/boto_iot_test.py | 10 +-- tests/unit/modules/boto_lambda_test.py | 10 +-- tests/unit/modules/boto_s3_bucket_test.py | 10 +-- tests/unit/modules/boto_secgroup_test.py | 1 + tests/unit/modules/boto_vpc_test.py | 14 ++-- tests/unit/modules/linux_sysctl_test.py | 19 +++-- tests/unit/modules/mac_sysctl_test.py | 10 +-- tests/unit/modules/mount_test.py | 14 ++-- tests/unit/modules/portage_config.py | 10 ++- tests/unit/modules/puppet_test.py | 15 ++-- tests/unit/modules/useradd_test.py | 6 +- tests/unit/pyobjects_test.py | 11 +++ tests/unit/states/boto_cloudtrail_test.py | 10 +-- tests/unit/states/boto_iot_test.py | 10 +-- tests/unit/states/boto_lambda_test.py | 10 +-- tests/unit/states/boto_s3_bucket_test.py | 10 +-- tests/unit/states/dockerio.py | 112 ------------------------- tests/unit/states/dockerio_test.py | 113 ++++++++++++++++++++++++++ tests/unit/utils/network.py | 12 +-- tests/unit/utils/utils_test.py | 11 +-- 25 files changed, 244 insertions(+), 212 deletions(-) delete mode 100644 tests/unit/states/dockerio.py create mode 100644 tests/unit/states/dockerio_test.py diff --git a/salt/modules/boto_elb.py b/salt/modules/boto_elb.py index 31df1fc..162abcd 100644 --- a/salt/modules/boto_elb.py +++ b/salt/modules/boto_elb.py @@ -57,6 +57,7 @@ log = logging.getLogger(__name__) # Import third party libs try: import boto + import boto.ec2 # pylint: enable=unused-import # connection settings were added in 2.33.0 required_boto_version = '2.33.0' if (_LooseVersion(boto.__version__) < @@ -64,7 +65,6 @@ try: msg = 'boto_elb requires boto {0}.'.format(required_boto_version) logging.debug(msg) raise ImportError() - import boto.ec2 from boto.ec2.elb import HealthCheck from boto.ec2.elb.attributes import AccessLogAttribute from boto.ec2.elb.attributes import ConnectionDrainingAttribute diff --git a/salt/modules/linux_sysctl.py b/salt/modules/linux_sysctl.py index b016ca6..7702d52 100644 --- a/salt/modules/linux_sysctl.py +++ b/salt/modules/linux_sysctl.py @@ -41,7 +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) - salt.utils.fopen(conf, 'w').close() + try: + salt.utils.fopen(conf, 'w').close() + except (IOError, OSError): + msg = 'Could not create file: {0}' + raise CommandExecutionError(msg.format(conf)) return conf diff --git a/tests/unit/cloud/clouds/dimensiondata_test.py b/tests/unit/cloud/clouds/dimensiondata_test.py index aa7f2c0..ee01d65 100644 --- a/tests/unit/cloud/clouds/dimensiondata_test.py +++ b/tests/unit/cloud/clouds/dimensiondata_test.py @@ -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 diff --git a/tests/unit/cloud/clouds/gce_test.py b/tests/unit/cloud/clouds/gce_test.py index 87824eb..c90f8ab 100644 --- a/tests/unit/cloud/clouds/gce_test.py +++ b/tests/unit/cloud/clouds/gce_test.py @@ -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 diff --git a/tests/unit/modules/boto_cloudtrail_test.py b/tests/unit/modules/boto_cloudtrail_test.py index 2f86101..264a795 100644 --- a/tests/unit/modules/boto_cloudtrail_test.py +++ b/tests/unit/modules/boto_cloudtrail_test.py @@ -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 diff --git a/tests/unit/modules/boto_iot_test.py b/tests/unit/modules/boto_iot_test.py index 73c362f..520bfe9 100644 --- a/tests/unit/modules/boto_iot_test.py +++ b/tests/unit/modules/boto_iot_test.py @@ -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 diff --git a/tests/unit/modules/boto_lambda_test.py b/tests/unit/modules/boto_lambda_test.py index 01ca245..ad7fb33 100644 --- a/tests/unit/modules/boto_lambda_test.py +++ b/tests/unit/modules/boto_lambda_test.py @@ -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 diff --git a/tests/unit/modules/boto_s3_bucket_test.py b/tests/unit/modules/boto_s3_bucket_test.py index f4b1992..5e7d6be 100644 --- a/tests/unit/modules/boto_s3_bucket_test.py +++ b/tests/unit/modules/boto_s3_bucket_test.py @@ -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 diff --git a/tests/unit/modules/boto_secgroup_test.py b/tests/unit/modules/boto_secgroup_test.py index cc88568..7fd51ad 100644 --- a/tests/unit/modules/boto_secgroup_test.py +++ b/tests/unit/modules/boto_secgroup_test.py @@ -23,6 +23,7 @@ import salt.loader from salt.ext.six.moves import range # pylint: disable=redefined-builtin try: import boto + import boto.ec2 # pylint: enable=unused-import HAS_BOTO = True except ImportError: HAS_BOTO = False diff --git a/tests/unit/modules/boto_vpc_test.py b/tests/unit/modules/boto_vpc_test.py index 64c7976..162bcae 100644 --- a/tests/unit/modules/boto_vpc_test.py +++ b/tests/unit/modules/boto_vpc_test.py @@ -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 diff --git a/tests/unit/modules/linux_sysctl_test.py b/tests/unit/modules/linux_sysctl_test.py index 89bea83..1eca7d5 100644 --- a/tests/unit/modules/linux_sysctl_test.py +++ b/tests/unit/modules/linux_sysctl_test.py @@ -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)) diff --git a/tests/unit/modules/mac_sysctl_test.py b/tests/unit/modules/mac_sysctl_test.py index e90ec64..533397b 100644 --- a/tests/unit/modules/mac_sysctl_test.py +++ b/tests/unit/modules/mac_sysctl_test.py @@ -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): diff --git a/tests/unit/modules/mount_test.py b/tests/unit/modules/mount_test.py index 290c368..b2cf904 100644 --- a/tests/unit/modules/mount_test.py +++ b/tests/unit/modules/mount_test.py @@ -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): ''' diff --git a/tests/unit/modules/portage_config.py b/tests/unit/modules/portage_config.py index 8da1ebe..6275442 100644 --- a/tests/unit/modules/portage_config.py +++ b/tests/unit/modules/portage_config.py @@ -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__': diff --git a/tests/unit/modules/puppet_test.py b/tests/unit/modules/puppet_test.py index 02bc2e1..2cdd696 100644 --- a/tests/unit/modules/puppet_test.py +++ b/tests/unit/modules/puppet_test.py @@ -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): ''' diff --git a/tests/unit/modules/useradd_test.py b/tests/unit/modules/useradd_test.py index 7e646b6..cc9e610 100644 --- a/tests/unit/modules/useradd_test.py +++ b/tests/unit/modules/useradd_test.py @@ -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 diff --git a/tests/unit/pyobjects_test.py b/tests/unit/pyobjects_test.py index f1c3e29..3eb4bd5 100644 --- a/tests/unit/pyobjects_test.py +++ b/tests/unit/pyobjects_test.py @@ -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")) ''' diff --git a/tests/unit/states/boto_cloudtrail_test.py b/tests/unit/states/boto_cloudtrail_test.py index 48fbd32..9e6dd95 100644 --- a/tests/unit/states/boto_cloudtrail_test.py +++ b/tests/unit/states/boto_cloudtrail_test.py @@ -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 diff --git a/tests/unit/states/boto_iot_test.py b/tests/unit/states/boto_iot_test.py index 8c2549d..81d68c8 100644 --- a/tests/unit/states/boto_iot_test.py +++ b/tests/unit/states/boto_iot_test.py @@ -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 diff --git a/tests/unit/states/boto_lambda_test.py b/tests/unit/states/boto_lambda_test.py index 4557aed..7b02391 100644 --- a/tests/unit/states/boto_lambda_test.py +++ b/tests/unit/states/boto_lambda_test.py @@ -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 diff --git a/tests/unit/states/boto_s3_bucket_test.py b/tests/unit/states/boto_s3_bucket_test.py index 4049e9a..03c406f 100644 --- a/tests/unit/states/boto_s3_bucket_test.py +++ b/tests/unit/states/boto_s3_bucket_test.py @@ -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 diff --git a/tests/unit/states/dockerio.py b/tests/unit/states/dockerio.py deleted file mode 100644 index c73b633..0000000 --- a/tests/unit/states/dockerio.py +++ /dev/null @@ -1,112 +0,0 @@ -# -*- coding: utf-8 -*- - -# Import Python libs -from __future__ import absolute_import -from contextlib import contextmanager - -# Import Salt Testing libs -from salttesting import skipIf, TestCase -from salttesting.mock import NO_MOCK, NO_MOCK_REASON, MagicMock - - -@contextmanager -def provision_state(module, fixture): - previous_dict = getattr(module, '__salt__', {}).copy() - try: - module.__dict__.setdefault('__salt__', {}).update(fixture) - yield - finally: - setattr(module, '__salt__', previous_dict) - - -@skipIf(NO_MOCK, NO_MOCK_REASON) -class DockerStateTestCase(TestCase): - def test_docker_run_success(self): - from salt.states import dockerio - salt_fixture = {'docker.retcode': MagicMock(return_value=0), - 'docker.run_all': MagicMock( - return_value={'stdout': '.\n..\n', - 'stderr': '', - 'status': True, - 'comment': 'Success', - 'retcode': 0})} - - with provision_state(dockerio, salt_fixture): - result = dockerio.run('ls /', 'ubuntu') - - self.assertEqual(result, {'name': 'ls /', - 'result': True, - 'comment': 'Success', - 'changes': {}}) - - def test_docker_run_failure(self): - from salt.states import dockerio - salt_fixture = {'docker.retcode': MagicMock(return_value=0), - 'docker.run_all': MagicMock( - return_value={'stdout': '', - 'stderr': 'Error', - 'status': False, - 'comment': 'Failure', - 'retcode': 1})} - - with provision_state(dockerio, salt_fixture): - result = dockerio.run('ls /', 'ubuntu') - - self.assertEqual(result, {'name': 'ls /', - 'result': False, - 'comment': 'Failure', - 'changes': {}}) - - def test_docker_run_onlyif(self): - from salt.states import dockerio - salt_fixture = {'docker.retcode': MagicMock(return_value=1), - 'docker.run_all': None} - with provision_state(dockerio, salt_fixture): - result = dockerio.run('ls /', 'ubuntu', - onlyif='ls -l') - self.assertEqual(result, {'name': 'ls /', - 'result': True, - 'comment': 'onlyif execution failed', - 'changes': {}}) - - def test_docker_run_unless(self): - from salt.states import dockerio - salt_fixture = {'docker.retcode': MagicMock(return_value=0), - 'docker.run_all': None} - with provision_state(dockerio, salt_fixture): - result = dockerio.run('ls /', 'ubuntu', - unless='ls -l') - self.assertEqual(result, {'name': 'ls /', - 'result': True, - 'comment': 'unless execution succeeded', - 'changes': {}}) - - def test_docker_run_docked_onlyif(self): - from salt.states import dockerio - salt_fixture = {'docker.retcode': MagicMock(return_value=1), - 'docker.run_all': None} - with provision_state(dockerio, salt_fixture): - result = dockerio.run('ls /', 'ubuntu', - docked_onlyif='ls -l') - self.assertEqual(result, {'name': 'ls /', - 'result': True, - 'comment': 'docked_onlyif execution failed', - 'changes': {}}) - - def test_docker_run_docked_unless(self): - from salt.states import dockerio - salt_fixture = {'docker.retcode': MagicMock(return_value=0), - 'docker.run_all': None} - with provision_state(dockerio, salt_fixture): - result = dockerio.run('ls /', 'ubuntu', - docked_unless='ls -l') - self.assertEqual(result, {'name': 'ls /', - 'result': True, - 'comment': ('docked_unless execution' - ' succeeded'), - 'changes': {}}) - - -if __name__ == '__main__': - from integration import run_tests - run_tests(DockerStateTestCase, needs_daemon=False) diff --git a/tests/unit/states/dockerio_test.py b/tests/unit/states/dockerio_test.py new file mode 100644 index 0000000..54f51be --- /dev/null +++ b/tests/unit/states/dockerio_test.py @@ -0,0 +1,113 @@ +# -*- coding: utf-8 -*- + +# Import Python libs +from __future__ import absolute_import +from contextlib import contextmanager + +# Import Salt Testing libs +from salttesting import skipIf, TestCase +from salttesting.mock import NO_MOCK, NO_MOCK_REASON, MagicMock + + +@contextmanager +def provision_state(module, fixture): + previous_dict = getattr(module, '__salt__', {}).copy() + try: + module.__dict__.setdefault('__salt__', {}).update(fixture) + yield + finally: + setattr(module, '__salt__', previous_dict) + + +@skipIf(NO_MOCK, NO_MOCK_REASON) +@skipIf(True, 'Skipped: This module has been deprecated.') +class DockerStateTestCase(TestCase): + def test_docker_run_success(self): + from salt.states import dockerio + salt_fixture = {'docker.retcode': MagicMock(return_value=0), + 'docker.run_all': MagicMock( + return_value={'stdout': '.\n..\n', + 'stderr': '', + 'status': True, + 'comment': 'Success', + 'retcode': 0})} + + with provision_state(dockerio, salt_fixture): + result = dockerio.run('ls /', 'ubuntu') + + self.assertEqual(result, {'name': 'ls /', + 'result': True, + 'comment': 'Success', + 'changes': {}}) + + def test_docker_run_failure(self): + from salt.states import dockerio + salt_fixture = {'docker.retcode': MagicMock(return_value=0), + 'docker.run_all': MagicMock( + return_value={'stdout': '', + 'stderr': 'Error', + 'status': False, + 'comment': 'Failure', + 'retcode': 1})} + + with provision_state(dockerio, salt_fixture): + result = dockerio.run('ls /', 'ubuntu') + + self.assertEqual(result, {'name': 'ls /', + 'result': False, + 'comment': 'Failure', + 'changes': {}}) + + def test_docker_run_onlyif(self): + from salt.states import dockerio + salt_fixture = {'docker.retcode': MagicMock(return_value=1), + 'docker.run_all': None} + with provision_state(dockerio, salt_fixture): + result = dockerio.run('ls /', 'ubuntu', + onlyif='ls -l') + self.assertEqual(result, {'name': 'ls /', + 'result': True, + 'comment': 'onlyif execution failed', + 'changes': {}}) + + def test_docker_run_unless(self): + from salt.states import dockerio + salt_fixture = {'docker.retcode': MagicMock(return_value=0), + 'docker.run_all': None} + with provision_state(dockerio, salt_fixture): + result = dockerio.run('ls /', 'ubuntu', + unless='ls -l') + self.assertEqual(result, {'name': 'ls /', + 'result': True, + 'comment': 'unless execution succeeded', + 'changes': {}}) + + def test_docker_run_docked_onlyif(self): + from salt.states import dockerio + salt_fixture = {'docker.retcode': MagicMock(return_value=1), + 'docker.run_all': None} + with provision_state(dockerio, salt_fixture): + result = dockerio.run('ls /', 'ubuntu', + docked_onlyif='ls -l') + self.assertEqual(result, {'name': 'ls /', + 'result': True, + 'comment': 'docked_onlyif execution failed', + 'changes': {}}) + + def test_docker_run_docked_unless(self): + from salt.states import dockerio + salt_fixture = {'docker.retcode': MagicMock(return_value=0), + 'docker.run_all': None} + with provision_state(dockerio, salt_fixture): + result = dockerio.run('ls /', 'ubuntu', + docked_unless='ls -l') + self.assertEqual(result, {'name': 'ls /', + 'result': True, + 'comment': ('docked_unless execution' + ' succeeded'), + 'changes': {}}) + + +if __name__ == '__main__': + from integration import run_tests + run_tests(DockerStateTestCase, needs_daemon=False) diff --git a/tests/unit/utils/network.py b/tests/unit/utils/network.py index 89db848..72ca857 100644 --- a/tests/unit/utils/network.py +++ b/tests/unit/utils/network.py @@ -151,15 +151,16 @@ class NetworkTestCase(TestCase): 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}} ) diff --git a/tests/unit/utils/utils_test.py b/tests/unit/utils/utils_test.py index 261af69..11f0baf 100644 --- a/tests/unit/utils/utils_test.py +++ b/tests/unit/utils/utils_test.py @@ -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): -- 2.8.2