def postgres_command_step_impl(context, config_name): """ :type context: behave.runner.Context :param config_name: string """ target_config = context.simulation.targets['postgres'][config_name] if target_config is None: raise Exception(f'target {config_name} not found') sql = context.text sql = sql.replace('"', '\\"') sql = replace_placeholders(context.variables, sql) shell = f'PGPASSWORD="******"password"]}" ' \ f'psql -h {target_config["host"]} -p {target_config["port"]} ' \ f'-U {target_config["user"]} ' \ f'-d {target_config["name"]} ' \ f'-c "{sql}"' print(f'$ {shell}') process = subprocess.Popen(shell, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
def redis_command_step_impl(context, config_name, command): """ :type context: behave.runner.Context :type config_name: str :type command: str """ command = replace_placeholders(context.variables, command) target_config = context.simulation.targets['redis'][config_name] if target_config is None: raise Exception(f'target {config_name} not found') shell = f'redis-cli -h {target_config["host"]} -p {target_config["port"]} -a \'{target_config["password"]}\' {command}' print(f'$ {shell}') process = subprocess.Popen(shell, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = read_process(process) process.wait() assert process.returncode == 0, f'Unexpected code {process.returncode}, stderr: {stderr}' print(stdout)
def redis_assert_keys_exists(context, config_name, pattern): """ :type context: behave.runner.Context :type config_name: str :type pattern: str :return: """ pattern = replace_placeholders(context.variables, pattern) target_config = context.simulation.targets['redis'][config_name] if target_config is None: raise Exception(f'target {config_name} not found') shell = f'redis-cli ' \ f'-h {target_config["host"]} -p {target_config["port"]} ' \ f'-a {target_config["password"]} ' \ f'KEYS \'{pattern}\'' print(f'$ {shell}') process = subprocess.Popen(shell, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = read_process(process) process.wait() assert process.returncode == 0, f'Unexpected code {process.returncode}, stderr: {stderr}' print(stdout) assert len(stdout.splitlines()) > 0, f'any key not found'
def postgres_assets_command_return_certain_count_step_impl( context, config_name, count): """ :type context: behave.runner.Context :type config_name: str :type count: int """ target_config = context.simulation.targets['postgres'][config_name] if target_config is None: raise Exception(f'target {config_name} not found') sql = context.text sql = replace_placeholders(context.variables, sql) shell = f'PGPASSWORD="******"password"]}" ' \ f'psql -h {target_config["host"]} -p {target_config["port"]} ' \ f'-U {target_config["user"]} ' \ f'-d {target_config["name"]} ' \ f'-c "{sql}"' print(f'$ {shell}') process = subprocess.Popen(shell, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = read_process(process) process.wait() assert process.returncode == 0, f'Unexpected code {process.returncode}, stderr: {stderr}' print(stdout) output_lines_count = len(stdout.splitlines()) tuples_count = output_lines_count - 1 - 1 - 1 - 1 # minus headers, delimiter, status line, last empty line assert tuples_count == count, f'wrong rows count returned, expect {count}, actual {tuples_count}'