Exemplo n.º 1
0
    def test_comma_separated_list_empty(self):
        self.create_fixture(typed(comma_separated_list, mock_value=""))
        loader = load_from_dict()

        metadata = Metadata("test", testing=True)
        config = configure(self.registry.defaults, metadata, loader)
        assert_that(config, has_entries(foo=has_entries(value=[], ), ))
Exemplo n.º 2
0
def create_object_graph(name,
                        debug=False,
                        testing=False,
                        import_name=None,
                        root_path=None,
                        loader=load_from_environ,
                        registry=_registry):
    """
    Create a new object graph.

    :param name: the name of the microservice
    :param debug: is development debugging enabled?
    :param testing: is unit testing enabled?
    :param loader: the configuration loader to use
    :param registry: the registry to use (defaults to the global)
    """
    metadata = Metadata(
        name=name,
        debug=debug,
        testing=testing,
        import_name=import_name,
        root_path=root_path,
    )

    config = Configuration(
        {key: get_defaults(value)
         for key, value in registry.all.items()})
    config.merge(loader(metadata))

    return ObjectGraph(
        metadata=metadata,
        config=config,
        registry=registry,
    )
Exemplo n.º 3
0
    def test_mock_value(self):
        self.create_fixture(required(boolean, mock_value="true"))
        loader = load_from_dict()

        metadata = Metadata("test", testing=True)
        config = configure(self.registry.defaults, metadata, loader)
        assert_that(config, has_entries(foo=has_entries(value=True, ), ))
Exemplo n.º 4
0
    def test_unsupported_arg(self):
        self.create_fixture(value=required(int, mock_value=0, spaghetti="foo"))
        loader = load_from_dict()

        metadata = Metadata("test", testing=True)
        config = configure(self.registry.defaults, metadata, loader)
        assert_that(config, has_entries(foo=has_entries(value=0, ), ))
Exemplo n.º 5
0
def load_secrets(metadata):
    """
    Load secrets using injected environment variables.

    Generate an "internal" naming prefix to differentiate secrets from other config.

    """
    return load_from_environ(Metadata(f"_{metadata.name}"))
Exemplo n.º 6
0
def test_load_from_environ_multipart_name():
    """
    Return configuration from environment.

    """
    metadata = Metadata("foo-bar")
    with envvar("FOO_BAR__BAZ", "blah"):
        config = load_from_environ(metadata)
    assert_that(config, is_(equal_to({"baz": "blah"})))
Exemplo n.º 7
0
def test_load_from_environ_json():
    """
    Return json configuration from environment.

    """
    metadata = Metadata("foo")
    with envvar("FOO__BAR", '["baz"]'):
        with envvar("FOO__BAZ", 'true'):
            config = load_from_environ_as_json(metadata)
    assert_that(config, is_(equal_to({"bar": ["baz"], "baz": True})))
Exemplo n.º 8
0
def test_empty_loader():
    """
    Return an empty loader.

    """
    metadata = Metadata("foo")
    loader = load_each(empty_loader)
    config = loader(metadata)

    assert_that(config, is_(equal_to({})))
Exemplo n.º 9
0
    def test_comma_separated_list_unconverted(self):
        self.create_fixture(value=required(comma_separated_list,
                                           mock_value=["abc", "def", "ghi"]))
        loader = load_from_dict()

        metadata = Metadata("test", testing=True)
        config = configure(self.registry.defaults, metadata, loader)
        assert_that(
            config,
            has_entries(foo=has_entries(value=["abc", "def", "ghi"], ), ))
Exemplo n.º 10
0
def test_load_partitioned():
    metadata = Metadata("foo")
    loader = load_partitioned(
        foo=load_from_dict(
            foo=dict(value="foo", ),
            baz=dict(
                value="foo",
                foo=dict(value="foo", ),
            ),
        ),
        bar=load_from_dict(
            bar=dict(value="bar", ),
            baz=dict(
                value="bar",
                bar=dict(value="bar", ),
            ),
        ),
    )
    config = loader(metadata)

    # configuration is loaded as a usual merge
    assert_that(
        config,
        is_(
            equal_to(
                dict(
                    foo=dict(value="foo", ),
                    bar=dict(value="bar", ),
                    baz=dict(
                        value="bar",
                        foo=dict(value="foo", ),
                        bar=dict(value="bar", ),
                    ),
                ))))

    # loader retains partitions
    assert_that(
        loader.foo,
        is_(
            equal_to(
                dict(
                    foo=dict(value="foo", ),
                    baz=dict(foo=dict(value="foo", ), ),
                ))))
    assert_that(
        loader.bar,
        is_(
            equal_to(
                dict(
                    bar=dict(value="bar", ),
                    baz=dict(
                        value="bar",
                        bar=dict(value="bar", ),
                    ),
                ))))
