Skip to content
This repository has been archived by the owner on Sep 23, 2020. It is now read-only.

Commit

Permalink
adding the beginning of the test framework
Browse files Browse the repository at this point in the history
  • Loading branch information
BuzzTroll committed Oct 15, 2010
1 parent 1060f06 commit 08a2b8f
Show file tree
Hide file tree
Showing 14 changed files with 393 additions and 11 deletions.
136 changes: 135 additions & 1 deletion src/python/epumgmt/api/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,135 @@
# See TODO.txt
# See TODO.txt
import epumgmt.main.em_args as em_args
import epumgmt.main.em_core as em_core
from epumgmt.api.exceptions import *
import string
import sys
import time

import logging

from epumgmt.api.exceptions import *
from epumgmt.main import get_class_by_keyword, get_all_configs
from epumgmt.main import Modules, ACTIONS
import epumgmt.main.em_core_creation as em_core_creation
import epumgmt.main.em_core_eventgather as em_core_eventgather
import epumgmt.main.em_core_fetchkill as em_core_fetchkill
import epumgmt.main.em_core_findworkers as em_core_findworkers
import epumgmt.main.em_core_logfetch as em_core_logfetch
import epumgmt.main.em_core_persistence as em_core_persistence
import epumgmt.main.em_core_status as em_core_status
import epumgmt.main.em_core_termination as em_core_termination


control_args = {}
for k in em_args.ALL_EC_ARGS_LIST:
control_args[k.name] = k

class EPUMgmtOpts(object):

def __init__(self, name=None, conf_file=None, cmd_opts=None):
"""
Create an option set. All parameters are default except for
the mandatory ones which are required to create the object.
Alternatively the values in this object can be populated with a
command line option parsed structure.
"""
# create a dictionary of command line options.
self.conf = conf_file
self.name = name

if cmd_opts:
for k in control_args:
ca = control_args[k]
ca.value = cmd_opts.__dict__[k]

if self.conf == None:
msg = "The conf_file argument is required"
raise InvalidInput(msg)
if self.name == None:
msg = "The name argument is required"
raise InvalidInput(msg)

def __setattr__(self, name, value):
if name not in control_args:
raise InvalidInput("No such option %s" % (name))
ca = control_args[name]
ca.value = value

def __getattr__(self, name):
if name not in control_args:
raise InvalidInput("No such option %s" % (name))
ca = control_args[name]
return ca.value

class EPUMgmtAction(object):

def __init__(self, opts):
ac = get_all_configs(opts.conf)

p_cls = get_class_by_keyword("Parameters", allconfigs=ac)
self.p = p_cls(ac, opts)

c_cls = get_class_by_keyword("Common", allconfigs=ac)
self.c = c_cls(self.p)

event_gather_cls = self.c.get_class_by_keyword("EventGather")
event_gather = event_gather_cls(self.p, self.c)

iaas_cls = self.c.get_class_by_keyword("IaaS")
iaas = iaas_cls(self.p, self.c)

persistence = em_core_persistence.Persistence(self.p, self.c)

runlogs_cls = self.c.get_class_by_keyword("Runlogs")
runlogs = runlogs_cls(self.p, self.c)

services_cls = self.c.get_class_by_keyword("Services")
services = services_cls(self.p, self.c)

event_gather.validate()
iaas.validate()
persistence.validate()
runlogs.validate()
services.validate()

self.m = Modules(event_gather, iaas, persistence, runlogs, services)

def set_logfile(self, fname):

self.c.log = logging.getLogger("epu_test_logger")

def create(self, runname, haservice):
return em_core_creation.create(self.p, self.c, self.m, runname)

def update(self, runname):
return em_core_eventgather.update_events(self.p, self.c, self.m, runname)

def kill(self, runname):
try:
em_core_findworkers.find(self.p, self.c, self.m, ACTIONS.KILLRUN, runname, once=True)
em_core_logfetch.fetch_all(self.p, self.c, self.m, runname)
except KeyboardInterrupt:
raise
except:
self.c.log.exception("Fetch failed, moving on to terminate anyhow")
return em_core_termination.terminate(self.p, self.c, self.m, runname)


