import os
import time
from fabric.api import task, local, env
from common import timed, virtual
from common import logger as log
from tempest import prepare_devstack, run_tests, run_remote_tests
from fabs import LAB, IMAGES_REPO, DEVSTACK_DISK, GLOBAL_TIMEOUT, DEFAULT_SETTINGS
from snap import destroy, create

env.update(DEFAULT_SETTINGS)

__all__ = ['prepare', 'install', 'setup',
           'run_test_original_file', 'run_test_custom_file',
           'run_test_ready_file', 'run_test_remote',
           'snapshot_create']


@task
@timed
@virtual
def prepare(topology='devstack'):
    ''' Prepare VMs of specific topology for Openstack '''
    log.info("Preparing boxes for %s Openstack" % topology)
    log.info("Preparing virtual machines for lab=%s" % LAB)
    url = IMAGES_REPO + DEVSTACK_DISK
    local("test -e %s || wget -nv %s" % (DEVSTACK_DISK, url))
    local("python ./tools/cloud/create.py  -l {lab} -s /opt/imgs "
          "-z ./{disk} -t {topo} > config_file".format(lab=LAB,
                                                       disk=DEVSTACK_DISK,
                                                       topo=topology))


@task
@timed
@virtual
def install(user='localadmin', password='ubuntu'):
    ''' Install devstack Openstack on prepared environment '''
    log.info("Installing devstack Openstack")
    tempest_repo = os.environ.get("TEMPEST_REPO", "")
    tempest_br = os.environ.get("TEMPEST_BRANCH", "")
    devstack_repo = os.environ.get("DEVSTACK_REPO", "")
    devstack_br = os.environ.get("DEVSTACK_BRANCH", "")
    local("python ./tools/deployers/install_devstack.py "
          "-c config_file  -u {user} -p {password} -r {repo} -b {br} "
          "-e {devstack_repo} -l {devstack_br}".format(
        user=user,
        password=password,
        repo=tempest_repo,
        br=tempest_br,
        devstack_repo=devstack_repo,
        devstack_br=devstack_br,
        ))


@task
@timed
def setup(topology='devstack', user='localadmin', password='ubuntu'):
    ''' Prepare and install devstack Openstack '''
    log.info("Full install of devstack Openstack")
    prepare(topology=topology)
    time.sleep(GLOBAL_TIMEOUT)
    install(user=user, password=password)


@task(alias='orig')
def run_test_original_file(private=True):
    ''' Copy tempest configuration from devstack installation and run tests with it locally '''
    prepare_devstack(web=False, copy=True, remote=False, private=private)
    run_tests()


@task(alias='custom')
def run_test_custom_file(private=True):
    ''' Configure tempest on devstack with custom configuration and run tests with newly created file locally '''
    prepare_devstack(web=True, copy=False, remote=False, private=private)
    run_tests()


@task(alias='ready')
def run_test_ready_file(private=True):
    ''' Use existing tempest configuration file in current directory and run tests with it locally '''
    prepare_devstack(web=False, copy=False, remote=False, private=private)
    run_tests()


@task(alias='remote')
def run_test_remote(private=True):
    ''' Use existing tempest configuration file on devstack box and run tests with it remotely '''
    prepare_devstack(web=False, copy=False, remote=True, private=private)
    run_remote_tests()


@task
@timed
def snapshot_create():
    ''' Create snapshot for devstack '''
    destroy()
    setup()
    create()

@@ -54,16 +54,35 @@
 cal, env
+, settings, run, cd
 %0Afrom co
@@ -107,16 +107,31 @@
  virtual
+, get_lab_vm_ip
 %0Afrom co
@@ -553,16 +553,42 @@
 _create'
+, 'set_branch', 'patchset'
 %5D%0A%0A%0A@tas
@@ -3399,8 +3399,1327 @@
 reate()%0A
+%0A@task%0A@timed%0Adef set_branch(component=%22neutron%22, branch=%22master%22):%0A    ''' Set Openstack component for particular branch or commit '''%0A    ip = get_lab_vm_ip()%0A    with settings(host_string=ip, abort_on_prompts=True, warn_only=True):%0A        stack_file = '~/devstack/stack-screenrc'%0A        run(%22screen -S stack -X quit%22)%0A        path = os.path.join(%22/opt%22, %22stack%22, component)%0A        with cd(path):%0A            run(%22git fetch --all; git checkout %7Bbr%7D%22.format(br=branch))%0A        run(%22screen -c %7B0%7D -d -m && sleep 1%22.format(stack_file))%0A%0A@task%0A@timed%0Adef patchset(component=%22neutron%22, patchset=None):%0A    ''' Set Openstack component for particular patchset of Gerrit '''%0A    ip = get_lab_vm_ip()%0A    if not patchset:%0A        raise Exception(%22Please provide patchset as 'refs/changes/44/129144/1'%22)%0A    with settings(host_string=ip, abort_on_prompts=True, warn_only=True):%0A        stack_file = '~/devstack/stack-screenrc'%0A        run(%22screen -S stack -X quit%22)%0A        path = os.path.join(%22/opt%22, %22stack%22, component)%0A        with cd(path):%0A            run(%22git fetch https://review.openstack.org/openstack/%7Bproject%7D%22%0A                %22 %7Bpatchset%7D && git checkout FETCH_HEAD%22.format(%0A                project=component,%0A                patchset=patchset))%0A        run(%22screen -c %7B0%7D -d -m && sleep 1%22.format(stack_file))%0A

Support suctom branch and patchset testing