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)`
|
|
|
|
|
|
|
|
|
2017-02-18 13:44:11 +00:00
|
|
|
tests.unit.utils.test_warnings
|
2013-07-20 14:32:50 +00:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
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
|
2014-11-21 19:05:13 +00:00
|
|
|
from __future__ import absolute_import
|
2013-10-04 08:05:00 +00:00
|
|
|
import sys
|
2013-07-20 14:32:50 +00:00
|
|
|
import warnings
|
|
|
|
|
|
|
|
# Import Salt Testing libs
|
2013-10-04 08:05:00 +00:00
|
|
|
from salttesting import TestCase
|
2013-07-20 14:32:50 +00:00
|
|
|
from salttesting.helpers import ensure_in_syspath
|
|
|
|
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-04 08:24:42 +00:00
|
|
|
from salt.version import SaltStackVersion
|
2013-07-20 14:32:50 +00:00
|
|
|
|
2013-10-04 08:05:00 +00:00
|
|
|
|
2013-07-20 14:32:50 +00:00
|
|
|
class WarnUntilTestCase(TestCase):
|
|
|
|
|
2013-10-04 08:05:00 +00:00
|
|
|
def test_warn_until_warning_raised(self):
|
2013-07-20 14:32:50 +00:00
|
|
|
# We *always* want *all* warnings thrown on this module
|
|
|
|
warnings.filterwarnings('always', '', DeprecationWarning, __name__)
|
|
|
|
|
2013-10-04 08:05:00 +00:00
|
|
|
def raise_warning(_version_info_=(0, 16, 0)):
|
|
|
|
warn_until(
|
|
|
|
(0, 17), 'Deprecation Message!',
|
|
|
|
_version_info_=_version_info_
|
|
|
|
|
|
|
|
)
|
2013-07-20 14:32:50 +00:00
|
|
|
|
2013-10-04 08:05:00 +00:00
|
|
|
def raise_named_version_warning(_version_info_=(0, 16, 0)):
|
2013-07-20 14:32:50 +00:00
|
|
|
warn_until(
|
2013-10-04 08:05:00 +00:00
|
|
|
'Hydrogen', 'Deprecation Message!',
|
|
|
|
_version_info_=_version_info_
|
2013-07-20 14:32:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# 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-10-04 08:05:00 +00:00
|
|
|
# raise_warning should show warning until version info is >= (0, 17)
|
|
|
|
with warnings.catch_warnings(record=True) as recorded_warnings:
|
|
|
|
raise_named_version_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(
|
2013-10-04 08:05:00 +00:00
|
|
|
(0, 17), 'Foo', _dont_call_warnings=True,
|
|
|
|
_version_info_=(0, 16)
|
2013-07-20 15:26:47 +00:00
|
|
|
)
|
|
|
|
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
|
|
|
|
with self.assertRaisesRegexp(
|
|
|
|
RuntimeError,
|
2017-02-18 13:44:11 +00:00
|
|
|
r'The warning triggered on filename \'(.*)test_warnings.py\', '
|
2013-08-16 19:26:48 +00:00
|
|
|
r'line number ([\d]+), is supposed to be shown until version '
|
2014-04-24 10:32:46 +00:00
|
|
|
r'0.17.0 is released. Current version is now 0.17.0. '
|
2013-10-03 18:02:21 +00:00
|
|
|
r'Please remove the warning.'):
|
2013-10-04 08:05:00 +00:00
|
|
|
raise_warning(_version_info_=(0, 17, 0))
|
|
|
|
|
|
|
|
# Let's set version info to (0, 17), a RuntimeError should be raised
|
|
|
|
with self.assertRaisesRegexp(
|
|
|
|
RuntimeError,
|
2017-02-18 13:44:11 +00:00
|
|
|
r'The warning triggered on filename \'(.*)test_warnings.py\', '
|
2013-10-04 08:05:00 +00:00
|
|
|
r'line number ([\d]+), is supposed to be shown until version '
|
2014-04-24 10:32:46 +00:00
|
|
|
r'(.*) is released. Current version is now '
|
|
|
|
r'([\d.]+). Please remove the warning.'):
|
2016-08-02 15:21:48 +00:00
|
|
|
raise_named_version_warning(_version_info_=(getattr(sys, 'maxint', None) or getattr(sys, 'maxsize'), 16, 0))
|
2013-07-20 14:32:50 +00:00
|
|
|
|
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,
|
2017-02-18 13:44:11 +00:00
|
|
|
r'The warning triggered on filename \'(.*)test_warnings.py\', '
|
2013-08-16 19:26:48 +00:00
|
|
|
r'line number ([\d]+), is supposed to be shown until version '
|
2014-04-24 10:32:46 +00:00
|
|
|
r'0.17.0 is released. Current version is now '
|
|
|
|
r'(.*). Please remove the warning.'):
|
2013-07-20 15:26:47 +00:00
|
|
|
warn_until(
|
|
|
|
(0, 17), 'Foo', _dont_call_warnings=True
|
|
|
|
)
|
|
|
|
|
2013-10-04 08:05:00 +00:00
|
|
|
with self.assertRaisesRegexp(
|
|
|
|
RuntimeError,
|
2017-02-18 13:44:11 +00:00
|
|
|
r'The warning triggered on filename \'(.*)test_warnings.py\', '
|
2013-10-04 08:05:00 +00:00
|
|
|
r'line number ([\d]+), is supposed to be shown until version '
|
2014-04-24 10:32:46 +00:00
|
|
|
r'(.*) is released. Current version is now '
|
|
|
|
r'(.*). Please remove the warning.'):
|
2013-10-04 08:05:00 +00:00
|
|
|
warn_until(
|
|
|
|
'Hydrogen', 'Foo', _dont_call_warnings=True,
|
2016-08-02 15:21:48 +00:00
|
|
|
_version_info_=(getattr(sys, 'maxint', None) or getattr(sys, 'maxsize'), 16, 0)
|
2013-10-04 08:05:00 +00:00
|
|
|
)
|
|
|
|
|
2013-10-04 08:24:42 +00:00
|
|
|
# version on the deprecation message gets properly formatted
|
|
|
|
with warnings.catch_warnings(record=True) as recorded_warnings:
|
|
|
|
vrs = SaltStackVersion.from_name('Helium')
|
|
|
|
warn_until(
|
|
|
|
'Helium', 'Deprecation Message until {version}!',
|
|
|
|
_version_info_=(vrs.major - 1, 0)
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
'Deprecation Message until {0}!'.format(vrs.formatted_version),
|
|
|
|
str(recorded_warnings[0].message)
|
|
|
|
)
|
|
|
|
|
2013-10-04 08:05:00 +00:00
|
|
|
def test_kwargs_warn_until_warning_raised(self):
|
2013-09-10 23:34:09 +00:00
|
|
|
# We *always* want *all* warnings thrown on this module
|
|
|
|
warnings.filterwarnings('always', '', DeprecationWarning, __name__)
|
|
|
|
|
|
|
|
def raise_warning(**kwargs):
|
2013-10-04 08:05:00 +00:00
|
|
|
_version_info_ = kwargs.pop('_version_info_', (0, 16, 0))
|
2013-09-10 23:34:09 +00:00
|
|
|
kwargs_warn_until(
|
|
|
|
kwargs,
|
|
|
|
(0, 17),
|
2013-10-04 08:05:00 +00:00
|
|
|
_version_info_=_version_info_
|
2013-09-10 23:34:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# raise_warning({...}) should show warning until version info is >= (0, 17)
|
|
|
|
with warnings.catch_warnings(record=True) as recorded_warnings:
|
2013-10-03 18:21:14 +00:00
|
|
|
raise_warning(foo=42) # with a kwarg
|
2013-09-10 23:34:09 +00:00
|
|
|
self.assertEqual(
|
|
|
|
'The following parameter(s) have been deprecated and '
|
2013-10-03 18:21:14 +00:00
|
|
|
'will be removed in \'0.17.0\': \'foo\'.',
|
2013-09-10 23:34:09 +00:00
|
|
|
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),
|
2013-10-04 08:05:00 +00:00
|
|
|
_version_info_=(0, 16, 0)
|
2013-09-10 23:34:09 +00:00
|
|
|
)
|
|
|
|
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.
|
|
|
|
with self.assertRaisesRegexp(
|
|
|
|
RuntimeError,
|
2017-02-18 13:44:11 +00:00
|
|
|
r'The warning triggered on filename \'(.*)test_warnings.py\', '
|
2013-09-10 23:34:09 +00:00
|
|
|
r'line number ([\d]+), is supposed to be shown until version '
|
2014-04-24 10:32:46 +00:00
|
|
|
r'0.17.0 is released. Current version is now 0.17.0. '
|
2013-10-03 18:21:14 +00:00
|
|
|
r'Please remove the warning.'):
|
2013-10-04 08:05:00 +00:00
|
|
|
raise_warning(_version_info_=(0, 17)) # no kwargs
|
|
|
|
|
2013-09-10 23:34:09 +00:00
|
|
|
with self.assertRaisesRegexp(
|
|
|
|
RuntimeError,
|
2017-02-18 13:44:11 +00:00
|
|
|
r'The warning triggered on filename \'(.*)test_warnings.py\', '
|
2013-09-10 23:34:09 +00:00
|
|
|
r'line number ([\d]+), is supposed to be shown until version '
|
2014-04-24 10:32:46 +00:00
|
|
|
r'0.17.0 is released. Current version is now 0.17.0. '
|
2013-10-03 18:21:14 +00:00
|
|
|
r'Please remove the warning.'):
|
2013-10-04 08:05:00 +00:00
|
|
|
raise_warning(bar='baz', qux='quux', _version_info_=(0, 17)) # some kwargs
|
2013-09-10 23:34:09 +00:00
|
|
|
|
2013-07-20 14:32:50 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
from integration import run_tests
|
|
|
|
run_tests(WarnUntilTestCase, needs_daemon=False)
|