def fetch_kill(self, runname):
em_core_findworkers.find(self.p, self.c, self.m, ACTIONS.FETCH_KILL, runname, once=True)
em_core_fetchkill.fetch_kill(self.p, self.c, self.m, runname)

def logfetch(self, runname):
return em_core_logfetch.fetch_all(self.p, self.c, self.m, runname)

def findworkers(self, runname, once=False):
return em_core_findworkers.find(self.p, self.c, self.m, ACTIONS.FIND_WORKERS_ONCE, runname)

def status(self, runname):
return em_core_status.status(self.p, self.c, self.m, runname)

def epumgmt_run(opts, dbgmsgs=None):
em_core.core(opts, dbgmsgs=dbgmsgs)

2 changes: 1 addition & 1 deletion src/python/epumgmt/defaults/iaas.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def contextualize_base_image(self, services, hostname):

def terminate_ids(self, instanceids):
con = self._get_connection()
con.terminate_instances(instanceids)
return con.terminate_instances(instanceids)

def state_map(self, vm_list):
"""Return dictionary of {vm.instanceid, state}"""
Expand Down
3 changes: 3 additions & 0 deletions src/python/epumgmt/main/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ def __init__(self, name, short_syntax, noval=False, since=None, deprecated=False
self.metavar = metavar
if not metavar:
self.metavar = string.upper(name)
self.value = None
if self.boolean:
self.value = False

def __repr__(self):
return "ControlArg: %s" % self.name
10 changes: 6 additions & 4 deletions src/python/epumgmt/main/em_cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import em_deprecated
import em_optparse
from epumgmt.api.exceptions import *
from epumgmt.api import *

def main(argv=None):
if os.name != 'posix':
Expand All @@ -18,17 +19,18 @@ def main(argv=None):
(opts, args) = parser.parse_args(argv[1:])
else:
(opts, args) = parser.parse_args()


epu = EPUMgmtOpts(cmd_opts=opts)

try:
dbgmsgs = em_deprecated.deprecated_args(opts)

# From here 'down' there is no concept of a commandline program, only
# 'args' which could be coming from any kind of protocol based request.
# To make such a thing, construct an opts objects with the expected
# member names (see the em_args module) and pass it in.

em_core.core(opts, dbgmsgs=dbgmsgs)

epumgmt_run(epu, dbgmsgs=dbgmsgs)

except InvalidInput, e:
msg = "Problem with input: %s" % e.msg
print >>sys.stderr, msg
Expand Down
2 changes: 1 addition & 1 deletion src/python/epumgmt/main/em_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def core(opts, dbgmsgs=None):

c_cls = get_class_by_keyword("Common", allconfigs=ac)
c = c_cls(p)

# now there is a logger finally:
if dbgmsgs:
c.log.debug(dbgmsgs)
Expand Down
1 change: 1 addition & 0 deletions src/python/epumgmt/main/em_core_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ def create(p, c, m, run_name):
msg += "\n\nSSH suggestion:\n%s" % ' '.join(sshcmd)
c.log.info(msg)

return vm
2 changes: 2 additions & 0 deletions src/python/epumgmt/main/em_core_fetchkill.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ def fetch_kill(p, c, m, run_name):
c.log.info("All fetched and killed with %d errors" % error_count)
else:
c.log.info("All fetched and killed")

return error_count

class FetchKillThread(Thread):

Expand Down
6 changes: 4 additions & 2 deletions src/python/epumgmt/main/em_core_findworkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ def find(p, c, m, action, run_name, once=False):
if m.persistence.new_vm_maybe(run_name, vm):
c.log.info("Found new worker: %s : %s"
% (vm.instanceid, vm.hostname))
new_nodes = launched_vms

# then "node_started"
launched_vms = vms_launched(p, c, m, run_name, "node_started")
for vm in launched_vms:
m.persistence.new_vm_maybe(run_name, vm)
started_nodes = launched_vms

allvms = m.persistence.get_run_vms_or_none(run_name)
c.log.debug("Know of %d VMs in run '%s'" % (len(allvms), run_name))
if once:
break
return (new_nodes, started_nodes)
time.sleep(15)

def vms_launched(p, c, m, run_name, eventname):
Expand Down Expand Up @@ -58,4 +60,4 @@ def _get_provisioner(p, c, m, run_name):
provisioner = vm
if not provisioner:
raise IncompatibleEnvironment("Cannot find a record of the provisioner for run '%s'" % run_name)
return provisioner
return provisioner
4 changes: 3 additions & 1 deletion src/python/epumgmt/main/em_core_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ def status(p, c, m, run_name):
out += "%s\n" % warning
c.log.warn(out + "\n")

return actives

def _widest_state(run_vms):
widest = 0
for vm in run_vms:
Expand All @@ -65,4 +67,4 @@ def _pad_txt(txt, widest):
difference -= 1
return txt



2 changes: 1 addition & 1 deletion src/python/epumgmt/main/em_core_termination.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ def terminate(p, c, m, run_name):
c.log.info("Terminating '%s', Host '%s'" % (vm.instanceid, vm.hostname))

instanceids = [vm.instanceid for vm in run_vms]
m.iaas.terminate_ids(instanceids)
return m.iaas.terminate_ids(instanceids)
28 changes: 28 additions & 0 deletions tests/nosetests/simple_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import unittest
import nose.tools
import sys
import os
import uuid

from epumgmt.api import *

import run_rabbit

class TestSimpleOps(unittest.TestCase):

def setUp(self):
self.runname = str(uuid.uuid1()).replace("-", "")
conf = os.path.join(os.environ['EPUMGMT_HOME'], "etc/epumgmt/main.conf")
self.epu_opts = EPUMgmtOpts(conf_file=conf)
self.my_vars_file = os.environ['EPU_TEST_VARS']
self.epu_opts.jsonvars = self.my_vars_file
self.epu_action = EPUMgmtAction(self.epu_opts)
self.epu_action.set_logfile(os.path.join(os.environ['EPUMGMT_HOME'], "tests/tests.logs"))


def tearDown(self):
self.epu_action.killrun(self.runname)

def test_main_sequence(self):
self.epu_opts.haservice = "provisioner"
self.epu_action.create(self.runname)
61 changes: 61 additions & 0 deletions tests/run-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#/bin/bash

PYTHON_EXE="/usr/bin/env python"

EPU_HOME_REL="`dirname $0`/.."
export EPUMGMT_HOME=`cd $EPU_HOME_REL; pwd`
NIMBUS_CONTROL_PYLIB="$NIMBUS_CONTROL_DIR/lib/python"
NIMBUS_CONTROL_PYSRC="$NIMBUS_CONTROL_DIR/src/python"
PYTHONPATH="$EPUMGMT_HOME/lib/python:$EPUMGMT_HOME/src/python"
export PYTHONPATH


TESTS_DIR_REL="`dirname $0`"
TESTS_DIR=`cd $TESTS_DIR_REL; pwd`

cd $TESTS_DIR

json_file=`mktemp`
out_file=`mktemp`
export EPU_TEST_VARS=$json_file
rabbit_instance=

function on_exit()
{
rm -f $json_file
rm -f $out_file
}

trap on_exit EXIT


echo "running rabbitmq VM on ec2, this may take a bit"
./run_rabbit.py $json_file $EPU_RABBIT_ID | tee $out_file
if [ $PIPESTATUS -ne 0 ]; then
echo "first attempt at rabbit failed. trying again without instance id"
./run_rabbit.py $json_file | tee $out_file
if [ $PIPESTATUS -ne 0 ]; then
echo "could not start rabbit, sorry"
exit 1
fi
fi
rabbit_instance=`tail -n 1 $out_file`
export EPU_RABBIT_ID=$rabbit_instance

echo $rabbit_instance
echo "export EPU_RABBIT_ID=$rabbit_instance" > test_env.sh

failed_tests=""
cd scripts
final_rc=0
for t in *tests.py
do
$PYTHON_EXE $t
if [ $? -ne 0 ]; then
failed_tests="$t $failed_tests"
final_rc=1
fi
done
#nosetests *tests.py

exit $final_rc
Loading

0 comments on commit 08a2b8f

Please sign in to comment.