mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-05-03 15:51:30 -07: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:
parent
c18da65089
commit
afdbb0d9d5
36 changed files with 1033 additions and 868 deletions
|
@ -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')
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue