Sign Up
Log In
Log In
or
Sign Up
Places
All Projects
Status Monitor
Collapse sidebar
SUSE:SLE-12-SP5:Update
salt.4663
0016-Unit-test-fixes-for-2015.8.7.patch
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File 0016-Unit-test-fixes-for-2015.8.7.patch of Package salt.4663
From ca8630eaa1bedb6981eec61c5e2079d3bcc31574 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Su=C3=A1rez=20Hern=C3=A1ndez?= <psuarezhernandez@suse.com> Date: Tue, 6 Sep 2016 11:21:05 +0100 Subject: [PATCH 16/38] Unit test fixes for 2015.8.7 * Fixing skipped boto tests to prevent errors if boto3 does not exists. * Skip utils_test if timelib is not installed (#32699) date_cast() throws a RuntimeError, not an ImportError * 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. * Fix missing first data in stream when subscribing stream using a function 'read_async'. * Make sure spm tests are picked up by runtests. Lists in py2 don't have the clear method * 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. * Fixed LoadAuthTestCase Fixed use of assert_has_calls in tests. The method logic was changed in mock-1.1.0. This mades the use of the method compatible with both <1.1.0 and >=1.1.0 * Fixed tests Fixed more lint * 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. * Remove test for file dir behavior Refs #34809 * modules.darwin_sysctl: __virtual__ return err msg. Updated message in darwin_sysctl module when return False if OS is not OSX. * rename darwin_sysctl.py to mac_sysctl.py * Fixed _interfaces_ifconfig output for SunOS test * Prevent tests failure if /etc/fstab does not exists * Prevent tests failures if boto does not exists * SPM: packaging doesn't work in Python 2.6. Fixed. --- salt/modules/boto_elb.py | 2 +- salt/modules/{darwin_sysctl.py => mac_sysctl.py} | 5 +- tests/unit/auth_test.py | 4 +- tests/unit/modules/boto_secgroup_test.py | 1 + tests/unit/modules/boto_vpc_test.py | 14 +- tests/unit/modules/cron_test.py | 2 +- tests/unit/modules/linux_sysctl_test.py | 19 +- .../{darwin_sysctl_test.py => mac_sysctl_test.py} | 32 ++-- tests/unit/modules/mount_test.py | 21 +-- tests/unit/modules/mysql_test.py | 6 +- tests/unit/modules/puppet_test.py | 15 +- tests/unit/netapi/rest_tornado/test_utils.py | 27 ++- tests/unit/{spm.py => spm_test.py} | 6 +- .../unit/states/{dockerio.py => dockerio_test.py} | 1 + tests/unit/states/file_test.py | 22 +-- tests/unit/utils/network.py | 196 +++++++++++++++++++++ tests/unit/utils/utils_test.py | 11 +- 17 files changed, 294 insertions(+), 90 deletions(-) rename salt/modules/{darwin_sysctl.py => mac_sysctl.py} (96%) rename tests/unit/modules/{darwin_sysctl_test.py => mac_sysctl_test.py} (74%) rename tests/unit/{spm.py => spm_test.py} (98%) rename tests/unit/states/{dockerio.py => dockerio_test.py} (98%) create mode 100644 tests/unit/utils/network.py diff --git a/salt/modules/boto_elb.py b/salt/modules/boto_elb.py index 33e7396..d300f23 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/darwin_sysctl.py b/salt/modules/mac_sysctl.py similarity index 96% rename from salt/modules/darwin_sysctl.py rename to salt/modules/mac_sysctl.py index dce868a..fe91666 100644 --- a/salt/modules/darwin_sysctl.py +++ b/salt/modules/mac_sysctl.py @@ -19,7 +19,10 @@ def __virtual__(): ''' Only run on Darwin (OS X) systems ''' - return __virtualname__ if __grains__['os'] == 'MacOS' else False + if __grains__['os'] == 'MacOS': + return __virtualname__ + return (False, 'The darwin_sysctl execution module cannot be loaded: ' + 'only available on MacOS systems.') def show(config_file=False): diff --git a/tests/unit/auth_test.py b/tests/unit/auth_test.py index b6bddb2..d68cb25 100644 --- a/tests/unit/auth_test.py +++ b/tests/unit/auth_test.py @@ -48,7 +48,7 @@ class LoadAuthTestCase(TestCase): 'eauth': 'pam' }, expected_extra_kws=auth.AUTH_INTERNAL_KEYWORDS) ret = self.lauth.load_name(valid_eauth_load) - format_call_mock.assert_has_calls(expected_ret) + format_call_mock.assert_has_calls((expected_ret,), any_order=True) def test_get_groups(self): valid_eauth_load = {'username': 'test_user', @@ -63,7 +63,7 @@ class LoadAuthTestCase(TestCase): 'eauth': 'pam' }, expected_extra_kws=auth.AUTH_INTERNAL_KEYWORDS) self.lauth.get_groups(valid_eauth_load) - format_call_mock.assert_has_calls(expected_ret) + format_call_mock.assert_has_calls((expected_ret,), any_order=True) @patch('zmq.Context', MagicMock()) diff --git a/tests/unit/modules/boto_secgroup_test.py b/tests/unit/modules/boto_secgroup_test.py index f1c6bb1..5099935 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 e7e09fa..cbf36a8 100644 --- a/tests/unit/modules/boto_vpc_test.py +++ b/tests/unit/modules/boto_vpc_test.py @@ -118,6 +118,13 @@ def _has_required_moto(): return True +@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__ = {} @@ -230,13 +237,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/cron_test.py b/tests/unit/modules/cron_test.py index 1231487..89c3e82 100644 --- a/tests/unit/modules/cron_test.py +++ b/tests/unit/modules/cron_test.py @@ -577,7 +577,7 @@ class PsTestCase(TestCase): '# Lines below here are managed by Salt, do not edit\n', '@hourly echo Hi!\n']) ret = cron.set_special('DUMMY_USER', '@hourly', 'echo Hi!') - write_cron_lines_mock.assert_has_calls(expected_write_call) + write_cron_lines_mock.assert_has_calls((expected_write_call,), any_order=True) def test__get_cron_date_time(self): ret = cron._get_cron_date_time(minute=STUB_CRON_TIMESTAMP['minute'], diff --git a/tests/unit/modules/linux_sysctl_test.py b/tests/unit/modules/linux_sysctl_test.py index aeef75b..eb8b7e2 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)) def test_persist_no_conf_success(self): diff --git a/tests/unit/modules/darwin_sysctl_test.py b/tests/unit/modules/mac_sysctl_test.py similarity index 74% rename from tests/unit/modules/darwin_sysctl_test.py rename to tests/unit/modules/mac_sysctl_test.py index 9b8e9ff..533397b 100644 --- a/tests/unit/modules/darwin_sysctl_test.py +++ b/tests/unit/modules/mac_sysctl_test.py @@ -7,7 +7,7 @@ from __future__ import absolute_import # Import Salt Libs -from salt.modules import darwin_sysctl +from salt.modules import mac_sysctl from salt.exceptions import CommandExecutionError # Import Salt Testing Libs @@ -25,13 +25,13 @@ from salttesting.mock import ( ensure_in_syspath('../../') # Globals -darwin_sysctl.__salt__ = {} +mac_sysctl.__salt__ = {} @skipIf(NO_MOCK, NO_MOCK_REASON) class DarwinSysctlTestCase(TestCase): ''' - TestCase for salt.modules.darwin_sysctl module + TestCase for salt.modules.mac_sysctl module ''' def test_get(self): @@ -39,8 +39,8 @@ class DarwinSysctlTestCase(TestCase): Tests the return of get function ''' mock_cmd = MagicMock(return_value='foo') - with patch.dict(darwin_sysctl.__salt__, {'cmd.run': mock_cmd}): - self.assertEqual(darwin_sysctl.get('kern.ostype'), 'foo') + with patch.dict(mac_sysctl.__salt__, {'cmd.run': mock_cmd}): + self.assertEqual(mac_sysctl.get('kern.ostype'), 'foo') def test_assign_cmd_failed(self): ''' @@ -49,9 +49,9 @@ class DarwinSysctlTestCase(TestCase): cmd = {'pid': 3548, 'retcode': 1, 'stderr': '', 'stdout': 'net.inet.icmp.icmplim: 250 -> 50'} mock_cmd = MagicMock(return_value=cmd) - with patch.dict(darwin_sysctl.__salt__, {'cmd.run_all': mock_cmd}): + with patch.dict(mac_sysctl.__salt__, {'cmd.run_all': mock_cmd}): self.assertRaises(CommandExecutionError, - darwin_sysctl.assign, + mac_sysctl.assign, 'net.inet.icmp.icmplim', 50) def test_assign(self): @@ -62,8 +62,8 @@ class DarwinSysctlTestCase(TestCase): 'stdout': 'net.inet.icmp.icmplim: 250 -> 50'} ret = {'net.inet.icmp.icmplim': '50'} mock_cmd = MagicMock(return_value=cmd) - with patch.dict(darwin_sysctl.__salt__, {'cmd.run_all': mock_cmd}): - self.assertEqual(darwin_sysctl.assign( + with patch.dict(mac_sysctl.__salt__, {'cmd.run_all': mock_cmd}): + self.assertEqual(mac_sysctl.assign( 'net.inet.icmp.icmplim', 50), ret) @patch('os.path.isfile', MagicMock(return_value=False)) @@ -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, - darwin_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): @@ -84,7 +84,7 @@ class DarwinSysctlTestCase(TestCase): Tests successful add of config file when previously not one ''' with patch('salt.utils.fopen', mock_open()) as m_open: - darwin_sysctl.persist('net.inet.icmp.icmplim', 50) + mac_sysctl.persist('net.inet.icmp.icmplim', 50) helper_open = m_open() helper_open.write.assert_called_once_with( '#\n# Kernel sysctl configuration\n#\n') @@ -97,7 +97,7 @@ class DarwinSysctlTestCase(TestCase): to_write = '#\n# Kernel sysctl configuration\n#\n' m_calls_list = [call.writelines(['net.inet.icmp.icmplim=50', '\n'])] with patch('salt.utils.fopen', mock_open(read_data=to_write)) as m_open: - darwin_sysctl.persist('net.inet.icmp.icmplim', 50, config=to_write) + mac_sysctl.persist('net.inet.icmp.icmplim', 50, config=to_write) helper_open = m_open() calls_list = helper_open.method_calls self.assertEqual(calls_list, m_calls_list) diff --git a/tests/unit/modules/mount_test.py b/tests/unit/modules/mount_test.py index 282539d..b2b23bf 100644 --- a/tests/unit/modules/mount_test.py +++ b/tests/unit/modules/mount_test.py @@ -100,15 +100,16 @@ class MountTestCase(TestCase): ''' mock = MagicMock(return_value={}) with patch.object(mount, 'fstab', mock): - self.assertTrue(mount.rm_fstab('name', 'device')) + with patch('salt.utils.fopen', mock_open()) as m_open: + self.assertTrue(mount.rm_fstab('name', 'device')) - mock = MagicMock(return_value={'name': 'name'}) - with patch.object(mount, 'fstab', mock): + mock_fstab = MagicMock(return_value={'name': 'name'}) + 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): ''' @@ -144,11 +145,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/mysql_test.py b/tests/unit/modules/mysql_test.py index 8cd3eca..85b71ba 100644 --- a/tests/unit/modules/mysql_test.py +++ b/tests/unit/modules/mysql_test.py @@ -293,10 +293,10 @@ class MySQLTestCase(TestCase): with patch.dict(mysql.__salt__, {'config.option': MagicMock()}): function(*args, **kwargs) if isinstance(expected_sql, dict): - calls = (call().cursor().execute('{0}'.format(expected_sql['sql']), expected_sql['sql_args'])) + calls = call().cursor().execute('{0}'.format(expected_sql['sql']), expected_sql['sql_args']) else: - calls = (call().cursor().execute('{0}'.format(expected_sql))) - connect_mock.assert_has_calls(calls) + calls = call().cursor().execute('{0}'.format(expected_sql)) + connect_mock.assert_has_calls((calls,), True) if __name__ == '__main__': diff --git a/tests/unit/modules/puppet_test.py b/tests/unit/modules/puppet_test.py index 6a43fd4..dcc488a 100644 --- a/tests/unit/modules/puppet_test.py +++ b/tests/unit/modules/puppet_test.py @@ -91,10 +91,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): ''' @@ -155,9 +157,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/netapi/rest_tornado/test_utils.py b/tests/unit/netapi/rest_tornado/test_utils.py index 1dd18d5..c22c288 100644 --- a/tests/unit/netapi/rest_tornado/test_utils.py +++ b/tests/unit/netapi/rest_tornado/test_utils.py @@ -103,7 +103,8 @@ class TestEventListener(AsyncTestCase): event_listener = saltnado.EventListener({}, # we don't use mod_opts, don't save? {'sock_dir': SOCK_DIR, 'transport': 'zeromq'}) - event_future = event_listener.get_event(1, 'evt1', self.stop) # get an event future + self._finished = False # fit to event_listener's behavior + event_future = event_listener.get_event(self, 'evt1', self.stop) # get an event future me.fire_event({'data': 'foo2'}, 'evt2') # fire an event we don't want me.fire_event({'data': 'foo1'}, 'evt1') # fire an event we do want self.wait() # wait for the future @@ -113,6 +114,27 @@ class TestEventListener(AsyncTestCase): self.assertEqual(event_future.result()['tag'], 'evt1') self.assertEqual(event_future.result()['data']['data'], 'foo1') + def test_set_event_handler(self): + ''' + Test subscribing events using set_event_handler + ''' + with eventpublisher_process(): + me = event.MasterEvent(SOCK_DIR) + event_listener = saltnado.EventListener({}, # we don't use mod_opts, don't save? + {'sock_dir': SOCK_DIR, + 'transport': 'zeromq'}) + self._finished = False # fit to event_listener's behavior + event_future = event_listener.get_event(self, + tag='evt', + callback=self.stop, + timeout=1, + ) # get an event future + me.fire_event({'data': 'foo'}, 'evt') # fire an event we do want + self.wait() + + # check that we subscribed the event we wanted + self.assertEqual(len(event_listener.timeout_map), 0) + def test_timeout(self): ''' Make sure timeouts work correctly @@ -121,7 +143,8 @@ class TestEventListener(AsyncTestCase): event_listener = saltnado.EventListener({}, # we don't use mod_opts, don't save? {'sock_dir': SOCK_DIR, 'transport': 'zeromq'}) - event_future = event_listener.get_event(1, + self._finished = False # fit to event_listener's behavior + event_future = event_listener.get_event(self, tag='evt1', callback=self.stop, timeout=1, diff --git a/tests/unit/spm.py b/tests/unit/spm_test.py similarity index 98% rename from tests/unit/spm.py rename to tests/unit/spm_test.py index d2c0bf7..1adc377 100644 --- a/tests/unit/spm.py +++ b/tests/unit/spm_test.py @@ -129,12 +129,12 @@ class SPMTest(TestCase): ('summary', 'Summary: {0}')): assert line.format(_F1['definition'][key]) in lines # Reinstall with force=False, should fail - self.ui._error.clear() + self.ui._error = [] self.client.run(['local', 'install', pkgpath]) assert len(self.ui._error) > 0 # Reinstall with force=True, should succeed __opts__['force'] = True - self.ui._error.clear() + self.ui._error = [] self.client.run(['local', 'install', pkgpath]) assert len(self.ui._error) == 0 __opts__['force'] = False @@ -167,7 +167,7 @@ class SPMTest(TestCase): ) for args in fail_args: - self.ui._error.clear() + self.ui._error = [] self.client.run(args) assert len(self.ui._error) > 0 diff --git a/tests/unit/states/dockerio.py b/tests/unit/states/dockerio_test.py similarity index 98% rename from tests/unit/states/dockerio.py rename to tests/unit/states/dockerio_test.py index c73b633..54f51be 100644 --- a/tests/unit/states/dockerio.py +++ b/tests/unit/states/dockerio_test.py @@ -20,6 +20,7 @@ def provision_state(module, fixture): @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 diff --git a/tests/unit/states/file_test.py b/tests/unit/states/file_test.py index 2168a6b..605d1e0 100644 --- a/tests/unit/states/file_test.py +++ b/tests/unit/states/file_test.py @@ -925,6 +925,7 @@ class FileTestCase(TestCase): # 'comment' function tests: 1 + @patch.object(os.path, 'exists', MagicMock(return_value=True)) def test_comment(self): ''' Test to comment out specified lines in a file. @@ -981,6 +982,7 @@ class FileTestCase(TestCase): # 'uncomment' function tests: 1 + @patch.object(os.path, 'exists', MagicMock(return_value=True)) def test_uncomment(self): ''' Test to uncomment specified commented lines in a file @@ -1318,26 +1320,6 @@ class FileTestCase(TestCase): (name, source, preserve=True), ret) - with patch.object(os.path, 'isdir', mock_t): - with patch.dict(filestate.__opts__, {'test': False}): - with patch.object(shutil, 'copy', - MagicMock(side_effect=[IOError, - True])): - comt = ('Failed to copy "{0}" to "{1}"' - .format(source, name)) - ret.update({'comment': comt, 'result': False}) - self.assertDictEqual(filestate.copy - (name, source, - preserve=True), ret) - - comt = ('Copied "{0}" to "{1}"'.format(source, - name)) - ret.update({'comment': comt, 'result': True, - 'changes': {name: source}}) - self.assertDictEqual(filestate.copy - (name, source, - preserve=True), ret) - # 'rename' function tests: 1 def test_rename(self): diff --git a/tests/unit/utils/network.py b/tests/unit/utils/network.py new file mode 100644 index 0000000..63ec554 --- /dev/null +++ b/tests/unit/utils/network.py @@ -0,0 +1,196 @@ +# -*- coding: utf-8 -*- +# Import Python libs +from __future__ import absolute_import + +# Import Salt Testing libs +from salttesting import skipIf +from salttesting import TestCase +from salttesting.helpers import ensure_in_syspath +from salttesting.mock import NO_MOCK, NO_MOCK_REASON, patch +ensure_in_syspath('../../') + +# Import salt libs +from salt.utils import network + +LINUX = '''\ +eth0 Link encap:Ethernet HWaddr e0:3f:49:85:6a:af + inet addr:10.10.10.56 Bcast:10.10.10.255 Mask:255.255.252.0 + inet6 addr: fe80::e23f:49ff:fe85:6aaf/64 Scope:Link + UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 + RX packets:643363 errors:0 dropped:0 overruns:0 frame:0 + TX packets:196539 errors:0 dropped:0 overruns:0 carrier:0 + collisions:0 txqueuelen:1000 + RX bytes:386388355 (368.4 MiB) TX bytes:25600939 (24.4 MiB) + +lo Link encap:Local Loopback + inet addr:127.0.0.1 Mask:255.0.0.0 + inet6 addr: ::1/128 Scope:Host + UP LOOPBACK RUNNING MTU:65536 Metric:1 + RX packets:548901 errors:0 dropped:0 overruns:0 frame:0 + TX packets:548901 errors:0 dropped:0 overruns:0 carrier:0 + collisions:0 txqueuelen:0 + RX bytes:613479895 (585.0 MiB) TX bytes:613479895 (585.0 MiB) +''' + +FREEBSD = ''' +em0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500 + options=4219b<RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING,VLAN_HWCSUM,TSO4,WOL_MAGIC,VLAN_HWTSO> + ether 00:30:48:ff:ff:ff + inet 10.10.10.250 netmask 0xffffffe0 broadcast 10.10.10.255 + inet 10.10.10.56 netmask 0xffffffc0 broadcast 10.10.10.63 + media: Ethernet autoselect (1000baseT <full-duplex>) + status: active +em1: flags=8c02<BROADCAST,OACTIVE,SIMPLEX,MULTICAST> metric 0 mtu 1500 + options=4219b<RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING,VLAN_HWCSUM,TSO4,WOL_MAGIC,VLAN_HWTSO> + ether 00:30:48:aa:aa:aa + media: Ethernet autoselect + status: no carrier +plip0: flags=8810<POINTOPOINT,SIMPLEX,MULTICAST> metric 0 mtu 1500 +lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> metric 0 mtu 16384 + options=3<RXCSUM,TXCSUM> + inet6 fe80::1%lo0 prefixlen 64 scopeid 0x8 + inet6 ::1 prefixlen 128 + inet 127.0.0.1 netmask 0xff000000 + nd6 options=3<PERFORMNUD,ACCEPT_RTADV> +tun0: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> metric 0 mtu 1500 + options=80000<LINKSTATE> + inet 10.12.0.1 --> 10.12.0.2 netmask 0xffffffff + Opened by PID 1964 +''' + +SOLARIS = '''\ +lo0: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1 + inet 127.0.0.1 netmask ff000000 +net0: flags=100001100943<UP,BROADCAST,RUNNING,PROMISC,MULTICAST,ROUTER,IPv4,PHYSRUNNING> mtu 1500 index 2 + inet 10.10.10.38 netmask ffffffe0 broadcast 10.10.10.63 +ilbint0: flags=110001100843<UP,BROADCAST,RUNNING,MULTICAST,ROUTER,IPv4,VRRP,PHYSRUNNING> mtu 1500 index 3 + inet 10.6.0.11 netmask ffffff00 broadcast 10.6.0.255 +ilbext0: flags=110001100843<UP,BROADCAST,RUNNING,MULTICAST,ROUTER,IPv4,VRRP,PHYSRUNNING> mtu 1500 index 4 + inet 10.10.11.11 netmask ffffffe0 broadcast 10.10.11.31 +ilbext0:1: flags=110001100843<UP,BROADCAST,RUNNING,MULTICAST,ROUTER,IPv4,VRRP,PHYSRUNNING> mtu 1500 index 4 + inet 10.10.11.12 netmask ffffffe0 broadcast 10.10.11.31 +vpn0: flags=1000011008d1<UP,POINTOPOINT,RUNNING,NOARP,MULTICAST,ROUTER,IPv4,PHYSRUNNING> mtu 1480 index 5 + inet tunnel src 10.10.11.12 tunnel dst 10.10.5.5 + tunnel hop limit 64 + inet 10.6.0.14 --> 10.6.0.15 netmask ff000000 +lo0: flags=2002000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv6,VIRTUAL> mtu 8252 index 1 + inet6 ::1/128 +net0: flags=120002004941<UP,RUNNING,PROMISC,MULTICAST,DHCP,IPv6,PHYSRUNNING> mtu 1500 index 2 + inet6 fe80::221:9bff:fefd:2a22/10 +ilbint0: flags=120002000840<RUNNING,MULTICAST,IPv6,PHYSRUNNING> mtu 1500 index 3 + inet6 ::/0 +ilbext0: flags=120002000840<RUNNING,MULTICAST,IPv6,PHYSRUNNING> mtu 1500 index 4 + inet6 ::/0 +vpn0: flags=120002200850<POINTOPOINT,RUNNING,MULTICAST,NONUD,IPv6,PHYSRUNNING> mtu 1480 index 5 + inet tunnel src 10.10.11.12 tunnel dst 10.10.5.5 + tunnel hop limit 64 + inet6 ::/0 --> fe80::b2d6:7c10 +''' + +FREEBSD_SOCKSTAT = '''\ +USER COMMAND PID FD PROTO LOCAL ADDRESS FOREIGN ADDRESS +root python2.7 1294 41 tcp4 127.0.0.1:61115 127.0.0.1:4506 +''' + + +@skipIf(NO_MOCK, NO_MOCK_REASON) +class NetworkTestCase(TestCase): + + def test_interfaces_ifconfig_linux(self): + interfaces = network._interfaces_ifconfig(LINUX) + self.assertEqual(interfaces, + {'eth0': {'hwaddr': 'e0:3f:49:85:6a:af', + 'inet': [{'address': '10.10.10.56', + 'broadcast': '10.10.10.255', + 'netmask': '255.255.252.0'}], + 'inet6': [{'address': 'fe80::e23f:49ff:fe85:6aaf', + 'prefixlen': '64', + 'scope': 'link'}], + 'up': True}, + 'lo': {'inet': [{'address': '127.0.0.1', + 'netmask': '255.0.0.0'}], + 'inet6': [{'address': '::1', + 'prefixlen': '128', + 'scope': 'host'}], + 'up': True}} + ) + + def test_interfaces_ifconfig_freebsd(self): + interfaces = network._interfaces_ifconfig(FREEBSD) + self.assertEqual(interfaces, + {'': {'up': False}, + 'em0': {'hwaddr': '00:30:48:ff:ff:ff', + 'inet': [{'address': '10.10.10.250', + 'broadcast': '10.10.10.255', + 'netmask': '255.255.255.224'}, + {'address': '10.10.10.56', + 'broadcast': '10.10.10.63', + 'netmask': '255.255.255.192'}], + 'up': True}, + 'em1': {'hwaddr': '00:30:48:aa:aa:aa', + 'up': False}, + 'lo0': {'inet': [{'address': '127.0.0.1', + 'netmask': '255.0.0.0'}], + 'inet6': [{'address': 'fe80::1', + 'prefixlen': '64', + 'scope': '0x8'}, + {'address': '::1', + 'prefixlen': '128', + 'scope': None}], + 'up': True}, + 'plip0': {'up': False}, + 'tun0': {'inet': [{'address': '10.12.0.1', + 'netmask': '255.255.255.255'}], + 'up': True}} + + ) + + def test_interfaces_ifconfig_solaris(self): + with patch('salt.utils.is_sunos', lambda: True): + 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'}], + 'up': True}, + 'ilbint0': {'inet': [{'address': '10.6.0.11', + 'broadcast': '10.6.0.255', + 'netmask': '255.255.255.0'}], + 'inet6': [{'address': '::', + 'prefixlen': '0'}], + 'up': True}, + 'lo0': {'inet': [{'address': '127.0.0.1', + 'netmask': '255.0.0.0'}], + 'inet6': [{'address': '::1', + 'prefixlen': '128'}], + 'up': True}, + 'net0': {'inet': [{'address': '10.10.10.38', + 'broadcast': '10.10.10.63', + 'netmask': '255.255.255.224'}], + 'inet6': [{'address': 'fe80::221:9bff:fefd:2a22', + 'prefixlen': '10'}], + 'up': True}, + 'vpn0': {'inet': [{'address': '10.6.0.14', + 'netmask': '255.0.0.0'}], + 'inet6': [{'address': '::', + 'prefixlen': '0'}], + 'up': True}} + ) + + def test_freebsd_remotes_on(self): + with patch('salt.utils.is_sunos', lambda: False): + with patch('salt.utils.is_freebsd', lambda: True): + with patch('subprocess.check_output', + return_value=FREEBSD_SOCKSTAT): + remotes = network._freebsd_remotes_on('4506', 'remote') + self.assertEqual(remotes, set(['127.0.0.1'])) + + +if __name__ == '__main__': + from integration import run_tests + run_tests(NetworkTestCase, needs_daemon=False) 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.10.2
Locations
Projects
Search
Status Monitor
Help
OpenBuildService.org
Documentation
API Documentation
Code of Conduct
Contact
Support
@OBShq
Terms
openSUSE Build Service is sponsored by
The Open Build Service is an
openSUSE project
.
Sign Up
Log In
Places
Places
All Projects
Status Monitor