2017-02-14 05:49:39 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2017-12-05 21:43:13 +01:00
|
|
|
# Copyright (c) 2017 Ansible Project
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
2017-02-14 05:49:39 +01:00
|
|
|
|
2020-06-22 22:27:08 +02:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2017-02-14 05:49:39 +01:00
|
|
|
import json
|
|
|
|
|
2017-11-29 00:43:24 +01:00
|
|
|
import pytest
|
2017-02-14 05:49:39 +01:00
|
|
|
|
2021-04-16 18:42:29 +02:00
|
|
|
from ansible.module_utils.common import warnings
|
|
|
|
|
2017-02-14 05:49:39 +01:00
|
|
|
|
2017-12-05 21:43:13 +01:00
|
|
|
@pytest.mark.parametrize('stdin', [{}], indirect=['stdin'])
|
2017-11-29 00:43:24 +01:00
|
|
|
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']
|
|
|
|
|
|
|
|
|
2017-12-05 21:43:13 +01:00
|
|
|
@pytest.mark.parametrize('stdin', [{}], indirect=['stdin'])
|
2021-04-16 18:42:29 +02:00
|
|
|
def test_deprecate(am, capfd, monkeypatch):
|
|
|
|
monkeypatch.setattr(warnings, '_global_deprecations', [])
|
|
|
|
|
2017-11-29 00:43:24 +01:00
|
|
|
am.deprecate('deprecation1')
|
2020-06-10 00:21:19 +02:00
|
|
|
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')
|
2017-11-29 00:43:24 +01:00
|
|
|
|
|
|
|
with pytest.raises(SystemExit):
|
2020-06-10 00:21:19 +02:00
|
|
|
am.exit_json(deprecations=['deprecation9', ('deprecation10', '2.4')])
|
2017-11-29 00:43:24 +01:00
|
|
|
|
|
|
|
out, err = capfd.readouterr()
|
|
|
|
output = json.loads(out)
|
|
|
|
assert ('warnings' not in output or output['warnings'] == [])
|
|
|
|
assert output['deprecations'] == [
|
2020-06-10 00:21:19 +02:00
|
|
|
{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},
|
2017-11-29 00:43:24 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2017-12-05 21:43:13 +01:00
|
|
|
@pytest.mark.parametrize('stdin', [{}], indirect=['stdin'])
|
2017-11-29 00:43:24 +01:00
|
|
|
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'] == [
|
2020-06-10 00:21:19 +02:00
|
|
|
{u'msg': u'Simple deprecation warning', u'version': None, u'collection_name': None},
|
2017-11-29 00:43:24 +01:00
|
|
|
]
|
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"
|