Exemple #1
0
def test_matcher_with_defaults_values_undefined():
    config = YamlLoader(
        path=config_directory.joinpath("defaults.yaml"), matcher=MatcherWithDefaults
    ).load()
    assert config["db"]["password"] is None
    assert config["mail"]["password"] == "My123~!@#$%^&*())_+Password!"
    assert config["logging"]["password"] == ":-test:-"
    assert config["sentry"]["password"] == ""
Exemple #2
0
def test_pydantic_validator_fail(name):
    with pytest.raises(ValidationError, match=r"db -> password"):
        YamlLoader(
            path=config_directory.joinpath("{}.yaml".format(name)),
            matcher=StrictMatcher,
            validator=PydanticValidator,
            schema=PydanticConfig,
        ).load()
Exemple #3
0
def test_strict_matcher_values_set(name):
    with mock.patch("piny.matchers.StrictMatcher.constructor") as expand_mock:
        expand_mock.return_value = config_map[name]
        config = YamlLoader(
            path=config_directory.joinpath("{}.yaml".format(name)),
            matcher=StrictMatcher,
        ).load()
        assert config[name]["password"] == config_map[name]
Exemple #4
0
def test_matcher_with_defaults_values_set():
    with mock.patch("piny.matchers.os.environ.get") as expand_mock:
        expand_mock.side_effect = lambda v, _: config_map[v.split("_")[0].lower()]
        config = YamlLoader(
            path=config_directory.joinpath("defaults.yaml"), matcher=MatcherWithDefaults
        ).load()
        assert config["db"]["password"] == config_map["db"]
        assert config["mail"]["password"] == config_map["mail"]
        assert config["sentry"]["password"] == config_map["sentry"]
        assert config["logging"]["password"] == config_map["logging"]
Exemple #5
0
def test_strict_matcher_default_do_not_matched():
    config = YamlLoader(
        path=config_directory.joinpath("defaults.yaml"), matcher=StrictMatcher
    ).load()
    assert config["db"]["password"] is None
    assert (
        config["mail"]["password"] == "${MAIL_PASSWORD:-My123~!@#$%^&*())_+Password!}"
    )
    assert config["logging"]["password"] == "${LOGGING_PASSWORD:-:-test:-}"
    assert config["sentry"]["password"] == "${SENTRY_PASSWORD:-}"
Exemple #6
0
def test_marshmallow_validator_fail(name):
    with pytest.raises(
            ValidationError,
            match=r"\{'db': \{'password': \['Field may not be null.'\]\}\}"):
        YamlLoader(
            path=config_directory.joinpath("{}.yaml".format(name)),
            matcher=StrictMatcher,
            validator=MarshmallowValidator,
            schema=MarshmallowConfig,
            strict=True,
        ).load(many=False)
Exemple #7
0
def test_trafaret_validator_fail(name):
    with pytest.raises(
            ValidationError,
            match=
            r"\{'db': DataError\(\{'password': DataError\(value is not a string\)\}\)\}",
    ):
        YamlLoader(
            path=config_directory.joinpath("{}.yaml".format(name)),
            matcher=StrictMatcher,
            validator=TrafaretValidator,
            schema=TrafaretConfig,
        ).load()
Exemple #8
0
def test_marshmallow_validator_success(name):
    with mock.patch("piny.matchers.StrictMatcher.constructor") as expand_mock:
        expand_mock.return_value = config_map[name]
        config = YamlLoader(
            path=config_directory.joinpath("{}.yaml".format(name)),
            matcher=StrictMatcher,
            validator=MarshmallowValidator,
            schema=MarshmallowConfig,
        ).load()

        assert config[name]["host"] == "db.example.com"
        assert config[name]["login"] == "user"
        assert config[name]["password"] == config_map[name]
Exemple #9
0
def test_base_matcher():
    """
    WATCH OUT! Black magic of Pytest in action!

    When placed at the beginning of test module this test make all other tests fail.
    Make sure this test is at the bottom!
    """
    with pytest.raises(NotImplementedError):
        with mock.patch(
            "piny.matchers.Matcher.matcher", new_callable=mock.PropertyMock
        ) as matcher_mock:
            matcher_mock.return_value = re.compile("")
            YamlLoader(
                path=config_directory.joinpath("defaults.yaml"), matcher=Matcher
            ).load()
Exemple #10
0
def create_app(path: str) -> Flask:
    """
    Application factory
    """
    # Get and validate config
    config = YamlLoader(
        path=path,
        matcher=StrictMatcher,
        validator=PydanticValidator,
        schema=Configuration,
    ).load()

    # Initialize app
    app = Flask(__name__)

    # Configure app
    configure_app(app, config)
    configure_logging(app)

    return app
Exemple #11
0
from pydantic import BaseModel
from piny import PydanticValidator, StrictMatcher, YamlLoader


class DBModel(BaseModel):
    login: str
    password: str


class ConfigModel(BaseModel):
    db: DBModel


config = YamlLoader(
    path="database.yaml",
    matcher=StrictMatcher,
    validator=PydanticValidator,
    schema=ConfigModel,
).load()
Exemple #12
0
from piny import YamlLoader

config = YamlLoader(path="config.yaml").load()
print(config)
# {'db': {'login': '******', 'password': '******'},
# 'mail': {'login': '******', 'password': '******'},
# 'sentry': {'dsn': None}}
Exemple #13
0
from piny import YamlLoader, StrictMatcher

config = YamlLoader(path="config.yaml", matcher=StrictMatcher).load()
Exemple #14
0
import marshmallow as ma
from piny import MarshmallowValidator, StrictMatcher, YamlLoader


class DBSchema(ma.Schema):
    login = ma.fields.String(required=True)
    password = ma.fields.String()


class ConfigSchema(ma.Schema):
    db = ma.fields.Nested(DBSchema)


config = YamlLoader(
    path="database.yaml",
    matcher=StrictMatcher,
    validator=MarshmallowValidator,
    schema=ConfigSchema,
    strict=True
).load(many=False)
Exemple #15
0
import trafaret
from piny import TrafaretValidator, StrictMatcher, YamlLoader


DBSchema = trafaret.Dict(login=trafaret.String, password=trafaret.String)
ConfigSchema = trafaret.Dict(db=DBSchema)

config = YamlLoader(
    path="database.yaml",
    matcher=StrictMatcher,
    validator=TrafaretValidator,
    schema=ConfigSchema,
).load()
Exemple #16
0
class GerritConfig(BaseModel):
    url: HttpUrl
    user: str
    password: str


class JiraConfig(BaseModel):
    url: HttpUrl
    user: str
    password: str


class SentryConfig(BaseModel):
    dsn: Optional[str] = None


class Settings(BaseModel):
    gerrit: GerritConfig
    jira: JiraConfig
    sentry: SentryConfig
    ca_certs: str


cfg = YamlLoader(
    os.environ.get("SKYLLA_CONFIG", "skylla.conf"),
    matcher=StrictMatcher,
    validator=PydanticValidator,
    schema=Settings,
    strict=True,
).load()
Exemple #17
0
def test_strict_matcher_values_undefined(name):
    config = YamlLoader(
        path=config_directory.joinpath("{}.yaml".format(name)), matcher=StrictMatcher
    ).load()
    assert config[name]["password"] is None