mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-06 13:22:48 +00:00
Save the command line arguments into a global context
* Once cli args are parsed, they're constant. So, save the parsed args
into the global context for everyone else to use them from now on.
* Port cli scripts to use the CLIARGS in the context
* Refactor call to parse cli args into the run() method
* Fix unittests for changes to the internals of CLI arg parsing
* Port callback plugins to use context.CLIARGS
* Got rid of the private self._options attribute
* Use context.CLIARGS in the individual callback plugins instead.
* Also output positional arguments in default and unixy plugins
* Code has been simplified since we're now dealing with a dict rather
than Optparse.Value
This commit is contained in:
@@ -5,6 +5,8 @@ from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
import pytest
|
||||
|
||||
from ansible import context
|
||||
from ansible.cli.adhoc import AdHocCLI, display
|
||||
from ansible.errors import AnsibleOptionsError
|
||||
|
||||
@@ -22,7 +24,7 @@ def test_with_command():
|
||||
module_name = 'command'
|
||||
adhoc_cli = AdHocCLI(args=['-m', module_name, '-vv'])
|
||||
adhoc_cli.parse()
|
||||
assert adhoc_cli.options.module_name == module_name
|
||||
assert context.CLIARGS['module_name'] == module_name
|
||||
assert display.verbosity == 2
|
||||
|
||||
|
||||
@@ -36,9 +38,8 @@ def test_with_extra_parameters():
|
||||
|
||||
def test_simple_command():
|
||||
""" Test valid command and its run"""
|
||||
adhoc_cli = AdHocCLI(['/bin/ansible', '-m', 'command', 'localhost'])
|
||||
adhoc_cli = AdHocCLI(['/bin/ansible', '-m', 'command', 'localhost', '-a', 'echo "hi"'])
|
||||
adhoc_cli.parse()
|
||||
adhoc_cli.options.module_args = "echo 'hi'"
|
||||
ret = adhoc_cli.run()
|
||||
assert ret == 0
|
||||
|
||||
@@ -63,9 +64,8 @@ def test_did_you_mean_playbook():
|
||||
|
||||
def test_play_ds_positive():
|
||||
""" Test _play_ds"""
|
||||
adhoc_cli = AdHocCLI(args=['/bin/ansible', 'localhost'])
|
||||
adhoc_cli = AdHocCLI(args=['/bin/ansible', 'localhost', '-m', 'command'])
|
||||
adhoc_cli.parse()
|
||||
adhoc_cli.options.module_name = 'command'
|
||||
ret = adhoc_cli._play_ds('command', 10, 2)
|
||||
assert ret['name'] == 'Ansible Ad-Hoc'
|
||||
assert ret['tasks'] == [{'action': {'module': 'command', 'args': {}}, 'async_val': 10, 'poll': 2}]
|
||||
@@ -73,9 +73,8 @@ def test_play_ds_positive():
|
||||
|
||||
def test_play_ds_with_include_role():
|
||||
""" Test include_role command with poll"""
|
||||
adhoc_cli = AdHocCLI(args=['/bin/ansible', 'localhost'])
|
||||
adhoc_cli = AdHocCLI(args=['/bin/ansible', 'localhost', '-m', 'include_role'])
|
||||
adhoc_cli.parse()
|
||||
adhoc_cli.options.module_name = 'include_role'
|
||||
ret = adhoc_cli._play_ds('include_role', None, 2)
|
||||
assert ret['name'] == 'Ansible Ad-Hoc'
|
||||
assert ret['gather_facts'] == 'no'
|
||||
@@ -88,5 +87,5 @@ def test_run_import_playbook():
|
||||
adhoc_cli.parse()
|
||||
with pytest.raises(AnsibleOptionsError) as exec_info:
|
||||
adhoc_cli.run()
|
||||
assert adhoc_cli.options.module_name == import_playbook
|
||||
assert context.CLIARGS['module_name'] == import_playbook
|
||||
assert "'%s' is not a valid action for ad-hoc commands" % import_playbook == str(exec_info.value)
|
||||
|
||||
@@ -26,6 +26,8 @@ import tarfile
|
||||
import tempfile
|
||||
import yaml
|
||||
|
||||
from ansible import arguments
|
||||
from ansible import context
|
||||
from ansible.cli.galaxy import GalaxyCLI
|
||||
from units.compat import unittest
|
||||
from units.compat.mock import call, patch
|
||||
@@ -47,7 +49,6 @@ class TestGalaxy(unittest.TestCase):
|
||||
|
||||
# creating framework for a role
|
||||
gc = GalaxyCLI(args=["ansible-galaxy", "init", "--offline", "delete_me"])
|
||||
gc.parse()
|
||||
gc.run()
|
||||
cls.role_dir = "./delete_me"
|
||||
cls.role_name = "delete_me"
|
||||
@@ -96,8 +97,14 @@ class TestGalaxy(unittest.TestCase):
|
||||
shutil.rmtree(cls.temp_dir)
|
||||
|
||||
def setUp(self):
|
||||
# Reset the stored command line args
|
||||
arguments.GlobalCLIArgs._Singleton__instance = None
|
||||
self.default_args = ['ansible-galaxy']
|
||||
|
||||
def tearDown(self):
|
||||
# Reset the stored command line args
|
||||
arguments.GlobalCLIArgs._Singleton__instance = None
|
||||
|
||||
def test_init(self):
|
||||
galaxy_cli = GalaxyCLI(args=self.default_args)
|
||||
self.assertTrue(isinstance(galaxy_cli, GalaxyCLI))
|
||||
@@ -120,12 +127,11 @@ class TestGalaxy(unittest.TestCase):
|
||||
def test_run(self):
|
||||
''' verifies that the GalaxyCLI object's api is created and that execute() is called. '''
|
||||
gc = GalaxyCLI(args=["ansible-galaxy", "install", "--ignore-errors", "imaginary_role"])
|
||||
gc.parse()
|
||||
with patch.object(ansible.cli.CLI, "execute", return_value=None) as mock_ex:
|
||||
with patch.object(ansible.cli.CLI, "run", return_value=None) as mock_run:
|
||||
gc.run()
|
||||
|
||||
# testing
|
||||
self.assertIsInstance(gc.galaxy, ansible.galaxy.Galaxy)
|
||||
self.assertEqual(mock_run.call_count, 1)
|
||||
self.assertTrue(isinstance(gc.api, ansible.galaxy.api.GalaxyAPI))
|
||||
self.assertEqual(mock_ex.call_count, 1)
|
||||
@@ -133,15 +139,16 @@ class TestGalaxy(unittest.TestCase):
|
||||
def test_execute_remove(self):
|
||||
# installing role
|
||||
gc = GalaxyCLI(args=["ansible-galaxy", "install", "-p", self.role_path, "-r", self.role_req, '--force'])
|
||||
gc.parse()
|
||||
gc.run()
|
||||
|
||||
# location where the role was installed
|
||||
role_file = os.path.join(self.role_path, self.role_name)
|
||||
|
||||
# removing role
|
||||
# Have to reset the arguments in the context object manually since we're doing the
|
||||
# equivalent of running the command line program twice
|
||||
arguments.GlobalCLIArgs._Singleton__instance = None
|
||||
gc = GalaxyCLI(args=["ansible-galaxy", "remove", role_file, self.role_name])
|
||||
gc.parse()
|
||||
gc.run()
|
||||
|
||||
# testing role was removed
|
||||
@@ -151,7 +158,6 @@ class TestGalaxy(unittest.TestCase):
|
||||
def test_exit_without_ignore_without_flag(self):
|
||||
''' tests that GalaxyCLI exits with the error specified if the --ignore-errors flag is not used '''
|
||||
gc = GalaxyCLI(args=["ansible-galaxy", "install", "--server=None", "fake_role_name"])
|
||||
gc.parse()
|
||||
with patch.object(ansible.utils.display.Display, "display", return_value=None) as mocked_display:
|
||||
# testing that error expected is raised
|
||||
self.assertRaises(AnsibleError, gc.run)
|
||||
@@ -161,7 +167,6 @@ class TestGalaxy(unittest.TestCase):
|
||||
''' tests that GalaxyCLI exits without the error specified if the --ignore-errors flag is used '''
|
||||
# testing with --ignore-errors flag
|
||||
gc = GalaxyCLI(args=["ansible-galaxy", "install", "--server=None", "fake_role_name", "--ignore-errors"])
|
||||
gc.parse()
|
||||
with patch.object(ansible.utils.display.Display, "display", return_value=None) as mocked_display:
|
||||
gc.run()
|
||||
self.assertTrue(mocked_display.called_once_with("- downloading role 'fake_role_name', owned by "))
|
||||
@@ -172,7 +177,6 @@ class TestGalaxy(unittest.TestCase):
|
||||
|
||||
# checking that the common results of parse() for all possible actions have been created/called
|
||||
self.assertIsInstance(galaxycli_obj.parser, ansible.cli.SortedOptParser)
|
||||
self.assertIsInstance(galaxycli_obj.galaxy, ansible.galaxy.Galaxy)
|
||||
formatted_call = {
|
||||
'import': 'usage: %prog import [options] github_user github_repo',
|
||||
'delete': 'usage: %prog delete [options] github_user github_repo',
|
||||
@@ -206,74 +210,74 @@ class TestGalaxy(unittest.TestCase):
|
||||
''' testing the options parser when the action 'delete' is given '''
|
||||
gc = GalaxyCLI(args=["ansible-galaxy", "delete"])
|
||||
self.run_parse_common(gc, "delete")
|
||||
self.assertEqual(gc.options.verbosity, 0)
|
||||
self.assertEqual(context.CLIARGS['verbosity'], 0)
|
||||
|
||||
def test_parse_import(self):
|
||||
''' testing the options parser when the action 'import' is given '''
|
||||
gc = GalaxyCLI(args=["ansible-galaxy", "import"])
|
||||
self.run_parse_common(gc, "import")
|
||||
self.assertEqual(gc.options.wait, True)
|
||||
self.assertEqual(gc.options.reference, None)
|
||||
self.assertEqual(gc.options.check_status, False)
|
||||
self.assertEqual(gc.options.verbosity, 0)
|
||||
self.assertEqual(context.CLIARGS['wait'], True)
|
||||
self.assertEqual(context.CLIARGS['reference'], None)
|
||||
self.assertEqual(context.CLIARGS['check_status'], False)
|
||||
self.assertEqual(context.CLIARGS['verbosity'], 0)
|
||||
|
||||
def test_parse_info(self):
|
||||
''' testing the options parser when the action 'info' is given '''
|
||||
gc = GalaxyCLI(args=["ansible-galaxy", "info"])
|
||||
self.run_parse_common(gc, "info")
|
||||
self.assertEqual(gc.options.offline, False)
|
||||
self.assertEqual(context.CLIARGS['offline'], False)
|
||||
|
||||
def test_parse_init(self):
|
||||
''' testing the options parser when the action 'init' is given '''
|
||||
gc = GalaxyCLI(args=["ansible-galaxy", "init"])
|
||||
self.run_parse_common(gc, "init")
|
||||
self.assertEqual(gc.options.offline, False)
|
||||
self.assertEqual(gc.options.force, False)
|
||||
self.assertEqual(context.CLIARGS['offline'], False)
|
||||
self.assertEqual(context.CLIARGS['force'], False)
|
||||
|
||||
def test_parse_install(self):
|
||||
''' testing the options parser when the action 'install' is given '''
|
||||
gc = GalaxyCLI(args=["ansible-galaxy", "install"])
|
||||
self.run_parse_common(gc, "install")
|
||||
self.assertEqual(gc.options.ignore_errors, False)
|
||||
self.assertEqual(gc.options.no_deps, False)
|
||||
self.assertEqual(gc.options.role_file, None)
|
||||
self.assertEqual(gc.options.force, False)
|
||||
self.assertEqual(context.CLIARGS['ignore_errors'], False)
|
||||
self.assertEqual(context.CLIARGS['no_deps'], False)
|
||||
self.assertEqual(context.CLIARGS['role_file'], None)
|
||||
self.assertEqual(context.CLIARGS['force'], False)
|
||||
|
||||
def test_parse_list(self):
|
||||
''' testing the options parser when the action 'list' is given '''
|
||||
gc = GalaxyCLI(args=["ansible-galaxy", "list"])
|
||||
self.run_parse_common(gc, "list")
|
||||
self.assertEqual(gc.options.verbosity, 0)
|
||||
self.assertEqual(context.CLIARGS['verbosity'], 0)
|
||||
|
||||
def test_parse_login(self):
|
||||
''' testing the options parser when the action 'login' is given '''
|
||||
gc = GalaxyCLI(args=["ansible-galaxy", "login"])
|
||||
self.run_parse_common(gc, "login")
|
||||
self.assertEqual(gc.options.verbosity, 0)
|
||||
self.assertEqual(gc.options.token, None)
|
||||
self.assertEqual(context.CLIARGS['verbosity'], 0)
|
||||
self.assertEqual(context.CLIARGS['token'], None)
|
||||
|
||||
def test_parse_remove(self):
|
||||
''' testing the options parser when the action 'remove' is given '''
|
||||
gc = GalaxyCLI(args=["ansible-galaxy", "remove"])
|
||||
self.run_parse_common(gc, "remove")
|
||||
self.assertEqual(gc.options.verbosity, 0)
|
||||
self.assertEqual(context.CLIARGS['verbosity'], 0)
|
||||
|
||||
def test_parse_search(self):
|
||||
''' testing the options parswer when the action 'search' is given '''
|
||||
gc = GalaxyCLI(args=["ansible-galaxy", "search"])
|
||||
self.run_parse_common(gc, "search")
|
||||
self.assertEqual(gc.options.platforms, None)
|
||||
self.assertEqual(gc.options.galaxy_tags, None)
|
||||
self.assertEqual(gc.options.author, None)
|
||||
self.assertEqual(context.CLIARGS['platforms'], None)
|
||||
self.assertEqual(context.CLIARGS['galaxy_tags'], None)
|
||||
self.assertEqual(context.CLIARGS['author'], None)
|
||||
|
||||
def test_parse_setup(self):
|
||||
''' testing the options parser when the action 'setup' is given '''
|
||||
gc = GalaxyCLI(args=["ansible-galaxy", "setup"])
|
||||
self.run_parse_common(gc, "setup")
|
||||
|
||||
self.assertEqual(gc.options.verbosity, 0)
|
||||
self.assertEqual(gc.options.remove_id, None)
|
||||
self.assertEqual(gc.options.setup_list, False)
|
||||
self.assertEqual(context.CLIARGS['verbosity'], 0)
|
||||
self.assertEqual(context.CLIARGS['remove_id'], None)
|
||||
self.assertEqual(context.CLIARGS['setup_list'], False)
|
||||
|
||||
|
||||
class ValidRoleTests(object):
|
||||
@@ -299,7 +303,6 @@ class ValidRoleTests(object):
|
||||
|
||||
# create role using default skeleton
|
||||
gc = GalaxyCLI(args=['ansible-galaxy', 'init', '-c', '--offline'] + galaxy_args + ['--init-path', cls.test_dir, cls.role_name])
|
||||
gc.parse()
|
||||
gc.run()
|
||||
cls.gc = gc
|
||||
|
||||
@@ -466,4 +469,4 @@ class TestGalaxyInitSkeleton(unittest.TestCase, ValidRoleTests):
|
||||
self.assertTrue(os.path.exists(os.path.join(self.role_dir, 'templates_extra', 'templates.txt')))
|
||||
|
||||
def test_skeleton_option(self):
|
||||
self.assertEquals(self.role_skeleton_path, self.gc.options.role_skeleton, msg='Skeleton path was not parsed properly from the command line')
|
||||
self.assertEquals(self.role_skeleton_path, context.CLIARGS['role_skeleton'], msg='Skeleton path was not parsed properly from the command line')
|
||||
|
||||
@@ -22,6 +22,7 @@ __metaclass__ = type
|
||||
from units.compat import unittest
|
||||
from units.mock.loader import DictDataLoader
|
||||
|
||||
from ansible import context
|
||||
from ansible.inventory.manager import InventoryManager
|
||||
from ansible.vars.manager import VariableManager
|
||||
|
||||
@@ -32,7 +33,7 @@ class TestPlaybookCLI(unittest.TestCase):
|
||||
def test_flush_cache(self):
|
||||
cli = PlaybookCLI(args=["ansible-playbook", "--flush-cache", "foobar.yml"])
|
||||
cli.parse()
|
||||
self.assertTrue(cli.options.flush_cache)
|
||||
self.assertTrue(context.CLIARGS['flush_cache'])
|
||||
|
||||
variable_manager = VariableManager()
|
||||
fake_loader = DictDataLoader({'foobar.yml': ""})
|
||||
|
||||
Reference in New Issue
Block a user