def test_run_command_baked_both_envs(mocker): run_mock = mocker.patch.object(util, "run") run_mock.return_value = mocker.Mock(returncode=0) cmd = util.BakedCommand(cmd=["ls"], env=dict(myvar="myvalue")) util.run_command(cmd, env=dict(myvar="myrealvalue")) # call_args[1] contains kwargs assert run_mock.call_args[1]["env"] == dict(myvar="myrealvalue")
def bake(self): """ Bake an ``ansible-galaxy`` command so it's ready to execute and returns \ None. :return: None """ options = self.options verbose_flag = util.verbose_flag(options) self._sh_command = util.BakedCommand( cmd=[self.command, *self.COMMANDS, *util.dict2args(options), *verbose_flag], env=self.env, )
def bake(self): """ Bake a ``testinfra`` command so it's ready to execute and returns None. :return: None """ options = self.options verbose_flag = util.verbose_flag(options) args = verbose_flag + self.additional_files_or_dirs self._testinfra_command = util.BakedCommand( cmd=["pytest", *util.dict2args(options), *self._tests, *args], cwd=self._config.scenario.directory, env=self.env, )
def bake(self): """ Bake an ``ansible-playbook`` command so it's ready to execute and \ returns ``None``. :return: None """ if not self._playbook: return # Pass a directory as inventory to let Ansible merge the multiple # inventory sources located under self.add_cli_arg("inventory", self._config.provisioner.inventory_directory) options = util.merge_dicts(self._config.provisioner.options, self._cli) verbose_flag = util.verbose_flag(options) if self._playbook != self._config.provisioner.playbooks.converge: if options.get("become"): del options["become"] # We do not pass user-specified Ansible arguments to the create and # destroy invocations because playbooks involved in those two # operations are not always provided by end users. And in those cases, # custom Ansible arguments can break the creation and destruction # processes. # # If users need to modify the creation of deletion, they can supply # custom playbooks and specify them in the scenario configuration. if self._config.action not in ["create", "destroy"]: ansible_args = list(self._config.provisioner.ansible_args) + list( self._config.ansible_args) else: ansible_args = [] self._ansible_command = util.BakedCommand( cmd=[ "ansible-playbook", *util.dict2args(options), *util.bool2args(verbose_flag), *ansible_args, self._playbook, # must always go last ], cwd=self._config.scenario.directory, env=self._env, )
def bake(self): """ Bake an ``ansible-playbook`` command so it's ready to execute and \ returns ``None``. :return: None """ if not self._playbook: return # Pass a directory as inventory to let Ansible merge the multiple # inventory sources located under self.add_cli_arg("inventory", self._config.provisioner.inventory_directory) options = util.merge_dicts(self._config.provisioner.options, self._cli) verbose_flag = util.verbose_flag(options) if self._playbook != self._config.provisioner.playbooks.converge: if options.get("become"): del options["become"] ansible_args = list(self._config.provisioner.ansible_args) + list( self._config.ansible_args) # if ansible_args: # if self._config.action not in ["create", "destroy"]: # # inserts ansible_args at index 1 # self._ansible_command.cmd.extend(ansible_args) self._ansible_command = util.BakedCommand( cmd=[ "ansible-playbook", *util.dict2args(options), *util.bool2args(verbose_flag), *ansible_args, self._playbook, # must always go last ], cwd=self._config.scenario.directory, env=self._env, stdout=self._out, stderr=self._err, )
def bake(self, name, host): """Prepare a command to run robot on a test instance.""" # The robot command line. robot_cmd = [ 'robot', *dict2args(self.robot_options), *self.data_sources # last ] LOG.info('robot command: %s' % ' '.join(robot_cmd)) ansible_connection = host.get('ansible_connection', 'ssh') if ansible_connection == 'docker': cmd = ['docker', 'exec', name, *robot_cmd] elif ansible_connection == 'ssh': ssh_host = host.get('ansible_host', name) ssh_user = host.get('ansible_user', None) ssh_port = host.get('ansible_port', None) ssh_ident = host.get('ansible_private_key_file', None) ssh_args = host.get('ansible_ssh_common_args', '').split() if ssh_port: ssh_args.extend(['-p', str(ssh_port)]) if ssh_ident: ssh_args.extend(['-i', ssh_ident]) ssh_dest = '@'.join([ssh_user, ssh_host]) if ssh_user else ssh_host LOG.info('ssh command: %s' % ' '.join(['ssh', *ssh_args, ssh_dest])) cmd = ['ssh', *ssh_args, ssh_dest, *robot_cmd] else: util.sysexit_with_message( f'Unsupported connection {ansible_connection}', 1) self._robot_command = util.BakedCommand( cmd=cmd, cwd=self._config.scenario.directory, env=self.env, )