def test_get_or_default_type_check_fail(): mydict = {'key': 'value'} try: get_or_default(mydict, 'key', 'default', int) pytest.fail('Should throw {}'.format(ConfigError)) except Exception as e: assert type(e) is ConfigError
def _has_diff_expression(self): diffs = self._get_diffs_from_current() exp_dict = {} for target_diff in self.target_diffs: if type(target_diff) is not dict: raise ConfigError( 'Invalid diff configuration specified:\n{}'.format( yaml.dump(target_diff))) id = get_or_default(target_diff, ID, None, str) if id is not None: path = get_or_default(target_diff, PATH, None, str) if path is not None: for detected_diff in diffs: if re.match(target_diff[PATH], detected_diff.a_rawpath.decode(UTF8)): LOG.info('[{}] path pattern {} matches {}'.format( self.id, target_diff[PATH], detected_diff.a_rawpath.decode(UTF8))) content = get_or_default(target_diff, CONTENT, None, str) if content is not None: if id not in exp_dict or not exp_dict[id]: exp_dict[id] = self._has_content_diff( content, detected_diff) else: exp_dict[id] = True if id not in exp_dict: exp_dict[id] = False return evaluate_expression(self.expression, exp_dict)
def configure(self, config): prehooks = get_or_default(config, PREHOOK, None, list) if prehooks is not None: self.prehooks = [] for prehook_config in prehooks: executor = Executor() executor.configure(prehook_config) self.prehooks.append(executor) posthooks = get_or_default(config, POSTHOOK, None, list) if posthooks is not None: self.posthooks = [] self.posthooks_success = [] self.posthooks_failure = [] for posthook_config in posthooks: executor = Executor() executor.configure(posthook_config) condition = get_or_default(posthook_config, CONDITION, ALWAYS, str).lower() if condition == ALWAYS: self.posthooks.append(executor) elif condition == FAILURE: self.posthooks_failure.append(executor) elif condition == SUCCESS: self.posthooks_success.append(executor) else: raise ConfigError( 'Invalid execution condition specified on prehook: {}'. format(condition))
def configure(self, config): self.config = config self.steps = get_or_default(config, STEPS, None, list) if self.steps is None: raise ConfigError('No steps specified to execute:\n{}'.format( yaml.dump(config))) self.batch = get_or_default(config, BATCH, False, bool) self.timeout = get_or_default(config, TIMEOUT, None, int) self.results = []
def configure(self, config): self.configmap_name = get_or_default(config, NAME, 'plumber-checkpoint', str) self.namespace = get_or_default(config, NAMESPACE, 'default', str) self.file_placeholder = get_or_default(config, PLACEHOLDER, '.plumber.checkpoint.yml', str) if bool(os.getenv('KUBERNETES_SERVICE_HOST')): kubeconfig.load_incluster_config() else: kubeconfig.load_kube_config() self.core_api = client.CoreV1Api()
def create_checkpoint_store(config=None): if config is not None: store_type = get_or_default(config, TYPE, None, str) if store_type is not None: store_type = store_type.lower() if CONFIG in config: store_config = config[CONFIG] else: store_config = {} if store_type == LOCALFILE: checkpoint_store = YamlFileStore() if PATH not in store_config: store_config[PATH] = DEFAULT_CHECKPOINT_FILENAME elif store_type == LOCALGIT: checkpoint_store = YamlGitFileStore() if PATH not in store_config: store_config[PATH] = DEFAULT_CHECKPOINT_FILENAME elif store_type == KUBECONFIG: checkpoint_store = KubeConfigStore() else: raise ConfigError('Unknown checkpoint type specified') checkpoint_store.configure(store_config) else: checkpoint_store = initialize_default_checkpoint_store() else: checkpoint_store = initialize_default_checkpoint_store() return checkpoint_store
def __init__(self, config): super(PlumberPlanner, self).__init__() self.config = config self.checkpoint_store = None self.pipes = None self.results = None self.checkpoint_unit = SINGLE self.posthooks_execute = False global_config = get_or_default(config, GLOBAL, None, dict) if global_config is not None: super(PlumberPlanner, self).configure(global_config) checkpointing_config = get_or_default(global_config, CHECKPOINTING, None, dict) if checkpointing_config is not None: checkpoint_unit = get_or_default(checkpointing_config, UNIT, 'single', str) if checkpoint_unit is not None: self.checkpoint_unit = checkpoint_unit.lower() self.checkpoint_store = create_checkpoint_store( checkpointing_config) else: self.checkpoint_store = create_checkpoint_store() else: self.checkpoint_store = create_checkpoint_store() self.current_checkpoint = self.checkpoint_store.get_data() if PIPES in config: self.pipes = [] declared_pipe_ids = set() for pipe_config in config[PIPES]: if ID not in pipe_config: raise ConfigError( 'Id not specified for pipe in configuration file') if pipe_config[ID] in declared_pipe_ids: raise ConfigError( 'Multiple pipes configured with id: {}'.format( pipe_config[ID])) declared_pipe_ids.add(pipe_config[ID]) pipe = PlumberPipe() pipe.configure( pipe_config, get_or_default(self.current_checkpoint, pipe_config[ID], {})) self.pipes.append(pipe)
def _has_diff_all(self): diffs = self._get_diffs_from_current() for target_diff in self.target_diffs: if type(target_diff) is not dict: raise ConfigError( 'Invalid diff configuration specified:\n{}'.format( yaml.dump(target_diff))) for detected_diff in diffs: path = get_or_default(target_diff, PATH, None, str) if path is not None: if re.match(path, detected_diff.a_rawpath.decode(UTF8)): LOG.info('[{}] path pattern {} matches {}'.format( self.id, path, detected_diff.a_rawpath.decode(UTF8))) content = get_or_default(target_diff, CONTENT, None, str) if content is not None: if self._has_content_diff(content, detected_diff): return True else: return True return False
def evaluate(self): if self.conditions is None: return True expression = get_or_default(self.config, EXPRESSION, None, str) if expression is not None: exp_values = {} for condition in self.conditions: exp_values[condition[ID]] = condition[CONDITION].evaluate() return evaluate_expression(expression, exp_values) else: for condition in self.conditions: if condition[CONDITION].evaluate(): return True return False
def configure(self, config, checkpoint): super(PlumberPipe, self).configure(config=config) id = get_or_default(config, ID, None, str) if id is None: raise ConfigError( 'Id not specified for pipe in configuration file:\n{}'.format( yaml.dump(config))) self.config = config self.checkpoint = checkpoint conditions = get_or_default(config, CONDITIONS, None, list) if conditions is not None: self.conditions = [] declared_conditions = set() for condition_config in conditions: id = get_or_default(condition_config, ID, None, str) if id is None: raise ConfigError( 'Id not specified for condition in the configuration file:\n{}' .format(yaml.dump(condition_config))) if id in declared_conditions: raise ConfigError( 'Multiple conditions specified with the id: {}\n'. format(id, yaml.dump(condition_config))) declared_conditions.add(id) self.conditions.append({ ID: id, CONDITION: _create_conditional( condition_config, get_or_default(checkpoint, id, {}, dict)) }) actions = get_or_default(config, ACTIONS, None, dict) if actions is not None: self.actions = Executor() self.actions.configure(actions)
def configure(self, config, checkpoint): self.id = get_or_default(config, ID, None, str) if self.id is None: raise ConfigError('id not specified:\n{}'.format( yaml.dump(config))) self.target_diffs = get_or_default(config, DIFF, None, list) if self.target_diffs is None: raise ConfigError( 'No diffs specified in the localdiff condition:\n{}'.format( yaml.dump(config))) branches = get_or_default(config, BRANCH, None, dict) if branches is not None: self.active_branch = get_or_default(branches, ACTIVE, None, str) self.target_branch = get_or_default(branches, TARGET, None, str) self.repo = Repo(current_path()) self.new_checkpoint = str(self.repo.head.commit) self.checkpoint = checkpoint self.expression = get_or_default(config, EXPRESSION, None, str)
def configure(self, config): self.path = get_or_default(config, PATH, None, str) if self.path is None: raise ConfigError('Path to yaml file not provided:\n{}'.format( self.parser.dump(config)))
def test_get_or_default(): mydict = {'key': 'value'} value = get_or_default(mydict, 'key', 'default') assert value == 'value'
def test_get_or_default_type_check(): mydict = {'key': 'value'} value = get_or_default(mydict, 'key', 'default', str) assert value == 'value'