def __init__(self, host, port, user, password, retries=60, delay=10, key_pair_files=None, timeout=10.0): self.host = None self.port = 22 self.user = user self.passwd = password self.keyPairFiles = key_pair_files self.ssh = SSHClient() self.ssh.set_missing_host_key_policy(AutoAddPolicy()) self.retryCnt = 0 self.delay = 0 self.timeout = 3.0 self.logger = MarvinLog('ssh').get_logger() # Check invalid host value and raise exception # At least host is required for connection if host is not None and host != '': self.host = host if retries is not None and retries > 0: self.retryCnt = retries if delay is not None and delay > 0: self.delay = delay if timeout is not None and timeout > 0: self.timeout = timeout if port is not None and port >= 0: self.port = port if self.create_connection() == FAILED: raise internalError("Connection Failed")
def __init__(self): self.__identifier = None self.__testClient = None self.__logFolderPath = None self.__parsedConfig = None ''' Contains Config File ''' self.__configFile = None ''' Signifies the Zone against which all tests will be Run ''' self.__zoneForTests = None ''' Signifies the flag whether to deploy the New DC or Not ''' self.__deployDcFlag = None self.conf = None self.__resultStream = stdout self.__testRunner = None self.__testResult = SUCCESS self.__startTime = None self.__testName = None self.__tcRunLogger = MarvinLog('marvin').get_logger() self.__testModName = '' self.__hypervisorType = None self.__halt_on_failure = False Plugin.__init__(self)
def setUp(self): self.logger = MarvinLog(MarvinLog.LOGGER_TEST).get_logger() self.apiclient = self.testClient.getApiClient() self.services = self.testClient.getParsedTestDataConfig() # Get Zone, Domain and templates self.domain = get_domain(self.apiclient) self.zone = get_zone(self.apiclient, self.testClient.getZoneForTests()) self.services["virtual_machine"]["zoneid"] = self.zone.id # Create an account with normal domain admin rights self.account_admin = Account.create(self.apiclient, self.services["account"], admin=True, domainid=self.domain.id) # Create an account with readonly domain admin rights self.services["account"]["accounttype"] = 4 self.account_ro_admin = Account.create(self.apiclient, self.services["account"], admin=True, domainid=self.domain.id) regusrkey_cmd = registerUserKeys.registerUserKeysCmd() regusrkey_cmd.id = self.account_admin.user[0].id self.user_admin = self.apiclient.registerUserKeys(regusrkey_cmd) regusrkey_cmd = registerUserKeys.registerUserKeysCmd() regusrkey_cmd.id = self.account_ro_admin.user[0].id self.user_ro_admin = self.apiclient.registerUserKeys(regusrkey_cmd) self.cleanup = [self.account_admin, self.account_ro_admin] return
def __init__(self, mgmt_details, dbsvr_details, async_timeout=3600, test_data_filepath=None, zone=None, hypervisor_type=None, halt_on_failure=False): self.__mgmtDetails = mgmt_details self.__dbSvrDetails = dbsvr_details self.__csConnection = None self.__dbConnection = None self.__testClient = None self.__asyncTimeOut = async_timeout self.__logger = MarvinLog('marvin').get_logger() self.__apiClient = None self.__userApiClient = None self.__asyncJobMgr = None self.__id = None self.__hypervisor = hypervisor_type self.__testDataFilePath = test_data_filepath self.__parsedTestDataConfig = None self.__zone = zone self.__setHypervisorInfo() self.__scenarioManager = None self.__halt_on_failure = halt_on_failure
def setUp(self): self.logger = MarvinLog(MarvinLog.LOGGER_TEST).get_logger() self.apiclient = self.testClient.getApiClient() self.services = self.testClient.getParsedTestDataConfig() # Get Zone, Domain and templates self.domain = get_domain(self.apiclient) self.zone = get_zone(self.apiclient, self.testClient.getZoneForTests()) template = get_template(self.apiclient, self.zone.id) self.services["virtual_machine"]["zoneid"] = self.zone.id # Create an account, network, VM, Port forwarding rule, LB rules self.account = Account.create(self.apiclient, self.services["account"], admin=True, domainid=self.domain.id) self.service_offering = get_default_virtual_machine_offering( self.apiclient) self.virtual_machine = VirtualMachine.create( self.apiclient, self.services["virtual_machine"], templateid=template.id, accountid=self.account.name, domainid=self.account.domainid, serviceofferingid=self.service_offering.id) self.ip_address = PublicIPAddress.create(self.apiclient, self.account.name, self.zone.id, self.account.domainid) ip_addrs = list_public_ip(self.apiclient, account=self.account.name, domainid=self.account.domainid, issourcenat=False) try: self.ip_addr = ip_addrs[0] except Exception as e: raise Exception( "Failed: During acquiring source NAT for account: %s, :%s" % (self.account.name, e)) self.nat_rule = NATRule.create(self.apiclient, self.virtual_machine, self.services["natrule"], self.ip_addr.id) self.lb_rule = LoadBalancerRule.create(self.apiclient, self.services["lbrule"], self.ip_addr.id, accountid=self.account.name) self.cleanup = [self.virtual_machine, self.account] return
def setUp(self): self.logger = MarvinLog(MarvinLog.LOGGER_TEST).get_logger() self.apiclient = self.testClient.getApiClient() self.hypervisor = self.testClient.getHypervisorInfo() self.cleanup = [] self.services = self.testClient.getParsedTestDataConfig() self.zone = get_zone(self.apiclient, self.testClient.getZoneForTests()) self.services["sleep"] = 5 self.services["timeout"] = 180 return
def setUp(self): self.logger = MarvinLog(MarvinLog.LOGGER_TEST).get_logger() self.apiclient = self.testClient.getApiClient() self.hypervisor = self.testClient.getHypervisorInfo() self.cleanup = [] self.services = self.testClient.getParsedTestDataConfig() self.zone = get_zone(self.apiclient, self.testClient.getZoneForTests()) self.services["sleep"] = 2 self.services["timeout"] = 240 # Default value is 120 seconds. That's just too much. self.services["configurableData"]["systemVmDelay"] = 60 return
def __init__(self, api_client, scenario_data, test_data, zone, randomizeNames, cleanup): self.api_client = api_client self.scenario_data = scenario_data self.test_data = test_data self.zone = zone self.logger = MarvinLog(MarvinLog.LOGGER_TEST).get_logger() self.dynamic_names = { 'accounts': {}, 'vpcs': {}, 'vms': {}, } self.randomizeNames = randomizeNames self.resources_to_cleanup = [] self.cleanup = cleanup
def setUp(self): self.logger = MarvinLog(MarvinLog.LOGGER_TEST).get_logger() self.apiclient = self.testClient.getApiClient() self.services = self.testClient.getParsedTestDataConfig() # Get Zone, Domain and templates self.domain = get_domain(self.apiclient) self.zone = get_zone(self.apiclient, self.testClient.getZoneForTests()) template = get_template(self.apiclient, self.zone.id) self.services["virtual_machine"]["zoneid"] = self.zone.id # Create an account, network, VM and IP addresses self.account = Account.create(self.apiclient, self.services["account"], admin=True, domainid=self.domain.id) self.service_offering = get_default_virtual_machine_offering( self.apiclient) self.vm_1 = VirtualMachine.create( self.apiclient, self.services["virtual_machine"], templateid=template.id, accountid=self.account.name, domainid=self.account.domainid, serviceofferingid=self.service_offering.id) src_nat_ip_addrs = list_public_ip(self.apiclient, account=self.account.name, domainid=self.account.domainid) try: src_nat_ip_addr = src_nat_ip_addrs[0] except Exception as e: self.fail("SSH failed for VM with IP: %s %s" % (src_nat_ip_addr.ipaddress, e)) self.lb_rule = LoadBalancerRule.create(self.apiclient, self.services["lbrule"], src_nat_ip_addr.id, self.account.name) self.lb_rule.assign(self.apiclient, [self.vm_1]) self.nat_rule = NATRule.create(self.apiclient, self.vm_1, self.services["natrule"], src_nat_ip_addr.id) self.cleanup = [] return
def setUpClass(cls): cls.logger = MarvinLog(MarvinLog.LOGGER_TEST).get_logger() testClient = super(TestPortForwarding, cls).getClsTestClient() cls.apiclient = testClient.getApiClient() cls.services = testClient.getParsedTestDataConfig() cls.hypervisor = testClient.getHypervisorInfo() # Get Zone, Domain and templates cls.domain = get_domain(cls.apiclient) cls.zone = get_zone(cls.apiclient, testClient.getZoneForTests()) template = get_template(cls.apiclient, cls.zone.id) if template == FAILED: assert False, "get_template() failed to return template with description %s" % cls.services[ "ostype"] # Create an account, network, VM and IP addresses cls.account = Account.create(cls.apiclient, cls.services["account"], admin=True, domainid=cls.domain.id) cls.services["virtual_machine"]["zoneid"] = cls.zone.id cls.service_offering = get_default_virtual_machine_offering( cls.apiclient) cls.virtual_machine = VirtualMachine.create( cls.apiclient, cls.services["virtual_machine"], templateid=template.id, accountid=cls.account.name, domainid=cls.account.domainid, serviceofferingid=cls.service_offering.id) cls._cleanup = [cls.virtual_machine, cls.account]
def setUpClass(cls): cls.logger = MarvinLog(MarvinLog.LOGGER_TEST).get_logger() cls.test_client = super(TestPublicIpAcl, cls).getClsTestClient() cls.api_client = cls.test_client.getApiClient() cls.attributes = cls.test_client.getParsedTestDataConfig() cls.class_cleanup = []
def setUpClass(cls): cls.logger = MarvinLog(MarvinLog.LOGGER_TEST).get_logger() cls.test_client = super(TestScenario1, cls).getClsTestClient() cls.api_client = cls.test_client.getApiClient() cls.scenario_manager = cls.test_client.getScenarioManager('scenario_1') cls.class_cleanup = []
def setUpClass(cls): cls.logger = MarvinLog(MarvinLog.LOGGER_TEST).get_logger() # We want to fail quicker if it's failure socket.setdefaulttimeout(60) cls.testClient = super(TestRouterIpTablesPolicies, cls).getClsTestClient() cls.apiclient = cls.testClient.getApiClient() cls.services = cls.testClient.getParsedTestDataConfig() # Get Zone, Domain and templates cls.domain = get_domain(cls.apiclient) cls.zone = get_zone(cls.apiclient, cls.testClient.getZoneForTests()) cls.template = get_template( cls.apiclient, cls.zone.id ) cls.services["vpc"]["cidr"] = '10.1.1.1/16' cls.services["virtual_machine"]["zoneid"] = cls.zone.id cls.services["virtual_machine"]["template"] = cls.template.id cls.account = Account.create( cls.apiclient, cls.services["account"], admin=True, domainid=cls.domain.id) cls.service_offering = get_default_virtual_machine_offering(cls.apiclient) cls.entity_manager = EntityManager(cls.apiclient, cls.services, cls.service_offering, cls.account, cls.zone, cls.logger) cls._cleanup = [cls.account] return
def setUpClass(cls): cls.logger = MarvinLog(MarvinLog.LOGGER_TEST).get_logger() cls.testClient = super(TestIsolatedNetworks, cls).getClsTestClient() cls.api_client = cls.testClient.getApiClient() cls.services = cls.testClient.getParsedTestDataConfig() # Get Zone, Domain and templates cls.domain = get_domain(cls.api_client) cls.zone = get_zone(cls.api_client, cls.testClient.getZoneForTests()) cls.services['mode'] = cls.zone.networktype cls.template = get_template(cls.api_client, cls.zone.id) cls.services["virtual_machine"]["zoneid"] = cls.zone.id # Create an account, network, VM and IP addresses cls.account = Account.create(cls.api_client, cls.services["account"], admin=True, domainid=cls.domain.id) cls.service_offering = get_default_virtual_machine_offering( cls.api_client) cls.services["egress_443"] = { "startport": 443, "endport": 443, "protocol": "TCP", "cidrlist": ["0.0.0.0/0"] } cls._cleanup = [cls.account] return
def __init__(self, config_file, deploy_dc_flag=False, test_mod_name="deploydc", zone=None, hypervisor_type=None): self.__configFile = config_file self.__deployFlag = deploy_dc_flag self.__tcRunLogger = MarvinLog('marvin').get_logger() self.__tcRunLogger.info("=== Marvin Init Logging Successful ===") self.__testModName = test_mod_name self.__testClient = None self.__tcResultFile = None self.__testDataFilePath = None self.__zoneForTests = zone self.__parsedConfig = None self.__hypervisorType = hypervisor_type
def setUpClass(cls): cls.logger = MarvinLog(MarvinLog.LOGGER_TEST).get_logger() cls.test_client = super(TestIpExclusionList, cls).getClsTestClient() cls.api_client = cls.test_client.getApiClient() cls.test_data = cls.test_client.getParsedTestDataConfig() cls.class_cleanup = []
def setUpClass(cls): cls.logger = MarvinLog(MarvinLog.LOGGER_TEST).get_logger() testClient = super(TestLoadBalance, cls).getClsTestClient() cls.apiclient = testClient.getApiClient() cls.services = testClient.getParsedTestDataConfig() # Get Zone, Domain and templates cls.domain = get_domain(cls.apiclient) cls.zone = get_zone(cls.apiclient, testClient.getZoneForTests()) cls.template = get_template(cls.apiclient, cls.zone.id) if cls.template == FAILED: assert False, "get_template() failed to return template with description %s" % cls.services[ "ostype"] cls.services["virtual_machine"]["zoneid"] = cls.zone.id # Create an account, network, VM and IP addresses cls.account = Account.create(cls.apiclient, cls.services["account"], admin=True, domainid=cls.domain.id) cls.service_offering = get_default_virtual_machine_offering( cls.apiclient) cls.vm_1 = VirtualMachine.create( cls.apiclient, cls.services["virtual_machine"], templateid=cls.template.id, accountid=cls.account.name, domainid=cls.account.domainid, serviceofferingid=cls.service_offering.id) cls.vm_2 = VirtualMachine.create( cls.apiclient, cls.services["virtual_machine"], templateid=cls.template.id, accountid=cls.account.name, domainid=cls.account.domainid, serviceofferingid=cls.service_offering.id) cls.vm_3 = VirtualMachine.create( cls.apiclient, cls.services["virtual_machine"], templateid=cls.template.id, accountid=cls.account.name, domainid=cls.account.domainid, serviceofferingid=cls.service_offering.id) cls.non_src_nat_ip = PublicIPAddress.create( cls.apiclient, cls.account.name, cls.zone.id, cls.account.domainid, cls.services["virtual_machine"]) # Open up firewall port for SSH cls.fw_rule = FireWallRule.create( cls.apiclient, ipaddressid=cls.non_src_nat_ip.ipaddress.id, protocol=cls.services["lbrule"]["protocol"], cidrlist=['0.0.0.0/0'], startport=cls.services["lbrule"]["publicport"], endport=cls.services["lbrule"]["publicport"]) cls._cleanup = [cls.account]
def setUpClass(cls): cls.logger = MarvinLog(MarvinLog.LOGGER_TEST).get_logger() cls.test_client = super(eval(cls.__name__), cls).getClsTestClient() cls.api_client = cls.test_client.getApiClient() cls.scenario_manager = cls.test_client.getScenarioManager('scenario_2') cls.class_cleanup = [] cls.ssh_timer = None cls.test_data = cls.scenario_manager.test_data cls.specific_test_data = cls.test_data[cls.__name__]
def __init__(self, config_file, deploy_dc_flag=False, test_mod_name="deploydc", zone=None, hypervisor_type=None, halt_on_failure=False): self.__configFile = config_file self.__deployFlag = deploy_dc_flag self.__tcRunLogger = MarvinLog('marvin').get_logger() self.__tcRunLogger.info("=== Marvin Init Logging Successful ===") self.__testModName = test_mod_name self.__testClient = None self.__tcResultFile = None self.__testDataFilePath = None self.__zoneForTests = zone self.__parsedConfig = None self.__hypervisorType = hypervisor_type self.__halt_on_failure = halt_on_failure
def setUpClass(cls): cls.logger = MarvinLog(MarvinLog.LOGGER_TEST).get_logger() testClient = super(TestPublicIP, cls).getClsTestClient() cls.apiclient = testClient.getApiClient() cls.services = testClient.getParsedTestDataConfig() # Get Zone, Domain and templates cls.domain = get_domain(cls.apiclient) cls.zone = get_zone(cls.apiclient, testClient.getZoneForTests()) cls.services['mode'] = cls.zone.networktype # Create Accounts & networks cls.account = Account.create(cls.apiclient, cls.services["account"], admin=True, domainid=cls.domain.id) cls.user = Account.create(cls.apiclient, cls.services["account"], domainid=cls.domain.id) cls.services["network"]["zoneid"] = cls.zone.id cls.network_offering = get_default_guest_network_offering( cls.apiclient) cls.services["network"]["networkoffering"] = cls.network_offering.id cls.account_network = Network.create(cls.apiclient, cls.services["network"], cls.account.name, cls.account.domainid) cls.user_network = Network.create(cls.apiclient, cls.services["network"], cls.user.name, cls.user.domainid) # Create Source NAT IP addresses PublicIPAddress.create(cls.apiclient, cls.account.name, cls.zone.id, cls.account.domainid) PublicIPAddress.create(cls.apiclient, cls.user.name, cls.zone.id, cls.user.domainid) cls._cleanup = [ cls.account_network, cls.user_network, cls.account, cls.user ] return
def setUpClass(cls): cls.logger = MarvinLog(MarvinLog.LOGGER_TEST).get_logger() cls.testClient = super(TestPasswordService, cls).getClsTestClient() cls.api_client = cls.testClient.getApiClient() cls.services = Services().services # Get Zone, Domain and templates cls.domain = get_domain(cls.api_client) cls.zone = get_zone(cls.api_client, cls.testClient.getZoneForTests()) cls.services['mode'] = cls.zone.networktype cls.template = get_template(cls.api_client, cls.zone.id) cls.services["virtual_machine"]["zoneid"] = cls.zone.id cls.services["virtual_machine"]["template"] = cls.template.id cls.service_offering = get_default_virtual_machine_offering( cls.api_client) cls._cleanup = []
def setUpClass(cls): cls.logger = MarvinLog(MarvinLog.LOGGER_TEST).get_logger() # We want to fail quicker if it's failure socket.setdefaulttimeout(60) cls.testClient = super(TestVPCRedundancy, cls).getClsTestClient() cls.api_client = cls.testClient.getApiClient() cls.services = cls.testClient.getParsedTestDataConfig() # Get Zone, Domain and templates cls.domain = get_domain(cls.api_client) cls.zone = get_zone(cls.api_client, cls.testClient.getZoneForTests()) cls.template = get_template(cls.api_client, cls.zone.id) cls.services["virtual_machine"]["zoneid"] = cls.zone.id cls.services["virtual_machine"]["template"] = cls.template.id cls.service_offering = get_default_virtual_machine_offering( cls.api_client) return
def setUpClass(cls, redundant=False): cls.logger = MarvinLog(MarvinLog.LOGGER_TEST).get_logger() test_client = super(TestVpcVpn, cls).getClsTestClient() cls.apiclient = test_client.getApiClient() cls.services = test_client.getParsedTestDataConfig() cls.zone = get_zone(cls.apiclient, test_client.getZoneForTests()) cls.domain = get_domain(cls.apiclient) cls.vpc_offering = get_default_redundant_vpc_offering( cls.apiclient) if redundant else get_default_vpc_offering( cls.apiclient) cls.logger.debug("VPC Offering '%s' selected", cls.vpc_offering.name) cls.network_offering = get_default_network_offering(cls.apiclient) cls.logger.debug("Network Offering '%s' selected", cls.network_offering.name) cls.virtual_machine_offering = get_default_virtual_machine_offering( cls.apiclient) cls.logger.debug("Virtual Machine Offering '%s' selected", cls.virtual_machine_offering.name) cls.account = Account.create(cls.apiclient, services=cls.services["account"]) cls.logger.debug("Successfully created account: %s, id: %s" % (cls.account.name, cls.account.id)) cls.hypervisor = test_client.getHypervisorInfo() cls._cleanup = [cls.account] cls.template = get_template(cls.apiclient, cls.zone.id) return
class MarvinInit: def __init__(self, config_file, deploy_dc_flag=False, test_mod_name="deploydc", zone=None, hypervisor_type=None, halt_on_failure=False): self.__configFile = config_file self.__deployFlag = deploy_dc_flag self.__tcRunLogger = MarvinLog('marvin').get_logger() self.__tcRunLogger.info("=== Marvin Init Logging Successful ===") self.__testModName = test_mod_name self.__testClient = None self.__tcResultFile = None self.__testDataFilePath = None self.__zoneForTests = zone self.__parsedConfig = None self.__hypervisorType = hypervisor_type self.__halt_on_failure = halt_on_failure def __parseConfig(self): ''' @Name: __parseConfig @Desc : Parses the configuration file passed and assigns the parsed configuration @Output : SUCCESS or FAILED ''' try: if not os.path.isfile(self.__configFile): self.__tcRunLogger.error("=== Marvin Parse Config Init Failed ===") return FAILED self.__parsedConfig = getSetupConfig(self.__configFile) self.__tcRunLogger.info("=== Marvin Parse Config Successful ===") return SUCCESS except Exception as e: self.__tcRunLogger.exception("=== Marvin Parse Config Init Failed: %s ===" % e) return FAILED def getParsedConfig(self): return self.__parsedConfig def getLogFolderPath(self): return self.__logFolderPath def getTestClient(self): return self.__testClient def getLogger(self): return self.__tcRunLogger def getResultFile(self): ''' @Name : getDebugFile @Desc : Creates the result file at a given path. @Output : Returns the Result file to be used for writing test outputs ''' self.__tcResultFile = open("results.txt", "w") return self.__tcResultFile def __setHypervisorAndZoneInfo(self): ''' @Name : __setHypervisorAndZoneInfo @Desc: Set the HyperVisor and Zone details; default to XenServer ''' try: if not self.__hypervisorType: if self.__parsedConfig and self.__parsedConfig.zones is not None: for zone in self.__parsedConfig.zones: for pod in zone.pods: if pod is not None: for cluster in pod.clusters: if cluster is not None and cluster.hypervisor is not None: self.__hypervisorType = cluster.hypervisor break if not self.__zoneForTests: if self.__parsedConfig and self.__parsedConfig.zones is not None: for zone in self.__parsedConfig.zones: self.__zoneForTests = zone.name break if not self.__hypervisorType: self.__hypervisorType = XEN_SERVER return SUCCESS except Exception as e: self.__tcRunLogger.exception("=== Set Hypervizor and Zone info Failed: %s ===" % e) return FAILED def init(self): ''' @Name : init @Desc :Initializes the marvin by 1. Parsing the configuration and creating a parsed config structure 2. Creates a timestamped log folder and provides all logs to be dumped there 3. Creates the DataCenter based upon configuration provided @Output : SUCCESS or FAILED ''' try: self.__tcRunLogger.info("=== Marvin Init Started ===") if ((self.__parseConfig() != FAILED) and (self.__setHypervisorAndZoneInfo()) and (self.__setTestDataPath() != FAILED) and (self.__createTestClient() != FAILED) and (self.__deployDC() != FAILED)): self.__tcRunLogger.info("=== Marvin Init Successful ===") return SUCCESS self.__tcRunLogger.error("=== Marvin Init Failed ===") return FAILED except Exception as e: self.__tcRunLogger.exception("=== Marvin Init Failed with exception: %s ===" % e) return FAILED def __createTestClient(self): ''' @Name : __createTestClient @Desc : Creates the TestClient during init based upon the parameters provided @Output: Returns SUCCESS or FAILED ''' try: mgt_details = self.__parsedConfig.mgtSvr[0] dbsvr_details = self.__parsedConfig.dbSvr self.__testClient = CSTestClient( mgt_details, dbsvr_details, test_data_filepath=self.__testDataFilePath, zone=self.__zoneForTests, hypervisor_type=self.__hypervisorType, halt_on_failure=self.__halt_on_failure ) if self.__testClient: return self.__testClient.createTestClient() return FAILED except Exception as e: self.__tcRunLogger.exception("=== Marvin Create Test Client Failed: %s ===" % e) return FAILED def __setTestDataPath(self): ''' @Name : __setTestDataPath @Desc : Sets the TestData Path for tests to run @Output:Returns SUCCESS or FAILED ''' try: if ((self.__parsedConfig.TestData is not None) and (self.__parsedConfig.TestData.Path is not None)): self.__testDataFilePath = self.__parsedConfig.TestData.Path self.__tcRunLogger.info("=== Marvin Setting TestData Successful ===") return SUCCESS except Exception as e: self.__tcRunLogger.exception("=== Marvin Setting TestData Successful Failed: %s ===" % e) return FAILED def __deployDC(self): ''' @Name : __deployDC @Desc : Deploy the DataCenter and returns accordingly. @Output: SUCCESS or FAILED ''' try: ret = SUCCESS if self.__deployFlag: deploy_obj = DeployDataCenters(self.__testClient, self.__parsedConfig, self.__tcRunLogger) ret = deploy_obj.deploy() if ret != SUCCESS: self.__tcRunLogger.error("=== Deploy DC Failed ===") return ret except Exception as e: self.__tcRunLogger.exception("=== Deploy DC Failed with exception: %s ===" % e) return FAILED
def __init__(self, test_client, cfg): self.__cfg = cfg self.__test_client = test_client self.__logger = MarvinLog('marvin').get_logger() self.__apiClient = None
class CSTestClient(object): ''' @Desc : CloudStackTestClient is encapsulated entity for creating and getting various clients viz., apiclient, user api client, dbconnection, test Data parsed information etc @Input : mgmt_details : Management Server Details dbsvr_details: Database Server details of Management \ Server. Retrieved from configuration file. async_timeout : Timeout for Async queries default_worker_threads : Number of worker threads logger : provides logging facilities for this library zone : The zone on which test suites using this test client will run ''' def __init__(self, mgmt_details, dbsvr_details, async_timeout=3600, test_data_filepath=None, zone=None, hypervisor_type=None, halt_on_failure=False): self.__mgmtDetails = mgmt_details self.__dbSvrDetails = dbsvr_details self.__csConnection = None self.__dbConnection = None self.__testClient = None self.__asyncTimeOut = async_timeout self.__logger = MarvinLog('marvin').get_logger() self.__apiClient = None self.__userApiClient = None self.__asyncJobMgr = None self.__id = None self.__hypervisor = hypervisor_type self.__testDataFilePath = test_data_filepath self.__parsedTestDataConfig = None self.__zone = zone self.__setHypervisorInfo() self.__scenarioManager = None self.__halt_on_failure = halt_on_failure @property def identifier(self): return self.__id @identifier.setter def identifier(self, id): self.__id = id def getHaltOnFailure(self): return self.__halt_on_failure def getParsedTestDataConfig(self): ''' @Name : getParsedTestDataConfig @Desc : Provides the TestData Config needed for Tests are to Run @Output : Returns the Parsed Test Data Dictionary ''' return copy.deepcopy(self.__parsedTestDataConfig) def getZoneForTests(self): ''' @Name : getZoneForTests @Desc : Provides the Zone against which Tests are to run If zone name provided to marvin plugin is none it will get it from Test Data Config File Even, if it is not available, return None @Output : Returns the Zone Name ''' return self.__zone def getHypervisorInfo(self): ''' @Name : getHypervisorInfo @Desc : Provides the hypervisor Information to test users @Output : Return Hypervisor Information ''' return self.__hypervisor def __setHypervisorInfo(self): ''' @Name : __setHypervisorInfo @Desc: Set the HyperVisor details; default to XenServer ''' try: if not self.__hypervisor: self.__hypervisor = XEN_SERVER return SUCCESS except Exception as e: self.__logger.exception("Set hypervisor failed: %s" % e) return FAILED def __createApiClient(self): try: ''' Step1 : Create a CS Connection Object ''' mgmt_details = self.__mgmtDetails self.__csConnection = CSConnection(mgmt_details, self.__asyncTimeOut, self.__logger) ''' Step2 : Create API Client with earlier created connection object ''' self.__apiClient = CloudStackAPIClient(self.__csConnection) ''' Step3: If API Key is not provided as part of Management Details, then verify and register ''' if mgmt_details.apiKey is None: list_user = listUsers.listUsersCmd() list_user.account = "admin" list_user_res = self.__apiClient.listUsers(list_user) if list_user_res is None or \ (validate_list(list_user_res)[0] != PASS): self.__logger.error("API Client Creation Failed") return FAILED user_id = list_user_res[0].id ret = self.__getKeys(user_id) if ret != FAILED: api_key = ret[0] security_key = ret[1] else: self.__logger.error( "API Client Creation Failed while Registering User") return FAILED mgmt_details.port = 8080 mgmt_details.apiKey = api_key mgmt_details.securityKey = security_key ''' Now Create the Connection objects and Api Client using new details ''' self.__csConnection = CSConnection(mgmt_details, self.__asyncTimeOut, self.__logger) self.__apiClient = CloudStackAPIClient(self.__csConnection) return SUCCESS except Exception as e: self.__logger.exception("Create API client failed: %s" % e) return FAILED def __createDbConnection(self): ''' @Name : ___createDbConnection @Desc : Creates the CloudStack DB Connection ''' host = "localhost" if self.__dbSvrDetails.dbSvr is None \ else self.__dbSvrDetails.dbSvr port = 3306 if self.__dbSvrDetails.port is None \ else self.__dbSvrDetails.port user = "******" if self.__dbSvrDetails.user is None \ else self.__dbSvrDetails.user passwd = 'cloud' if self.__dbSvrDetails.passwd is None \ else self.__dbSvrDetails.passwd db = 'cloud' if self.__dbSvrDetails.db is None \ else self.__dbSvrDetails.db self.__dbConnection = DbConnection(host, port, user, passwd, db) def __getKeys(self, userid): ''' @Name : ___getKeys @Desc : Retrieves the API and Secret Key for the provided Userid @Input: userid: Userid to register @Output: FAILED or tuple with apikey and secretkey ''' try: register_user = registerUserKeys.registerUserKeysCmd() register_user.id = userid register_user_res = \ self.__apiClient.registerUserKeys(register_user) if not register_user_res: return FAILED return (register_user_res.apikey, register_user_res.secretkey) except Exception as e: self.__logger.exception("Key retrival failed: %s" % e) return FAILED def createTestClient(self): ''' @Name : createTestClient @Desc : Creates the Test Client. The test Client is used by test suites Here we create ParsedTestData Config. Creates a DB Connection. Creates an API Client @Output : FAILED In case of an issue\Failure SUCCESS in case of Success of this function ''' try: ''' 1. Create Config Object Provides the Configuration Object to test suites through getConfigParser. The purpose of this config object is to parse the default config and provide dictionary of the config so users can use that configuration. Users can later call getConfig on this object and it will return the default parsed config dictionary from default configuration file. They can overwrite it with providing their own configuration file as well. ''' ''' 1. Check Config,Zone,Hypervisor Information ''' self.__configObj = ConfigManager(self.__testDataFilePath) if not self.__configObj or not self.__hypervisor: self.__logger.error( "Either Hypervisor is None or Not able to create ConfigManager Object" ) return FAILED self.__parsedTestDataConfig = self.__configObj.getConfig() self.__logger.info("Parsing Test data successful") ''' 2. Create DB Connection ''' self.__createDbConnection() ''' 3. Creates API Client ''' ret = self.__createApiClient() if ret == FAILED: self.__logger.error("=== Test Client Creation Failed ===") else: self.__logger.info("=== Test Client Creation Successful ===") return ret except Exception as e: self.__logger.exception("Test Client creation failed: %s" % e) return FAILED def isAdminContext(self): """ @Name : isAdminContext @Desc:A user is a regular user if he fails to listDomains; if he is a domain-admin, he can list only domains that are non-ROOT; if he is an admin, he can list the ROOT domain successfully """ try: listdom = listDomains.listDomainsCmd() listdom.name = 'ROOT' listdomres = self.__apiClient.listDomains(listdom) if listdomres != FAILED: rootdom = listdomres[0].name if rootdom == 'ROOT': return ADMIN else: return DOMAIN_ADMIN return USER except: return USER def __createUserApiClient(self, UserName, DomainName, acctType=0): ''' @Name : ___createUserApiClient @Desc : Creates a User API Client with given UserName\DomainName Parameters @Input: UserName: Username to be created in cloudstack DomainName: Domain under which the above account be created accType: Type of Account EX: Root,Non Root etc @Output: Return the API client for the user ''' try: if not self.isAdminContext(): return self.__apiClient mgmt_details = self.__mgmtDetails listDomain = listDomains.listDomainsCmd() listDomain.listall = True 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 cmd.domainid = domId cmd.listall = True try: accounts = self.__apiClient.listAccounts(cmd) acctId = accounts[0].id except: createAcctCmd = createAccount.createAccountCmd() createAcctCmd.accounttype = acctType createAcctCmd.domainid = domId createAcctCmd.email = "test-" + random_gen() \ + "@cloudstack.org" createAcctCmd.firstname = UserName createAcctCmd.lastname = UserName createAcctCmd.password = '******' createAcctCmd.username = UserName acct = self.__apiClient.createAccount(createAcctCmd) acctId = acct.id listuser = listUsers.listUsersCmd() listuser.username = UserName listuser.domainid = domId listuser.listall = True listuserRes = self.__apiClient.listUsers(listuser) userId = listuserRes[0].id apiKey = listuserRes[0].apikey securityKey = listuserRes[0].secretkey if apiKey is None: ret = self.__getKeys(userId) if ret != FAILED: mgmt_details.apiKey = ret[0] mgmt_details.securityKey = ret[1] else: self.__logger.error( "User API Client Creation While Registering User Failed" ) return FAILED else: mgmt_details.port = 8080 mgmt_details.apiKey = apiKey mgmt_details.securityKey = securityKey newUserConnection = \ CSConnection(mgmt_details, self.__csConnection.asyncTimeout, self.__csConnection.logger) self.__userApiClient = CloudStackAPIClient(newUserConnection) self.__userApiClient.connection = newUserConnection self.__userApiClient.hypervisor = self.__hypervisor return self.__userApiClient except Exception as e: self.__logger.exception("API user creation failed: %s" % e) return FAILED def close(self): if self.__csConnection is not None: self.__csConnection.close() def getDbConnection(self): ''' @Name : getDbConnection @Desc : Retrieves the DB Connection Handle ''' return self.__dbConnection def getConfigParser(self): ''' @Name : getConfigParser @Desc : Provides the ConfigManager Interface to TestClients ''' return self.__configObj def getApiClient(self): if self.__apiClient: self.__apiClient.id = self.identifier return self.__apiClient return None def getUserApiClient(self, UserName=None, DomainName=None, type=0): """ @Name : getUserApiClient @Desc : Provides the User API Client to test Users 0 - user ; 1 - admin;2 - domain admin @OutPut : FAILED In case of an issue else User API Client """ if UserName is None or DomainName is None: return FAILED return self.__createUserApiClient(UserName, DomainName, type) def submitCmdsAndWait(self, cmds, workers=1, apiclient=None): ''' @Desc : FixME, httplib has issue if more than one thread submitted ''' if not apiclient: apiclient = self.__apiClient if self.__asyncJobMgr is None: self.__asyncJobMgr = asyncJobMgr(apiclient, self.__dbConnection) return self.__asyncJobMgr.submitCmdsAndWait(cmds, workers) def submitJob(self, job, ntimes=1, nums_threads=10, interval=1): ''' @Desc : submit one job and execute the same job ntimes, with nums_threads of threads ''' if self.__asyncJobMgr is None: self.__asyncJobMgr = asyncJobMgr(self.__apiClient, self.__dbConnection) self.__asyncJobMgr.submitJobExecuteNtimes(job, ntimes, nums_threads, interval) def submitJobs(self, jobs, nums_threads=10, interval=1): ''' @Desc :submit n jobs, execute them with nums_threads of threads ''' if self.__asyncJobMgr is None: self.__asyncJobMgr = asyncJobMgr(self.__apiClient, self.__dbConnection) self.__asyncJobMgr.submitJobs(jobs, nums_threads, interval) def getScenarioManager(self, scenarioName, randomizeNames=True, cleanup=True): if not self.__scenarioManager: scenarioHumanReadableName = self.getParsedTestDataConfig( )['scenarios'][scenarioName]['metadata']['name'] self.__logger.info( "=== Scenario Manager, Scenario \"%s\" Started ===", scenarioHumanReadableName) self.__scenarioManager = TestScenarioManager( api_client=self.getApiClient(), scenario_data=self.getParsedTestDataConfig()['scenarios'] [scenarioName], test_data=self.getParsedTestDataConfig()['test_data'], zone=get_zone(self.getApiClient(), self.getZoneForTests()), randomizeNames=randomizeNames, cleanup=cleanup) try: self.__scenarioManager.setup_infra() except: self.__logger.exception(">>>>>>>>>>>>>>>>>>>>>>> %s ", traceback.format_exc()) raise self.__logger.info( "=== Scenario Manager, Scenario \"%s\" Created ===", scenarioHumanReadableName) return self.__scenarioManager def finalize(self): if self.__scenarioManager: self.__scenarioManager.finalize()
class TestScenarioManager: def __init__(self, api_client, scenario_data, test_data, zone, randomizeNames, cleanup): self.api_client = api_client self.scenario_data = scenario_data self.test_data = test_data self.zone = zone self.logger = MarvinLog(MarvinLog.LOGGER_TEST).get_logger() self.dynamic_names = { 'accounts': {}, 'vpcs': {}, 'vms': {}, } self.randomizeNames = randomizeNames self.resources_to_cleanup = [] self.cleanup = cleanup @property def test_data(self): return self.test_data def get_dynamic_name(self, section, name): if name in self.dynamic_names[section]: return self.dynamic_names[section][name] else: return name def setup_infra(self): scenario_data = self.scenario_data['data'] self.deploy_domains(scenario_data['domains']) def deploy_domains(self, domains_data): for domain in domains_data: self.deploy_domain(domain['data']) def deploy_domain(self, domain_data): if domain_data['name'] == 'ROOT': domain_list = Domain.list( api_client=self.api_client, name=domain_data['name'] ) domain = domain_list[0] else: self.logger.debug('>>> DOMAIN => Creating "%s"...', domain_data['name']) domain = Domain.create( api_client=self.api_client, name=domain_data['name'] + ('-' + random_gen() if self.randomizeNames else '') ) self.logger.debug('>>> DOMAIN => ID: %s => Name: %s => Path: %s => State: %s', domain.id, domain.name, domain.path, domain.state) self.deploy_accounts(domain_data['accounts'], domain) def deploy_accounts(self, accounts_data, domain): for account in accounts_data: self.deploy_account(account['data'], domain) def deploy_account(self, account_data, domain): self.logger.debug('>>> ACCOUNT => Creating "%s"...', account_data['username']) account = None if not self.randomizeNames: account_list = Account.list(api_client=self.api_client, name=account_data['username'], listall=True) if isinstance(account_list, list) and len(account_list) >= 1: if account_list[0].name == account_data['username']: account = account_list[0] self.logger.debug('>>> ACCOUNT => Loaded from (pre) existing account, ID: %s', account.id) return if not account: account = Account.create( api_client=self.api_client, services=account_data, domainid=domain.uuid, randomizeID=self.randomizeNames ) self.resources_to_cleanup.append(account) self.dynamic_names['accounts'][account_data['username']] = account.name self.logger.debug('>>> ACCOUNT => ID: %s => Name: %s => State: %s => Domain: %s', account.id, account.name, account.state, account.domainid) self.deploy_vpcs(account_data['vpcs'], account) self.deploy_isolatednetworks(account_data['isolatednetworks'], account) self.deploy_vms(account_data['virtualmachines'], account) self.deploy_vpcs_publicipaddresses(account_data['vpcs'], account_data['virtualmachines']) self.deploy_isolatednetworks_publicipaddresses(account_data['isolatednetworks'], account_data['virtualmachines']) self.deploy_privatenetworks(account_data['privatenetworks'], account, domain) self.deploy_vpcs_privategateways(account_data['vpcs']) self.enable_vpcs_localvpngateway(account_data['vpcs']) self.deploy_vpcs_remotevpngateways(account_data['vpcs'], account) def deploy_vpcs(self, vpcs_data, account): for vpc in vpcs_data: self.deploy_vpc(vpc['data'], account) def deploy_vpc(self, vpc_data, account): self.logger.debug('>>> VPC => Creating "%s"...', vpc_data['name']) vpc = VPC.create( api_client=self.api_client, data=vpc_data, zone=self.zone, account=account, randomizeID=self.randomizeNames ) self.dynamic_names['vpcs'][vpc_data['name']] = vpc.name self.logger.debug('>>> VPC => ID: %s => Name: %s => CIDR: %s => State: %s => Offering: %s ' '=> Account: %s => Domain: %s', vpc.id, vpc.name, vpc.cidr, vpc.state, vpc.vpcofferingid, vpc.account, vpc.domainid) self.deploy_acls(vpc_data['acls'], vpc) self.deploy_networks(vpc_data['networks'], vpc) def deploy_acls(self, acls_data, vpc): for acl in acls_data: self.deploy_acl(acl['data'], vpc) def deploy_acl(self, acl_data, vpc): self.logger.debug('>>> ACL => Creating "%s"...', acl_data['name']) acl = NetworkACLList.create( api_client=self.api_client, data=acl_data, vpc=vpc ) self.logger.debug('>>> ACL => ID: %s => Name: %s => VPC: %s', acl.id, acl.name, acl.vpcid) self.deploy_rules(acl_data['rules'], acl) def deploy_rules(self, rules_data, acl): for rule in rules_data: self.deploy_rule(rule['data'], acl) def deploy_rule(self, rule_data, acl): self.logger.debug('>>> ACL RULE => Creating...') rule = NetworkACL.create( api_client=self.api_client, data=rule_data, acl=acl ) self.logger.debug('>>> ACL RULE => ID: %s => Number: %s => Action: %s => Traffic Type: %s ' '=> CIDR List: %s => Protocol: %s => Start Port: %s => End Port: %s => ACL: %s', rule.id, rule.number, rule.action, rule.traffictype, rule.cidrlist, rule.protocol.upper(), rule.startport, rule.endport, rule.aclid) def deploy_networks(self, networks_data, vpc): for network in networks_data: self.deploy_network(network['data'], vpc) def deploy_network(self, network_data, vpc): acl = get_network_acl(api_client=self.api_client, name=network_data['aclname'], vpc=vpc) self.logger.debug('>>> TIER => Creating "%s"...', network_data['name']) network = Network.create( self.api_client, data=network_data, vpc=vpc, zone=self.zone, acl=acl ) self.logger.debug('>>> TIER => ID: %s => Name: %s => CIDR: %s => Gateway: %s => Type: %s ' '=> Traffic Type: %s => State: %s => Offering: %s => ACL: %s ' '=> Physical Network: %s => VPC: %s => Domain: %s', network.id, network.name, network.cidr, network.gateway, network.type, network.traffictype, network.state, network.networkofferingid, network.aclid, network.physicalnetworkid, network.vpcid, network.domainid) def deploy_vms(self, vms_data, account): for vm in vms_data: self.deploy_vm(vm['data'], account) def deploy_vm(self, vm_data, account): network_and_ip_list = [] for nic in vm_data['nics']: network = get_network(api_client=self.api_client, name=nic['data']['networkname']) network_and_ip = { 'networkid': network.id } if 'guestip' in nic['data']: network_and_ip['ip'] = nic['data']['guestip'] network_and_ip_list.append(network_and_ip) self.logger.debug('>>> VM => Creating "%s"...', vm_data['name']) vm = VirtualMachine.create( self.api_client, data=vm_data, zone=self.zone, account=account, network_and_ip_list=network_and_ip_list ) self.dynamic_names['vms'][vm_data['name']] = vm.name self.logger.debug('>>> VM => ID: %s => Name: %s => IP: %s => State: %s => Offering: %s ' '=> Template: %s => Hypervisor: %s => Domain: %s', vm.id, vm.name, vm.ipaddress, vm.state, vm.serviceofferingid, vm.templateid, vm.hypervisor, vm.domainid) def deploy_vpcs_publicipaddresses(self, vpcs_data, virtualmachines_data): for vpc in vpcs_data: self.deploy_vpc_publicipaddresses(vpc['data'], virtualmachines_data) def deploy_vpc_publicipaddresses(self, vpc_data, virtualmachines_data): vpc = get_vpc(api_client=self.api_client, name=self.dynamic_names['vpcs'][vpc_data['name']]) for publicipaddress in vpc_data['publicipaddresses']: self.deploy_publicipaddress(publicipaddress['data'], virtualmachines_data, vpc) def deploy_publicipaddress(self, publicipaddress_data, virtualmachines_data, vpc): self.logger.debug('>>> PUBLIC IP ADDRESS => Creating...') publicipaddress = PublicIPAddress.create( api_client=self.api_client, data=publicipaddress_data, vpc=vpc ) ipaddress = publicipaddress.ipaddress self.logger.debug('>>> PUBLIC IP ADDRESS => ID: %s => IP: %s => State: %s => Source NAT: %s ' '=> Static NAT: %s => ACL: %s => VLAN: %s => Physical Network: %s => Network: %s ' '=> VPC: %s => Domain: %s', ipaddress.id, ipaddress.ipaddress, ipaddress.state, ipaddress.issourcenat, ipaddress.isstaticnat, ipaddress.aclid, ipaddress.vlanid, ipaddress.physicalnetworkid, ipaddress.networkid, ipaddress.vpcid, ipaddress.domainid) self.deploy_portforwards(publicipaddress_data['portforwards'], virtualmachines_data, vpc, publicipaddress) def deploy_portforwards(self, portforwards_data, virtualmachines_data, vpc, publicipaddress): for portforward_data in portforwards_data: for virtualmachine_data in virtualmachines_data: if virtualmachine_data['data']['name'] == portforward_data['data']['virtualmachinename']: for nic_data in virtualmachine_data['data']['nics']: if nic_data['data']['guestip'] == portforward_data['data']['nic']: network = get_network( api_client=self.api_client, name=nic_data['data']['networkname'], vpc=vpc ) virtualmachine = get_virtual_machine( api_client=self.api_client, name=self.dynamic_names['vms'][virtualmachine_data['data']['name']], network=network ) self.logger.debug('>>> PORT FORWARD => Creating...') portforward = NATRule.create( api_client=self.api_client, data=portforward_data['data'], network=network, virtual_machine=virtualmachine, ipaddress=publicipaddress ) Tag.create( api_client=self.api_client, resourceType='UserVm', resourceIds=[virtualmachine.id], tags=[ { 'key': 'sship', 'value': publicipaddress.ipaddress.ipaddress }, { 'key': 'sshport', 'value': portforward_data['data']['publicport'] } ] ) self.logger.debug('>>> PORT FORWARD => ID: %s => Public Start Port: %s ' '=> Public End Port: %s => Private Start Port: %s ' '=> Private End Port: %s => CIDR List: %s => Protocol: %s ' '=> State: %s => IP: %s => VM: %s', portforward.id, portforward.publicport, portforward.publicendport, portforward.privateport, portforward.privateendport, portforward.cidrlist, portforward.protocol, portforward.state, portforward.ipaddressid, portforward.virtualmachineid) def deploy_privatenetworks(self, privatenetworks_data, account, domain): for privatenetwork in privatenetworks_data: self.deploy_privatenetwork(privatenetwork['data'], account, domain) def deploy_privatenetwork(self, privatenetwork_data, account, domain): self.logger.debug('>>> PRIVATE GATEWAY NETWORK => Creating "%s"...', privatenetwork_data['name']) private_gateways_network = Network.create( api_client=self.api_client, data=privatenetwork_data, zone=self.zone, domain=domain, account=account ) self.logger.debug('>>> PRIVATE GATEWAY NETWORK => ID: %s => Name: %s => CIDR: %s => Type: %s ' '=> Traffic Type: %s => State: %s => Offering: %s => Broadcast Domain Type: %s ' '=> Broadcast URI: %s => Physical Network: %s => Domain: %s', private_gateways_network.id, private_gateways_network.name, private_gateways_network.cidr, private_gateways_network.type, private_gateways_network.traffictype, private_gateways_network.state, private_gateways_network.networkofferingid, private_gateways_network.broadcastdomaintype, private_gateways_network.broadcasturi, private_gateways_network.physicalnetworkid, private_gateways_network.domainid) def deploy_vpcs_privategateways(self, vpcs_data): for vpc in vpcs_data: self.deploy_vpc_privategateways(vpc['data']) def deploy_vpc_privategateways(self, vpc_data): vpc = get_vpc(api_client=self.api_client, name=self.dynamic_names['vpcs'][vpc_data['name']]) for privategateway in vpc_data['privategateways']: self.deploy_privategateway(privategateway['data'], vpc) def deploy_privategateway(self, privategateway_data, vpc): self.logger.debug('>>> PRIVATE GATEWAY => Creating "%s"...', privategateway_data['ip']) private_gateway = PrivateGateway.create( api_client=self.api_client, data=privategateway_data, vpc=vpc ) self.logger.debug('>>> PRIVATE GATEWAY => ID: %s => IP: %s => CIDR: %s => State: %s ' '=> Source NAT: %s => ACL: %s => Network: %s => VPC: %s => Domain: %s', private_gateway.id, private_gateway.ipaddress, private_gateway.cidr, private_gateway.state, private_gateway.sourcenatsupported, private_gateway.aclid, private_gateway.networkid, private_gateway.vpcid, private_gateway.domainid) self.deploy_staticroutes(privategateway_data['staticroutes'], vpc) def deploy_staticroutes(self, staticroutes_data, vpc): for staticroute_data in staticroutes_data: self.logger.debug('>>> STATIC ROUTE => Creating...') staticroute = StaticRoute.create( api_client=self.api_client, data=staticroute_data['data'], vpc=vpc ) self.logger.debug('>>> STATIC ROUTE => ID: %s => CIDR: %s => Next Hop: %s => State: %s ' '=> VPC: %s => Domain: %s', staticroute.id, staticroute.cidr, staticroute.nexthop, staticroute.state, staticroute.vpcid, staticroute.domainid) def enable_vpcs_localvpngateway(self, vpcs_data): for vpc_data in vpcs_data: vpc = get_vpc(api_client=self.api_client, name=self.dynamic_names['vpcs'][vpc_data['data']['name']]) if vpc_data['data']['vpnconnections']: self.logger.debug('>>> VPN LOCAL GATEWAY => Creating on VPC "%s"...', vpc_data['data']['name']) localvpngateway = Vpn.createVpnGateway(api_client=self.api_client, vpc=vpc) self.logger.debug('>>> VPN LOCAL GATEWAY => ID: %s => IP: %s => VPC: %s => Domain: %s', localvpngateway['id'], localvpngateway['publicip'], localvpngateway['vpcid'], localvpngateway['domainid']) def deploy_vpcs_remotevpngateways(self, vpcs_data, account): for vpc_data in vpcs_data: for vpnconnection_data in vpc_data['data']['vpnconnections']: vpc = get_vpc(api_client=self.api_client, name=self.dynamic_names['vpcs'][vpc_data['data']['name']]) vpc_vpngateway = get_vpngateway(api_client=self.api_client, vpc=vpc) remotevpc = get_vpc(api_client=self.api_client, name=self.dynamic_names['vpcs'][vpnconnection_data]) remotevpc_vpngateway = get_vpngateway(api_client=self.api_client, vpc=remotevpc) self.logger.debug('>>> VPN CUSTOMER GATEWAY => Creating to VPC "%s"...', vpnconnection_data) vpncustomergateway = VpnCustomerGateway.create( api_client=self.api_client, name="remotegateway_to_" + remotevpc.name, gateway=remotevpc_vpngateway.publicip, cidrlist=remotevpc.cidr, presharedkey='notasecret', ikepolicy='aes128-sha256;modp2048', esppolicy='aes128-sha256;modp2048', account=account.name, domainid=account.domainid ) self.logger.debug('>>> VPN CUSTOMER GATEWAY => ID: %s => Name: %s => CIDR List: %s ' '=> Gateway: %s => Domain: %s', vpncustomergateway.id, vpncustomergateway.name, vpncustomergateway.cidrlist, vpncustomergateway.gateway, vpncustomergateway.domainid) self.logger.debug('>>> VPN CONNECTION => Creating from VPC "%s" to VPC "%s"...', vpc_data['data']['name'], vpnconnection_data) vpnconnection = Vpn.createVpnConnection( api_client=self.api_client, s2svpngatewayid=vpc_vpngateway.id, s2scustomergatewayid=vpncustomergateway.id ) self.logger.debug('>>> VPN CONNECTION => ID: %s => VPN Local Gateway: %s ' '=> VPN Customer Gateway: %s => State: %s', vpnconnection['id'], vpnconnection['s2svpngatewayid'], vpnconnection['s2scustomergatewayid'], vpnconnection['state']) def deploy_isolatednetworks(self, isolatednetworks_data, account): for isolatednetwork in isolatednetworks_data: self.deploy_isolatednetwork(isolatednetwork['data'], account) def deploy_isolatednetwork(self, isolatednetwork_data, account): self.logger.debug('>>> ISOLATED NETWORK => Creating "%s"...', isolatednetwork_data['name']) isolatednetwork = Network.create( self.api_client, data=isolatednetwork_data, account=account, zone=self.zone ) self.logger.debug('>>> ISOLATED NETWORK => ID: %s => Name: %s => CIDR: %s ' '=> Gateway: %s => Type: %s => Traffic Type: %s => State: %s ' '=> Offering: %s => Physical Network: %s => Domain: %s', isolatednetwork.id, isolatednetwork.name, isolatednetwork.cidr, isolatednetwork.gateway, isolatednetwork.type, isolatednetwork.traffictype, isolatednetwork.state, isolatednetwork.networkofferingid, isolatednetwork.physicalnetworkid, isolatednetwork.domainid) def deploy_isolatednetworks_publicipaddresses(self, isolatednetworks_data, virtualmachines_data): for isolatednetwork in isolatednetworks_data: network = get_network(self.api_client, isolatednetwork['data']['name']) self.deploy_isolatednetwork_egresses(isolatednetwork['data'], network) self.deploy_isolatednetwork_publicipaddresses(isolatednetwork['data'], virtualmachines_data, network) def deploy_isolatednetwork_egresses(self, isolatednetwork_data, network): self.logger.debug('>>> ISOLATED NETWORK EGRESS RULE => Creating...') for egress_data in isolatednetwork_data['egressrules']: egress = EgressFireWallRule.create( self.api_client, network=network, data=egress_data['data'] ) self.logger.debug('>>> ISOLATED NETWORK EGRESS RULE => ID: %s => Start Port: %s ' '=> End Port: %s => CIDR: %s => Protocol: %s => State: %s ' '=> Network: %s', egress.id, egress.startport, egress.endport, egress.cidrlist, egress.protocol, egress.state, egress.networkid) def deploy_isolatednetwork_publicipaddresses(self, isolatednetwork_data, virtual_machines, network): for ipaddress in isolatednetwork_data['publicipaddresses']: self.deploy_isolatednetwork_publicipaddress(ipaddress['data'], virtual_machines, network) def deploy_isolatednetwork_publicipaddress(self, ipaddress_data, virtualmachines_data, network): self.logger.debug('>>> ISOLATED NETWORK PUBLIC IP ADDRESS => Creating...') publicipaddress = PublicIPAddress.create( api_client=self.api_client, data=ipaddress_data, network=network ) self.logger.debug('>>> ISOLATED NETWORK PUBLIC IP ADDRESS => Created! TODO: MISSING FIELDS!') self.deploy_firewallrules(ipaddress_data, publicipaddress) self.deploy_portforwards(ipaddress_data['portforwards'], virtualmachines_data, None, publicipaddress) def deploy_firewallrules(self, ipaddress_data, publicipaddress): self.logger.debug('>>> ISOLATED NETWORK FIREWALL RULE => Creating...') for firewallrule in ipaddress_data['firewallrules']: self.deploy_firewallrule(firewallrule, publicipaddress) def deploy_firewallrule(self, firewallrule, publicipaddress): firewall = FireWallRule.create( self.api_client, data=firewallrule['data'], ipaddress=publicipaddress ) self.logger.debug('>>> ISOLATED NETWORKS FIREWALL RULE => ID: %s => Start Port: %s ' '=> End Port: %s => CIDR: %s => Protocol: %s => State: %s ' '=> Network: %s => IP: %s', firewall.id, firewall.startport, firewall.endport, firewall.cidrlist, firewall.protocol, firewall.state, firewall.networkid, firewall.ipaddress) def get_vpc(self, name): vpc = VPC(get_vpc(api_client=self.api_client, name=name).__dict__, api_client=self.api_client) return vpc def get_virtual_machine(self, vm_name): virtual_machine = VirtualMachine(get_virtual_machine( api_client=self.api_client, name=self.get_dynamic_name('vms', vm_name) ).__dict__, []) virtual_machine_tags = Tag.list( api_client=self.api_client, resourceId=[virtual_machine.id], listall=True) for key in virtual_machine_tags: if key.key == 'sshport': virtual_machine.ssh_port = int(key.value.encode('ascii', 'ignore')) if key.key == 'sship': virtual_machine.ssh_ip = key.value.encode('ascii', 'ignore') return virtual_machine def get_network(self, name=None, id=None): return Network(get_network(api_client=self.api_client, name=name, id=id).__dict__) def get_network_acl_list(self, name=None, id=None): return NetworkACLList(get_network_acl(api_client=self.api_client, name=name, id=id).__dict__) def get_default_allow_acl_list(self): return self.get_network_acl_list(name='default_allow') def get_default_deny_acl_list(self): return self.get_network_acl_list(name='default_deny') def deploy_network_acl_list(self, acl_list_name, acl_config, network=None, vpc=None): if network: networkid=network.id if network.vpcid: vpcid=network.vpcid acl_list = NetworkACLList.create(self.api_client, name=acl_list_name, services=[], vpcid=vpcid, vpc=vpc) NetworkACL.create(self.api_client, acl_config, networkid=networkid, aclid=acl_list.id) return acl_list def finalize(self): if self.cleanup: try: self.logger.info('=== Scenario Manager, Cleaning Up of "%s" Started ===', self.scenario_data['metadata']['name']) cleanup_resources(self.api_client, self.resources_to_cleanup, self.logger) self.logger.info('=== Scenario Manager, Cleaning Up of "%s" Finished ===', self.scenario_data['metadata']['name']) except: self.logger.exception('Oops! Unable to clean up resources of "%s"!', self.scenario_data['metadata']['name']) else: self.logger.info('=== Scenario Manager, Skipping Cleaning Up of "%s" ===', self.scenario_data['metadata']['name'])
class CSTestClient(object): ''' @Desc : CloudStackTestClient is encapsulated entity for creating and getting various clients viz., apiclient, user api client, dbconnection, test Data parsed information etc @Input : mgmt_details : Management Server Details dbsvr_details: Database Server details of Management \ Server. Retrieved from configuration file. async_timeout : Timeout for Async queries default_worker_threads : Number of worker threads logger : provides logging facilities for this library zone : The zone on which test suites using this test client will run ''' def __init__(self, mgmt_details, dbsvr_details, async_timeout=3600, test_data_filepath=None, zone=None, hypervisor_type=None, halt_on_failure=False): self.__mgmtDetails = mgmt_details self.__dbSvrDetails = dbsvr_details self.__csConnection = None self.__dbConnection = None self.__testClient = None self.__asyncTimeOut = async_timeout self.__logger = MarvinLog('marvin').get_logger() self.__apiClient = None self.__userApiClient = None self.__asyncJobMgr = None self.__id = None self.__hypervisor = hypervisor_type self.__testDataFilePath = test_data_filepath self.__parsedTestDataConfig = None self.__zone = zone self.__setHypervisorInfo() self.__scenarioManager = None self.__halt_on_failure = halt_on_failure @property def identifier(self): return self.__id @identifier.setter def identifier(self, id): self.__id = id def getHaltOnFailure(self): return self.__halt_on_failure def getParsedTestDataConfig(self): ''' @Name : getParsedTestDataConfig @Desc : Provides the TestData Config needed for Tests are to Run @Output : Returns the Parsed Test Data Dictionary ''' return copy.deepcopy(self.__parsedTestDataConfig) def getZoneForTests(self): ''' @Name : getZoneForTests @Desc : Provides the Zone against which Tests are to run If zone name provided to marvin plugin is none it will get it from Test Data Config File Even, if it is not available, return None @Output : Returns the Zone Name ''' return self.__zone def getHypervisorInfo(self): ''' @Name : getHypervisorInfo @Desc : Provides the hypervisor Information to test users @Output : Return Hypervisor Information ''' return self.__hypervisor def __setHypervisorInfo(self): ''' @Name : __setHypervisorInfo @Desc: Set the HyperVisor details; default to XenServer ''' try: if not self.__hypervisor: self.__hypervisor = XEN_SERVER return SUCCESS except Exception as e: self.__logger.exception("Set hypervisor failed: %s" % e) return FAILED def __createApiClient(self): try: ''' Step1 : Create a CS Connection Object ''' mgmt_details = self.__mgmtDetails self.__csConnection = CSConnection(mgmt_details, self.__asyncTimeOut, self.__logger) ''' Step2 : Create API Client with earlier created connection object ''' self.__apiClient = CloudStackAPIClient(self.__csConnection) ''' Step3: If API Key is not provided as part of Management Details, then verify and register ''' if mgmt_details.apiKey is None: list_user = listUsers.listUsersCmd() list_user.account = "admin" list_user_res = self.__apiClient.listUsers(list_user) if list_user_res is None or \ (validate_list(list_user_res)[0] != PASS): self.__logger.error("API Client Creation Failed") return FAILED user_id = list_user_res[0].id ret = self.__getKeys(user_id) if ret != FAILED: api_key = ret[0] security_key = ret[1] else: self.__logger.error("API Client Creation Failed while Registering User") return FAILED mgmt_details.port = 8080 mgmt_details.apiKey = api_key mgmt_details.securityKey = security_key ''' Now Create the Connection objects and Api Client using new details ''' self.__csConnection = CSConnection(mgmt_details, self.__asyncTimeOut, self.__logger) self.__apiClient = CloudStackAPIClient(self.__csConnection) return SUCCESS except Exception as e: self.__logger.exception("Create API client failed: %s" % e) return FAILED def __createDbConnection(self): ''' @Name : ___createDbConnection @Desc : Creates the CloudStack DB Connection ''' host = "localhost" if self.__dbSvrDetails.dbSvr is None \ else self.__dbSvrDetails.dbSvr port = 3306 if self.__dbSvrDetails.port is None \ else self.__dbSvrDetails.port user = "******" if self.__dbSvrDetails.user is None \ else self.__dbSvrDetails.user passwd = 'cloud' if self.__dbSvrDetails.passwd is None \ else self.__dbSvrDetails.passwd db = 'cloud' if self.__dbSvrDetails.db is None \ else self.__dbSvrDetails.db self.__dbConnection = DbConnection(host, port, user, passwd, db) def __getKeys(self, userid): ''' @Name : ___getKeys @Desc : Retrieves the API and Secret Key for the provided Userid @Input: userid: Userid to register @Output: FAILED or tuple with apikey and secretkey ''' try: register_user = registerUserKeys.registerUserKeysCmd() register_user.id = userid register_user_res = \ self.__apiClient.registerUserKeys(register_user) if not register_user_res: return FAILED return (register_user_res.apikey, register_user_res.secretkey) except Exception as e: self.__logger.exception("Key retrival failed: %s" % e) return FAILED def createTestClient(self): ''' @Name : createTestClient @Desc : Creates the Test Client. The test Client is used by test suites Here we create ParsedTestData Config. Creates a DB Connection. Creates an API Client @Output : FAILED In case of an issue\Failure SUCCESS in case of Success of this function ''' try: ''' 1. Create Config Object Provides the Configuration Object to test suites through getConfigParser. The purpose of this config object is to parse the default config and provide dictionary of the config so users can use that configuration. Users can later call getConfig on this object and it will return the default parsed config dictionary from default configuration file. They can overwrite it with providing their own configuration file as well. ''' ''' 1. Check Config,Zone,Hypervisor Information ''' self.__configObj = ConfigManager(self.__testDataFilePath) if not self.__configObj or not self.__hypervisor: self.__logger.error("Either Hypervisor is None or Not able to create ConfigManager Object") return FAILED self.__parsedTestDataConfig = self.__configObj.getConfig() self.__logger.info("Parsing Test data successful") ''' 2. Create DB Connection ''' self.__createDbConnection() ''' 3. Creates API Client ''' ret = self.__createApiClient() if ret == FAILED: self.__logger.error("=== Test Client Creation Failed ===") else: self.__logger.info("=== Test Client Creation Successful ===") return ret except Exception as e: self.__logger.exception("Test Client creation failed: %s" % e) return FAILED def isAdminContext(self): """ @Name : isAdminContext @Desc:A user is a regular user if he fails to listDomains; if he is a domain-admin, he can list only domains that are non-ROOT; if he is an admin, he can list the ROOT domain successfully """ try: listdom = listDomains.listDomainsCmd() listdom.name = 'ROOT' listdomres = self.__apiClient.listDomains(listdom) if listdomres != FAILED: rootdom = listdomres[0].name if rootdom == 'ROOT': return ADMIN else: return DOMAIN_ADMIN return USER except: return USER def __createUserApiClient(self, UserName, DomainName, acctType=0): ''' @Name : ___createUserApiClient @Desc : Creates a User API Client with given UserName\DomainName Parameters @Input: UserName: Username to be created in cloudstack DomainName: Domain under which the above account be created accType: Type of Account EX: Root,Non Root etc @Output: Return the API client for the user ''' try: if not self.isAdminContext(): return self.__apiClient mgmt_details = self.__mgmtDetails listDomain = listDomains.listDomainsCmd() listDomain.listall = True 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 cmd.domainid = domId cmd.listall = True try: accounts = self.__apiClient.listAccounts(cmd) acctId = accounts[0].id except: createAcctCmd = createAccount.createAccountCmd() createAcctCmd.accounttype = acctType createAcctCmd.domainid = domId createAcctCmd.email = "test-" + random_gen() \ + "@cloudstack.org" createAcctCmd.firstname = UserName createAcctCmd.lastname = UserName createAcctCmd.password = '******' createAcctCmd.username = UserName acct = self.__apiClient.createAccount(createAcctCmd) acctId = acct.id listuser = listUsers.listUsersCmd() listuser.username = UserName listuser.domainid = domId listuser.listall = True listuserRes = self.__apiClient.listUsers(listuser) userId = listuserRes[0].id apiKey = listuserRes[0].apikey securityKey = listuserRes[0].secretkey if apiKey is None: ret = self.__getKeys(userId) if ret != FAILED: mgmt_details.apiKey = ret[0] mgmt_details.securityKey = ret[1] else: self.__logger.error("User API Client Creation While Registering User Failed") return FAILED else: mgmt_details.port = 8080 mgmt_details.apiKey = apiKey mgmt_details.securityKey = securityKey newUserConnection = \ CSConnection(mgmt_details, self.__csConnection.asyncTimeout, self.__csConnection.logger) self.__userApiClient = CloudStackAPIClient(newUserConnection) self.__userApiClient.connection = newUserConnection self.__userApiClient.hypervisor = self.__hypervisor return self.__userApiClient except Exception as e: self.__logger.exception("API user creation failed: %s" % e) return FAILED def close(self): if self.__csConnection is not None: self.__csConnection.close() def getDbConnection(self): ''' @Name : getDbConnection @Desc : Retrieves the DB Connection Handle ''' return self.__dbConnection def getConfigParser(self): ''' @Name : getConfigParser @Desc : Provides the ConfigManager Interface to TestClients ''' return self.__configObj def getApiClient(self): if self.__apiClient: self.__apiClient.id = self.identifier return self.__apiClient return None def getUserApiClient(self, UserName=None, DomainName=None, type=0): """ @Name : getUserApiClient @Desc : Provides the User API Client to test Users 0 - user ; 1 - admin;2 - domain admin @OutPut : FAILED In case of an issue else User API Client """ if UserName is None or DomainName is None: return FAILED return self.__createUserApiClient(UserName, DomainName, type) def submitCmdsAndWait(self, cmds, workers=1, apiclient=None): ''' @Desc : FixME, httplib has issue if more than one thread submitted ''' if not apiclient: apiclient = self.__apiClient if self.__asyncJobMgr is None: self.__asyncJobMgr = asyncJobMgr(apiclient, self.__dbConnection) return self.__asyncJobMgr.submitCmdsAndWait(cmds, workers) def submitJob(self, job, ntimes=1, nums_threads=10, interval=1): ''' @Desc : submit one job and execute the same job ntimes, with nums_threads of threads ''' if self.__asyncJobMgr is None: self.__asyncJobMgr = asyncJobMgr(self.__apiClient, self.__dbConnection) self.__asyncJobMgr.submitJobExecuteNtimes(job, ntimes, nums_threads, interval) def submitJobs(self, jobs, nums_threads=10, interval=1): ''' @Desc :submit n jobs, execute them with nums_threads of threads ''' if self.__asyncJobMgr is None: self.__asyncJobMgr = asyncJobMgr(self.__apiClient, self.__dbConnection) self.__asyncJobMgr.submitJobs(jobs, nums_threads, interval) def getScenarioManager(self, scenarioName, randomizeNames=True, cleanup=True): if not self.__scenarioManager: scenarioHumanReadableName = self.getParsedTestDataConfig()['scenarios'][scenarioName]['metadata']['name'] self.__logger.info("=== Scenario Manager, Scenario \"%s\" Started ===", scenarioHumanReadableName) self.__scenarioManager = TestScenarioManager( api_client=self.getApiClient(), scenario_data=self.getParsedTestDataConfig()['scenarios'][scenarioName], test_data=self.getParsedTestDataConfig()['test_data'], zone=get_zone(self.getApiClient(), self.getZoneForTests()), randomizeNames=randomizeNames, cleanup=cleanup ) try: self.__scenarioManager.setup_infra() except: self.__logger.exception(">>>>>>>>>>>>>>>>>>>>>>> %s ", traceback.format_exc()) raise self.__logger.info("=== Scenario Manager, Scenario \"%s\" Created ===", scenarioHumanReadableName) return self.__scenarioManager def finalize(self): if self.__scenarioManager: self.__scenarioManager.finalize()
def test_create_marvin_log_with_name(self): name = 'test-log' marvin_log = MarvinLog(name) self.assertIsNotNone(marvin_log) self.assertEquals(name, marvin_log.get_logger().name)
class DeployDataCenters(object): """ @Desc : Deploys the Data Center with information provided. Once the Deployment is successful, it will export the DataCenter settings to an obj file ( can be used if wanted to delete the created DC) """ def __init__(self, test_client, cfg): self.__test_client = test_client self.__config = cfg self.__logger = MarvinLog('marvin').get_logger() self.__apiClient = None self.__cleanUp = { } def __persistDcConfig(self): try: ts = strftime("%b_%d_%Y_%H_%M_%S", localtime()) dc_file_path = "dc_entries_" + str(ts) + ".obj" file_to_write = open(dc_file_path, 'w') if file_to_write: pickle.dump(self.__cleanUp, file_to_write) self.__logger.info("=== Data Center Settings are dumped to %s ===" % dc_file_path) except Exception as e: self.__logger.exception("=== Persisting DataCenter config failed: %s ===" % e) def __cleanAndExit(self): try: self.__logger.info("=== deploy dc failed, so cleaning the created entries ===") if not test_data.get("deleteDC", None): self.__logger.info("=== Deploy DC Clean Up flag not set. So, exiting ===") exit(1) self.__logger.info("=== Deploy DC Failed, So Cleaning to Exit ===") remove_dc_obj = DeleteDataCenters(self.__test_client, self.__cleanUp) if remove_dc_obj: if remove_dc_obj.removeDataCenter() == FAILED: self.__logger.error("=== Removing DataCenter Failed ===") else: self.__logger.info("=== Removing DataCenter Successful ===") except Exception as e: self.__logger.exception("=== Clean up failed: %s ===" % e) finally: exit(1) def __addToCleanUp(self, type, id): if type not in self.__cleanUp.keys(): self.__cleanUp[type] = [] self.__cleanUp[type].append(id) if "order" not in self.__cleanUp.keys(): self.__cleanUp["order"] = [] if type not in self.__cleanUp["order"]: self.__cleanUp["order"].append(type) def addHosts(self, hosts, zoneId, podId, clusterId, hypervisor): if hosts is None: self.__logger.warn("=== Invalid Hosts Information ===") return failed_cnt = 0 for host in hosts: try: hostcmd = addHost.addHostCmd() hostcmd.clusterid = clusterId hostcmd.hosttags = host.hosttags hostcmd.hypervisor = host.hypervisor hostcmd.password = host.password hostcmd.podid = podId hostcmd.url = host.url hostcmd.username = host.username hostcmd.zoneid = zoneId hostcmd.hypervisor = hypervisor hostcmd.hosttags = hypervisor ret = self.__apiClient.addHost(hostcmd) if ret: self.__logger.info("=== Add Host Successful ===") self.__addToCleanUp("Host", ret[0].id) except Exception as e: failed_cnt += 1 self.__logger.exception("=== Adding Host (%s) Failed: %s ===" % (str(host.url), e)) if failed_cnt == len(hosts): self.__cleanAndExit() def createClusters(self, clusters, zoneId, podId): try: if clusters is None: return for cluster in clusters: clustercmd = addCluster.addClusterCmd() clustercmd.clustername = cluster.clustername clustercmd.clustertype = cluster.clustertype clustercmd.hypervisor = cluster.hypervisor clustercmd.password = cluster.password clustercmd.podid = podId clustercmd.url = cluster.url clustercmd.username = cluster.username clustercmd.zoneid = zoneId clusterresponse = self.__apiClient.addCluster(clustercmd) if clusterresponse[0].id: clusterId = clusterresponse[0].id self.__logger.debug( "Cluster Name : %s Id : %s Created Successfully" % (str(cluster.clustername), str(clusterId))) self.__addToCleanUp("Cluster", clusterId) self.addHosts(cluster.hosts, zoneId, podId, clusterId, cluster.hypervisor) self.waitForHost(zoneId, clusterId) self.createPrimaryStorages(cluster.primaryStorages, zoneId, podId, clusterId) except Exception as e: self.__logger.exception("=== Cluster %s Creation Failed: %s ===" % (str(cluster.clustername), e)) self.__cleanAndExit() def waitForHost(self, zoneId, clusterId): """ Wait for the hosts in the zoneid, clusterid to be up 2 retries with 30s delay """ try: retry, timeout = 2, 30 cmd = listHosts.listHostsCmd() cmd.clusterid, cmd.zoneid = clusterId, zoneId hosts = self.__apiClient.listHosts(cmd) while retry != 0: for host in hosts: if host.state != 'Up': break sleep(timeout) retry -= 1 except Exception as e: self.__logger.exception("=== List Hosts Failed: %s ===" % e) self.__cleanAndExit() def createPrimaryStorages(self, primaryStorages, zoneId, podId=None, clusterId=None): try: if primaryStorages is None: return for primary in primaryStorages: primarycmd = createStoragePool.createStoragePoolCmd() if primary.details: for key, value in vars(primary.details).iteritems(): primarycmd.details.append({ key: value }) primarycmd.name = primary.name primarycmd.tags = primary.tags primarycmd.url = primary.url if primary.scope == 'zone' or clusterId is None: primarycmd.scope = 'zone' primarycmd.hypervisor = primary.hypervisor primarycmd.tags = 'zone' else: primarycmd.podid = podId primarycmd.clusterid = clusterId primarycmd.tags = 'cluster' primarycmd.zoneid = zoneId ret = self.__apiClient.createStoragePool(primarycmd) if ret.id: self.__logger.info("=== Creating Storage Pool Successful ===") self.__addToCleanUp("StoragePool", ret.id) except Exception as e: self.__logger.exception("=== Create Storage Pool Failed: %s ===" % e) self.__cleanAndExit() def createPods(self, pods, zoneId, networkId=None): try: if pods is None: return for pod in pods: createpod = createPod.createPodCmd() createpod.name = pod.name createpod.gateway = pod.gateway createpod.netmask = pod.netmask createpod.startip = pod.startip createpod.endip = pod.endip createpod.zoneid = zoneId createpodResponse = self.__apiClient.createPod(createpod) if createpodResponse.id: podId = createpodResponse.id self.__logger.debug("Pod Name : %s Id : %s Created Successfully" % (str(pod.name), str(podId))) self.__addToCleanUp("Pod", podId) if pod.guestIpRanges is not None and networkId is not None: self.createVlanIpRanges("Basic", pod.guestIpRanges, zoneId, podId, networkId) self.createClusters(pod.clusters, zoneId, podId) except Exception as e: self.__logger.exception("=== Pod: %s Creation Failed: %s ===" % (str(pod.name), e)) self.__cleanAndExit() def createVlanIpRanges(self, mode, ipranges, zoneId, podId=None, networkId=None, forvirtualnetwork=None): try: if ipranges is None: return for iprange in ipranges: vlanipcmd = createVlanIpRange.createVlanIpRangeCmd() vlanipcmd.account = iprange.account vlanipcmd.domainid = iprange.domainid vlanipcmd.endip = iprange.endip vlanipcmd.gateway = iprange.gateway vlanipcmd.netmask = iprange.netmask vlanipcmd.networkid = networkId vlanipcmd.podid = podId vlanipcmd.startip = iprange.startip vlanipcmd.zoneid = zoneId vlanipcmd.vlan = iprange.vlan if mode == "Basic": if forvirtualnetwork: vlanipcmd.forvirtualnetwork = "true" else: vlanipcmd.forvirtualnetwork = "false" else: vlanipcmd.forvirtualnetwork = "true" ret = self.__apiClient.createVlanIpRange(vlanipcmd) if ret.id: self.__logger.info("=== Creating Vlan Ip Range Successful ===") self.__addToCleanUp("VlanIpRange", ret.id) except Exception as e: self.__logger.exception("=== Create Vlan Ip Range Failed: %s ===" % e) self.__cleanAndExit() def createSecondaryStorages(self, secondaryStorages, zoneId): try: if secondaryStorages is None: return for secondary in secondaryStorages: secondarycmd = addImageStore.addImageStoreCmd() secondarycmd.url = secondary.url secondarycmd.provider = secondary.provider secondarycmd.details = [] if secondarycmd.provider.lower() in ("smb"): for key, value in vars(secondary.details).iteritems(): secondarycmd.details.append({ 'key': key, 'value': value }) if secondarycmd.provider.lower() in ("nfs", "smb"): secondarycmd.zoneid = zoneId ret = self.__apiClient.addImageStore(secondarycmd) if ret.id: self.__logger.info("=== Add Image Store Successful ===") self.__addToCleanUp("ImageStore", ret.id) except Exception as e: self.__logger.exception("=== Add Image Store Failed: %s ===" % e) self.__cleanAndExit() def createCacheStorages(self, cacheStorages, zoneId): try: if cacheStorages is None: return for cache in cacheStorages: cachecmd = createSecondaryStagingStore.createSecondaryStagingStoreCmd() cachecmd.url = cache.url cachecmd.provider = cache.provider cachecmd.zoneid = zoneId cachecmd.details = [] if cache.details: for key, value in vars(cache.details).iteritems(): cachecmd.details.append({ 'key': key, 'value': value }) ret = self.__apiClient.createSecondaryStagingStore(cachecmd) if ret.id: self.__logger.info("=== Creating Secondary StagingStore Successful ===") self.__addToCleanUp("SecondaryStagingStore", ret.id) except Exception as e: self.__logger.exception("=== Creating Secondary Staging Storage Failed: %s ===" % e) self.__cleanAndExit() def createNetworks(self, networks, zoneId): try: if networks is None: return for network in networks: networkcmd = createNetwork.createNetworkCmd() networkcmd.displaytext = network.displaytext networkcmd.name = network.name networkcmd.networkofferingid = network.networkofferingid networkcmd.zoneid = zoneId ipranges = network.ipranges if ipranges: iprange = ipranges.pop() networkcmd.startip = iprange.startip networkcmd.endip = iprange.endip networkcmd.gateway = iprange.gateway networkcmd.netmask = iprange.netmask networkcmdresponse = self.__apiClient.createNetwork(networkcmd) if networkcmdresponse.id: networkId = networkcmdresponse.id self.__logger.info( "=== Creating Network Name : %s Id : %s Successful ===" % (str(network.name), str(networkId))) self.__addToCleanUp("Network", networkId) return networkId except Exception as e: self.__logger.exception("=== Network : %s Creation Failed: %s ===" % (str(network.name), e)) self.__cleanAndExit() def createPhysicalNetwork(self, net, zoneid): try: phynet = createPhysicalNetwork.createPhysicalNetworkCmd() phynet.zoneid = zoneid phynet.name = net.name phynet.isolationmethods = net.isolationmethods phynetwrk = self.__apiClient.createPhysicalNetwork(phynet) if phynetwrk.id: self.__logger.info("=== Creating Physical Network Name : %s Id : %s Successful ===" % ( str(phynet.name), str(phynetwrk.id))) self.__addToCleanUp("PhysicalNetwork", phynetwrk.id) self.addTrafficTypes(phynetwrk.id, net.traffictypes) return phynetwrk except Exception as e: self.__logger.exception("=== Physical Network Creation Failed: %s ===" % e) self.__cleanAndExit() def updatePhysicalNetwork(self, networkid, state="Enabled", vlan=None): try: upnet = updatePhysicalNetwork.updatePhysicalNetworkCmd() upnet.id = networkid upnet.state = state if vlan: upnet.vlan = vlan ret = self.__apiClient.updatePhysicalNetwork(upnet) return ret except Exception as e: self.__logger.exception("=== Update Physical Network Failed: %s ===" % e) self.__cleanAndExit() def enableProvider(self, provider_id): try: upnetprov = \ updateNetworkServiceProvider.updateNetworkServiceProviderCmd() upnetprov.id = provider_id upnetprov.state = "Enabled" ret = self.__apiClient.updateNetworkServiceProvider(upnetprov) if ret.id: self.__logger.info("=== Update Network Service Provider Successfull ===") except Exception as e: self.__logger.exception("=== Update Network Service Provider Failed: %s ===" % e) self.__cleanAndExit() def configureProviders(self, phynetwrk, providers): """ We will enable the virtualrouter elements for all zones. """ try: for provider in providers: pnetprov = listNetworkServiceProviders.listNetworkServiceProvidersCmd() pnetprov.physicalnetworkid = phynetwrk.id pnetprov.state = "Disabled" pnetprov.name = provider.name pnetprovres = self.__apiClient.listNetworkServiceProviders( pnetprov) if pnetprovres and len(pnetprovres) > 0: if provider.name == 'VirtualRouter' or provider.name == 'VpcVirtualRouter': vrprov = listVirtualRouterElements.listVirtualRouterElementsCmd() vrprov.nspid = pnetprovres[0].id vrprovresponse = self.__apiClient.listVirtualRouterElements(vrprov) vrprovid = vrprovresponse[0].id vrconfig = configureVirtualRouterElement.configureVirtualRouterElementCmd() vrconfig.enabled = "true" vrconfig.id = vrprovid self.__apiClient.configureVirtualRouterElement(vrconfig) self.enableProvider(pnetprovres[0].id) elif provider.name in ['NiciraNvp']: netprov = addNetworkServiceProvider.addNetworkServiceProviderCmd() netprov.name = provider.name netprov.physicalnetworkid = phynetwrk.id result = self.__apiClient.addNetworkServiceProvider( netprov) if result.id: self.__logger.info("=== AddNetworkServiceProvider Successful ===") self.__addToCleanUp("NetworkServiceProvider", result.id) if provider.devices is not None: for device in provider.devices: if provider.name == 'NiciraNvp': cmd = addNiciraNvpDevice.addNiciraNvpDeviceCmd() cmd.hostname = device.hostname cmd.username = device.username cmd.password = device.password cmd.transportzoneuuid = device.transportzoneuuid cmd.physicalnetworkid = phynetwrk.id ret = self.__apiClient.addNiciraNvpDevice(cmd) self.__logger.info("=== Add NiciraNvp Successful ===") self.__addToCleanUp("NiciraNvp", ret.id) else: raise InvalidParameterException( "Device %s doesn't match any know provider type" % device) self.enableProvider(result.id) except Exception as e: self.__logger.exception("=== List Network Service Providers Failed: %s ===" % e) self.__cleanAndExit() def addTrafficTypes(self, physical_network_id, traffictypes): [self.addTrafficType(physical_network_id, traffic_type) for traffic_type in traffictypes] def addTrafficType(self, physical_network_id, traffictype): try: traffic_type = addTrafficType.addTrafficTypeCmd() traffic_type.physicalnetworkid = physical_network_id traffic_type.traffictype = traffictype.typ traffic_type.kvmnetworklabel = traffictype.kvm \ if traffictype.kvm is not None else None traffic_type.xennetworklabel = traffictype.xen \ if traffictype.xen is not None else None traffic_type.simulatorlabel = traffictype.simulator \ if traffictype.simulator is not None else None ret = self.__apiClient.addTrafficType(traffic_type) if ret.id: self.__logger.info("=== Add TrafficType Successful ===") self.__addToCleanUp("TrafficType", ret.id) return ret except Exception as e: self.__logger.exception("=== Add TrafficType Failed: %s ===" % e) self.__cleanAndExit() def enableZone(self, zoneid, allocation_state="Enabled"): try: zoneCmd = updateZone.updateZoneCmd() zoneCmd.id = zoneid zoneCmd.allocationstate = allocation_state ret = self.__apiClient.updateZone(zoneCmd) if ret.id: self.__logger.info("=== Enable Zone Successful ===") return ret except Exception as e: self.__logger.exception("=== Enable Zone Failed: %s ===" % e) self.__cleanAndExit() def updateZoneDetails(self, zoneid, details): try: zoneCmd = updateZone.updateZoneCmd() zoneCmd.id = zoneid zoneCmd.details = details ret = self.__apiClient.updateZone(zoneCmd) if ret.id: self.__logger.info("=== Update Zone Successful ===") return ret except Exception as e: self.__logger.exception("=== Update Zone Failed: %s ===" % e) self.__cleanAndExit() def createZone(self, zone, rec=0): try: zoneresponse = self.__apiClient.createZone(zone) if zoneresponse.id: self.__logger.info("=== Create Zone Successful ===") self.__logger.debug("Zone Name : %s Id : %s " % (str(zone.name), str(zoneresponse.id))) self.__addToCleanUp("Zone", zoneresponse.id) return zoneresponse.id else: self.__logger.exception("=== Zone : %s Creation Failed ===" % str(zone.name)) if not rec: zone.name = zone.name + "_" + random_gen() self.__logger.info("=== Recreating Zone With New Name : %s ===" % zone.name) return self.createZone(zone, 1) except Exception as e: self.__logger.exception("=== Create Zone Failed: %s ===" % e) return FAILED def createZones(self, zones): try: for zone in zones: zonecmd = createZone.createZoneCmd() zonecmd.dns1 = zone.dns1 zonecmd.dns2 = zone.dns2 zonecmd.internaldns1 = zone.internaldns1 zonecmd.internaldns2 = zone.internaldns2 zonecmd.name = zone.name zonecmd.localstorageenabled = zone.localstorageenabled zonecmd.networktype = zone.networktype zonecmd.domain = zone.domain zonecmd.guestcidraddress = zone.guestcidraddress zoneId = self.createZone(zonecmd) if zoneId == FAILED: self.__logger.error("=== Zone: %s Creation Failed. So Exiting ===" % str(zone.name)) self.__cleanAndExit() for pnet in zone.physical_networks: phynetwrk = self.createPhysicalNetwork(pnet, zoneId) self.configureProviders(phynetwrk, pnet.providers) self.updatePhysicalNetwork(phynetwrk.id, "Enabled", vlan=pnet.vlan) if zone.networktype == "Basic": listnetworkoffering = listNetworkOfferings.listNetworkOfferingsCmd() if len(filter(lambda x: x.typ == 'Public', zone.physical_networks[0].traffictypes)) > 0: listnetworkoffering.name = "DefaultSharedNetscalerEIPandELBNetworkOffering" else: listnetworkoffering.name = "DefaultSharedNetworkOfferingWithSGService" if zone.networkofferingname is not None: listnetworkoffering.name = zone.networkofferingname listnetworkofferingresponse = self.__apiClient.listNetworkOfferings(listnetworkoffering) guestntwrk = configGenerator.network() guestntwrk.displaytext = "guestNetworkForBasicZone" guestntwrk.name = "guestNetworkForBasicZone" guestntwrk.zoneid = zoneId guestntwrk.networkofferingid = listnetworkofferingresponse[0].id networkid = self.createNetworks([guestntwrk], zoneId) self.createPods(zone.pods, zoneId, networkid) if self.isEipElbZone(zone): self.createVlanIpRanges(zone.networktype, zone.ipranges, zoneId, forvirtualnetwork=True) isPureAdvancedZone = zone.networktype == "Advanced" if isPureAdvancedZone: self.createPods(zone.pods, zoneId) self.createVlanIpRanges(zone.networktype, zone.ipranges, zoneId) '''Note: Swift needs cache storage first''' self.createCacheStorages(zone.cacheStorages, zoneId) self.createSecondaryStorages(zone.secondaryStorages, zoneId) if zone.primaryStorages: self.createPrimaryStorages(zone.primaryStorages, zoneId) enabled = getattr(zone, 'enabled', 'True') if enabled == 'True' or enabled is None: self.enableZone(zoneId, "Enabled") details = getattr(zone, 'details') if details is not None: det = [d.__dict__ for d in details] self.updateZoneDetails(zoneId, det) return except Exception as e: self.__logger.exception("=== Create Zones Failed: %e ===" % e) def isEipElbZone(self, zone): if zone.networktype == "Basic" and len( filter(lambda x: x.typ == 'Public', zone.physical_networks[0].traffictypes)) > 0: return True return False def setClient(self): """ @Name : setClient @Desc : Sets the API Client retrieved from test client """ self.__apiClient = self.__test_client.getApiClient() def updateConfiguration(self, globalCfg): try: if globalCfg is None or self.__apiClient is None: return None for config in globalCfg: updateCfg = updateConfiguration.updateConfigurationCmd() updateCfg.name = config.name updateCfg.value = config.value ret = self.__apiClient.updateConfiguration(updateCfg) if ret.id: self.__logger.info("=== Update Configuration Successfull ===") except Exception as e: self.__logger.exception("=== Update Configuration Failed: %s ===" % e) self.__cleanAndExit() @staticmethod def copyAttributesToCommand(source, command): map(lambda attr: setattr(command, attr, getattr(source, attr, None)), filter(lambda attr: not attr.startswith("__") and attr not in ["required", "isAsync"], dir(command))) def deploy(self): try: self.__logger.info("=== Deploy DC Started ===") ''' Step1 : Set the Client ''' self.setClient() ''' Step2: Update the Configuration ''' self.updateConfiguration(self.__config.globalConfig) ''' Step3 :Deploy the Zone ''' self.createZones(self.__config.zones) ''' Persist the Configuration to an external file post DC creation ''' self.__persistDcConfig() self.__logger.info("=== Deploy DC Successful ===") return SUCCESS except Exception as e: self.__logger.exception("=== Deploy DC Failed: %s ===" % e) self.__cleanAndExit() return FAILED
class MarvinPlugin(Plugin): """ Custom plugin for the cloudstackTestCases to be run using nose """ name = "marvin" def __init__(self): self.__identifier = None self.__testClient = None self.__logFolderPath = None self.__parsedConfig = None ''' Contains Config File ''' self.__configFile = None ''' Signifies the Zone against which all tests will be Run ''' self.__zoneForTests = None ''' Signifies the flag whether to deploy the New DC or Not ''' self.__deployDcFlag = None self.conf = None self.__resultStream = stdout self.__testRunner = None self.__testResult = SUCCESS self.__startTime = None self.__testName = None self.__tcRunLogger = MarvinLog('marvin').get_logger() self.__testModName = '' self.__hypervisorType = None self.__halt_on_failure = False Plugin.__init__(self) def configure(self, options, conf): """enable the marvin plugin when the --with-marvin directive is given to nose. The enableOpt value is set from the command line directive and self.enabled (True|False) determines whether marvin's tests will run. By default non-default plugins like marvin will be disabled """ self.enabled = True if hasattr(options, self.enableOpt): if not getattr(options, self.enableOpt): self.enabled = False return self.__configFile = options.configFile self.__deployDcFlag = options.deployDc self.__zoneForTests = options.zone self.__hypervisorType = options.hypervisor_type self.__halt_on_failure = options.haltOnFailure self.conf = conf if self.startMarvin() == FAILED: self.__tcRunLogger.error( 'Starting Marvin Failed, exiting. Please Check') exit(1) def options(self, parser, env): """ Register command line options """ parser.add_option("--marvin-config", action="store", default=env.get('MARVIN_CONFIG', './datacenter.cfg'), dest="configFile", help="Marvin's configuration file is required." "The config file containing the datacenter and " "other management server " "information is specified") parser.add_option("--deploy", action="store_true", default=False, dest="deployDc", help="Deploys the DC with Given Configuration." "Requires only when DC needs to be deployed") parser.add_option("--zone", action="store", default=None, dest="zone", help="Runs all tests against this specified zone") parser.add_option("--hypervisor", action="store", default=None, dest="hypervisor_type", help="Runs all tests against the specified " "zone and hypervisor Type") parser.add_option("--log-folder-path", action="store", default=None, dest="logFolder", help="Collects all logs under the user specified" "folder") parser.add_option("--halt-on-failure", action="store_true", default=False, dest="haltOnFailure", help="Halt the test on failure") Plugin.options(self, parser, env) def wantClass(self, cls): if cls.__name__ == 'cloudstackTestCase': return False if issubclass(cls, cloudstackTestCase): return True return None def __checkImport(self, filename): ''' @Name : __checkImport @Desc : Verifies if a test module is importable. Returns False or True based upon the result. ''' try: if os.path.isfile(filename): ret = os.path.splitext(filename) if ret[1] == ".py": os.system("python " + filename) return True return False except ImportError as e: self.__tcRunLogger.exception("File %s has import errors: %s" % (filename, e)) return False def wantFile(self, filename): ''' @Desc : Only python files will be used as test modules ''' return self.__checkImport(filename) def loadTestsFromTestCase(self, cls): if cls.__name__ != 'cloudstackTestCase': self.__identifier = cls.__name__ self._injectClients(cls) def beforeTest(self, test): self.__testModName = test.__str__() self.__testName = test.__str__().split()[0] if not self.__testName: self.__testName = "test" self.__testClient.identifier = '-'. \ join([self.__identifier, self.__testName]) if self.__tcRunLogger: self.__tcRunLogger.name = test.__str__() def startTest(self, test): """ Currently used to record start time for tests Dump Start Msg of TestCase to Log """ self.__tcRunLogger.info("=== Started Test %s ===" % str(self.__testName)) self.__startTime = time.time() def printMsg(self, status, tname, err): if status in [FAILED, EXCEPTION] and self.__tcRunLogger: self.__tcRunLogger.fatal("%s: %s: %s" % (status, tname, err)) write_str = "=== TestName: %s | Status : %s ===" % (tname, status) self.__resultStream.write(write_str) self.__tcRunLogger.info(write_str) def addSuccess(self, test, capt): ''' Adds the Success Messages to logs ''' self.printMsg(SUCCESS, self.__testName, "Test Case Passed") self.__testResult = SUCCESS def handleError(self, test, err): ''' Adds Exception throwing test cases and information to log. ''' self.printMsg(EXCEPTION, self.__testName, err) self.__testResult = EXCEPTION def prepareTestRunner(self, runner): if self.__testRunner: return self.__testRunner def handleFailure(self, test, err): ''' Adds Failing test cases and information to log. ''' self.printMsg(FAILED, self.__testName, err) self.__testResult = FAILED def startMarvin(self): ''' @Name : startMarvin @Desc : Initializes the Marvin creates the test Client creates the runlogger for logging Parses the config and creates a parsedconfig Creates a debugstream for tc debug log ''' try: obj_marvininit = MarvinInit(self.__configFile, self.__deployDcFlag, None, self.__zoneForTests, self.__hypervisorType) if obj_marvininit and obj_marvininit.init() == SUCCESS: self.__testClient = obj_marvininit.getTestClient() self.__parsedConfig = obj_marvininit.getParsedConfig() self.__resultStream = obj_marvininit.getResultFile() self.__testRunner = nose.core.TextTestRunner( stream=self.__resultStream, descriptions=True, verbosity=2, config=self.conf) return SUCCESS return FAILED except Exception as e: self.__tcRunLogger.exception( ("=== Start Marvin failed: %s ===" % e)) return FAILED def stopTest(self, test): """ Currently used to record end time for tests """ endTime = time.time() if self.__startTime: totTime = int(endTime - self.__startTime) self.__tcRunLogger.info( "TestCaseName: %s; Time Taken: %s Seconds; StartTime: %s; EndTime: %s; Result: %s" % (self.__testName, str(totTime), str(time.ctime(self.__startTime)), str( time.ctime(endTime)), self.__testResult)) def _injectClients(self, test): setattr(test, "debug", self.__tcRunLogger.debug) setattr(test, "info", self.__tcRunLogger.info) setattr(test, "warn", self.__tcRunLogger.warning) setattr(test, "error", self.__tcRunLogger.error) setattr(test, "testClient", self.__testClient) setattr(test, "config", self.__parsedConfig) if self.__testClient.identifier is None: self.__testClient.identifier = self.__identifier setattr(test, "clstestclient", self.__testClient) if hasattr(test, "user"): # when the class-level attr applied. all test runs as 'user' self.__testClient.getUserApiClient(test.UserName, test.DomainName, test.AcctType) def finalize(self, result): self.__tcRunLogger.info('=== Finalizing... ===') self.__testClient.finalize()
class TestReleaseIP(cloudstackTestCase): def setUp(self): self.logger = MarvinLog(MarvinLog.LOGGER_TEST).get_logger() self.apiclient = self.testClient.getApiClient() self.services = self.testClient.getParsedTestDataConfig() self.vpc_offering = get_default_vpc_offering(self.apiclient) self.network_offering = get_default_network_offering(self.apiclient) # Get Zone, Domain and templates self.domain = get_domain(self.apiclient) self.zone = get_zone(self.apiclient, self.testClient.getZoneForTests()) template = get_template( self.apiclient, self.zone.id ) self.services["virtual_machine"]["zoneid"] = self.zone.id # Create an account, network, VM, Port forwarding rule, LB rules self.account = Account.create( self.apiclient, self.services["account"], admin=True, domainid=self.domain.id ) self.service_offering = get_default_virtual_machine_offering(self.apiclient) self.vpc = VPC.create( self.apiclient, self.services["vpc"], vpcofferingid=self.vpc_offering.id, zoneid=self.zone.id, account=self.account.name, domainid=self.account.domainid) ntwk = Network.create( api_client=self.apiclient, services=self.services["network_1"], accountid=self.account.name, domainid=self.domain.id, networkofferingid=self.network_offering.id, zoneid=self.zone.id, vpcid=self.vpc.id ) networkids = [] networkids.append(ntwk.id) self.virtual_machine = VirtualMachine.create( self.apiclient, self.services["virtual_machine"], templateid=template.id, accountid=self.account.name, domainid=self.account.domainid, serviceofferingid=self.service_offering.id, networkids=networkids ) self.ip_address = PublicIPAddress.create( self.apiclient, self.account.name, self.zone.id, self.account.domainid, vpcid=self.vpc.id ) ip_addrs = list_public_ip( self.apiclient, account=self.account.name, domainid=self.account.domainid, issourcenat=False ) try: self.ip_addr = ip_addrs[0] except Exception as e: raise Exception( "Failed: During acquiring source NAT for account: %s, :%s" % (self.account.name, e)) self.nat_rule = NATRule.create( self.apiclient, self.virtual_machine, self.services["natrule"], self.ip_addr.id, networkid=ntwk.id ) self.lb_rule = LoadBalancerRule.create( self.apiclient, self.services["lbrule"], self.ip_addr.id, accountid=self.account.name, networkid=ntwk.id ) self.cleanup = [ self.virtual_machine, self.account ] return def tearDown(self): cleanup_resources(self.apiclient, self.cleanup) @attr(tags=['advanced']) def test_01_release_ip(self): """Test for release public IP address""" self.logger.debug("Deleting Public IP : %s" % self.ip_addr.id) self.ip_address.delete(self.apiclient) retriesCount = 10 isIpAddressDisassociated = False while retriesCount > 0: listResponse = list_public_ip( self.apiclient, id=self.ip_addr.id ) if listResponse is None: isIpAddressDisassociated = True break retriesCount -= 1 time.sleep(60) # End while self.assertTrue( isIpAddressDisassociated, "Failed to disassociate IP address") # ListPortForwardingRules should not list # associated rules with Public IP address try: list_nat_rule = list_nat_rules( self.apiclient, id=self.nat_rule.id ) self.logger.debug("List NAT Rule response" + str(list_nat_rule)) except CloudstackAPIException: self.logger.debug("Port Forwarding Rule is deleted") # listLoadBalancerRules should not list # associated rules with Public IP address try: list_lb_rule = list_lb_rules( self.apiclient, id=self.lb_rule.id ) self.logger.debug("List LB Rule response" + str(list_lb_rule)) except CloudstackAPIException: self.logger.debug("Port Forwarding Rule is deleted") # SSH Attempt though public IP should fail with self.assertRaises(Exception): SshClient( self.ip_addr.ipaddress, self.services["natrule"]["publicport"], self.virtual_machine.username, self.virtual_machine.password, retries=2, delay=0 ) return
def setUp(self): self.logger = MarvinLog(MarvinLog.LOGGER_TEST).get_logger() self.apiclient = self.testClient.getApiClient() self.services = self.testClient.getParsedTestDataConfig() self.vpc_offering = get_default_vpc_offering(self.apiclient) self.network_offering = get_default_network_offering(self.apiclient) # Get Zone, Domain and templates self.domain = get_domain(self.apiclient) self.zone = get_zone(self.apiclient, self.testClient.getZoneForTests()) template = get_template( self.apiclient, self.zone.id ) self.services["virtual_machine"]["zoneid"] = self.zone.id # Create an account, network, VM, Port forwarding rule, LB rules self.account = Account.create( self.apiclient, self.services["account"], admin=True, domainid=self.domain.id ) self.service_offering = get_default_virtual_machine_offering(self.apiclient) self.vpc = VPC.create( self.apiclient, self.services["vpc"], vpcofferingid=self.vpc_offering.id, zoneid=self.zone.id, account=self.account.name, domainid=self.account.domainid) ntwk = Network.create( api_client=self.apiclient, services=self.services["network_1"], accountid=self.account.name, domainid=self.domain.id, networkofferingid=self.network_offering.id, zoneid=self.zone.id, vpcid=self.vpc.id ) networkids = [] networkids.append(ntwk.id) self.virtual_machine = VirtualMachine.create( self.apiclient, self.services["virtual_machine"], templateid=template.id, accountid=self.account.name, domainid=self.account.domainid, serviceofferingid=self.service_offering.id, networkids=networkids ) self.ip_address = PublicIPAddress.create( self.apiclient, self.account.name, self.zone.id, self.account.domainid, vpcid=self.vpc.id ) ip_addrs = list_public_ip( self.apiclient, account=self.account.name, domainid=self.account.domainid, issourcenat=False ) try: self.ip_addr = ip_addrs[0] except Exception as e: raise Exception( "Failed: During acquiring source NAT for account: %s, :%s" % (self.account.name, e)) self.nat_rule = NATRule.create( self.apiclient, self.virtual_machine, self.services["natrule"], self.ip_addr.id, networkid=ntwk.id ) self.lb_rule = LoadBalancerRule.create( self.apiclient, self.services["lbrule"], self.ip_addr.id, accountid=self.account.name, networkid=ntwk.id ) self.cleanup = [ self.virtual_machine, self.account ] return
def setUp(self): self.logger = MarvinLog(MarvinLog.LOGGER_TEST).get_logger() self.apiclient = self.testClient.getApiClient() self.services = self.testClient.getParsedTestDataConfig() # Get Zone, Domain and templates self.domain = get_domain(self.apiclient) self.zone = get_zone(self.apiclient, self.testClient.getZoneForTests()) template = get_template( self.apiclient, self.zone.id ) self.services["virtual_machine"]["zoneid"] = self.zone.id # Create an account, network, VM and IP addresses self.account = Account.create( self.apiclient, self.services["account"], admin=True, domainid=self.domain.id ) self.vpc_offering = get_default_vpc_offering(self.apiclient) self.logger.debug("VPC Offering '%s' selected", self.vpc_offering.name) self.network_offering = get_default_network_offering(self.apiclient) self.logger.debug("Network Offering '%s' selected", self.network_offering.name) self.virtual_machine_offering = get_default_virtual_machine_offering(self.apiclient) self.logger.debug("Virtual Machine Offering '%s' selected", self.virtual_machine_offering.name) self.default_allow_acl = get_network_acl(self.apiclient, 'default_allow') self.logger.debug("ACL '%s' selected", self.default_allow_acl.name) self.template = get_template(self.apiclient, self.zone.id) self.logger.debug("Template '%s' selected" % self.template.name) self.vpc1 = VPC.create(self.apiclient, self.services['vpcs']['vpc1'], vpcofferingid=self.vpc_offering.id, zoneid=self.zone.id, domainid=self.domain.id, account=self.account.name) self.logger.debug("VPC '%s' created, CIDR: %s", self.vpc1.name, self.vpc1.cidr) self.network1 = Network.create(self.apiclient, self.services['networks']['network1'], networkofferingid=self.network_offering.id, aclid=self.default_allow_acl.id, vpcid=self.vpc1.id, zoneid=self.zone.id, domainid=self.domain.id, accountid=self.account.name) self.logger.debug("Network '%s' created, CIDR: %s, Gateway: %s", self.network1.name, self.network1.cidr, self.network1.gateway) self.vm1 = VirtualMachine.create(self.apiclient, self.services['vms']['vm1'], templateid=self.template.id, serviceofferingid=self.virtual_machine_offering.id, networkids=[self.network1.id], zoneid=self.zone.id, domainid=self.domain.id, accountid=self.account.name) self.logger.debug("VM '%s' created, Network: %s, IP %s", self.vm1.name, self.network1.name, self.vm1.nic[0].ipaddress) src_nat_ip_addrs = list_public_ip( self.apiclient, account=self.account.name, domainid=self.account.domainid ) try: src_nat_ip_addr = src_nat_ip_addrs[0] except Exception as e: self.fail("SSH failed for VM with IP: %s %s" % (src_nat_ip_addr.ipaddress, e)) self.lb_rule = LoadBalancerRule.create( self.apiclient, self.services["lbrule"], src_nat_ip_addr.id, self.account.name, self.network1.id, self.vpc1.id ) self.lb_rule.assign(self.apiclient, [self.vm1]) self.nat_rule = NATRule.create( self.apiclient, self.vm1, self.services["natrule"], src_nat_ip_addr.id ) self.cleanup = [] return
class TestDeleteAccount(cloudstackTestCase): def setUp(self): self.logger = MarvinLog(MarvinLog.LOGGER_TEST).get_logger() self.apiclient = self.testClient.getApiClient() self.services = self.testClient.getParsedTestDataConfig() # Get Zone, Domain and templates self.domain = get_domain(self.apiclient) self.zone = get_zone(self.apiclient, self.testClient.getZoneForTests()) template = get_template( self.apiclient, self.zone.id ) self.services["virtual_machine"]["zoneid"] = self.zone.id # Create an account, network, VM and IP addresses self.account = Account.create( self.apiclient, self.services["account"], admin=True, domainid=self.domain.id ) self.vpc_offering = get_default_vpc_offering(self.apiclient) self.logger.debug("VPC Offering '%s' selected", self.vpc_offering.name) self.network_offering = get_default_network_offering(self.apiclient) self.logger.debug("Network Offering '%s' selected", self.network_offering.name) self.virtual_machine_offering = get_default_virtual_machine_offering(self.apiclient) self.logger.debug("Virtual Machine Offering '%s' selected", self.virtual_machine_offering.name) self.default_allow_acl = get_network_acl(self.apiclient, 'default_allow') self.logger.debug("ACL '%s' selected", self.default_allow_acl.name) self.template = get_template(self.apiclient, self.zone.id) self.logger.debug("Template '%s' selected" % self.template.name) self.vpc1 = VPC.create(self.apiclient, self.services['vpcs']['vpc1'], vpcofferingid=self.vpc_offering.id, zoneid=self.zone.id, domainid=self.domain.id, account=self.account.name) self.logger.debug("VPC '%s' created, CIDR: %s", self.vpc1.name, self.vpc1.cidr) self.network1 = Network.create(self.apiclient, self.services['networks']['network1'], networkofferingid=self.network_offering.id, aclid=self.default_allow_acl.id, vpcid=self.vpc1.id, zoneid=self.zone.id, domainid=self.domain.id, accountid=self.account.name) self.logger.debug("Network '%s' created, CIDR: %s, Gateway: %s", self.network1.name, self.network1.cidr, self.network1.gateway) self.vm1 = VirtualMachine.create(self.apiclient, self.services['vms']['vm1'], templateid=self.template.id, serviceofferingid=self.virtual_machine_offering.id, networkids=[self.network1.id], zoneid=self.zone.id, domainid=self.domain.id, accountid=self.account.name) self.logger.debug("VM '%s' created, Network: %s, IP %s", self.vm1.name, self.network1.name, self.vm1.nic[0].ipaddress) src_nat_ip_addrs = list_public_ip( self.apiclient, account=self.account.name, domainid=self.account.domainid ) try: src_nat_ip_addr = src_nat_ip_addrs[0] except Exception as e: self.fail("SSH failed for VM with IP: %s %s" % (src_nat_ip_addr.ipaddress, e)) self.lb_rule = LoadBalancerRule.create( self.apiclient, self.services["lbrule"], src_nat_ip_addr.id, self.account.name, self.network1.id, self.vpc1.id ) self.lb_rule.assign(self.apiclient, [self.vm1]) self.nat_rule = NATRule.create( self.apiclient, self.vm1, self.services["natrule"], src_nat_ip_addr.id ) self.cleanup = [] return def tearDown(self): cleanup_resources(self.apiclient, self.cleanup) return @attr(tags=['advanced']) def test_01_getsecretkey(self): """Test if we can get the secretkey""" userkeys = None userinfo = None try: userkeys = User.registerUserKeys(self.apiclient, self.account.user[0].id) except CloudstackAPIException: self.logger.debug("Registered user keys") except Exception as e: self.logger.debug("Exception %s raised while registering user keys" % e) try: userinfo = User.list(self.apiclient, id=self.account.user[0].id)[0] except CloudstackAPIException: self.logger.debug("Retrieved user") except Exception as e: self.logger.debug("Exception %s raised while retrieving user" % e) self.assertEqual( userkeys.apikey, userinfo.apikey, "API key is different" ) self.assertNotEqual( userkeys.secretkey, userinfo.secretkey, "Secret key is visible" ) return @attr(tags=['advanced']) def test_02_delete_account(self): """Test for delete account""" # Validate the Following # 1. after account.cleanup.interval (global setting) # time all the PF/LB rules should be deleted # 2. verify that list(LoadBalancer/PortForwarding)Rules # API does not return any rules for the account # 3. The domR should have been expunged for this account self.account.delete(self.apiclient) interval = list_configurations( self.apiclient, name='account.cleanup.interval' ) self.assertEqual( isinstance(interval, list), True, "Check if account.cleanup.interval config present" ) # Sleep to ensure that all resources are deleted time.sleep(int(interval[0].value)) # ListLoadBalancerRules should not list # associated rules with deleted account # Unable to find account testuser1 in domain 1 : Exception try: list_lb_rules( self.apiclient, account=self.account.name, domainid=self.account.domainid ) except CloudstackAPIException: self.logger.debug("Port Forwarding Rule is deleted") # ListPortForwardingRules should not # list associated rules with deleted account try: list_nat_rules( self.apiclient, account=self.account.name, domainid=self.account.domainid ) except CloudstackAPIException: self.logger.debug("NATRule is deleted") # Retrieve router for the user account try: routers = list_routers( self.apiclient, account=self.account.name, domainid=self.account.domainid ) self.assertEqual( routers, None, "Check routers are properly deleted." ) except CloudstackAPIException: self.logger.debug("Router is deleted") except Exception as e: raise Exception( "Encountered %s raised while fetching routers for account: %s" % (e, self.account.name)) return
class SshClient(object): """ @Desc : SSH Library for Marvin. Facilitates SSH,SCP services to marvin users @Input: host: Host to connect port: port on host to connect user: Username to be used for connecting passwd: Password for connection retries and delay applies for establishing connection timeout : Applies while executing command """ def __init__(self, host, port, user, password, retries=60, delay=10, key_pair_files=None, timeout=10.0): self.host = None self.port = 22 self.user = user self.passwd = password self.keyPairFiles = key_pair_files self.ssh = SSHClient() self.ssh.set_missing_host_key_policy(AutoAddPolicy()) self.retryCnt = 0 self.delay = 0 self.timeout = 3.0 self.logger = MarvinLog('ssh').get_logger() # Check invalid host value and raise exception # At least host is required for connection if host is not None and host != '': self.host = host if retries is not None and retries > 0: self.retryCnt = retries if delay is not None and delay > 0: self.delay = delay if timeout is not None and timeout > 0: self.timeout = timeout if port is not None and port >= 0: self.port = port if self.create_connection() == FAILED: raise internalError("Connection Failed") def execute(self, command): stdin, stdout, stderr = self.ssh.exec_command(command) output = stdout.readlines() errors = stderr.readlines() results = [] if output is not None and len(output) == 0: if errors is not None and len(errors) > 0: for error in errors: results.append(error.rstrip()) else: for strOut in output: results.append(strOut.rstrip()) self.logger.debug("Executing command via host %s: %s Output: %s" % (str(self.host), command, results)) return results def create_connection(self): """ @Name: createConnection @Desc: Creates an ssh connection for retries mentioned,along with sleep mentioned @Output: SUCCESS on successful connection FAILED If connection through ssh failed """ ret = FAILED while self.retryCnt >= 0: try: self.logger.debug("Trying SSH Connection to host %s on port %s as user %s. RetryCount: %s" % (self.host, str(self.port), self.user, str(self.retryCnt))) if self.keyPairFiles is None: self.ssh.connect(hostname=self.host, port=self.port, username=self.user, password=self.passwd, timeout=self.timeout) else: self.ssh.connect(hostname=self.host, port=self.port, username=self.user, password=self.passwd, key_filename=self.keyPairFiles, timeout=self.timeout, look_for_keys=False ) self.logger.debug("Connection to host %s on port %s is SUCCESSFUL" % (str(self.host), str(self.port))) ret = SUCCESS break except BadHostKeyException as e: self.logger.debug("Failed to create connection: %s" % e) except AuthenticationException as e: self.logger.debug("Failed to create connection: %s" % e) except SSHException as e: self.logger.debug("Failed to create connection: %s" % e) except socket.error as e: self.logger.debug("Failed to create connection: %s" % e) except Exception as e: self.logger.debug("Failed to create connection: %s" % e) finally: if self.retryCnt == 0 or ret == SUCCESS: break self.retryCnt -= 1 time.sleep(self.delay) return ret def run_command(self, command): """ @Name: runCommand @Desc: Runs a command over ssh and returns the result along with status code @Input: command to execute @Output: 1: status of command executed. SUCCESS : If command execution is successful FAILED : If command execution has failed 2: stdin,stdout,stderr values of command output """ ret = {"status": FAILED, "stdin": None, "stdout": None, "stderr": INVALID_INPUT} if command is None or command == '': return ret try: stdin, stdout, stderr = self.ssh.exec_command(command, timeout=self.timeout) if stdout is not None: status_check = stdout.channel.recv_exit_status() if status_check == 0: ret["status"] = SUCCESS ret["stdout"] = stdout.readlines() if stderr is not None: ret["stderr"] = stderr.readlines() except Exception as e: self.logger.debug("Failed to run command: %s" % e) finally: self.logger.debug("Connection to host %s on port %s is SUCCESSFUL" % (str(self.host), command)) return ret def scp(self, src_file, dest_path): transport = Transport((self.host, int(self.port))) transport.connect(username=self.user, password=self.passwd) sftp = SFTPClient.from_transport(transport) try: sftp.put(src_file, dest_path) except IOError as e: raise e def __del__(self): self.close() def close(self): if self.ssh is not None: self.ssh.close() self.ssh = None
class MarvinInit: def __init__(self, config_file, deploy_dc_flag=False, test_mod_name="deploydc", zone=None, hypervisor_type=None, halt_on_failure=False): self.__configFile = config_file self.__deployFlag = deploy_dc_flag self.__tcRunLogger = MarvinLog('marvin').get_logger() self.__tcRunLogger.info("=== Marvin Init Logging Successful ===") self.__testModName = test_mod_name self.__testClient = None self.__tcResultFile = None self.__testDataFilePath = None self.__zoneForTests = zone self.__parsedConfig = None self.__hypervisorType = hypervisor_type self.__halt_on_failure = halt_on_failure def __parseConfig(self): ''' @Name: __parseConfig @Desc : Parses the configuration file passed and assigns the parsed configuration @Output : SUCCESS or FAILED ''' try: if not os.path.isfile(self.__configFile): self.__tcRunLogger.error( "=== Marvin Parse Config Init Failed ===") return FAILED self.__parsedConfig = getSetupConfig(self.__configFile) self.__tcRunLogger.info("=== Marvin Parse Config Successful ===") return SUCCESS except Exception as e: self.__tcRunLogger.exception( "=== Marvin Parse Config Init Failed: %s ===" % e) return FAILED def getParsedConfig(self): return self.__parsedConfig def getLogFolderPath(self): return self.__logFolderPath def getTestClient(self): return self.__testClient def getLogger(self): return self.__tcRunLogger def getResultFile(self): ''' @Name : getDebugFile @Desc : Creates the result file at a given path. @Output : Returns the Result file to be used for writing test outputs ''' self.__tcResultFile = open("results.txt", "w") return self.__tcResultFile def __setHypervisorAndZoneInfo(self): ''' @Name : __setHypervisorAndZoneInfo @Desc: Set the HyperVisor and Zone details; default to XenServer ''' try: if not self.__hypervisorType: if self.__parsedConfig and self.__parsedConfig.zones is not None: for zone in self.__parsedConfig.zones: for pod in zone.pods: if pod is not None: for cluster in pod.clusters: if cluster is not None and cluster.hypervisor is not None: self.__hypervisorType = cluster.hypervisor break if not self.__zoneForTests: if self.__parsedConfig and self.__parsedConfig.zones is not None: for zone in self.__parsedConfig.zones: self.__zoneForTests = zone.name break if not self.__hypervisorType: self.__hypervisorType = XEN_SERVER return SUCCESS except Exception as e: self.__tcRunLogger.exception( "=== Set Hypervizor and Zone info Failed: %s ===" % e) return FAILED def init(self): ''' @Name : init @Desc :Initializes the marvin by 1. Parsing the configuration and creating a parsed config structure 2. Creates a timestamped log folder and provides all logs to be dumped there 3. Creates the DataCenter based upon configuration provided @Output : SUCCESS or FAILED ''' try: self.__tcRunLogger.info("=== Marvin Init Started ===") if ((self.__parseConfig() != FAILED) and (self.__setHypervisorAndZoneInfo()) and (self.__setTestDataPath() != FAILED) and (self.__createTestClient() != FAILED) and (self.__deployDC() != FAILED)): self.__tcRunLogger.info("=== Marvin Init Successful ===") return SUCCESS self.__tcRunLogger.error("=== Marvin Init Failed ===") return FAILED except Exception as e: self.__tcRunLogger.exception( "=== Marvin Init Failed with exception: %s ===" % e) return FAILED def __createTestClient(self): ''' @Name : __createTestClient @Desc : Creates the TestClient during init based upon the parameters provided @Output: Returns SUCCESS or FAILED ''' try: mgt_details = self.__parsedConfig.mgtSvr[0] dbsvr_details = self.__parsedConfig.dbSvr self.__testClient = CSTestClient( mgt_details, dbsvr_details, test_data_filepath=self.__testDataFilePath, zone=self.__zoneForTests, hypervisor_type=self.__hypervisorType, halt_on_failure=self.__halt_on_failure) if self.__testClient: return self.__testClient.createTestClient() return FAILED except Exception as e: self.__tcRunLogger.exception( "=== Marvin Create Test Client Failed: %s ===" % e) return FAILED def __setTestDataPath(self): ''' @Name : __setTestDataPath @Desc : Sets the TestData Path for tests to run @Output:Returns SUCCESS or FAILED ''' try: if ((self.__parsedConfig.TestData is not None) and (self.__parsedConfig.TestData.Path is not None)): self.__testDataFilePath = self.__parsedConfig.TestData.Path self.__tcRunLogger.info( "=== Marvin Setting TestData Successful ===") return SUCCESS except Exception as e: self.__tcRunLogger.exception( "=== Marvin Setting TestData Successful Failed: %s ===" % e) return FAILED def __deployDC(self): ''' @Name : __deployDC @Desc : Deploy the DataCenter and returns accordingly. @Output: SUCCESS or FAILED ''' try: ret = SUCCESS if self.__deployFlag: deploy_obj = DeployDataCenters(self.__testClient, self.__parsedConfig, self.__tcRunLogger) ret = deploy_obj.deploy() if ret != SUCCESS: self.__tcRunLogger.error("=== Deploy DC Failed ===") return ret except Exception as e: self.__tcRunLogger.exception( "=== Deploy DC Failed with exception: %s ===" % e) return FAILED
class TestSSVMs(cloudstackTestCase): def setUp(self): self.logger = MarvinLog(MarvinLog.LOGGER_TEST).get_logger() self.apiclient = self.testClient.getApiClient() self.hypervisor = self.testClient.getHypervisorInfo() self.cleanup = [] self.services = self.testClient.getParsedTestDataConfig() self.zone = get_zone(self.apiclient, self.testClient.getZoneForTests()) self.services["sleep"] = 5 self.services["timeout"] = 180 return def tearDown(self): try: # Clean up, terminate the created templates cleanup_resources(self.apiclient, self.cleanup) except Exception as e: raise Exception("Warning: Exception during cleanup : %s" % e) return @attr(tags=['advanced']) def test_01_list_sec_storage_vm(self): self.test_list_svm_vm('secondarystoragevm') @attr(tags=['advanced']) def test_02_list_cpvm_vm(self): self.test_list_svm_vm('consoleproxy') @attr(tags=['advanced']) def test_03_destroy_ssvm(self): """Test destroy SSVM """ # Validate the following # 1. SSVM should be completely destroyed and a new one will spin up # 2. listSystemVMs will show a different name for the # systemVM from what it was before # 3. new SSVM will have a public/private and link-local-ip # 4. cloud process within SSVM must be up and running list_ssvm_response = list_ssvms( self.apiclient, systemvmtype='secondarystoragevm', state='Running', zoneid=self.zone.id ) self.assertEqual( isinstance(list_ssvm_response, list), True, "Check list response returns a valid list" ) ssvm_response = list_ssvm_response[0] old_name = ssvm_response.name self.logger.debug("Destroying SSVM: %s" % ssvm_response.id) cmd = destroySystemVm.destroySystemVmCmd() cmd.id = ssvm_response.id self.apiclient.destroySystemVm(cmd) timeout = self.services["timeout"] while True: list_ssvm_response = list_ssvms( self.apiclient, zoneid=self.zone.id, systemvmtype='secondarystoragevm' ) if isinstance(list_ssvm_response, list): if list_ssvm_response[0].state == 'Running': break if timeout == 0: self.logger.debug( "Warning: List SSVM didn't return systemvms in Running state. This is a known issue, ignoring it for now!") return time.sleep(self.services["sleep"]) timeout = timeout - 1 ssvm_response = list_ssvm_response[0] # Verify Name, Public IP, Private IP and Link local IP # for newly created SSVM self.assertNotEqual( ssvm_response.name, old_name, "Check SSVM new name with name of destroyed SSVM" ) self.assertEqual( hasattr(ssvm_response, 'privateip'), True, "Check whether SSVM has private IP field" ) self.assertEqual( hasattr(ssvm_response, 'linklocalip'), True, "Check whether SSVM has link local IP field" ) self.assertEqual( hasattr(ssvm_response, 'publicip'), True, "Check whether SSVM has public IP field" ) # Wait for the agent to be up self.wait_for_system_vm_agent(ssvm_response.name) return @attr(tags=['advanced']) def test_04_destroy_cpvm(self): """Test destroy CPVM """ # Validate the following # 1. CPVM should be completely destroyed and a new one will spin up # 2. listSystemVMs will show a different name for the systemVM from # what it was before # 3. new CPVM will have a public/private and link-local-ip # 4. cloud process within CPVM must be up and running list_cpvm_response = list_ssvms( self.apiclient, systemvmtype='consoleproxy', zoneid=self.zone.id ) self.assertEqual( isinstance(list_cpvm_response, list), True, "Check list response returns a valid list" ) cpvm_response = list_cpvm_response[0] old_name = cpvm_response.name self.logger.debug("Destroying CPVM: %s" % cpvm_response.id) cmd = destroySystemVm.destroySystemVmCmd() cmd.id = cpvm_response.id self.apiclient.destroySystemVm(cmd) timeout = self.services["timeout"] while True: list_cpvm_response = list_ssvms( self.apiclient, systemvmtype='consoleproxy', zoneid=self.zone.id ) if isinstance(list_cpvm_response, list): if list_cpvm_response[0].state == 'Running': break if timeout == 0: self.logger.debug( "Warning: List CPVM didn't return systemvms in Running state. This is a known issue, ignoring it for now!") return time.sleep(self.services["sleep"]) timeout = timeout - 1 cpvm_response = list_cpvm_response[0] # Verify Name, Public IP, Private IP and Link local IP # for newly created CPVM self.assertNotEqual( cpvm_response.name, old_name, "Check SSVM new name with name of destroyed CPVM" ) self.assertEqual( hasattr(cpvm_response, 'privateip'), True, "Check whether CPVM has private IP field" ) self.assertEqual( hasattr(cpvm_response, 'linklocalip'), True, "Check whether CPVM has link local IP field" ) self.assertEqual( hasattr(cpvm_response, 'publicip'), True, "Check whether CPVM has public IP field" ) # Wait for the agent to be up self.wait_for_system_vm_agent(cpvm_response.name) return def wait_for_system_vm_agent(self, vmname): self.logger.debug("Waiting for system VM %s agent to be UP" % vmname) timeout = self.services["timeout"] sleep_interval = self.services["sleep"] while timeout > 0: list_host_response = list_hosts( self.apiclient, name=vmname ) if list_host_response and list_host_response[0].state == 'Up': self.debug("System VM %s agent is UP" % vmname) break time.sleep(sleep_interval) timeout = timeout - sleep_interval if timeout <= 0 and list_host_response[0].state != 'Up': self.logger.debug( "Warning: List CPVM didn't return systemvms in Running state. This is a known issue, ignoring it for now!") return def test_list_svm_vm(self, svm_type): # Validate the following: # 1. listSystemVM # should return only ONE SVM per zone # 2. The returned SVM should be in Running state # 3. listSystemVM for should list publicip, privateip and link-localip # 4. The gateway programmed on the SVM by listSystemVm should be # the same as the gateway returned by listVlanIpRanges # 5. DNS entries must match those given for the zone list_svm_response = list_ssvms( self.apiclient, systemvmtype=svm_type, state='Running', ) self.assertEqual( isinstance(list_svm_response, list), True, "Check list response returns a valid list" ) # Verify SSVM response self.assertNotEqual( len(list_svm_response), 0, "Check list System VMs response" ) list_zones_response = list_zones(self.apiclient) self.assertEqual( isinstance(list_zones_response, list), True, "Check list response returns a valid list" ) self.logger.debug("Number of zones: %s" % len(list_zones_response)) self.logger.debug("Number of System VMs: %s" % len(list_svm_response)) # Number of Sec storage VMs = No of Zones self.assertEqual( len(list_svm_response), len(list_zones_response), "Check number of System VMs with number of zones" ) # For each secondary storage VM check private IP, # public IP, link local IP and DNS for svm in list_svm_response: self.logger.debug("SVM state: %s" % svm.state) self.assertEqual( svm.state, 'Running', "Check whether state of System VM is running" ) self.assertEqual( hasattr(svm, 'privateip'), True, "Check whether System VM has private IP field" ) self.assertEqual( hasattr(svm, 'linklocalip'), True, "Check whether System VM has link local IP field" ) self.assertEqual( hasattr(svm, 'publicip'), True, "Check whether System VM has public IP field" ) # Fetch corresponding ip ranges information from listVlanIpRanges ipranges_response = list_vlan_ipranges( self.apiclient, zoneid=svm.zoneid ) self.assertEqual( isinstance(ipranges_response, list), True, "Check list response returns a valid list" ) iprange = ipranges_response[0] # Execute the following assertion in all zones except basic Zones if not (self.zone.networktype.lower() == 'basic'): self.assertEqual( svm.gateway, iprange.gateway, "Check gateway with that of corresponding ip range" ) # Fetch corresponding zone information from listZones zone_response = list_zones( self.apiclient, id=svm.zoneid ) self.assertEqual( isinstance(zone_response, list), True, "Check list response returns a valid list" ) self.assertEqual( svm.dns1, zone_response[0].dns1, "Check DNS1 with that of corresponding zone" ) self.assertEqual( svm.dns2, zone_response[0].dns2, "Check DNS2 with that of corresponding zone" ) return
def setUpClass(cls): cls.logger = MarvinLog(MarvinLog.LOGGER_TEST).get_logger() # We want to fail quicker if it's failure socket.setdefaulttimeout(60) cls.testClient = super(TestRouterIpTablesPolicies, cls).getClsTestClient() cls.apiclient = cls.testClient.getApiClient() cls.services = cls.testClient.getParsedTestDataConfig() # Get Zone, Domain and templates cls.domain = get_domain(cls.apiclient) cls.zone = get_zone(cls.apiclient, cls.testClient.getZoneForTests()) cls.template = get_template(cls.apiclient, cls.zone.id) cls.services["vpc"]["cidr"] = '10.1.1.1/16' cls.services["virtual_machine"]["zoneid"] = cls.zone.id cls.services["virtual_machine"]["template"] = cls.template.id cls.vpc_offering = get_default_vpc_offering(cls.apiclient) cls.logger.debug("VPC Offering '%s' selected", cls.vpc_offering.name) cls.network_offering = get_default_network_offering(cls.apiclient) cls.logger.debug("Network Offering '%s' selected", cls.network_offering.name) cls.default_allow_acl = get_network_acl(cls.apiclient, 'default_allow') cls.logger.debug("ACL '%s' selected", cls.default_allow_acl.name) cls.account = Account.create(cls.apiclient, cls.services["account"], admin=True, domainid=cls.domain.id) cls.vpc1 = VPC.create(cls.apiclient, cls.services['vpcs']['vpc1'], vpcofferingid=cls.vpc_offering.id, zoneid=cls.zone.id, domainid=cls.domain.id, account=cls.account.name) cls.logger.debug("VPC '%s' created, CIDR: %s", cls.vpc1.name, cls.vpc1.cidr) cls.network1 = Network.create( cls.apiclient, cls.services['networks']['network1'], networkofferingid=cls.network_offering.id, aclid=cls.default_allow_acl.id, vpcid=cls.vpc1.id, zoneid=cls.zone.id, domainid=cls.domain.id, accountid=cls.account.name) cls.logger.debug("Network '%s' created, CIDR: %s, Gateway: %s", cls.network1.name, cls.network1.cidr, cls.network1.gateway) cls.service_offering = get_default_virtual_machine_offering( cls.apiclient) cls.entity_manager = EntityManager(cls.apiclient, cls.services, cls.service_offering, cls.account, cls.zone, cls.network1, cls.logger) cls._cleanup = [cls.account] return
def test_create_marvin_log_with_default_name(self): marvin_log = MarvinLog() self.assertIsNotNone(marvin_log) self.assertEquals('marvin.utils.MarvinLog', marvin_log.get_logger().name)
class DeleteDataCenters: """ @Desc : Deletes the Data Center using the settings provided. test_client :Client for deleting the DC. dc_cfg_file : obj file exported by DeployDataCenter when successfully created DC. This file is serialized one containing entries with successful DC. dc_cfg: If dc_cfg_file, is not available, we can use the dictionary of elements to delete. tc_run_logger: Logger to dump log messages. """ def __init__(self, test_client, cfg): self.__cfg = cfg self.__test_client = test_client self.__logger = MarvinLog('marvin').get_logger() self.__apiClient = None def __deleteCmds(self, cmd_name, cmd_obj): """ @Name : __deleteCmds @Desc : Deletes the entities provided by cmd """ if cmd_name.lower() == "deletehostcmd": cmd_obj.forcedestroylocalstorage = "true" cmd_obj.force = "true" ''' Step1 : Prepare Host For Maintenance ''' host_maint_cmd = prepareHostForMaintenance.prepareHostForMaintenanceCmd() host_maint_cmd.id = cmd_obj.id host_maint_resp = self.__apiClient.prepareHostForMaintenance(host_maint_cmd) if host_maint_resp: ''' Step2 : List Hosts for Resource State ''' list_host_cmd = listHosts.listHostsCmd() list_host_cmd.id = cmd_obj.id retries = 3 for i in xrange(retries): list_host_resp = self.__apiClient.listHosts(list_host_cmd) if list_host_resp and (list_host_resp[0].resourcestate == 'Maintenance'): break sleep(30) if cmd_name.lower() == "deletestoragepoolcmd": cmd_obj.forced = "true" store_maint_cmd = enableStorageMaintenance.enableStorageMaintenanceCmd() store_maint_cmd.id = cmd_obj.id store_maint_resp = self.__apiClient.enableStorageMaintenance(store_maint_cmd) if store_maint_resp: list_store_cmd = listStoragePools.listStoragePoolsCmd() list_store_cmd.id = cmd_obj.id retries = 3 for i in xrange(retries): store_maint_resp = self.__apiClient.listStoragePools(list_store_cmd) if store_maint_resp and (store_maint_resp[0].state == 'Maintenance'): break sleep(30) return cmd_obj def __setClient(self): self.__apiClient = self.__test_client.getApiClient() def __cleanEntries(self): """ @Name : __cleanAndEntries @Description: Cleans up the created DC in order of creation """ try: ret = FAILED if "order" in self.__cfg.keys() and len(self.__cfg["order"]): self.__cfg["order"].reverse() self.__logger.info("=== Clean Up Entries: %s ===" % self.__cfg) for type in self.__cfg["order"]: self.__logger.info("=== CleanUp Started For Type: %s ===" % type) if type: temp_ids = self.__cfg[type] ids = [items for items in temp_ids if items] for id in ids: del_mod = "delete" + type del_cmd = getattr(globals()[del_mod], del_mod + "Cmd") del_cmd_obj = del_cmd() del_cmd_obj.id = id del_cmd_obj = self.__deleteCmds(del_mod + "Cmd", del_cmd_obj) del_func = getattr(self.__apiClient, del_mod) del_cmd_resp = del_func(del_cmd_obj) if del_cmd_resp: self.__logger.error("=== %s CleanUp Failed. ID: %s ===" % (type, id)) else: self.__logger.info("=== %s CleanUp Successful. ID : %s ===" % (type, id)) ret = SUCCESS except Exception as e: self.__logger.exception("=== Clean Up Entries failed: %s ===" % e) finally: return ret def removeDataCenter(self): """ @Name : removeDataCenter @Desc : Removes the Data Center provided by Configuration If Input dc file configuration is None, uses the cfg provided else uses the dc file to get the configuration """ try: self.__setClient() self.__logger.info("=== DeployDC: CleanUp Started ===") ret = self.__cleanEntries() except Exception as e: self.__logger.exception("=== DeployDC: CleanUp failed: %s ===" % e) finally: return ret
return self.__option_is_set(options.remove) and self.__path_points_to_file(options.remove) def __options_for_remove_data_center_are_invalid(self, options): return self.__option_is_set(options.remove) and not self.__path_points_to_file(options.remove) @staticmethod def __deploy_and_remove_data_center(options): return options.input and options.remove @staticmethod def __nothing_to_do(options): return options.input is None and options.remove is None @staticmethod def __option_is_set(flag): return flag is not None @staticmethod def __path_points_to_file(path): return os.path.isfile(path) if __name__ == "__main__": logger = MarvinLog('marvin').get_logger() try: Application().main(sys.argv[1:]) except Exception as e: logger.exception("Aborting run due to: %s" % e) exit(1) exit(0)