Пример #1
0
    def discover(self):
        self.history = OrderedDict()
        try:
            order = self.device.clean['order']
        except KeyError:
            raise Exception("Key 'order' is missing for device "
                            "'{d}'".format(d=self.device.name))

        # Insert the 'images' value into necessary clean sections
        if self.device.clean['images']:
            initialize_clean_sections(self.image_handler, order)
        # self.device.clean['change_boot_variable'] = {'images': []}

        all_data = {}
        all_schema = {}
        sections = []
        common_data = {}
        self.parameters['common_data'] = common_data
        for section in order:
            try:
                data = self.device.clean[section] or {}
            except KeyError:
                # Cannot find section - raise exception
                raise Exception("Cannot find '{section}' in the provided "
                                "sections even though it was provided in "
                                "the order list '{order}'".\
                                        format(section=section, order=order))

            # Load it up
            # If source isnt provided then check if it is inside the clean json
            if 'source' not in data:
                # Check if that one exists in the json
                task = _get_clean(section, clean_data, self.device)
            else:
                task = load_class(data, self.device)

            # Verify if schema exists for this section
            if hasattr(task, 'schema'):
                # if the stage has schema defined then build the bigger schema
                all_schema[task.__name__] = task.schema
                all_data[task.__name__] = data
                # unwrap to get original method, tmp fix need to handle in genie core infra
                task = unwrap(task)

            func = copy_func(task)
            func.uid = task.__name__
            func.parameters = ParameterDict()
            func.parameters['device'] = self.device
            func.parameters['common_data'] = common_data
            func.source = Source(self, objcls=func.__class__)

            for parameter, value in data.items():
                func.parameters[parameter] = value

            # Bind it and append to the section list
            new_section = func.__get__(self, func.__testcls__)
            self.history[new_section.uid] = new_section

            # Add processor, add parameters to it if any
            if self.device.clean.get('device_recovery'):
                processor = partial(recovery_processor,
                                    **self.device.clean.get('device_recovery'))
                processors.add(new_section,
                               pre=[block_section],
                               post=[processor],
                               exception=[])

            sections.append(new_section)

        recovery_data = self.device.clean.get('device_recovery')
        # if recovery info not provided, don't need to check schema
        if recovery_data:
            recovery_schema = recovery_processor.schema
            all_schema['device_recovery'] = recovery_schema
            all_data['device_recovery'] = recovery_data

        try:
            Schema(all_schema).validate(all_data)
        except SchemaMissingKeyError as e:
            # proto type
            raise ValueError(
                "Clean schema check failed. The following keys are missing from clean yaml file:\n\n{}"
                .format(self._format_missing_key_msg(
                    e.missing_list))) from None
        except SchemaTypeError as e:

            raise TypeError(
                "Clean schema check failed. Incorrect value type was provided for the "
                "following key:\n\n{}\n\nExpected type {} but got type {}".
                format(self._format_missing_key_msg([e.path]), str(e.type),
                       type(e.data))) from None
        except SchemaUnsupportedKeyError as e:
            raise ValueError(
                "Clean schema check failed. The following keys are not supported:\n\n{}"
                .format(self._format_missing_key_msg(
                    e.unsupported_keys))) from None

        return sections