예제 #1
0
def test_set_entry_from_config_file(servicelib_yaml):
    assert config_client.instance().get("worker.num_processes") == 10
    assert config.get("worker.num_processes") == 10

    with pytest.raises(Exception) as exc:
        config_client.instance().set("worker.num_processes", 42)

    assert str(exc.value) == "File-based config is read-only"
    assert config_client.instance().get("worker.num_processes") == 10
    assert config.get("worker.num_processes") == 10
예제 #2
0
 def __init__(self, initial_config, config_file, pid_file, log_file):
     self.initial_config = initial_config
     self.config_file = config_file
     self.pid_file = pid_file
     self.log_file = log_file
     self.port = utils.available_port()
     self.read_only = False
     self.client = config_client.instance(url=self.url)
     self.client.poll_interval = 1
     self.p = None
예제 #3
0
 def child(conn):
     c = client.instance(url=config_server.url)
     while True:
         try:
             msg = conn.recv()
             if msg == "quit":
                 return
             conn.send(c.get(msg))
         except Exception as exc:
             print(exc, file=sys.stderr)
             sys.stderr.flush()
예제 #4
0
def _diff(_, args):
    source = client.instance(url=args.src).dump()
    dest = client.instance(url=args.dest).dump()

    def diff0(src, src_base, dst, dst_base):
        src_keys = set(src)
        dst_keys = set(dst)

        def name(bits):
            return ".".join(bits)

        for k in sorted(src_keys.difference(dst_keys)):
            print("Only in {}: {}".format(args.src, name(src_base + [k])))

        for k in sorted(src_keys.intersection(dst_keys)):
            src_val, dst_val = src[k], dst[k]
            if src_val == dst_val:
                continue

            if isinstance(src_val, dict):
                diff0(src_val, src_base + [k], dst_val, dst_base + [k])
            else:
                src_val = [
                    line + "\n"
                    for line in json.dumps(src_val, indent=1).split("\n")
                ]
                dst_val = [
                    line + "\n"
                    for line in json.dumps(dst_val, indent=1).split("\n")
                ]
                delta = difflib.unified_diff(src_val, dst_val,
                                             name(src_base + [k]),
                                             name(dst_base + [k]))
                print("".join(delta))

        for k in sorted(dst_keys.difference(src_keys)):
            print("Only in {}: {}".format(args.dest, name(dst_base + [k])))

    diff0(source, [], dest, [])
예제 #5
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--config-url",
                        metavar="URL",
                        help="URL of the config server",
                        default=None)
    parser.add_argument("--verbose",
                        action="store_true",
                        help="verbose operation",
                        default=False)
    subparsers = parser.add_subparsers(help="commands")

    get_p = subparsers.add_parser("get", help="get the value of a config key")
    get_p.add_argument("key", metavar="<key>")
    get_p.set_defaults(func=_get)

    set_p = subparsers.add_parser("set", help="set the value of a config key")
    set_p.add_argument("key", metavar="<key>")
    set_p.add_argument("value", metavar="<value>")
    set_p.set_defaults(func=_set)

    del_p = subparsers.add_parser(
        "delete", help="remove a setting value from the config source")
    del_p.add_argument("key", metavar="<key>")
    del_p.set_defaults(func=_delete)

    dump_p = subparsers.add_parser("dump",
                                   help="Dump config in JSON format to stdout")
    dump_p.set_defaults(func=_dump)

    diff_p = subparsers.add_parser("diff",
                                   help="compare config from two sources")
    diff_p.add_argument("src", metavar="<source url>")
    diff_p.add_argument("dest", metavar="<dest url>")
    diff_p.set_defaults(func=_diff)

    args = parser.parse_args()

    logutils.configure_logging(level=args.verbose and "DEBUG" or "WARN")

    c = client.instance(url=args.config_url)
    return args.func(c, args)
예제 #6
0
def test_unsupported_source():
    with pytest.raises(ValueError) as exc:
        config_client.instance("ftp://wherever")
    assert str(exc.value).startswith("Unsupported URL scheme")
예제 #7
0
    with pytest.raises(Exception) as exc:
        config_client.instance().set("worker.num_processes", 42)

    assert str(exc.value) == "File-based config is read-only"
    assert config_client.instance().get("worker.num_processes") == 10
    assert config.get("worker.num_processes") == 10


def test_unsupported_source():
    with pytest.raises(ValueError) as exc:
        config_client.instance("ftp://wherever")
    assert str(exc.value).startswith("Unsupported URL scheme")


@pytest.mark.parametrize(
    "instance,expected",
    [
        (
            config_client.instance("file:///some/path"),
            "FileConfigClient(url=file:///some/path)",
        ),
        (
            config_client.instance("http://localhost/blah"),
            "HTTPConfigClient(url=http://localhost/blah)",
        ),
    ],
)
def test_repr(instance, expected):
    assert repr(instance) == expected