2013-07-20 14:32:50 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
2013-09-16 16:24:00 +00:00
|
|
|
:codeauthor: :email:`Pedro Algarvio (pedro@algarvio.me)`
|
|
|
|
:copyright: © 2013 by the SaltStack Team, see AUTHORS for more details
|
|
|
|
:license: Apache 2.0, see LICENSE for more details.
|
|
|
|
|
|
|
|
|
2013-07-20 14:32:50 +00:00
|
|
|
tests.unit.utils.warnings_test
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2013-09-10 23:34:09 +00:00
|
|
|
Test ``salt.utils.warn_until`` and ``salt.utils.kwargs_warn_until``
|
2013-07-20 14:32:50 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
# Import python libs
|
|
|
|
import warnings
|
|
|
|
|
|
|
|
# Import Salt Testing libs
|
|
|
|
from salttesting import skipIf, TestCase
|
|
|
|
from salttesting.helpers import ensure_in_syspath
|
2013-08-26 10:06:02 +00:00
|
|
|
from salttesting.mock import NO_MOCK, NO_MOCK_REASON, patch
|
2013-07-20 14:32:50 +00:00
|
|
|
ensure_in_syspath('../../')
|
|
|
|
|
|
|
|
# Import salt libs
|
2013-09-10 23:34:09 +00:00
|
|
|
from salt.utils import warn_until, kwargs_warn_until
|
2013-10-03 17:55:01 +00:00
|
|
|
from salt.version import SaltStackVersion
|
2013-07-20 14:32:50 +00:00
|
|
|
|
2013-08-26 10:06:02 +00:00
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
2013-07-20 14:32:50 +00:00
|
|
|
class WarnUntilTestCase(TestCase):
|
|
|
|
|
|
|
|
@patch('salt.version')
|
2013-09-10 23:34:09 +00:00
|
|
|
def test_warn_until_warning_raised(self, salt_version_mock):
|
2013-07-20 14:32:50 +00:00
|
|
|
# We *always* want *all* warnings thrown on this module
|
|
|
|
warnings.filterwarnings('always', '', DeprecationWarning, __name__)
|
|
|
|
|
|
|
|
# Define a salt version info
|
|
|
|
salt_version_mock.__version_info__ = (0, 16)
|
2013-10-03 17:55:01 +00:00
|
|
|
# Let SaltStackVersion be the original one, not a MagicMock'ed one
|
|
|
|
salt_version_mock.SaltStackVersion = SaltStackVersion
|
2013-07-20 14:32:50 +00:00
|
|
|
|
|
|
|
def raise_warning():
|
|
|
|
warn_until(
|
|
|
|
(0, 17), 'Deprecation Message!'
|
|
|
|
)
|
|
|
|
|
|
|
|
# raise_warning should show warning until version info is >= (0, 17)
|
|
|
|
with warnings.catch_warnings(record=True) as recorded_warnings:
|
|
|
|
raise_warning()
|
|
|
|
self.assertEqual(
|
|
|
|
'Deprecation Message!', str(recorded_warnings[0].message)
|
|
|
|
)
|
|
|
|
|
2013-07-20 15:26:47 +00:00
|
|
|
# the deprecation warning is not issued because we passed
|
|
|
|
# _dont_call_warning
|
|
|
|
with warnings.catch_warnings(record=True) as recorded_warnings:
|
|
|
|
warn_until(
|
|
|
|
(0, 17), 'Foo', _dont_call_warnings=True
|
|
|
|
)
|
|
|
|
self.assertEqual(0, len(recorded_warnings))
|
|
|
|
|
2013-07-20 14:32:50 +00:00
|
|
|
# Let's set version info to (0, 17), a RuntimeError should be raised
|
|
|
|
salt_version_mock.__version_info__ = (0, 17)
|
|
|
|
with self.assertRaisesRegexp(
|
|
|
|
RuntimeError,
|
|
|
|
r'The warning triggered on filename \'(.*)warnings_test.py\', '
|
2013-08-16 19:26:48 +00:00
|
|
|
r'line number ([\d]+), is supposed to be shown until version '
|
2013-10-03 18:02:21 +00:00
|
|
|
r'\'0.17.0\' is released. Current version is now \'0.17.0\'. '
|
|
|
|
r'Please remove the warning.'):
|
2013-07-20 14:32:50 +00:00
|
|
|
raise_warning()
|
|
|
|
|
2013-07-20 15:26:47 +00:00
|
|
|
# Even though we're calling warn_until, we pass _dont_call_warnings
|
|
|
|
# because we're only after the RuntimeError
|
|
|
|
with self.assertRaisesRegexp(
|
|
|
|
RuntimeError,
|
|
|
|
r'The warning triggered on filename \'(.*)warnings_test.py\', '
|
2013-08-16 19:26:48 +00:00
|
|
|
r'line number ([\d]+), is supposed to be shown until version '
|
2013-10-03 18:02:21 +00:00
|
|
|
r'\'0.17.0\' is released. Current version is now \'0.17.0\'. '
|
|
|
|
r'Please remove the warning.'):
|
2013-07-20 15:26:47 +00:00
|
|
|
warn_until(
|
|
|
|
(0, 17), 'Foo', _dont_call_warnings=True
|
|
|
|
)
|
|
|
|
|
2013-09-10 23:34:09 +00:00
|
|
|
@patch('salt.version')
|
|
|
|
def test_kwargs_warn_until_warning_raised(self, salt_version_mock):
|
|
|
|
# We *always* want *all* warnings thrown on this module
|
|
|
|
warnings.filterwarnings('always', '', DeprecationWarning, __name__)
|
|
|
|
|
|
|
|
# Define a salt version info
|
|
|
|
salt_version_mock.__version_info__ = (0, 16)
|
|
|
|
|
|
|
|
def raise_warning(**kwargs):
|
|
|
|
kwargs_warn_until(
|
|
|
|
kwargs,
|
|
|
|
(0, 17),
|
|
|
|
)
|
|
|
|
|
|
|
|
# raise_warning({...}) should show warning until version info is >= (0, 17)
|
|
|
|
with warnings.catch_warnings(record=True) as recorded_warnings:
|
|
|
|
raise_warning(foo=42) # with a kwarg
|
|
|
|
self.assertEqual(
|
|
|
|
'The following parameter(s) have been deprecated and '
|
|
|
|
'will be removed in 0.17: \'foo\'.',
|
|
|
|
str(recorded_warnings[0].message)
|
|
|
|
)
|
|
|
|
# With no **kwargs, should not show warning until version info is >= (0, 17)
|
|
|
|
with warnings.catch_warnings(record=True) as recorded_warnings:
|
|
|
|
kwargs_warn_until(
|
|
|
|
{}, # no kwargs
|
|
|
|
(0, 17),
|
|
|
|
)
|
|
|
|
self.assertEqual(0, len(recorded_warnings))
|
|
|
|
|
|
|
|
# Let's set version info to (0, 17), a RuntimeError should be raised
|
|
|
|
# regardless of whether or not we pass any **kwargs.
|
|
|
|
salt_version_mock.__version_info__ = (0, 17)
|
|
|
|
with self.assertRaisesRegexp(
|
|
|
|
RuntimeError,
|
|
|
|
r'The warning triggered on filename \'(.*)warnings_test.py\', '
|
|
|
|
r'line number ([\d]+), is supposed to be shown until version '
|
|
|
|
r'\'0.17\' is released. Current version is now \'0.17\'. Please '
|
|
|
|
r'remove the warning.'):
|
|
|
|
raise_warning() # no kwargs
|
|
|
|
with self.assertRaisesRegexp(
|
|
|
|
RuntimeError,
|
|
|
|
r'The warning triggered on filename \'(.*)warnings_test.py\', '
|
|
|
|
r'line number ([\d]+), is supposed to be shown until version '
|
|
|
|
r'\'0.17\' is released. Current version is now \'0.17\'. Please '
|
|
|
|
r'remove the warning.'):
|
|
|
|
raise_warning(bar='baz', qux='quux') # some kwargs
|
|
|
|
|
2013-07-20 14:32:50 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
from integration import run_tests
|
|
|
|
run_tests(WarnUntilTestCase, needs_daemon=False)
|