2018-12-18 02:10:26 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright: (c) 2018, Toshio Kuratomi <tkuratomi@ansible.com>
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
# Make coding more python3-ish
|
|
|
|
from __future__ import (absolute_import, division)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
try:
|
|
|
|
import argparse
|
|
|
|
except ImportError:
|
|
|
|
argparse = None
|
|
|
|
|
|
|
|
import optparse
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2018-12-19 08:28:33 +00:00
|
|
|
from ansible.module_utils.common.collections import ImmutableDict
|
2018-12-20 18:07:49 +00:00
|
|
|
from ansible.utils import context_objects as co
|
2018-12-18 02:10:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
MAKE_IMMUTABLE_DATA = ((u'くらとみ', u'くらとみ'),
|
|
|
|
(42, 42),
|
2018-12-19 08:28:33 +00:00
|
|
|
({u'café': u'くらとみ'}, ImmutableDict({u'café': u'くらとみ'})),
|
2018-12-18 02:10:26 +00:00
|
|
|
([1, u'café', u'くらとみ'], (1, u'café', u'くらとみ')),
|
|
|
|
(set((1, u'café', u'くらとみ')), frozenset((1, u'café', u'くらとみ'))),
|
|
|
|
({u'café': [1, set(u'ñ')]},
|
2018-12-19 08:28:33 +00:00
|
|
|
ImmutableDict({u'café': (1, frozenset(u'ñ'))})),
|
2018-12-18 02:10:26 +00:00
|
|
|
([set((1, 2)), {u'くらとみ': 3}],
|
2018-12-19 08:28:33 +00:00
|
|
|
(frozenset((1, 2)), ImmutableDict({u'くらとみ': 3}))),
|
2018-12-18 02:10:59 +00:00
|
|
|
)
|
2018-12-18 02:10:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('data, expected', MAKE_IMMUTABLE_DATA)
|
|
|
|
def test_make_immutable(data, expected):
|
2018-12-19 08:28:33 +00:00
|
|
|
assert co._make_immutable(data) == expected
|
2018-12-18 02:10:26 +00:00
|
|
|
|
|
|
|
|
2018-12-18 02:10:59 +00:00
|
|
|
def test_cliargs_from_dict():
|
|
|
|
old_dict = {'tags': [u'production', u'webservers'],
|
|
|
|
'check_mode': True,
|
|
|
|
'start_at_task': u'Start with くらとみ'}
|
|
|
|
expected = frozenset((('tags', (u'production', u'webservers')),
|
|
|
|
('check_mode', True),
|
|
|
|
('start_at_task', u'Start with くらとみ')))
|
|
|
|
|
2018-12-19 08:28:33 +00:00
|
|
|
assert frozenset(co.CLIArgs(old_dict).items()) == expected
|
2018-12-18 02:10:59 +00:00
|
|
|
|
|
|
|
|
2018-12-18 02:10:26 +00:00
|
|
|
def test_cliargs():
|
|
|
|
class FakeOptions:
|
|
|
|
pass
|
|
|
|
options = FakeOptions()
|
|
|
|
options.tags = [u'production', u'webservers']
|
|
|
|
options.check_mode = True
|
|
|
|
options.start_at_task = u'Start with くらとみ'
|
|
|
|
|
|
|
|
expected = frozenset((('tags', (u'production', u'webservers')),
|
|
|
|
('check_mode', True),
|
|
|
|
('start_at_task', u'Start with くらとみ')))
|
|
|
|
|
2018-12-19 08:28:33 +00:00
|
|
|
assert frozenset(co.CLIArgs.from_options(options).items()) == expected
|
2018-12-18 02:10:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipIf(argparse is None)
|
|
|
|
def test_cliargs_argparse():
|
|
|
|
parser = argparse.ArgumentParser(description='Process some integers.')
|
|
|
|
parser.add_argument('integers', metavar='N', type=int, nargs='+',
|
|
|
|
help='an integer for the accumulator')
|
|
|
|
parser.add_argument('--sum', dest='accumulate', action='store_const',
|
|
|
|
const=sum, default=max,
|
|
|
|
help='sum the integers (default: find the max)')
|
|
|
|
args = parser.parse_args([u'--sum', u'1', u'2'])
|
|
|
|
|
|
|
|
expected = frozenset((('accumulate', sum), ('integers', (1, 2))))
|
|
|
|
|
2018-12-19 08:28:33 +00:00
|
|
|
assert frozenset(co.CLIArgs.from_options(args).items()) == expected
|
2018-12-18 02:10:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Can get rid of this test when we port ansible.cli from optparse to argparse
|
|
|
|
def test_cliargs_optparse():
|
|
|
|
parser = optparse.OptionParser(description='Process some integers.')
|
|
|
|
parser.add_option('--sum', dest='accumulate', action='store_const',
|
2018-12-18 02:10:59 +00:00
|
|
|
const=sum, default=max,
|
|
|
|
help='sum the integers (default: find the max)')
|
2018-12-18 02:10:26 +00:00
|
|
|
opts, args = parser.parse_args([u'--sum', u'1', u'2'])
|
|
|
|
opts.integers = args
|
|
|
|
|
|
|
|
expected = frozenset((('accumulate', sum), ('integers', (u'1', u'2'))))
|
|
|
|
|
2018-12-19 08:28:33 +00:00
|
|
|
assert frozenset(co.CLIArgs.from_options(opts).items()) == expected
|