Skip to content

Commit

Permalink
make mh commands, specifically ghost inspector tests, work in context…
Browse files Browse the repository at this point in the history
… of mh-opsworks

includes:
* removed .mh-ui-testing config file stuff; most options can be derived
  from the mh-opsworks cluster env
* `gi` commands now have explicit options rather than doing a
  pass-through to py.test
* top-level `mh` command now takes a `--working-dir` option and will chdir if specified
  • Loading branch information
lbjay committed May 4, 2016
1 parent c0e5f70 commit 1fe810e
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 70 deletions.
17 changes: 7 additions & 10 deletions mh_cli/__init__.py
@@ -1,21 +1,18 @@
__version__ = '0.9.4'
__version__ = '0.10.0'

import os
import click
click.disable_unicode_literals_warning = True

from mh_cli.common import ClickState, config_options, pass_state
from mh_cli.common import ClickState, pass_state

@click.group()
@click.option('--working-dir', help='change to this dir before executing cmds')
@click.pass_context
def cli(ctx):
def cli(ctx, working_dir):
ctx.ensure_object(ClickState)

@cli.command()
@config_options
@pass_state
def config(state):
"""Save typing by setting common options in a config file"""
state.save_config()
if working_dir is not None:
os.chdir(working_dir)

from gi import gi
from inbox import inbox
Expand Down
46 changes: 1 addition & 45 deletions mh_cli/common.py
Expand Up @@ -22,42 +22,7 @@ def __init__(self):
self.ssh_user = None
self.driver = 'firefox'
self.inbox_path = '/var/matterhorn/inbox'
self.conf_file = os.path.join(os.path.expanduser('~'), '.mh-ui-testing')
self.load_config()

def load_config(self):
self.config = ConfigParser.ConfigParser()

if os.path.isfile(self.conf_file):
self.config.read(self.conf_file)
if self.config.has_section('mh'):
# load settings into envvars to be picked up by click options
for k, v in self.config.items('mh'):
varname = 'MHUIT_%s' % k.upper()
# not if empty or already set
if v and varname not in os.environ:
os.environ[varname] = v
else:
self.config.add_section('mh')
self.config.add_section('pytest')
self.config.set('pytest', 'testpaths', 'gi_tests')
self.config.set('pytest', 'addopts', '')
with open(self.conf_file, 'wb') as fh:
self.config.write(fh)

def save_config(self):
for opt in [
'username',
'password',
'host',
'driver',
'inbox_path',
'ssh_user'
]:
if hasattr(self, opt) and getattr(self, opt) is not None:
self.config.set('mh', opt, getattr(self, opt))
with open(self.conf_file, 'wb') as fh:
self.config.write(fh)
self.working_dir = None

@property
def base_url(self):
Expand Down Expand Up @@ -139,15 +104,6 @@ def inbox_options(f):
f = inbox_path_option(f)
return f

def config_options(f):
f = password_option(f, required=False)
f = username_option(f, required=False)
f = host_option(f, required=False)
f = driver_option(f)
f = user_option(f)
f = inbox_path_option(f)
return f

def init_fabric(click_cmd):
@wraps(click_cmd)
def wrapped(state, *args, **kwargs):
Expand Down
101 changes: 86 additions & 15 deletions mh_cli/gi.py
@@ -1,29 +1,100 @@
import click
from mh_cli import cli
from mh_cli.common import pass_state
from mh_cli.common import pass_state, host_option, state_callback

from os.path import dirname
from fabric.api import local, lcd
from urlparse import urlunparse
from fabric.api import local, lcd, settings, quiet, abort, hide

def test_option(f):
return click.option('--test',
expose_value=False,
help='ID of a ghost inspector test',
multiple=True,
callback=state_callback)(f)

def suite_option(f):
return click.option('--suite',
expose_value=False,
help='ID of a ghost inspector suite',
multiple=True,
callback=state_callback)(f)

def key_option(f):
return click.option('--key',
expose_value=False,
help='ghost inspector API key',
envvar='GI_API_KEY',
callback=state_callback)(f)

def var_option(f):
return click.option('--var',
expose_value=False,
help='extra test variable(s); repeatable',
multiple=True,
callback=state_callback)(f)

def path_option(f):
return click.option('--path',
expose_value=False,
help='url path to add to base start url',
default='',
callback=state_callback)(f)

def protocol_option(f):
return click.option('--protocol',
expose_value=False,
help='defaults to http',
default='http',
callback=state_callback)(f)

def gi_list_options(f):
f = test_option(f)
f = suite_option(f)
f = key_option(f)
return f

def gi_exec_options(f):
f = gi_list_options(f)
f = var_option(f)
f = path_option(f)
f = host_option(f, required=True)
f = protocol_option(f)
return f

@cli.group()
def gi():
"""Do stuff with Ghost Inspector tests"""

@gi.command(name='list', context_settings=dict(ignore_unknown_options=True))
@click.argument('gi_args', nargs=-1, type=click.UNPROCESSED)
@gi.command(name='list')
@gi_list_options
@pass_state
def gi_list(state, gi_args):
def gi_list(state):
"""Collect and list available tests"""
cmd = "py.test -c %s --verbose --collect-only %s" % (state.conf_file, " ".join(gi_args))
with(lcd(dirname(dirname(__file__)))):
local(cmd)
params = ['--gi_key %s' % state.key]
if state.test:
params.extend('--gi_test %s' % x for x in state.test)
if state.suite:
params.extend('--gi_suite %s' % x for x in state.suite)
_pytest_cmd('py.test --collect-only ' + ' '.join(params))


@gi.command(name='exec', context_settings=dict(ignore_unknown_options=True))
@click.argument('gi_args', nargs=-1, type=click.UNPROCESSED)
@gi.command(name='exec')
@gi_exec_options
@pass_state
def gi_exec(state, gi_args):
def gi_exec(state):
"""Execute tests"""
cmd = "py.test -c %s --verbose %s" % (state.conf_file, " ".join(gi_args))
with(lcd(dirname(dirname(__file__)))):
start_url = urlunparse((state.protocol, state.host, state.path, '', '', ''))
params = [
'--gi_start_url %s' % start_url,
'--gi_key %s' % state.key
]
if state.test:
params.extend('--gi_test %s' % x for x in state.test)
if state.suite:
params.extend('--gi_suite %s' % x for x in state.suite)
if state.var:
params.extend('--gi_param %s' % x for x in state.var)
_pytest_cmd('py.test ' + ' '.join(params))

def _pytest_cmd(cmd):
with settings(hide('running', 'warnings'), warn_only=True):
local(cmd)

0 comments on commit 1fe810e

Please sign in to comment.