<commit_before>#!/usr/bin/env python2
from __future__ import print_function
from subprocess import call, check_call, check_output
import logging
import os
import subprocess
import time
from datetime import datetime
from behave import *
from cluster_under_test import *
from tasks import init_pg_servers_play_run
from execution import run_remotely

def run_ansible(context, more_vars=None):
    """Run ansible playbook for provided inventory file and redirect output
    including colors to a separate file. Run

    tail -f /tmp/detailed-test-output.txt

    in a separate terminal to observe ansible progress.
    """
    cmd = init_pg_servers_play_run(ClusterUnderTest.zone,
           ClusterUnderTest.db_instance_name, more_vars)
    run_with_details(cmd)

def run_with_details(cmd):
    """Execute command via shell, pretend to be a psedo-tty and save colorized output
    to a separate file for later analysis. Run

    tail -f /tmp/detailed-test-output.txt

    in a separate terminal to observe progress.
 
    """
    logging.info(cmd)
    check_call("""script -e -f -q /tmp/detailed-test-output.txt -c "{}" """.format(cmd),
            shell=True, stdout=open(os.devnull, 'w'))

@when(u'I initialize postgres cluster to {goal}')
def init_pg_servers(context, goal):
    run_ansible(context)

@when(u'I restore backup to a postgres cluster')
def step_impl(context):
    restore_step(context, pit=False)

@when(u'I run full backup')
def step_impl(context):
    run_remotely(host=ClusterUnderTest.INITIAL_MASTER, timeout=2592000, # wait forever
            command="sudo -u postgres /var/local/postgresql/full_backup_to_aws.sh >>/var/local/postgresql/log/backup.log 2>&1")

@when(u'I restore backup to a postgres cluster with remembered PIT')
def restore_step(context, pit=True):
    folder_latest = check_output("pyenv/bin/invoke restore_cluster {} {} | tail -1".format(
        ClusterUnderTest.zone, ClusterUnderTest.db_instance_name), shell=True)
    cmd = "pyenv/bin/invoke restore_cluster {} {} --backup-folder='{}'".format(
        ClusterUnderTest.zone, ClusterUnderTest.db_instance_name,
        folder_latest.strip())
    if pit:
        cmd += " --target-time '{}'".format(context.time_for_pit_recovery)
    run_with_details(cmd)

@when(u'I memorize current time for later PIT recovery')
def step_impl(context):<commit_msg>Make PIT recovery test more reliable<commit_after>#!/usr/bin/env python2
from __future__ import print_function
from subprocess import call, check_call, check_output
import logging
import os
import subprocess
import time
from datetime import datetime
from behave import *
from cluster_under_test import *
from tasks import init_pg_servers_play_run
from execution import run_remotely

def run_ansible(context, more_vars=None):
    """Run ansible playbook for provided inventory file and redirect output
    including colors to a separate file. Run

    tail -f /tmp/detailed-test-output.txt

    in a separate terminal to observe ansible progress.
    """
    cmd = init_pg_servers_play_run(ClusterUnderTest.zone,
           ClusterUnderTest.db_instance_name, more_vars)
    run_with_details(cmd)

def run_with_details(cmd):
    """Execute command via shell, pretend to be a psedo-tty and save colorized output
    to a separate file for later analysis. Run

    tail -f /tmp/detailed-test-output.txt

    in a separate terminal to observe progress.
 
    """
    logging.info(cmd)
    check_call("""script -e -f -q /tmp/detailed-test-output.txt -c "{}" """.format(cmd),
            shell=True, stdout=open(os.devnull, 'w'))

@when(u'I initialize postgres cluster to {goal}')
def init_pg_servers(context, goal):
    run_ansible(context)

@when(u'I restore backup to a postgres cluster')
def step_impl(context):
    restore_step(context, pit=False)

@when(u'I run full backup')
def step_impl(context):
    run_remotely(host=ClusterUnderTest.INITIAL_MASTER, timeout=2592000, # wait forever
            command="sudo -u postgres /var/local/postgresql/full_backup_to_aws.sh >>/var/local/postgresql/log/backup.log 2>&1")

@when(u'I restore backup to a postgres cluster with remembered PIT')
def restore_step(context, pit=True):
    folder_latest = check_output("pyenv/bin/invoke restore_cluster {} {} | tail -1".format(
        ClusterUnderTest.zone, ClusterUnderTest.db_instance_name), shell=True)
    cmd = "pyenv/bin/invoke restore_cluster {} {} --backup-folder='{}'".format(
        ClusterUnderTest.zone, ClusterUnderTest.db_instance_name,
        folder_latest.strip())
    if pit:
        cmd += " --target-time '{}'".format(context.time_for_pit_recovery)
    run_with_details(cmd)

@when(u'I memorize current time for later PIT recovery')
def step_impl(context):
    time.sleep(1) # let db operations finish and commit; round up to a second
{'fraction_non_alphanumeric': 0.10144321271700481, 'fraction_numerical': 0.004810709056682703, 'mean_word_length': 4.2319474835886215, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2544988', 'n_tokens_mistral': 1502, 'n_tokens_neox': 1465, 'n_words': 414}