finally, test framework works now

This commit is contained in:
Edison Su 2011-08-16 02:08:34 -07:00
parent f4a6c314c5
commit 449a12870b
5 changed files with 183 additions and 82 deletions

View File

@ -67,7 +67,7 @@ public class VMTemplateHostDaoImpl extends GenericDaoBase<VMTemplateHostVO, Long
+ "WHERE host_id = ? and type_id = ?"; + "WHERE host_id = ? and type_id = ?";
protected static final String DOWNLOADS_STATE_DC= protected static final String DOWNLOADS_STATE_DC=
"SELECT t.id, t.host_id, t.pool_id, t.template_id, t.created, t.last_updated, t.job_id, " "SELECT t.id, t.host_id, t.template_id, t.created, t.last_updated, t.job_id, "
+ "t.download_pct, t.size, t.physical_size, t.download_state, t.error_str, t.local_path, " + "t.download_pct, t.size, t.physical_size, t.download_state, t.error_str, t.local_path, "
+ "t.install_path, t.url, t.destroyed, t.is_copy FROM template_host_ref t, host h " + "t.install_path, t.url, t.destroyed, t.is_copy FROM template_host_ref t, host h "
+ "where t.host_id = h.id and h.data_center_id=? " + "where t.host_id = h.id and h.data_center_id=? "

View File

@ -1,5 +1,40 @@
import unittest
from functools import partial
import os
import sys
import logging
def testCaseLogger(message, logger=None):
if logger is not None:
logger.debug(message)
class TestCaseExecuteEngine(object): class TestCaseExecuteEngine(object):
def __init__(self, deployCfgFile, testCaseFolder): def __init__(self, testclient, testCaseFolder, testcaseLogFile=None, testResultLogFile=None):
self.depolyCfgFile = deployCfgFile self.testclient = testclient
self.testCaseFolder = testCaseFolder self.testCaseFolder = testCaseFolder
self.logger = None
if testcaseLogFile is not None:
logger = logging.getLogger("testcase")
fh = logging.FileHandler(testcaseLogFile)
logger.addHandler(fh)
logger.setLevel(logging.DEBUG)
self.logger = logger
if testResultLogFile is not None:
fp = open(testResultLogFile, "w")
self.testResultLogFile = fp
else:
self.testResultLogFile = sys.stdout
def injectTestCase(self, testSuites):
for test in testSuites:
if isinstance(test, unittest.BaseTestSuite):
self.injectTestCase(test)
else:
setattr(test, "testClient", self.testclient)
setattr(test, "debug", partial(testCaseLogger, logger=self.logger))
def run(self):
loader = unittest.loader.TestLoader()
suite = loader.discover(self.testCaseFolder)
self.injectTestCase(suite)
unittest.TextTestRunner(stream=self.testResultLogFile, verbosity=2).run(suite)

View File

