def test_errors_when_no_config_file_exists(m_isfile: MagicMock): m_isfile.return_value = False with pytest.raises(Exception) as ex: load_config() config_locations = [os.getcwd(), os.path.expanduser("~")] assert str( ex.value ) == f"No valid configuration file found. Inspected locations: {config_locations}"
def test_errors_when_config_is_invalid_yaml(m_isfile: MagicMock): m_isfile.side_effect = _only_files_exist(all_config_files) invalid_config = "}}:" with patch("clin.config.open", mock_open( read_data=invalid_config)), pytest.raises(Exception) as ex: load_config() assert str(ex.value).startswith( f"Failed to parse configuration file: {all_config_files[0]}")
def test_errors_when_config_does_not_contain_environments(m_isfile: MagicMock): m_isfile.side_effect = _only_files_exist(all_config_files) invalid_config = """ dev: xxx: nope """ with patch("clin.config.open", mock_open( read_data=invalid_config)), pytest.raises(Exception) as ex: load_config() assert str(ex.value) == "Environments section not found in configuration"
def apply( env: str, token: Optional[str], execute: bool, verbose: bool, show_diff: bool, show_payload: bool, file: str, ): """Create or update Nakadi resource from single yaml manifest file\n Values to fill {{VARIABLES}} are taken from system environment""" configure_logging(verbose) try: config = load_config() kind, spec = load_manifest(Path(file), DEFAULT_YAML_LOADER, os.environ) processor = Processor(config, token, execute, show_diff, show_payload) processor.apply(env, kind, spec) except (ProcessingError, NakadiError, ConfigurationError, YamlError) as ex: logging.error(ex) exit(-1) except Exception as ex: logging.exception(ex) exit(-1)
def dump(env: str, token: Optional[str], output: str, verbose: bool, event_type: str): """Print manifest of existing Nakadi event type""" configure_logging(verbose) try: config = load_config() if env not in config.environments: logging.error(f"Environment not found in configuration: {env}") exit(-1) nakadi = Nakadi(config.environments[env].nakadi_url, token) et = nakadi.get_event_type(event_type) if not et: logging.error("Event type not found in Nakadi %s: %s", env, event_type) exit(-1) if output.lower() == "yaml": logging.info(pretty_yaml(et.to_spec())) elif output.lower() == "json": logging.info(pretty_json(et.to_spec())) else: logging.error("Invalid output format: %s", output) exit(-1) except (NakadiError, ConfigurationError) as ex: logging.error(ex) exit(-1) except Exception as ex: logging.exception(ex) exit(-1)
def test_reads_config_from_project_config_before_user_config( m_isfile: MagicMock): m_isfile.side_effect = _only_files_exist(all_config_files) with patch("clin.config.open", mock_open(read_data=VALID_CONFIG)) as m_open: config = load_config() assert len(config.environments) == 2 m_open.assert_called_with(os.path.abspath(".clin"))
def test_reads_config_from_single_config_file(m_isfile: MagicMock, config_file: str): m_isfile.side_effect = _only_files_exist([config_file]) with patch("clin.config.open", mock_open(read_data=VALID_CONFIG)) as m_open: config = load_config() assert len(config.environments) == 2 assert "dev" in config.environments assert config.environments["dev"].nakadi_url == "https://nakadi.dev" assert "prod" in config.environments assert config.environments["prod"].nakadi_url == "https://nakadi.prod" m_open.assert_called_with(config_file)
def dump( token: Optional[str], verbose: bool, env: str, output: str, include_envelope: bool, event_type: str, ): """Print manifest of existing Nakadi event type""" configure_logging(verbose) try: config = load_config() if env not in config.environments: logging.error(f"Environment not found in configuration: {env}") exit(-1) nakadi = Nakadi(config.environments[env].nakadi_url, token) entity = nakadi.get_event_type(event_type) if entity and config.environments[env].nakadi_sql_url: nakadi_sql = NakadiSql(config.environments[env].nakadi_sql_url, token) entity = nakadi_sql.get_sql_query(entity) or entity if entity is None: logging.error("Event type not found in Nakadi %s: %s", env, event_type) exit(-1) payload = (entity.to_envelope().to_manifest() if include_envelope else entity.to_spec()) if output.lower() == "yaml": logging.info(pretty_yaml(payload)) elif output.lower() == "json": logging.info(pretty_json(payload)) else: logging.error("Invalid output format: %s", output) exit(-1) except (NakadiError, ConfigurationError) as ex: logging.error(ex) exit(-1) except Exception as ex: logging.exception(ex) exit(-1)
def process( token: Optional[str], verbose: bool, execute: bool, show_diff: bool, show_payload: bool, id: Tuple[str], env: Tuple[str], file: str, ): """Create or update multiple Nakadi resources from a clin file""" configure_logging(verbose) try: config = load_config() processor = Processor(config, token, execute, show_diff, show_payload) file_path: Path = Path(file) master = load_yaml(file_path, DEFAULT_YAML_LOADER, os.environ) scope = calculate_scope(master, file_path.parent, DEFAULT_YAML_LOADER, id, env) for task in (scope[Kind.EVENT_TYPE] + scope[Kind.SQL_QUERY] + scope[Kind.SUBSCRIPTION]): logging.debug( "[%s] applying file %s to %s environment", task.id, task.path, task.target, ) processor.apply(task.target, task.envelope) except (ProcessingError, ConfigurationError, YamlError) as ex: logging.error(ex) exit(-1) except Exception as ex: logging.exception(ex) exit(-1)
def process( token: Optional[str], execute: bool, verbose: bool, show_diff: bool, show_payload: bool, file: str, ): """Create or update multiple Nakadi resources from clin file""" configure_logging(verbose) try: config = load_config() processor = Processor(config, token, execute, show_diff, show_payload) file_path: Path = Path(file) master = load_yaml(file_path, DEFAULT_YAML_LOADER, os.environ) scope = calculate_scope(master, file_path.parent, DEFAULT_YAML_LOADER) event_types = [et for et in scope if et.kind == "event-type"] subscriptions = [sub for sub in scope if sub.kind == "subscription"] for task in event_types + subscriptions: logging.debug( "[%s] applying file %s to %s environment", task.id, task.path, task.target, ) processor.apply(task.target, task.kind, task.spec) except (ProcessingError, ConfigurationError, YamlError) as ex: logging.error(ex) exit(-1) except Exception as ex: logging.exception(ex) exit(-1)