mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-06 13:22:48 +00:00
Refactor parsing of CLI args so that we can modify them in the base class
Implement tag and skip_tag handling in the CLI() class. Change tag and skip_tag command line options to be accepted multiple times on the CLI and add them together rather than overwrite. * Make it configurable whether to merge or overwrite multiple --tags arguments * Make the base CLI class an abstractbaseclass so we can implement functionality in parse() but still make subclasses implement it. * Deprecate the overwrite feature of --tags with a message that the default will change in 2.4 and go away in 2.5. * Add documentation for merge_multiple_cli_flags * Fix galaxy search so its tags argument does not conflict with generic tags * Unit tests and more integration tests for tags
This commit is contained in:
committed by
Brian Coca
parent
9962245b92
commit
1efe782b46
@@ -193,12 +193,19 @@ test_win_group3: setup
|
||||
ansible-playbook test_win_group3.yml -i inventory.winrm -e outputdir=$(TEST_DIR) -e @$(VARS_FILE) $(CREDENTIALS_ARG) -v $(TEST_FLAGS)
|
||||
|
||||
test_tags: setup
|
||||
# Run these using en_US.UTF-8 because list-tasks is a user output function and so it tailors its output to the user's locale. For unicode tags, this means replacing non-ascii chars with "?"
|
||||
# Run everything by default
|
||||
[ "$$(ansible-playbook --list-tasks test_tags.yml -i $(INVENTORY) -e outputdir=$(TEST_DIR) -e @$(VARS_FILE) $(CREDENTIALS_ARG) -v | fgrep Task_with | xargs)" = "Task_with_tag TAGS: [tag] Task_with_always_tag TAGS: [always] Task_without_tag TAGS: []" ]
|
||||
[ "$$(LC_ALL=en_US.UTF-8 ansible-playbook --list-tasks test_tags.yml -i $(INVENTORY) -e outputdir=$(TEST_DIR) -e @$(VARS_FILE) $(CREDENTIALS_ARG) -v | fgrep Task_with | xargs)" = "Task_with_tag TAGS: [tag] Task_with_always_tag TAGS: [always] Task_with_unicode_tag TAGS: [くらとみ] Task_with_list_of_tags TAGS: [café, press] Task_without_tag TAGS: []" ]
|
||||
# Run the exact tags, and always
|
||||
[ "$$(ansible-playbook --list-tasks --tags tag test_tags.yml -i $(INVENTORY) -e outputdir=$(TEST_DIR) -e @$(VARS_FILE) $(CREDENTIALS_ARG) -v | fgrep Task_with | xargs)" = "Task_with_tag TAGS: [tag] Task_with_always_tag TAGS: [always]" ]
|
||||
[ "$$(LC_ALL=en_US.UTF-8 ansible-playbook --list-tasks --tags tag test_tags.yml -i $(INVENTORY) -e outputdir=$(TEST_DIR) -e @$(VARS_FILE) $(CREDENTIALS_ARG) -v | fgrep Task_with | xargs)" = "Task_with_tag TAGS: [tag] Task_with_always_tag TAGS: [always]" ]
|
||||
# Skip one tag
|
||||
[ "$$(ansible-playbook --list-tasks --skip-tags tag test_tags.yml -i $(INVENTORY) -e outputdir=$(TEST_DIR) -e @$(VARS_FILE) $(CREDENTIALS_ARG) -v | fgrep Task_with | xargs)" = "Task_with_always_tag TAGS: [always] Task_without_tag TAGS: []" ]
|
||||
[ "$$(LC_ALL=en_US.UTF-8 ansible-playbook --list-tasks --skip-tags tag test_tags.yml -i $(INVENTORY) -e outputdir=$(TEST_DIR) -e @$(VARS_FILE) $(CREDENTIALS_ARG) -v | fgrep Task_with | xargs)" = "Task_with_always_tag TAGS: [always] Task_with_unicode_tag TAGS: [くらとみ] Task_with_list_of_tags TAGS: [café, press] Task_without_tag TAGS: []" ]
|
||||
# Skip a unicode tag
|
||||
[ "$$(LC_ALL=en_US.UTF-8 ansible-playbook --list-tasks --skip-tags くらとみ test_tags.yml -i $(INVENTORY) -e outputdir=$(TEST_DIR) -e @$(VARS_FILE) $(CREDENTIALS_ARG) -v | fgrep Task_with | xargs)" = "Task_with_tag TAGS: [tag] Task_with_always_tag TAGS: [always] Task_with_list_of_tags TAGS: [café, press] Task_without_tag TAGS: []" ]
|
||||
# Run just a unicode tag and always
|
||||
[ "$$(LC_ALL=en_US.UTF-8 ansible-playbook --list-tasks --tags くらとみ test_tags.yml -i $(INVENTORY) -e outputdir=$(TEST_DIR) -e @$(VARS_FILE) $(CREDENTIALS_ARG) -v | fgrep Task_with | xargs)" = "Task_with_always_tag TAGS: [always] Task_with_unicode_tag TAGS: [くらとみ]" ]
|
||||
# Run a tag from a list of tags and always
|
||||
[ "$$(LC_ALL=en_US.UTF-8 ansible-playbook --list-tasks --tags café test_tags.yml -i $(INVENTORY) -e outputdir=$(TEST_DIR) -e @$(VARS_FILE) $(CREDENTIALS_ARG) -v | fgrep Task_with | xargs)" = "Task_with_always_tag TAGS: [always] Task_with_list_of_tags TAGS: [café, press]" ]
|
||||
|
||||
blocks: setup
|
||||
# remove old output log
|
||||
|
||||
@@ -10,6 +10,14 @@
|
||||
- name: Task_with_always_tag
|
||||
debug: msg=
|
||||
tags: always
|
||||
- name: Task_with_unicode_tag
|
||||
debug: msg=
|
||||
tags: くらとみ
|
||||
- name: Task_with_list_of_tags
|
||||
debug: msg=
|
||||
tags:
|
||||
- café
|
||||
- press
|
||||
- name: Task_without_tag
|
||||
debug: msg=
|
||||
|
||||
|
||||
@@ -154,12 +154,11 @@ class TestGalaxy(unittest.TestCase):
|
||||
|
||||
def run_parse_common(self, galaxycli_obj, action):
|
||||
with patch.object(ansible.cli.SortedOptParser, "set_usage") as mocked_usage:
|
||||
galaxy_parser = galaxycli_obj.parse()
|
||||
galaxycli_obj.parse()
|
||||
|
||||
# checking that the common results of parse() for all possible actions have been created/called
|
||||
self.assertTrue(galaxy_parser)
|
||||
self.assertTrue(isinstance(galaxycli_obj.parser, ansible.cli.SortedOptParser))
|
||||
self.assertTrue(isinstance(galaxycli_obj.galaxy, ansible.galaxy.Galaxy))
|
||||
self.assertIsInstance(galaxycli_obj.parser, ansible.cli.SortedOptParser)
|
||||
self.assertIsInstance(galaxycli_obj.galaxy, ansible.galaxy.Galaxy)
|
||||
if action in ['import', 'delete']:
|
||||
formatted_call = 'usage: %prog ' + action + ' [options] github_user github_repo'
|
||||
elif action == 'info':
|
||||
@@ -194,61 +193,61 @@ class TestGalaxy(unittest.TestCase):
|
||||
# testing action 'delete'
|
||||
gc = GalaxyCLI(args=["delete", "-c"])
|
||||
self.run_parse_common(gc, "delete")
|
||||
self.assertTrue(gc.options.verbosity==0)
|
||||
self.assertEqual(gc.options.verbosity, 0)
|
||||
|
||||
# testing action 'import'
|
||||
gc = GalaxyCLI(args=["import", "-c"])
|
||||
self.run_parse_common(gc, "import")
|
||||
self.assertTrue(gc.options.wait==True)
|
||||
self.assertTrue(gc.options.reference==None)
|
||||
self.assertTrue(gc.options.check_status==False)
|
||||
self.assertTrue(gc.options.verbosity==0)
|
||||
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)
|
||||
|
||||
# testing action 'info'
|
||||
gc = GalaxyCLI(args=["info", "-c"])
|
||||
self.run_parse_common(gc, "info")
|
||||
self.assertTrue(gc.options.offline==False)
|
||||
self.assertEqual(gc.options.offline, False)
|
||||
|
||||
# testing action 'init'
|
||||
gc = GalaxyCLI(args=["init", "-c"])
|
||||
self.run_parse_common(gc, "init")
|
||||
self.assertTrue(gc.options.offline==False)
|
||||
self.assertTrue(gc.options.force==False)
|
||||
self.assertEqual(gc.options.offline, False)
|
||||
self.assertEqual(gc.options.force, False)
|
||||
|
||||
# testing action 'install'
|
||||
gc = GalaxyCLI(args=["install", "-c"])
|
||||
self.run_parse_common(gc, "install")
|
||||
self.assertTrue(gc.options.ignore_errors==False)
|
||||
self.assertTrue(gc.options.no_deps==False)
|
||||
self.assertTrue(gc.options.role_file==None)
|
||||
self.assertTrue(gc.options.force==False)
|
||||
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)
|
||||
|
||||
# testing action 'list'
|
||||
gc = GalaxyCLI(args=["list", "-c"])
|
||||
self.run_parse_common(gc, "list")
|
||||
self.assertTrue(gc.options.verbosity==0)
|
||||
self.assertEqual(gc.options.verbosity, 0)
|
||||
|
||||
# testing action 'login'
|
||||
gc = GalaxyCLI(args=["login", "-c"])
|
||||
self.run_parse_common(gc, "login")
|
||||
self.assertTrue(gc.options.verbosity==0)
|
||||
self.assertTrue(gc.options.token==None)
|
||||
self.assertEqual(gc.options.verbosity, 0)
|
||||
self.assertEqual(gc.options.token, None)
|
||||
|
||||
# testing action 'remove'
|
||||
gc = GalaxyCLI(args=["remove", "-c"])
|
||||
self.run_parse_common(gc, "remove")
|
||||
self.assertTrue(gc.options.verbosity==0)
|
||||
self.assertEqual(gc.options.verbosity, 0)
|
||||
|
||||
# testing action 'search'
|
||||
gc = GalaxyCLI(args=["search", "-c"])
|
||||
self.run_parse_common(gc, "search")
|
||||
self.assertTrue(gc.options.platforms==None)
|
||||
self.assertTrue(gc.options.tags==None)
|
||||
self.assertTrue(gc.options.author==None)
|
||||
self.assertEqual(gc.options.platforms, None)
|
||||
self.assertEqual(gc.options.galaxy_tags, None)
|
||||
self.assertEqual(gc.options.author, None)
|
||||
|
||||
# testing action 'setup'
|
||||
gc = GalaxyCLI(args=["setup", "-c"])
|
||||
self.run_parse_common(gc, "setup")
|
||||
self.assertTrue(gc.options.verbosity==0)
|
||||
self.assertTrue(gc.options.remove_id==None)
|
||||
self.assertTrue(gc.options.setup_list==False)
|
||||
self.assertEqual(gc.options.verbosity, 0)
|
||||
self.assertEqual(gc.options.remove_id, None)
|
||||
self.assertEqual(gc.options.setup_list, False)
|
||||
|
||||
Reference in New Issue
Block a user