@ -1,5 +1,6 @@
import json import json
import os import os
from optparse import OptionParser
class Struct: class Struct:
'''The recursive class for building and representing objects with.''' '''The recursive class for building and representing objects with.'''
def __init__(self, obj): def __init__(self, obj):
@ -63,7 +64,7 @@ class dbServer():
self.dbSvr = None self.dbSvr = None
self.port = 3306 self.port = 3306
self.user = "cloud" self.user = "cloud"
self.passwd = "" self.passwd = "cloud"
self.db = "cloud" self.db = "cloud"
class configuration(): class configuration():
@ -115,7 +116,6 @@ class pod():
self.clusters = [] self.clusters = []
'''Used in basic network mode''' '''Used in basic network mode'''
self.guestIpRanges = [] self.guestIpRanges = []
self.primaryStorages = []
class cluster(): class cluster():
def __init__(self): def __init__(self):
@ -128,6 +128,7 @@ class cluster():
self.url = None self.url = None
self.username = None self.username = None
self.hosts = [] self.hosts = []
self.primaryStorages = []
class host(): class host():
def __init__(self): def __init__(self):
@ -183,68 +184,67 @@ class secondaryStorage():
def describe_setup_in_basic_mode(): def describe_setup_in_basic_mode():
zs = cloudstackConfiguration() zs = cloudstackConfiguration()
z = zone() for l in range(2):
z.dns1 = "8.8.8.8" z = zone()
z.dns2 = "4.4.4.4" z.dns1 = "8.8.8.8"
z.internaldns1 = "192.168.110.254" z.dns2 = "4.4.4.4"
z.internaldns2 = "192.168.110.253" z.internaldns1 = "192.168.110.254"
z.guestcidraddress = "10.1.1.0/24" z.internaldns2 = "192.168.110.253"
z.name = "test" z.name = "test"+str(l)
z.networktype = 'Basic' z.networktype = 'Basic'
'''create 10 pods''' '''create 10 pods'''
for i in range(1): for i in range(5):
p = pod() p = pod()
p.name = "test" +str(i) p.name = "test" +str(l) + str(i)
p.gateway = "192.168.%d.1"%i p.gateway = "192.168.%d.1"%i
p.netmask = "255.255.255.0" p.netmask = "255.255.255.0"
p.startip = "192.168.%d.200"%i p.startip = "192.168.%d.200"%i
p.endip = "192.168.%d.220"%i p.endip = "192.168.%d.220"%i
'''add two pod guest ip ranges''' '''add two pod guest ip ranges'''
for j in range(1): for j in range(5):
ip = iprange() ip = iprange()
ip.gateway = p.gateway ip.gateway = p.gateway
ip.netmask = p.netmask ip.netmask = p.netmask
ip.startip = "192.168.%d.2"%i ip.startip = "192.168.%d.%d"%(i,j*20)
ip.endip = "192.168.%d.100"%i ip.endip = "192.168.%d.%d"%(i,j*20+10)
p.guestIpRanges.append(ip) p.guestIpRanges.append(ip)
'''add 10 clusters''' '''add 10 clusters'''
for j in range(1): for j in range(5):
c = cluster() c = cluster()
c.clustername = "test"+str(i) + str(j) c.clustername = "test"+str(l)+str(i) + str(j)
c.clustertype = "CloudManaged" c.clustertype = "CloudManaged"
c.hypervisor = "Simulator" c.hypervisor = "Simulator"
'''add 10 hosts''' '''add 10 hosts'''
for k in range(1): for k in range(5):
h = host() h = host()
h.clustername = "host"+str(i) + str(j) + str(k) h.username = "root"
h.username = "root" h.password = "password"
h.password = "password" h.url = "http://Sim/%d%d%d%d"%(l,i,j,k)
h.url = "http://Sim" c.hosts.append(h)
c.hosts.append(h)
p.clusters.append(c) '''add 2 primary storages'''
for m in range(2):
'''add 2 primary storages''' primary = primaryStorage()
for j in range(2): primary.name = "primary"+str(l) + str(i) + str(j) + str(m)
primary = primaryStorage() primary.url = "nfs://localhost/path"+str(l) + str(i) + str(j) + str(m)
primary.name = "primary"+str(j) c.primaryStorages.append(primary)
primary.url = "nfs://localhost/path"+str(i) + str(j)
p.primaryStorages.append(primary)
z.pods.append(p)
'''add two secondary'''
for i in range(2):
secondary = secondaryStorage()
secondary.url = "nfs://localhost/path"+str(i)
z.secondaryStorages.append(secondary)
zs.zones.append(z) p.clusters.append(c)
z.pods.append(p)
'''add two secondary'''
for i in range(5):
secondary = secondaryStorage()
secondary.url = "nfs://localhost/path"+str(l) + str(i)
z.secondaryStorages.append(secondary)
zs.zones.append(z)
'''Add one mgt server''' '''Add one mgt server'''
mgt = managementServer() mgt = managementServer()
@ -260,11 +260,11 @@ def describe_setup_in_basic_mode():
''''add loggers''' ''''add loggers'''
testClientLogger = logger() testClientLogger = logger()
testClientLogger.name = "TestClient" testClientLogger.name = "TestClient"
testClientLogger.file = "/var/log/cloud/testclient.log" testClientLogger.file = "/tmp/testclient.log"
testCaseLogger = logger() testCaseLogger = logger()
testCaseLogger.name = "TestCase" testCaseLogger.name = "TestCase"
testCaseLogger.file = "/var/log/cloud/testcase.log" testCaseLogger.file = "/tmp/testcase.log"
zs.logger.append(testClientLogger) zs.logger.append(testClientLogger)
zs.logger.append(testCaseLogger) zs.logger.append(testCaseLogger)
@ -287,7 +287,13 @@ def get_setup_config(file):
config = cloudstackConfiguration() config = cloudstackConfiguration()
fp = open(file, 'r') fp = open(file, 'r')
config = json.load(fp) config = json.load(fp)
return config return Struct(config)
if __name__ == "__main__": if __name__ == "__main__":
generate_setup_config(describe_setup_in_basic_mode, "/tmp/x.json") parser = OptionParser()
parser.add_option("-o", "--output", action="store", default="./datacenterCfg", dest="output", help="the path where the json config file generated, by default is ./datacenterCfg")
(options, args) = parser.parse_args()
generate_setup_config(describe_setup_in_basic_mode, options.output)