Exemplo n.º 11
0
def test_two_stage_loader_basic():
    metadata = Metadata("foo")
    initial_loader = load_from_dict(foo="bar", )

    loader = two_stage_loader(initial_loader, secondary_loader)
    config = loader(metadata)

    assert_that(config, is_(equal_to(dict(
        foo="bar",
        bar="bazman",
    ))))
Exemplo n.º 12
0
    def test_nullable(self):
        self.create_fixture(value=typed(
            int,
            default_value=0,
            nullable=True,
            mock_value=None,
        ))
        loader = load_from_dict()

        metadata = Metadata("test", testing=True)
        config = configure(self.registry.defaults, metadata, loader)
        assert_that(config, has_entries(foo=has_entries(value=None, ), ))
Exemplo n.º 13
0
def test_two_stage_loader_prefer_primary():
    metadata = Metadata("foo")
    initial_loader = load_from_dict(
        foo="bar",
        bar="hello",
    )

    loader = two_stage_loader(
        initial_loader,
        secondary_loader,
        prefer_secondary=False,
    )
    config = loader(metadata)

    assert_that(config, is_(equal_to(dict(
        foo="bar",
        bar="hello",
    ))))
Exemplo n.º 14
0
def create_object_graph(name,
                        debug=False,
                        testing=False,
                        import_name=None,
                        root_path=None,
                        loader=load_from_environ,
                        registry=_registry,
                        profiler=None,
                        cache=None):
    """
    Create a new object graph.

    :param name: the name of the microservice
    :param debug: is development debugging enabled?
    :param testing: is unit testing enabled?
    :param loader: the configuration loader to use
    :param registry: the registry to use (defaults to the global)

    """
    metadata = Metadata(
        name=name,
        debug=debug,
        testing=testing,
        import_name=import_name,
        root_path=root_path,
    )

    defaults = registry.defaults
    config = configure(defaults, metadata, loader)

    if profiler is None:
        profiler = NoopProfiler()

    if cache is None or isinstance(cache, str):
        cache = create_cache(cache)

    return ObjectGraph(
        metadata=metadata,
        config=config,
        registry=registry,
        profiler=profiler,
        cache=cache,
        loader=loader,
    )
Exemplo n.º 15
0
def test_load_from_environ():
    """
    Return configuration from environment.

    """
    metadata = Metadata("foo-dow")
    with envvar("FOO_DOW__BAR", "baz"):
        with envvar("FOO_DOW__BAZ", "bar"):
            with envvar("FOO_DOW__BAR_BAZ__THIS", "that"):
                config = load_from_environ(metadata)
    assert_that(
        config,
        is_(equal_to({
            "bar": "baz",
            "baz": "bar",
            "bar_baz": {
                "this": "that"
            }
        })))
Exemplo n.º 16
0
def test_load_each():
    """
    Return the merged union of two loaders.

    """
    metadata = Metadata("foo")
    with settings(dumps(dict(foo="bar"))) as settings_:
        with envvar("FOO__SETTINGS", settings_.name):
            with envvar("FOO__BAR", "baz"):
                loader = load_each(load_from_json_file, load_from_environ)
                config = loader(metadata)

    assert_that(
        config,
        is_(equal_to({
            "bar": "baz",
            "foo": "bar",
            "settings": settings_.name
        })))
Exemplo n.º 17
0
def test_load_multiple_values_for_on_componentfrom_environ():
    """
    Return configuration from environment.

    """
    metadata = Metadata("foo")
    with envvar("FOO__BAR", "baz"):
        with envvar("FOO__FOO__THIS", "that"):
            with envvar("FOO__FOO__THAT", "this"):
                config = load_from_environ(metadata)
    assert_that(
        config,
        is_(equal_to({
            "bar": "baz",
            "foo": {
                "this": "that",
                "that": "this"
            }
        })))
Exemplo n.º 18
0
def test_load_config_and_secrets():
    metadata = Metadata("foo")
    loader = load_config_and_secrets(
        config=load_from_dict(credentials=dict(username="******", ), ),
        secrets=load_from_dict(credentials=dict(password="******", ), ),
    )

    config = loader(metadata)

    # configuration is loaded as a usual merge
    assert_that(
        config,
        is_(
            equal_to(
                dict(credentials=dict(
                    username="******",
                    password="******",
                ), ))))

    assert_that(loader.config,
                is_(equal_to(dict(credentials=dict(username="******", ), ))))
    assert_that(loader.secrets,
                is_(equal_to(dict(credentials=dict(password="******", ), ))))
Exemplo n.º 19
0
 def setup(self):
     self.metadata = Metadata("test")
     self.registry = Registry()