def perform_action_on_vm(session, vm_name, action, expected_status_values=None): """ Perform supported action on the VM :param session: session for logging into restful api of the kimchi :param vm_name: name of the vm :param vm_name: action to be perform on VM :return: """ session.logging.info("--> vm_utils.perform_action_on_vm().vm_name:%s |action:%s", vm_name, action) if not vm_name: session.logging.error("VM name cannot be None") raise ValueError("VM name cannot be None") response = session.request_post_json( vm_uri + "/" + vm_name + "/" + action, expected_status_values=expected_status_values ) if "id" in response: try: task_resp = utils.wait_task_status_change(session, response["id"]) session.logging.debug("task response, task_resp:%s", task_resp) except Exception, err: session.logging.debug("VM failed to %s. %s", action, err.message) raise Exception("VM failed to start" + err.message)
def test_S004_remove_valid_and_invalid_devices(self): """ method to test remove action in cio_ignore api call with combination of valid and invalid devices invalid devices includes list of invalid id for single device, invalid range and empty device id """ self.logging.info( '--> TestCioIgnoreList.test_S004_remove_valid_' 'and_invalid_devices()') uri_cio_ignore_remove = self.uri_cio_ignore + '/remove' if not self.remove_devices: raise unittest.SkipTest( 'Skipping test_S004_remove_valid_and_invalid_devices() since' ' removable devices are not provided in config file') devices = ['invalid_device', '0.1.0900-0.0.0001', ' '] devices.extend(self.remove_devices) # appending valid devices to invalid devices list which composes # of invalid device id, invalid range and empty device id input_json = {"devices": devices} try: self.logging.debug( 'Performing post operation remove on cio_ignore ' 'list with input json %s' % input_json) resp = self.session.request_post_json( uri_cio_ignore_remove, body=input_json, expected_status_values=[200, 202]) if resp: self.logging.debug('task json returned from cio_ignore remove' ': %s' % resp) self.validator.validate_json(resp, self.task_schema) task_id = resp['id'] task_resp = utils.wait_task_status_change( self.session, task_id, task_final_status='failed') self.validator.validate_json(task_resp, self.task_schema) assert (task_resp['status'] == 'failed'),\ "remove task which was expected to fail, finished" \ " successfully. task json response: %s" % task_resp self.logging.debug( 'Result is as expected. Task response %s' % task_resp) else: self.logging.info('Remove action on /cio_ignore returned' ' None/Empty response instead of task json') self.fail('Remove action on /cio_ignore returned ' 'None/Empty response instead of task json') except APIRequestError as error: self.logging.error(error.__str__()) raise Exception(error) finally: self.logging.info( '<-- TestCioIgnoreList.test_S004_remove_valid_' 'and_invalid_devices()')
def test_S009_configure_valid_nwdevice(self): """ method to test "configure" action of /nwdevices api to configure valid network i/o device """ self.logging.info( '--> TestNwdevices.test_S009_configure_valid_nwdevice()') if not self.unconfigured_device: raise unittest.SkipTest('Skipping test_S009_configure_valid_nwdevice()' ' since un-configured device is not specified' ' in config file') uri_configure_device = self.uri_nwdevices + '/' + self.unconfigured_device + '/configure' try: self.logging.debug( 'Configuring un-configured network i/o device %s using uri' ' %s' % (self.unconfigured_device, uri_configure_device)) resp = self.session.request_post_json(uri_configure_device, expected_status_values=[200, 202]) if resp: self.logging.debug('task json returned from configure network' ' i/o device: %s' % resp) self.validator.validate_json(resp, self.task_schema) task_id = resp['id'] task_resp = utils.wait_task_status_change( self.session, task_id, task_final_status='finished') self.validator.validate_json(task_resp, self.task_schema) assert (task_resp['status'] == 'finished'),\ "configure task failed. task json response: " \ "%s" % task_resp self.logging.info('Retrieving device information ' 'after configure action') conf_device = self.session.request_get_json( self.uri_nwdevices + '/enccw' + self.unconfigured_device, expected_status_values=[200]) self.validator.validate_json(conf_device, self.nwdevice_schema) if conf_device['state'] == 'Unconfigured': self.fail('Device is not configured through configure action. Device details after configure action: %s' % conf_device) self.session.logging.debug('Device %s is configured ' 'successfully. Device details %s' %(self.unconfigured_device, conf_device)) else: self.logging.info('configure action on network i/o device %s' ' returned None/Empty response instead' ' of task json' % self.unconfigured_device) self.fail('configure action on network i/o device %s' ' returned None/Empty response instead' ' of task json' % self.unconfigured_device) except APIRequestError as error: self.logging.error(error.__str__()) raise Exception(error) finally: self.logging.info('<-- TestNwdevices.test_S009_configure_valid_nwdevice()')
def test_S003_remove_invalid_devices(self): """ method to test remove action in cio_ignore api call with invalid devices (includes list of invalid id for single device, invalid range and empty device id) """ self.logging.info( '--> TestCioIgnoreList.test_S003_remove_invalid_devices()') uri_cio_ignore_remove = self.uri_cio_ignore + '/remove' devices = ['invalid_device', '0.1.0900-0.0.0001', ' '] # devices list composes of invalid device id, invalid range and # empty device id input_json = {"devices": devices} try: self.logging.debug( 'Performing post operation remove on cio_ignore ' 'list with input json %s' % input_json) resp = self.session.request_post_json( uri_cio_ignore_remove, body=input_json, expected_status_values=[200, 202]) if resp: self.logging.debug('task json returned from cio_ignore remove' ': %s' % resp) self.validator.validate_json(resp, self.task_schema) task_id = resp['id'] task_resp = utils.wait_task_status_change( self.session, task_id, task_final_status='failed') self.validator.validate_json(task_resp, self.task_schema) assert (task_resp['status'] == 'failed'),\ "remove task which was expected to fail, finished" \ " successfully. task json response: %s" % task_resp self.logging.debug( 'As expected, failed to remove invalid devices. ' 'Task response %s' % task_resp) else: self.logging.info('Remove action on /cio_ignore returned' ' None/Empty response instead of task json') self.fail('Remove action on /cio_ignore returned ' 'None/Empty response instead of task json') except APIRequestError as error: self.logging.error(error.__str__()) raise Exception(error) finally: self.logging.info( '<-- TestCioIgnoreList.test_S003_remove_invalid_devices()')
def test_S002_remove_valid_devices(self): """ method to test remove action in cio_ignore api call with valid devices """ self.logging.info( '--> TestCioIgnoreList.test_S002_remove_valid_devices()') uri_cio_ignore_remove = self.uri_cio_ignore + '/remove' self.logging.debug('Reading devices information from config file') if not self.remove_devices: raise unittest.SkipTest( 'Skipping test_S002_remove_valid_devices() since ' 'removable devices are not provided in config file') input_json = {"devices": self.remove_devices} try: self.logging.debug( 'Performing post operation remove on cio_ignore ' 'list with inpust json %s' % input_json) resp = self.session.request_post_json( uri_cio_ignore_remove, body=input_json, expected_status_values=[200, 202]) if resp: self.logging.debug('task json returned from cio_ignore remove' ': %s' % resp) self.validator.validate_json(resp, self.task_schema) self.logging.info('Waiting for task completion') task_id = resp['id'] task_resp = utils.wait_task_status_change( self.session, task_id, task_final_status='finished') self.validator.validate_json(task_resp, self.task_schema) assert (task_resp['status'] == 'finished'),\ "remove task failed. task json response:" \ " %s" % task_resp self.logging.debug('Successfully removed devices %s from ' 'ignore list. Task response: %s' % (self.remove_devices, task_resp)) else: self.logging.info('Remove action on /cio_ignore returned' ' None/Empty response instead of task json') self.fail('Remove action on /cio_ignore returned ' 'None/Empty response instead of task json') except APIRequestError as error: self.logging.error(error.__str__()) raise Exception(error) finally: self.logging.info( '<-- TestCioIgnoreList.test_S002_remove_valid_devices()')
def create_vm( session, vm_name="test_vm", template_name="test", persistent=None, graphics_type=None, graphics_listen=None, storagepool=None, expected_status=None, ): """ Create VM :param session: session for logging into restful api of the kimchi :param vm_name: Name of VM to be created. Default is test_vm :param template_name: Name of Template to be used for VM creation. Default is test. If template already exists then will be used for VM creation :param persistent : True or False. Default in True :param graphics_type : The type of graphics. It can be VNC or spice or None :param storagepool : Storage Pool for the new VM :param graphics_listen : The network which the vnc/spice server listens on. :param expected_status: expected return value from kimchi api :return: VM creation response by adding Name of VM. """ session.logging.info( "-->vm_utils.create_vm(), vm_name: %s |template_name:%s |persistent:%s|graphics_type:%s |storagepool:%s|listen:%s |expected_status:%s", vm_name, template_name, persistent, graphics_type, storagepool, graphics_listen, expected_status, ) vm_data = {"name": vm_name, "template": template_utils.template_uri + template_name} if persistent is not None: vm_data["persistent"] = persistent if graphics_type is not None and graphics_type in ["vnc", "spice"]: vm_data["graphics[type]"] = graphics_type if storagepool is not None: vm_data["storagepool"] = storagepool if graphics_listen is not None: vm_data["graphics[listen]"] = graphics_listen created_template = False # checking if template exist, otherwise create it try: template_resp = session.request_get_json(template_utils.template_uri + template_name) session.logging.debug("Template already exist. VM will use the same:%s", template_resp) except Exception: session.logging.debug("Creating new template with name:%s", template_name) template_resp = template_utils.create_template(session, name=template_name) session.logging.debug("Created new template:%s", template_resp) created_template = True # If template created set this to True # Create VM try: vm_resp = session.request_post_json(vm_uri, vm_data, expected_status) vm_resp["name"] = vm_name finally: # Making sure that if Template created above, delete it even VM creation raise exception if created_template: session.logging.debug("Going to delete the template if create above. template_name:%s", template_name) template_utils.delete_template(session, template_name) session.logging.debug("Deleted template create above. template_name:%s", template_name) task_resp = utils.wait_task_status_change(session, vm_resp["id"]) session.logging.debug("task response, task_resp:%s", task_resp) if task_resp: session.logging.info("<--vm_utils.create_vm(), vm_resp:%s", vm_resp) return vm_resp