View File

@ -0,0 +1,28 @@
import deployDataCenter
import TestCaseExecuteEngine
from optparse import OptionParser
import os
if __name__ == "__main__":
parser = OptionParser()
parser.add_option("-c", "--config", action="store", default="./datacenterCfg", dest="config", help="the path where the json config file generated, by default is ./datacenterCfg")
parser.add_option("-d", "--directory", dest="testCaseFolder", help="the test case directory")
parser.add_option("-r", "--result", dest="result", help="test result log file")
parser.add_option("-t", dest="testcaselog", help="test case log file")
(options, args) = parser.parse_args()
if options.testCaseFolder is None:
parser.print_usage()
exit(1)
testResultLogFile = None
if options.result is not None:
testResultLogFile = options.result
testCaseLogFile = None
if options.testcaselog is not None:
testCaseLogFile = options.testcaselog
deploy = deployDataCenter.deployDataCenters(options.config)
deploy.loadCfg()
testcaseEngine = TestCaseExecuteEngine.TestCaseExecuteEngine(deploy.testClient, options.testCaseFolder, testCaseLogFile, testResultLogFile)
testcaseEngine.run()

View File

@ -5,17 +5,17 @@ import cloudstackTestClient
import sys import sys
import logging import logging
from cloudstackAPI import * from cloudstackAPI import *
from optparse import OptionParser
class deployDataCenters(): class deployDataCenters():
def __init__(self, cfgFile): def __init__(self, cfgFile):
self.configFile = cfgFile self.configFile = cfgFile
def addHosts(self, hosts, zoneId, podId, clusterId): def addHosts(self, hosts, zoneId, podId, clusterId, hypervisor):
if hosts is None: if hosts is None:
return return
for host in hosts: for host in hosts:
hostcmd = addHost.addHostCmd() hostcmd = addHost.addHostCmd()
hostcmd.clusterid = clusterId hostcmd.clusterid = clusterId
hostcmd.clustername = host.clustername
hostcmd.cpunumber = host.cpunumer hostcmd.cpunumber = host.cpunumer
hostcmd.cpuspeed = host.cpuspeed hostcmd.cpuspeed = host.cpuspeed
hostcmd.hostmac = host.hostmac hostcmd.hostmac = host.hostmac
@ -27,6 +27,7 @@ class deployDataCenters():
hostcmd.url = host.url hostcmd.url = host.url
hostcmd.username = host.username hostcmd.username = host.username
hostcmd.zoneid = zoneId hostcmd.zoneid = zoneId
hostcmd.hypervisor = hypervisor
self.apiClient.addHost(hostcmd) self.apiClient.addHost(hostcmd)
def createClusters(self, clusters, zoneId, podId): def createClusters(self, clusters, zoneId, podId):
@ -44,22 +45,22 @@ class deployDataCenters():
clustercmd.username = cluster.username clustercmd.username = cluster.username
clustercmd.zoneid = zoneId clustercmd.zoneid = zoneId
clusterresponse = self.apiClient.addCluster(clustercmd) clusterresponse = self.apiClient.addCluster(clustercmd)
clusterId = clusterresponse.id clusterId = clusterresponse[0].id
self.addhosts(cluster.hosts, zoneId, podId, clusterId) self.addHosts(cluster.hosts, zoneId, podId, clusterId, cluster.hypervisor)
self.createPrimaryStorages(cluster.primaryStorages, zoneId, podId, clusterId)
def createPrimaryStorages(self, primaryStorages, zoneId, podId): def createPrimaryStorages(self, primaryStorages, zoneId, podId, clusterId):
if primaryStorages is None: if primaryStorages is None:
return return
for primary in primaryStorages: for primary in primaryStorages:
primarycmd = createStoragePool.createStoragePoolCmd() primarycmd = createStoragePool.createStoragePoolCmd()
primarycmd.clusterid = primary.clusterid
primarycmd.details = primary.details primarycmd.details = primary.details
primarycmd.name = primary.name primarycmd.name = primary.name
primarycmd.podid = podId primarycmd.podid = podId
primarycmd.tags = primary.tags primarycmd.tags = primary.tags
primarycmd.url = primary.url primarycmd.url = primary.url
primarycmd.zoneid = zoneId primarycmd.zoneid = zoneId
primarycmd.clusterid = clusterId
self.apiClient.createStoragePool(primarycmd) self.apiClient.createStoragePool(primarycmd)
def createpods(self, pods, zone, zoneId): def createpods(self, pods, zone, zoneId):
@ -80,7 +81,7 @@ class deployDataCenters():
self.createipRanges("Basic", pod.guestIpRanges, zoneId, podId) self.createipRanges("Basic", pod.guestIpRanges, zoneId, podId)
self.createClusters(pod.clusters, zoneId, podId) self.createClusters(pod.clusters, zoneId, podId)
self.createPrimaryStorages(pod.primaryStorages, zoneId, podId)
def createipRanges(self, mode, ipranges, zoneId, podId=None, networkId=None): def createipRanges(self, mode, ipranges, zoneId, podId=None, networkId=None):
if ipranges is None: if ipranges is None:
@ -146,8 +147,8 @@ class deployDataCenters():
createzone.internaldns1 = zone.internaldns1 createzone.internaldns1 = zone.internaldns1
createzone.internaldns2 = zone.internaldns2 createzone.internaldns2 = zone.internaldns2
createzone.name = zone.name createzone.name = zone.name
createzone.guestcidraddress = zone.guestcidraddress
createzone.securitygroupenabled = zone.securitygroupenabled createzone.securitygroupenabled = zone.securitygroupenabled
createzone.networktype = zone.networktype
createzone.vlan = zone.vlan createzone.vlan = zone.vlan
zoneresponse = self.apiClient.createZone(createzone) zoneresponse = self.apiClient.createZone(createzone)
@ -163,21 +164,28 @@ class deployDataCenters():
self.createnetworks(zone.networks, zoneId) self.createnetworks(zone.networks, zoneId)
'''create secondary storage''' '''create secondary storage'''
self.createSecondaryStorages(zone.secondaryStorages, zoneId) self.createSecondaryStorages(zone.secondaryStorages, zoneId)
def deploy(self): def loadCfg(self):
try: try:
config = configGenerator.get_setup_config(self.configFile) self.config = configGenerator.get_setup_config(self.configFile)
except: except:
raise cloudstackException.InvalidParameterException("Failed to load cofig" + sys.exc_value) raise cloudstackException.InvalidParameterException("Failed to load cofig" + sys.exc_value)
mgt = self.config.mgtSvr[0]
mgt = config.mgtSvr[0] loggers = self.config.logger
loggers = config.logger
testClientLogFile = None testClientLogFile = None
self.testCaseLogFile = None
self.testResultLogFile = None
if loggers is not None and len(loggers) > 0: if loggers is not None and len(loggers) > 0:
for log in loggers: for log in loggers:
if log.name == "TestClient": if log.name == "TestClient":
testClientLogFile = log.file testClientLogFile = log.file
elif log.name == "TestCase":
self.testCaseLogFile = log.file
elif log.name == "TestResult":
self.testResultLogFile = log.file
testClientLogger = None testClientLogger = None
if testClientLogFile is not None: if testClientLogFile is not None:
testClientLogger = logging.getLogger("testClient") testClientLogger = logging.getLogger("testClient")
@ -188,9 +196,33 @@ class deployDataCenters():
self.testClient = cloudstackTestClient.cloudstackTestClient(mgt.mgtSvrIp, mgt.port, mgt.apiKey, mgt.securityKey, logging=testClientLogger) self.testClient = cloudstackTestClient.cloudstackTestClient(mgt.mgtSvrIp, mgt.port, mgt.apiKey, mgt.securityKey, logging=testClientLogger)
'''config database''' '''config database'''
dbSvr = config.dbSvr dbSvr = self.config.dbSvr
self.testClient.dbConfigure(dbSvr.dbSvr, dbSvr.port, dbSvr.user, dbSvr.passwd, dbSvr.db) self.testClient.dbConfigure(dbSvr.dbSvr, dbSvr.port, dbSvr.user, dbSvr.passwd, dbSvr.db)
self.apiClient = self.testClient.getApiClient() self.apiClient = self.testClient.getApiClient()
def deploy(self):
self.loadCfg()
self.createZones(self.config.zones)
self.createZones(config.zones) if __name__ == "__main__":
parser = OptionParser()
parser.add_option("-i", "--intput", action="store", default="./datacenterCfg", dest="input", help="the path where the json config file generated, by default is ./datacenterCfg")
(options, args) = parser.parse_args()
deploy = deployDataCenters(options.input)
deploy.deploy()
'''
create = createStoragePool.createStoragePoolCmd()
create.clusterid = 1
create.podid = 2
create.name = "fdffdf"
create.url = "nfs://jfkdjf/fdkjfkd"
create.zoneid = 2
deploy = deployDataCenters("./datacenterCfg")
deploy.loadCfg()
deploy.apiClient.createStoragePool(create)
'''