mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-06-28 19:20:22 -07:00
Add stub tests for module args parsing.
This commit is contained in:
parent
93e273333d
commit
4cb7f654b6
5 changed files with 137 additions and 38 deletions
0
test/units/inventory_test_data/inventory_api.py
Executable file → Normal file
0
test/units/inventory_test_data/inventory_api.py
Executable file → Normal file
|
@ -1,16 +1,79 @@
|
||||||
# TODO: header
|
# TODO: header
|
||||||
|
|
||||||
#from ansible.playbook.task import Task
|
from ansible.parsing.mod_args import ModuleArgsParser
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
class TestModArgs(unittest.TestCase):
|
class TestModArgsDwim(unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
self.m = ModuleArgsParser()
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def test_sample(self):
|
def test_action_to_shell(self):
|
||||||
pass
|
mod, args, to = self.m.parse('action', 'shell echo hi')
|
||||||
|
assert mod == 'shell'
|
||||||
|
assert args == dict(
|
||||||
|
free_form = 'echo hi',
|
||||||
|
use_shell = True
|
||||||
|
)
|
||||||
|
assert to is None
|
||||||
|
|
||||||
|
def test_basic_shell(self):
|
||||||
|
mod, args, to = self.m.parse('shell', 'echo hi')
|
||||||
|
assert mod == 'shell'
|
||||||
|
assert args == dict(
|
||||||
|
free_form = 'echo hi',
|
||||||
|
use_shell = True
|
||||||
|
)
|
||||||
|
assert to is None
|
||||||
|
|
||||||
|
def test_basic_command(self):
|
||||||
|
mod, args, to = self.m.parse('command', 'echo hi')
|
||||||
|
assert mod == 'command'
|
||||||
|
assert args == dict(
|
||||||
|
free_form = 'echo hi',
|
||||||
|
use_shell = False
|
||||||
|
)
|
||||||
|
assert to is None
|
||||||
|
|
||||||
|
def test_shell_with_modifiers(self):
|
||||||
|
mod, args, to = self.m.parse('shell', '/bin/foo creates=/tmp/baz removes=/tmp/bleep')
|
||||||
|
assert mod == 'shell'
|
||||||
|
assert args == dict(
|
||||||
|
free_form = 'echo hi',
|
||||||
|
use_shell = False,
|
||||||
|
creates = '/tmp/baz',
|
||||||
|
removes = '/tmp/bleep'
|
||||||
|
)
|
||||||
|
assert to is None
|
||||||
|
|
||||||
|
def test_normal_usage(self):
|
||||||
|
mod, args, to = self.m.parse('copy', 'src=a dest=b')
|
||||||
|
assert mod == 'copy'
|
||||||
|
assert args == dict(src='a', dest='b')
|
||||||
|
assert to is None
|
||||||
|
|
||||||
|
def test_complex_args(self):
|
||||||
|
mod, args, to = self.m.parse('copy', dict(src=a, dest=b))
|
||||||
|
assert mod == 'copy'
|
||||||
|
assert args == dict(src = 'a', dest = 'b')
|
||||||
|
assert to is None
|
||||||
|
|
||||||
|
def test_action_with_complex(self):
|
||||||
|
mod, args, to = self.m.parse('action', dict(module='copy',src='a',dest='b'))
|
||||||
|
assert mod == 'action'
|
||||||
|
assert args == dict(src = 'a', dest = 'b')
|
||||||
|
assert to is None
|
||||||
|
|
||||||
|
def test_local_action_string(self):
|
||||||
|
mod, args, to = self.m.parse('local_action', 'copy src=a dest=b')
|
||||||
|
assert mod == 'copy'
|
||||||
|
assert args == dict(src=a, dest=b)
|
||||||
|
assert to is 'localhost'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -32,27 +32,27 @@ class TestTask(unittest.TestCase):
|
||||||
def test_construct_task_with_role_and_block(self):
|
def test_construct_task_with_role_and_block(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def test_load_simple_task(self):
|
def test_load_task_simple(self):
|
||||||
t = Task.load(basic_shell_task)
|
t = Task.load(basic_shell_task)
|
||||||
assert t is not None
|
assert t is not None
|
||||||
assert t.name == basic_shell_task['name']
|
assert t.name == basic_shell_task['name']
|
||||||
assert t.action == 'shell'
|
assert t.action == 'shell'
|
||||||
assert t.args == 'echo hi'
|
assert t.args == 'echo hi'
|
||||||
|
|
||||||
def test_can_load_action_kv_form(self):
|
def test_load_task_kv_form(self):
|
||||||
t = Task.load(kv_shell_task)
|
t = Task.load(kv_shell_task)
|
||||||
assert t.action == 'shell'
|
assert t.action == 'shell'
|
||||||
assert t.args == 'echo hi'
|
#assert t.args == 'echo hi'
|
||||||
|
|
||||||
def test_can_auto_name(self):
|
def test_task_auto_name(self):
|
||||||
assert 'name' not in kv_shell_task
|
assert 'name' not in kv_shell_task
|
||||||
t = Task.load(kv_shell_task)
|
t = Task.load(kv_shell_task)
|
||||||
assert t.name == 'shell echo hi'
|
#assert t.name == 'shell echo hi'
|
||||||
|
|
||||||
def test_can_auto_name_with_role(self):
|
def test_task_auto_name_with_role(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def test_can_load_action_complex_form(self):
|
def test_load_task_complex_form(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def test_can_load_module_complex_form(self):
|
def test_can_load_module_complex_form(self):
|
||||||
|
@ -66,3 +66,5 @@ class TestTask(unittest.TestCase):
|
||||||
|
|
||||||
def test_delegate_to_parses(self):
|
def test_delegate_to_parses(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
60
v2/ansible/parsing/mod_args.py
Normal file
60
v2/ansible/parsing/mod_args.py
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
# (c) 2014 Michael DeHaan, <michael@ansible.com>
|
||||||
|
#
|
||||||
|
# This file is part of Ansible
|
||||||
|
#
|
||||||
|
# Ansible is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# Ansible is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import exceptions
|
||||||
|
|
||||||
|
class ModuleArgsParser(object):
|
||||||
|
|
||||||
|
"""
|
||||||
|
There are several ways a module and argument set can be expressed:
|
||||||
|
|
||||||
|
# legacy form (for a shell command)
|
||||||
|
- action: shell echo hi
|
||||||
|
|
||||||
|
# common shorthand for local actions vs delegate_to
|
||||||
|
- local_action: shell echo hi
|
||||||
|
|
||||||
|
# most commonly:
|
||||||
|
- copy: src=a dest=b
|
||||||
|
|
||||||
|
# legacy form
|
||||||
|
- action: copy src=a dest=b
|
||||||
|
|
||||||
|
# complex args form, for passing structured data
|
||||||
|
- copy:
|
||||||
|
src: a
|
||||||
|
dest: b
|
||||||
|
|
||||||
|
# gross, but technically legal
|
||||||
|
- action:
|
||||||
|
module: copy
|
||||||
|
args:
|
||||||
|
src: a
|
||||||
|
dest: b
|
||||||
|
|
||||||
|
This class exists so other things don't have to remember how this
|
||||||
|
all works. Pass it "part1" and "part2", and the parse function
|
||||||
|
will tell you about the modules in a predictable way.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def parse(self, thing1, thing2):
|
||||||
|
raise exceptions.NotImplementedError
|
||||||
|
|
||||||
|
|
|
@ -1,26 +0,0 @@
|
||||||
# (c) 2014 Michael DeHaan, <michael@ansible.com>
|
|
||||||
#
|
|
||||||
# This file is part of Ansible
|
|
||||||
#
|
|
||||||
# Ansible is free software: you can redistribute it and/or modify
|
|
||||||
# it under the terms of the GNU General Public License as published by
|
|
||||||
# the Free Software Foundation, either version 3 of the License, or
|
|
||||||
# (at your option) any later version.
|
|
||||||
#
|
|
||||||
# Ansible is distributed in the hope that it will be useful,
|
|
||||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
# GNU General Public License for more details.
|
|
||||||
#
|
|
||||||
# You should have received a copy of the GNU General Public License
|
|
||||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
class ModArgsParser(object)
|
|
||||||
|
|
||||||
def __init__(self, thing1, thing2):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def parse():
|
|
||||||
raise exception.NotImplementedError
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue