def execute(cmd, raise_error=True): """Executes a command in a subprocess. Returns a tuple of (exitcode, out, err), where out is the string output from stdout and err is the string output from stderr when executing the command. :param cmd: Command string to execute :param raise_error: If returncode is not 0 (success), then raise a RuntimeError? Default: True) """ env = os.environ.copy() # Make sure that we use the programs in the # current source directory's bin/ directory. env['PATH'] = melange.melange_bin_path() + ':' + env['PATH'] process = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) result = process.communicate() (out, err) = result exitcode = process.returncode if process.returncode != 0 and raise_error: msg = "Command %(cmd)s did not succeed. Returned an exit "\ "code of %(exitcode)d."\ "\n\nSTDOUT: %(out)s"\ "\n\nSTDERR: %(err)s" % locals() raise RuntimeError(msg) return exitcode, out, err
def test_deallocated_ips_get_deleted(self): block = factory_models.PublicIpBlockFactory() ip = factory_models.IpAddressFactory(ip_block_id=block.id) block.deallocate_ip(ip.address) days = config.Config.get('keep_deallocated_ips_for_days') self._push_back_deallocated_date(ip, days) script = melange.melange_bin_path('melange-delete-deallocated-ips') config_file = functional.test_config_file() functional.execute("{0} --config-file={1}".format(script, config_file)) self.assertIsNone(models.IpAddress.get(ip.id))
def run_melange_manage(command): melange_manage = melange.melange_bin_path('melange-manage') config_file = functional.test_config_file() return functional.execute("%(melange_manage)s %(command)s " "--config-file=%(config_file)s" % locals())