def show_volume_backup(self, backup, check=True): """Step to show volume backup using CLI. Args: backup (object): cinder volume backup object to show check (bool): flag whether to check step or not Raises: AssertionError: if check failed """ cmd = 'cinder backup-show ' + backup.id exit_code, stdout, stderr = self.execute_command( cmd, timeout=config.BACKUP_SHOW_TIMEOUT, check=check) backup_table = output_parser.table(stdout) show_result = {key: value for key, value in backup_table['values']} if check: assert_that(show_result['id'], is_(backup.id)) if backup.name: assert_that(show_result['name'], is_(backup.name)) if backup.description: assert_that(show_result['description'], is_(backup.description)) if backup.container: assert_that(show_result['container'], is_(backup.container))
def live_evacuate(self, source_host, target_host, servers, check=True): """Step to execute host-evacuate-live. This step is executed using CLI because there is no API for it. Args: source_host (str): source host target_host (str): target host servers (list): list of server objects check (bool): flag whether to check result or not Raises: AssertionError: if check failed """ cmd = ('nova host-evacuate-live --target-host {0} {1}'.format( target_host, source_host)) exit_code, stdout, stderr = self.execute_command( cmd, timeout=config.LIVE_EVACUATE_CLI_TIMEOUT, check=check) if check: evacuation_table = output_parser.table(stdout) ids = [] for id, accepted, err_message in evacuation_table['values']: ids.append(id) assert_that(accepted, equal_to('True')) assert_that(err_message, equal_to('')) server_ids = [server.id for server in servers] assert_that(ids, has_items(*server_ids))
def show_volume(self, volume, check=True): """Step to show volume using CLI. Args: volume (object): cinder volume object to show check (bool): flag whether to check step or not Raises: AssertionError: if check failed """ cmd = 'cinder show ' + volume.id exit_code, stdout, stderr = self.execute_command( cmd, timeout=config.VOLUME_SHOW_TIMEOUT, check=check) volume_table = output_parser.table(stdout) show_result = {key: value for key, value in volume_table['values']} if check: assert_that(show_result['id'], is_(volume.id)) if volume.name: assert_that(show_result['name'], is_(volume.name)) if volume.description: assert_that(show_result['description'], is_(volume.description))
def show_volume_snapshot(self, snapshot, check=True): """Step to show volume snapshot using CLI. Args: snapshot (object): cinder volume snapshot object to show check (bool): flag whether to check step or not Raises: AssertionError: if check failed """ cmd = 'cinder snapshot-show ' + snapshot.id exit_code, stdout, stderr = self.execute_command( cmd, timeout=config.SNAPSHOT_SHOW_TIMEOUT, check=check) snapshot_table = output_parser.table(stdout) show_result = {key: value for key, value in snapshot_table['values']} if check: assert_that(show_result['id'], is_(snapshot.id)) if snapshot.name: assert_that(show_result['name'], is_(snapshot.name)) if snapshot.description: assert_that(show_result['description'], is_(snapshot.description))
def create_volume(self, size=1, name=None, description=None, image=None, check=True): """Step to create volume using CLI. Args: size(int): size of created volume (in GB) name (str): name of created volume description (str): volume description image (str): glance image name or ID to create volume from metadata(str): volume metadata check (bool): flag whether to check step or not Returns: dict: cinder volume """ metadata = '{0}={1}'.format(config.STEPLER_PREFIX, config.STEPLER_PREFIX) cmd = 'cinder create ' + str(size) + ' --metadata ' + metadata if image: cmd += ' --image ' + image if name: cmd += ' --name ' + moves.shlex_quote(name) if description is not None: cmd += ' --description ' + moves.shlex_quote(description) exit_code, stdout, stderr = self.execute_command( cmd, timeout=config.VOLUME_AVAILABLE_TIMEOUT, check=check) volume_table = output_parser.table(stdout) volume = {key: value for key, value in volume_table['values']} return volume
def preview_stack(self, name, template_file, parameters=None, check=True): """Step to preview stack. Args: name (str): name of stack preview template_file (str): path to stack template file parameters (dict, optional): additional parameters to template check (bool): flag whether to check step or not Returns: dict: stack preview result Raises: AssertionError: if stack preview returns not 'None' stack's id """ parameters = parameters or {} cmd = 'heat stack-preview {name} -f {file}'.format(name=name, file=template_file) for key, value in parameters.items(): cmd += ' --parameters {}={}'.format(key, value) exit_code, stdout, stderr = self.execute_command( cmd, timeout=config.STACK_PREVIEW_TIMEOUT, check=check) stack_table = output_parser.table(stdout) stack = {key: value for key, value in stack_table['values']} if check: assert_that(stack['id'], is_('None')) return stack
def create_volume_snapshot(self, volume, name=None, description=None, check=True): """Step to create volume snapshot using CLI. Args: volume (object): cinder volume name (str): name of snapshot to create description (str): snapshot description check (bool): flag whether to check step or not Returns: dict: cinder volume snapshot """ cmd = 'cinder snapshot-create' if name: cmd += ' --name ' + name if description is not None: cmd += ' --description ' + moves.shlex_quote(description) cmd += ' ' + volume.id exit_code, stdout, stderr = self.execute_command( cmd, timeout=config.SNAPSHOT_AVAILABLE_TIMEOUT, check=check) snapshot_table = output_parser.table(stdout) snapshot = {key: value for key, value in snapshot_table['values']} return snapshot
def create_volume_backup(self, volume, name=None, description=None, container=None, check=True): """Step to create volume backup using CLI. Args: volume (object): cinder volume name (str): name of backup to create description (str): description container (str): name of the backup service container check (bool): flag whether to check step or not Returns: dict: cinder volume backup """ cmd = 'cinder backup-create' if name: cmd += ' --name ' + name if description is not None: cmd += ' --description ' + moves.shlex_quote(description) if container: cmd += ' --container ' + container cmd += ' ' + volume.id exit_code, stdout, stderr = self.execute_command( cmd, timeout=config.BACKUP_AVAILABLE_TIMEOUT, check=check) backup_table = output_parser.table(stdout) backup = {key: value for key, value in backup_table['values']} return backup
def get_stack_event(self, stack, resource, event, check=True): """Step to get stack's events list. Args: stack (obj): heat stack resource (str): name of the resource the event belongs to event (str): ID of event to display details for check (bool): flag whether to check step or not Raises: AssertionError: if stack event is empty Returns: dict: stack event """ warnings.warn("`heat event-show` is deprecated") cmd = 'heat event-show {stack} {resource} {event}'.format( stack=stack.id, resource=resource, event=event) exit_code, stdout, stderr = self.execute_command( cmd, timeout=config.STACK_UPDATING_TIMEOUT, check=check) event_table = output_parser.table(stdout) event = {key: value for key, value in event_table['values']} if check: assert_that(event, is_not(empty())) return event
def test_parse_non_ascii_table(): """Test correct find indentation for tables with non-ASCII symbols.""" raw_data = (u'+-------------+--------------------------------------+\n' u'| Property | Value |\n' u'+-------------+--------------------------------------+\n' u'| name | シンダー |\n' u'+-------------+--------------------------------------+') table = output_parser.table(raw_data) assert table['values'][0][1] == u"シンダー"
def test_parse_multiline_cells(): """Test correct parsing multiline cells for 2-columns tables.""" raw_data = (u'+------+-------+\n' u'| foo | bar |\n' u'+------+-------+\n' u'| data | [ |\n' u'| | 1, |\n' u'| | 2, |\n' u'| | 3 |\n' u'| | ] |\n' u'+------+-------+') table = output_parser.table(raw_data) assert len(table['values']) == 1
def create_image(self, image_file=None, image_name=None, disk_format=None, container_format=None, api_version=2, check=True): """Step to create image. Args: image_file (str|None): image file to be uploaded; it should be located on the same node where CLI is running image_name (str|None): name of created image disk_format (str|None): disk format of image container_format (str|None): container format of image api_version (int): API version of Glance (1 or 2) check (bool): flag whether to check result or not Returns: tuple: execution result (image dict, exit_code, stdout, stderr) Raises: AnsibleExecutionException: if command execution failed """ image = None cmd = 'glance image-create' if image_file: cmd += ' --file ' + moves.shlex_quote(image_file) if image_name: cmd += ' --name ' + moves.shlex_quote(image_name) if disk_format: cmd += ' --disk-format ' + disk_format if container_format: cmd += ' --container-format ' + container_format if 'disk-format' not in cmd and 'container-format' not in cmd: cmd += ' <&-' # otherwise stderr: <stdin: is not a tty\nerror: Must provide # --container-format, --disk-format when using stdin> # This problem is only for remote execution (Ansible) exit_code, stdout, stderr = self.execute_command( cmd, environ={'OS_IMAGE_API_VERSION': api_version}, check=check) if check: image_table = output_parser.table(stdout) image = {key: value for key, value in image_table['values']} return image, exit_code, stdout, stderr
def create_router(self, name=None, project=None, username=None, password=None, distributed=None, expected_error=False, check=True): """Step to create router using CLI. Args: name (str): name of created router project (str): name of the project username (str): user name password (str): user password distributed (bool): flag whether to create DVR or not expected_error (bool): flag whether to expect error during router creation or not check (bool): flag whether to check step or not Returns: tuple: (router or None, exit_code, stdout, stderr) """ name = name or next(utils.generate_ids()) router = None cmd = 'neutron router-create ' + moves.shlex_quote(name) if project: cmd += ' --os-project-name ' + project if username and password: cmd += ' --os-username {0} --os-password {1}'.format( username, password) if distributed is not None: cmd += ' --distributed ' + str(distributed) exit_code, stdout, stderr = self.execute_command( cmd, timeout=config.ROUTER_AVAILABLE_TIMEOUT, check=check) if not expected_error: router_table = output_parser.table(stdout) router = {key: value for key, value in router_table['values']} if check: assert_that(router, is_not(empty())) return router, exit_code, stdout, stderr
def show_stack(self, stack, check=True): """Step to show stack. Args: stack (obj): heat stack to show check (bool): flag whether to check step or not Raises: AssertionError: if output contains wrong stack's name or id """ cmd = 'heat stack-show {}'.format(stack.id) exit_code, stdout, stderr = self.execute_command( cmd, timeout=config.STACK_SHOW_TIMEOUT, check=check) stack_table = output_parser.table(stdout) show_result = {key: value for key, value in stack_table['values']} if check: assert_that(show_result, has_entries(stack_name=stack.stack_name, id=stack.id))
def show_volume_transfer(self, volume_transfer, check=True): """Step to show volume transfer using CLI. Args: volume_transfer (object): cinder volume transfer object to show check (bool): flag whether to check step or not Raises: AssertionError: if check failed """ cmd = 'cinder transfer-show ' + volume_transfer.id exit_code, stdout, stderr = self.execute_command( cmd, timeout=config.TRANSFER_SHOW_TIMEOUT, check=check) transfer_table = output_parser.table(stdout) transfer_dict = {key: value for key, value in transfer_table['values']} if check: assert_that(transfer_dict['id'], is_(volume_transfer.id)) if volume_transfer.name: assert_that(transfer_dict['name'], is_(volume_transfer.name))
def list_images(self, property_filter=None, api_version=config.CURRENT_GLANCE_VERSION, check=True): """Step to get glance images list. Args: property_filter (str): filter Glance images list api_version (int): the API version of Glance check (bool): flag whether to check result or not Returns: list: execution result: images_list Raises: AnsibleExecutionException: if command execution failed """ images = [] cmd = 'glance image-list' if property_filter: if api_version == 2: cmd = '{} --property {}'.format(cmd, property_filter) else: cmd = '{} --property-filter {}'.format(cmd, property_filter) exit_code, stdout, stderr = self.execute_command( cmd, environ={'OS_IMAGE_API_VERSION': api_version}, check=check) if check: images_table = output_parser.table(stdout)['values'] if api_version == 1: # Take two first elements from information of each image # because it is id and name of image images_list = [img[0:2] for img in images_table] images = {id_img: name_img for id_img, name_img in images_list} else: images = { id_img: name_img for id_img, name_img in images_table } return images
def create_volume_transfer(self, volume, name=None, check=True): """Step to create volume transfer using CLI. Args: volume (object): cinder volume name (str): name of transfer to create check (bool): flag whether to check step or not Returns: dict: cinder volume transfer """ cmd = 'cinder transfer-create' if name: cmd += ' --name ' + name cmd += ' ' + volume.id exit_code, stdout, stderr = self.execute_command( cmd, timeout=config.TRANSFER_CREATE_TIMEOUT, check=check) transfer_table = output_parser.table(stdout) transfer = {key: value for key, value in transfer_table['values']} return transfer
def show_image(self, image, api_version=config.CURRENT_GLANCE_VERSION, check=True): """Step to show glance image. Args: image (obj): glance image api_version (int): the API version of Glance check (bool): flag whether to check result or not Returns: tuple: execution result (image_show, exit_code, stdout, stderr) Raises: AssertionError: if check failed """ cmd = 'glance image-show {0}'.format(image.id) exit_code, stdout, stderr = self.execute_command( cmd, environ={'OS_IMAGE_API_VERSION': api_version}, check=check) if check: image_table = output_parser.table(stdout)['values'] image = {line[0]: line[1] for line in image_table} return image, exit_code, stdout, stderr