ansible/test/units/module_utils/basic/test_deprecate_warn.py

78 lines
3.3 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
#
# Copyright (c) 2017 Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
2020-06-22 22:27:08 +02:00
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import json
import pytest
from ansible.module_utils.common import warnings
@pytest.mark.parametrize('stdin', [{}], indirect=['stdin'])
def test_warn(am, capfd):
am.warn('warning1')
with pytest.raises(SystemExit):
am.exit_json(warnings=['warning2'])
out, err = capfd.readouterr()
assert json.loads(out)['warnings'] == ['warning1', 'warning2']
@pytest.mark.parametrize('stdin', [{}], indirect=['stdin'])
def test_deprecate(am, capfd, monkeypatch):
monkeypatch.setattr(warnings, '_global_deprecations', [])
am.deprecate('deprecation1')
am.deprecate('deprecation2', '2.3') # pylint: disable=ansible-deprecated-no-collection-name
am.deprecate('deprecation3', version='2.4') # pylint: disable=ansible-deprecated-no-collection-name
am.deprecate('deprecation4', date='2020-03-10') # pylint: disable=ansible-deprecated-no-collection-name
am.deprecate('deprecation5', collection_name='ansible.builtin')
am.deprecate('deprecation6', '2.3', collection_name='ansible.builtin')
am.deprecate('deprecation7', version='2.4', collection_name='ansible.builtin')
am.deprecate('deprecation8', date='2020-03-10', collection_name='ansible.builtin')
with pytest.raises(SystemExit):
am.exit_json(deprecations=['deprecation9', ('deprecation10', '2.4')])
out, err = capfd.readouterr()
output = json.loads(out)
assert ('warnings' not in output or output['warnings'] == [])
assert output['deprecations'] == [
{u'msg': u'deprecation1', u'version': None, u'collection_name': None},
{u'msg': u'deprecation2', u'version': '2.3', u'collection_name': None},
{u'msg': u'deprecation3', u'version': '2.4', u'collection_name': None},
{u'msg': u'deprecation4', u'date': '2020-03-10', u'collection_name': None},
{u'msg': u'deprecation5', u'version': None, u'collection_name': 'ansible.builtin'},
{u'msg': u'deprecation6', u'version': '2.3', u'collection_name': 'ansible.builtin'},
{u'msg': u'deprecation7', u'version': '2.4', u'collection_name': 'ansible.builtin'},
{u'msg': u'deprecation8', u'date': '2020-03-10', u'collection_name': 'ansible.builtin'},
{u'msg': u'deprecation9', u'version': None, u'collection_name': None},
{u'msg': u'deprecation10', u'version': '2.4', u'collection_name': None},
]
@pytest.mark.parametrize('stdin', [{}], indirect=['stdin'])
def test_deprecate_without_list(am, capfd):
with pytest.raises(SystemExit):
am.exit_json(deprecations='Simple deprecation warning')
out, err = capfd.readouterr()
output = json.loads(out)
assert ('warnings' not in output or output['warnings'] == [])
assert output['deprecations'] == [
{u'msg': u'Simple deprecation warning', u'version': None, u'collection_name': None},
]
Allow to deprecate options and aliases by date (#68177) * Allow to deprecate options and aliases by date instead of only by version. * Update display.deprecate(). * Adjust behavior to conform to tested behavior, extend tests, and improve C# style. * Parse date and fail on invalid date. This is mainly to make sure that people start using invalid dates, and we eventually have a mess to clean up. * C# code: improve validation and update/extend tests. * Make sure that deprecate() is not called with both date and version. * Forgot to remove no longer necessary formatting. * Adjust order of warnings in C# code. * Adjust unrelated test. * Fix grammar (and make that test pass). * Don't parse date, and adjust message to be same as in #67684. * Sanity tests: disable date in past test. * Validate-modules: validate ISO 8601 date format. * Validate-modules: switch schema declaration for deprecated_aliases to improve error messages for invalid dates. * Use DateTime instead of string for date deprecation. * Validate that date in deprecated_aliases is actually a DateTime. * Fix tests. * Fix rebasing error. * Adjust error codes for pylint, and add removed_at_date and deprecated_aliases.date checks to validate-modules. * Make deprecation date in the past error codes optional. * Make sure not both version and date are specified for AnsibleModule.deprecate() calls. * Stop using Python 3.7+ API. * Make sure errors are actually reported. Re-add 'ansible-' prefix. * Avoid crashing when 'name' isn't there. * Linting. * Update lib/ansible/module_utils/csharp/Ansible.Basic.cs Co-authored-by: Jordan Borean <jborean93@gmail.com> * Adjust test to latest change. * Prefer date over version if both end up in Display.deprecated(). Co-authored-by: Jordan Borean <jborean93@gmail.com>
2020-05-27 02:23:56 +02:00
@pytest.mark.parametrize('stdin', [{}], indirect=['stdin'])
def test_deprecate_without_list(am, capfd):
with pytest.raises(AssertionError) as ctx:
am.deprecate('Simple deprecation warning', date='', version='')
assert ctx.value.args[0] == "implementation error -- version and date must not both be set"