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 = ?";
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.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=? "

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):
def __init__(self, deployCfgFile, testCaseFolder):
self.depolyCfgFile = deployCfgFile
def __init__(self, testclient, testCaseFolder, testcaseLogFile=None, testResultLogFile=None):
self.testclient = testclient
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 os
from optparse import OptionParser
class Struct:
'''The recursive class for building and representing objects with.'''
def __init__(self, obj):
@ -63,7 +64,7 @@ class dbServer():
self.dbSvr = None
self.port = 3306
self.user = "cloud"
self.passwd = ""
self.passwd = "cloud"
self.db = "cloud"
class configuration():
@ -115,7 +116,6 @@ class pod():
self.clusters = []
'''Used in basic network mode'''
self.guestIpRanges = []
self.primaryStorages = []
class cluster():
def __init__(self):
@ -128,6 +128,7 @@ class cluster():
self.url = None
self.username = None
self.hosts = []
self.primaryStorages = []
class host():
def __init__(self):
@ -183,68 +184,67 @@ class secondaryStorage():
def describe_setup_in_basic_mode():
zs = cloudstackConfiguration()
z = zone()
z.dns1 = "8.8.8.8"
z.dns2 = "4.4.4.4"
z.internaldns1 = "192.168.110.254"
z.internaldns2 = "192.168.110.253"
z.guestcidraddress = "10.1.1.0/24"
z.name = "test"
z.networktype = 'Basic'
for l in range(2):
z = zone()
z.dns1 = "8.8.8.8"
z.dns2 = "4.4.4.4"
z.internaldns1 = "192.168.110.254"
z.internaldns2 = "192.168.110.253"
z.name = "test"+str(l)
z.networktype = 'Basic'
'''create 10 pods'''
for i in range(1):
p = pod()
p.name = "test" +str(i)
p.gateway = "192.168.%d.1"%i
p.netmask = "255.255.255.0"
p.startip = "192.168.%d.200"%i
p.endip = "192.168.%d.220"%i
'''create 10 pods'''
for i in range(5):
p = pod()
p.name = "test" +str(l) + str(i)
p.gateway = "192.168.%d.1"%i
p.netmask = "255.255.255.0"
p.startip = "192.168.%d.200"%i
p.endip = "192.168.%d.220"%i
'''add two pod guest ip ranges'''
for j in range(1):
ip = iprange()
ip.gateway = p.gateway
ip.netmask = p.netmask
ip.startip = "192.168.%d.2"%i
ip.endip = "192.168.%d.100"%i
'''add two pod guest ip ranges'''
for j in range(5):
ip = iprange()
ip.gateway = p.gateway
ip.netmask = p.netmask
ip.startip = "192.168.%d.%d"%(i,j*20)
ip.endip = "192.168.%d.%d"%(i,j*20+10)
p.guestIpRanges.append(ip)
p.guestIpRanges.append(ip)
'''add 10 clusters'''
for j in range(1):
c = cluster()
c.clustername = "test"+str(i) + str(j)
c.clustertype = "CloudManaged"
c.hypervisor = "Simulator"
'''add 10 clusters'''
for j in range(5):
c = cluster()
c.clustername = "test"+str(l)+str(i) + str(j)
c.clustertype = "CloudManaged"
c.hypervisor = "Simulator"
'''add 10 hosts'''
for k in range(1):
h = host()
h.clustername = "host"+str(i) + str(j) + str(k)
h.username = "root"
h.password = "password"
h.url = "http://Sim"
c.hosts.append(h)
'''add 10 hosts'''
for k in range(5):
h = host()
h.username = "root"
h.password = "password"
h.url = "http://Sim/%d%d%d%d"%(l,i,j,k)
c.hosts.append(h)
p.clusters.append(c)
'''add 2 primary storages'''
for j in range(2):
primary = primaryStorage()
primary.name = "primary"+str(j)
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)
'''add 2 primary storages'''
for m in range(2):
primary = primaryStorage()
primary.name = "primary"+str(l) + str(i) + str(j) + str(m)
primary.url = "nfs://localhost/path"+str(l) + str(i) + str(j) + str(m)
c.primaryStorages.append(primary)
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'''
mgt = managementServer()
@ -260,11 +260,11 @@ def describe_setup_in_basic_mode():
''''add loggers'''
testClientLogger = logger()
testClientLogger.name = "TestClient"
testClientLogger.file = "/var/log/cloud/testclient.log"
testClientLogger.file = "/tmp/testclient.log"
testCaseLogger = logger()
testCaseLogger.name = "TestCase"
testCaseLogger.file = "/var/log/cloud/testcase.log"
testCaseLogger.file = "/tmp/testcase.log"
zs.logger.append(testClientLogger)
zs.logger.append(testCaseLogger)
@ -287,7 +287,13 @@ def get_setup_config(file):
config = cloudstackConfiguration()
fp = open(file, 'r')
config = json.load(fp)
return config
return Struct(config)
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 logging
from cloudstackAPI import *
from optparse import OptionParser
class deployDataCenters():
def __init__(self, cfgFile):
self.configFile = cfgFile
def addHosts(self, hosts, zoneId, podId, clusterId):
def addHosts(self, hosts, zoneId, podId, clusterId, hypervisor):
if hosts is None:
return
for host in hosts:
hostcmd = addHost.addHostCmd()
hostcmd.clusterid = clusterId
hostcmd.clustername = host.clustername
hostcmd.cpunumber = host.cpunumer
hostcmd.cpuspeed = host.cpuspeed
hostcmd.hostmac = host.hostmac
@ -27,6 +27,7 @@ class deployDataCenters():
hostcmd.url = host.url
hostcmd.username = host.username
hostcmd.zoneid = zoneId
hostcmd.hypervisor = hypervisor
self.apiClient.addHost(hostcmd)
def createClusters(self, clusters, zoneId, podId):
@ -44,22 +45,22 @@ class deployDataCenters():
clustercmd.username = cluster.username
clustercmd.zoneid = zoneId
clusterresponse = self.apiClient.addCluster(clustercmd)
clusterId = clusterresponse.id
clusterId = clusterresponse[0].id
self.addhosts(cluster.hosts, zoneId, podId, clusterId)
def createPrimaryStorages(self, primaryStorages, zoneId, podId):
self.addHosts(cluster.hosts, zoneId, podId, clusterId, cluster.hypervisor)
self.createPrimaryStorages(cluster.primaryStorages, zoneId, podId, clusterId)
def createPrimaryStorages(self, primaryStorages, zoneId, podId, clusterId):
if primaryStorages is None:
return
for primary in primaryStorages:
primarycmd = createStoragePool.createStoragePoolCmd()
primarycmd.clusterid = primary.clusterid
primarycmd.details = primary.details
primarycmd.name = primary.name
primarycmd.podid = podId
primarycmd.tags = primary.tags
primarycmd.url = primary.url
primarycmd.zoneid = zoneId
primarycmd.clusterid = clusterId
self.apiClient.createStoragePool(primarycmd)
def createpods(self, pods, zone, zoneId):
@ -80,7 +81,7 @@ class deployDataCenters():
self.createipRanges("Basic", pod.guestIpRanges, zoneId, podId)
self.createClusters(pod.clusters, zoneId, podId)
self.createPrimaryStorages(pod.primaryStorages, zoneId, podId)
def createipRanges(self, mode, ipranges, zoneId, podId=None, networkId=None):
if ipranges is None:
@ -146,8 +147,8 @@ class deployDataCenters():
createzone.internaldns1 = zone.internaldns1
createzone.internaldns2 = zone.internaldns2
createzone.name = zone.name
createzone.guestcidraddress = zone.guestcidraddress
createzone.securitygroupenabled = zone.securitygroupenabled
createzone.networktype = zone.networktype
createzone.vlan = zone.vlan
zoneresponse = self.apiClient.createZone(createzone)
@ -163,21 +164,28 @@ class deployDataCenters():
self.createnetworks(zone.networks, zoneId)
'''create secondary storage'''
self.createSecondaryStorages(zone.secondaryStorages, zoneId)
def deploy(self):
def loadCfg(self):
try:
config = configGenerator.get_setup_config(self.configFile)
self.config = configGenerator.get_setup_config(self.configFile)
except:
raise cloudstackException.InvalidParameterException("Failed to load cofig" + sys.exc_value)
mgt = self.config.mgtSvr[0]
mgt = config.mgtSvr[0]
loggers = config.logger
loggers = self.config.logger
testClientLogFile = None
self.testCaseLogFile = None
self.testResultLogFile = None
if loggers is not None and len(loggers) > 0:
for log in loggers:
if log.name == "TestClient":
testClientLogFile = log.file
elif log.name == "TestCase":
self.testCaseLogFile = log.file
elif log.name == "TestResult":
self.testResultLogFile = log.file
testClientLogger = None
if testClientLogFile is not None:
testClientLogger = logging.getLogger("testClient")
@ -188,9 +196,33 @@ class deployDataCenters():
self.testClient = cloudstackTestClient.cloudstackTestClient(mgt.mgtSvrIp, mgt.port, mgt.apiKey, mgt.securityKey, logging=testClientLogger)
'''config database'''
dbSvr = config.dbSvr
dbSvr = self.config.dbSvr
self.testClient.dbConfigure(dbSvr.dbSvr, dbSvr.port, dbSvr.user, dbSvr.passwd, dbSvr.db)
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)
'''