diff --git a/tools/testClient/TestCaseExecuteEngine.py b/tools/testClient/TestCaseExecuteEngine.py index 8057d5750dd..d305ed8d60b 100644 --- a/tools/testClient/TestCaseExecuteEngine.py +++ b/tools/testClient/TestCaseExecuteEngine.py @@ -52,6 +52,8 @@ class TestCaseExecuteEngine(object): setattr(test, "debug", partial(testCaseLogger, logger=testcaselogger)) setattr(test.__class__, "clstestclient", self.testclient) + if hasattr(test, "UserName"): + self.testclient.createNewApiClient(test.UserName, test.DomainName, test.AcctType) def run(self): loader = unittest.loader.TestLoader() suite = loader.discover(self.testCaseFolder) diff --git a/tools/testClient/cloudstackTestCase.py b/tools/testClient/cloudstackTestCase.py index 2424a670ac2..2a36296da17 100644 --- a/tools/testClient/cloudstackTestCase.py +++ b/tools/testClient/cloudstackTestCase.py @@ -5,6 +5,17 @@ except ImportError: import unittest import cloudstackTestClient +def UserName(Name, DomainName, AcctType): + def wrapper(cls): + orig_init = cls.__init__ + def __init__(self, *args, **kws): + cls.UserName = Name + cls.DomainName = DomainName + cls.AcctType = AcctType + orig_init(self, *args, **kws) + cls.__init__ = __init__ + return cls + return wrapper class cloudstackTestCase(unittest.case.TestCase): clstestclient = None diff --git a/tools/testClient/cloudstackTestClient.py b/tools/testClient/cloudstackTestClient.py index 0d8f8108ea9..f58e0f18b1d 100644 --- a/tools/testClient/cloudstackTestClient.py +++ b/tools/testClient/cloudstackTestClient.py @@ -1,6 +1,7 @@ import cloudstackConnection import asyncJobMgr import dbConnection +import uuid from cloudstackAPI import * class cloudstackTestClient(object): @@ -15,7 +16,57 @@ class cloudstackTestClient(object): def dbConfigure(self, host="localhost", port=3306, user='cloud', passwd='cloud', db='cloud'): self.dbConnection = dbConnection.dbConnection(host, port, user, passwd, db) + + def createNewApiClient(self, UserName, DomainName, acctType): + listDomain = listDomains.listDomainsCmd() + listDomain.name = DomainName + try: + domains = self.apiClient.listDomains(listDomain) + domId = domains[0].id + except: + cdomain = createDomain.createDomainCmd() + cdomain.name = DomainName + domain = self.apiClient.createDomain(cdomain) + domId = domain.id + cmd = listAccounts.listAccountsCmd() + cmd.name = UserName + exist = True + try: + accounts = self.apiClient.listAccounts(cmd) + acctId = accounts[0].id + except: + createAcctCmd = createAccount.createAccountCmd() + createAcctCmd.accounttype = acctType + createAcctCmd.domainid = domId + createAcctCmd.email = str(uuid.uuid4()) + "@citrix.com" + createAcctCmd.firstname = UserName + createAcctCmd.lastname = UserName + createAcctCmd.password = "password" + createAcctCmd.username = UserName + acct = self.apiClient.createAccount(createAcctCmd) + acctId = acct.id + + listuser = listUsers.listUsersCmd() + listuser.username = UserName + + listuserRes = self.apiClient.listUsers(listuser) + userId = listuserRes[0].id + apiKey = listuserRes[0].apikey + securityKey = listuserRes[0].secretkey + + if apiKey is None: + registerUser = registerUserKeys.registerUserKeysCmd() + registerUser.id = userId + registerUserRes = self.apiClient.registerUserKeys(registerUser) + apiKey = registerUserRes.apikey + securityKey = registerUserRes.secretkey + + nConnection = cloudstackConnection.cloudConnection(self.connection.mgtSvr, self.connection.port, apiKey, securityKey, self.connection.asyncTimeout, self.connection.logging) + self.connection.close() + self.connection = nConnection + + self.apiClient = cloudstackAPIClient.CloudStackAPIClient(self.connection) def close(self): if self.connection is not None: self.connection.close() diff --git a/tools/testClient/testcase/test_1.py b/tools/testClient/testcase/test_1.py index bba55febc2e..3f9ecadfadb 100644 --- a/tools/testClient/testcase/test_1.py +++ b/tools/testClient/testcase/test_1.py @@ -1,5 +1,6 @@ from cloudstackTestCase import * +@UserName("edison", "edison", "0") class TestCase1(cloudstackTestCase): def test_cloudstackapi(self):