def test_parse_invalid_action(self): ''' testing the options parser when an invalid action is given ''' gc = GalaxyCLI(args=["ansible-galaxy", "NOT_ACTION"]) self.assertRaises(SystemExit, gc.parse)
def test_display_min(self): gc = GalaxyCLI(args=self.default_args) role_info = {'name': 'some_role_name'} display_result = gc._display_role_info(role_info) self.assertTrue(display_result.find('some_role_name') > -1)
def test_parse_no_action(self): ''' testing the options parser when no action is given ''' gc = GalaxyCLI(args=["ansible-galaxy", ""]) self.assertRaises(SystemExit, gc.parse)
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)
def test_init(self): galaxy_cli = GalaxyCLI(args=self.default_args) self.assertTrue(isinstance(galaxy_cli, GalaxyCLI))
def test_parse_remove(self): ''' testing the options parser when the action 'remove' is given ''' gc = GalaxyCLI(args=["ansible-galaxy", "remove", "foo"]) gc.parse() self.assertEqual(context.CLIARGS['verbosity'], 0)
def run(self): super(PlaybookCLI, self).run() # Note: slightly wrong, this is written so that implicit localhost # Manage passwords sshpass = None becomepass = None passwords = {} if self.options.role_file: args = ["install"] if self.options.ignore_errors: args.append("--ignore-errors") force = "" if self.options.force: args.append("--force") if self.options.roles_path: args.extend(["--roles-path", self.options.roles_path]) if self.options.no_deps: args.append("--no-deps") args.extend(["--role-file", self.options.role_file]) gc = GalaxyCLI(args=args) gc.parse() gc.run() # initial error check, to make sure all specified playbooks are accessible # before we start running anything through the playbook executor for playbook in self.args: if not os.path.exists(playbook): raise AnsibleError("the playbook: %s could not be found" % playbook) if not (os.path.isfile(playbook) or stat.S_ISFIFO(os.stat(playbook).st_mode)): raise AnsibleError( "the playbook: %s does not appear to be a file" % playbook) # don't deal with privilege escalation or passwords when we don't need to if not self.options.listhosts and not self.options.listtasks and not self.options.listtags and not self.options.syntax: self.normalize_become_options() (sshpass, becomepass) = self.ask_passwords() passwords = {'conn_pass': sshpass, 'become_pass': becomepass} loader, inventory, variable_manager = self._play_prereqs(self.options) # (which is not returned in list_hosts()) is taken into account for # warning if inventory is empty. But it can't be taken into account for # checking if limit doesn't match any hosts. Instead we don't worry about # limit if only implicit localhost was in inventory to start with. # # Fix this when we rewrite inventory by making localhost a real host (and thus show up in list_hosts()) no_hosts = False if len(inventory.list_hosts()) == 0: # Empty inventory display.warning( "provided hosts list is empty, only localhost is available") no_hosts = True inventory.subset(self.options.subset) if len(inventory.list_hosts()) == 0 and no_hosts is False: # Invalid limit raise AnsibleError("Specified --limit does not match any hosts") # flush fact cache if requested if self.options.flush_cache: self._flush_cache(inventory, variable_manager) # create the playbook executor, which manages running the plays via a task queue manager pbex = PlaybookExecutor(playbooks=self.args, inventory=inventory, variable_manager=variable_manager, loader=loader, options=self.options, passwords=passwords) results = pbex.run() if isinstance(results, list): for p in results: display.display('\nplaybook: %s' % p['playbook']) for idx, play in enumerate(p['plays']): if play._included_path is not None: loader.set_basedir(play._included_path) else: pb_dir = os.path.realpath( os.path.dirname(p['playbook'])) loader.set_basedir(pb_dir) msg = "\n play #%d (%s): %s" % (idx + 1, ','.join( play.hosts), play.name) mytags = set(play.tags) msg += '\tTAGS: [%s]' % (','.join(mytags)) if self.options.listhosts: playhosts = set(inventory.get_hosts(play.hosts)) msg += "\n pattern: %s\n hosts (%d):" % ( play.hosts, len(playhosts)) for host in playhosts: msg += "\n %s" % host display.display(msg) all_tags = set() if self.options.listtags or self.options.listtasks: taskmsg = '' if self.options.listtasks: taskmsg = ' tasks:\n' def _process_block(b): taskmsg = '' for task in b.block: if isinstance(task, Block): taskmsg += _process_block(task) else: if task.action == 'meta': continue all_tags.update(task.tags) if self.options.listtasks: cur_tags = list( mytags.union(set(task.tags))) cur_tags.sort() if task.name: taskmsg += " %s" % task.get_name( ) else: taskmsg += " %s" % task.action taskmsg += "\tTAGS: [%s]\n" % ', '.join( cur_tags) return taskmsg all_vars = variable_manager.get_vars(play=play) play_context = PlayContext(play=play, options=self.options) for block in play.compile(): block = block.filter_tagged_tasks( play_context, all_vars) if not block.has_tasks(): continue taskmsg += _process_block(block) if self.options.listtags: cur_tags = list(mytags.union(all_tags)) cur_tags.sort() taskmsg += " TASK TAGS: [%s]\n" % ', '.join( cur_tags) display.display(taskmsg) return 0 else: return results
def test_parse_list(self): ''' testing the options parser when the action 'list' is given ''' gc = GalaxyCLI(args=["list"]) self.run_parse_common(gc, "list") self.assertEqual(gc.options.verbosity, 0)
def test_parse(self): ''' systematically testing that the expected options parser is created ''' # testing no action given gc = GalaxyCLI(args=["-c"]) self.assertRaises(AnsibleOptionsError, gc.parse) # testing action that doesn't exist gc = GalaxyCLI(args=["NOT_ACTION", "-c"]) self.assertRaises(AnsibleOptionsError, gc.parse) # testing action 'delete' gc = GalaxyCLI(args=["delete", "-c"]) self.run_parse_common(gc, "delete") self.assertTrue(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) # testing action 'info' gc = GalaxyCLI(args=["info", "-c"]) self.run_parse_common(gc, "info") self.assertTrue(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) # 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) # testing action 'list' gc = GalaxyCLI(args=["list", "-c"]) self.run_parse_common(gc, "list") self.assertTrue(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) # testing action 'remove' gc = GalaxyCLI(args=["remove", "-c"]) self.run_parse_common(gc, "remove") self.assertTrue(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) # 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)
def test_parse_delete(self): ''' testing the options parser when the action 'delete' is given ''' gc = GalaxyCLI(args=["delete"]) self.run_parse_common(gc, "delete") self.assertEqual(gc.options.verbosity, 0)
def test_parse_info(self): ''' testing the options parser when the action 'info' is given ''' gc = GalaxyCLI(args=["info"]) self.run_parse_common(gc, "info") self.assertEqual(gc.options.offline, False)
def test_parse_invalid_action(self): ''' testing the options parser when an invalid action is given ''' gc = GalaxyCLI(args=["NOT_ACTION"]) self.assertRaises(AnsibleOptionsError, gc.parse)
def test_parse_no_action(self): ''' testing the options parser when no action is given ''' gc = GalaxyCLI(args=[""]) self.assertRaises(AnsibleOptionsError, gc.parse)
def test_call_GalaxyCLI(execute_verify): galaxy_args = ['ansible-galaxy', 'collection', 'verify', 'namespace.collection'] GalaxyCLI(args=galaxy_args).run() assert execute_verify.call_count == 1
def test_parse_init(self): ''' testing the options parser when the action 'init' is given ''' gc = GalaxyCLI(args=["ansible-galaxy", "init", "foo"]) gc.parse() self.assertEqual(context.CLIARGS['offline'], False) self.assertEqual(context.CLIARGS['force'], 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)
def test_parse_login(self): ''' testing the options parser when the action 'login' is given ''' gc = GalaxyCLI(args=["ansible-galaxy", "login"]) gc.parse() self.assertEqual(context.CLIARGS['verbosity'], 0) self.assertEqual(context.CLIARGS['token'], None)
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)
def test_invalid_collection_name(name): expected = "Invalid collection name, must be in the format <namespace>.<collection>" gc = GalaxyCLI(args=['ansible-galaxy', 'collection', 'init', name]) with pytest.raises(AnsibleError, match=expected): gc.run()
ansible_vars_path = os.path.join(tmpdir, 'vars.yml') inventory_path = os.path.join(tmpdir, 'inventory') if (options.no_ansible_galaxy and options.no_ansible): ansible_vars_path = None inventory_path = None vars_file = conf.write_ansible_vars(dstname=ansible_vars_path) inventory_file = conf.write_ansible_inventory(dstname=inventory_path) exit_code = 0 if not options.no_ansible_galaxy: print("[+] launching ansible-galaxy") default_opts = ["ansible-galaxy"] default_opts += ["install"] default_opts += ["-r", "ansible-requirements.yml"] default_opts += ["--force"] galaxy_cli = GalaxyCLI(default_opts) galaxy_cli.parse() exit_code = galaxy_cli.run() if exit_code: clean_and_exit(tmpdir, exit_code) if not options.no_ansible: print("[+] launching ansible-playbook") default_opts = ["ansible-playbook"] default_opts += ["-i", "default_groups"] default_opts += ["-i", inventory_file] default_opts += ["-e", "@{}".format(vars_file)] if '-u' not in options.ansible_args: default_opts += ["-u", "vagrant"] if options.offline: default_opts += [