Exemple #1
0
def test_for_git_failures(git_modules_dir, modules_repo):
    coroot = os.path.join(git_modules_dir, "testproject2")
    subprocess.check_output(
        [
            "git", "clone",
            os.path.join(git_modules_dir, "repos", "testproject"),
            "testproject2"
        ],
        cwd=git_modules_dir,
        stderr=subprocess.STDOUT,
    )
    os.chdir(coroot)
    Config.load_config()

    ProjectTool().execute("install", [])

    gp = module.gitprovider
    module.gitprovider = BadModProvider(gp,
                                        os.path.join(coroot, "libs", "mod6"))
    try:
        # test all tools, perhaps isolate to other test case
        ProjectTool().execute("install", [])
        ModuleTool().execute("list", [])
        ModuleTool().execute("update", [])
        ModuleTool().execute("status", [])
        ModuleTool().execute("push", [])
    finally:
        module.gitprovider = gp
Exemple #2
0
def test_environment_deprecated_options(caplog):
    for (deprecated_option, new_option) in [
        (cfg.agent_interval, cfg.agent_deploy_interval),
        (cfg.agent_splay, cfg.agent_deploy_splay_time),
    ]:

        Config.set(deprecated_option.section, deprecated_option.name, "22")
        caplog.clear()
        assert new_option.get() == 22
        assert "Config option %s is deprecated. Use %s instead." % (
            deprecated_option.name, new_option.name) in caplog.text

        Config.set(new_option.section, new_option.name, "23")
        caplog.clear()
        assert new_option.get() == 23
        assert "Config option %s is deprecated. Use %s instead." % (
            deprecated_option.name, new_option.name) not in caplog.text

        Config.load_config()  # Reset config options to default values
        assert new_option.get() != 23
        assert deprecated_option.get() != 23
        Config.set(new_option.section, new_option.name, "24")
        caplog.clear()
        assert new_option.get() == 24
        assert "Config option %s is deprecated. Use %s instead." % (
            deprecated_option.name, new_option.name) not in caplog.text
Exemple #3
0
def test_complex_checkout(git_modules_dir, modules_repo):
    coroot = os.path.join(git_modules_dir, "testproject")
    subprocess.check_output([
        "git", "clone",
        os.path.join(git_modules_dir, "repos", "testproject")
    ],
                            cwd=git_modules_dir,
                            stderr=subprocess.STDOUT)
    os.chdir(coroot)
    Config.load_config()

    ProjectTool().execute("install", [])
    expected = ["mod1", "mod2", "mod3", "mod6", "mod7"]
    for i in expected:
        dirname = os.path.join(coroot, "libs", i)
        assert os.path.exists(os.path.join(dirname, "signal"))
        assert not os.path.exists(os.path.join(dirname, "badsignal"))

    assert not os.path.exists(os.path.join(coroot, "libs", "mod5"))

    # test all tools, perhaps isolate to other test case
    ModuleTool().execute("list", [])
    ModuleTool().execute("update", [])
    ModuleTool().execute("status", [])
    ModuleTool().execute("push", [])
Exemple #4
0
async def test_bind_address_ipv6(async_finalizer, client):
    @protocol.method(path="/test", operation="POST", client_types=["api"])
    async def test_endpoint():
        pass

    class TestSlice(ServerSlice):
        @protocol.handle(test_endpoint)
        async def test_endpoint_handle(self):
            return 200

    # Get free port on all interfaces
    sock = netutil.bind_sockets(0, "::", family=socket.AF_INET6)[0]
    (_addr, free_port, _flowinfo, _scopeid) = sock.getsockname()
    sock.close()

    # Configure server
    Config.load_config()
    Config.set("server", "bind-port", str(free_port))
    Config.set("server", "bind-address", "::1")
    Config.set("client_rest_transport", "port", str(free_port))
    Config.set("client_rest_transport", "host", "::1")

    # Start server
    rs = Server()
    rs.add_slice(TestSlice("test"))
    await rs.start()
    async_finalizer(rs.stop)

    # Check if server is reachable on loopback interface
    result = await client.test_endpoint()
    assert result.code == 200
Exemple #5
0
async def test_validate_rs256(jwks, tmp_path):
    """
    Test that inmanta can download a rs256 public key
    """
    port = str(list(jwks._sockets.values())[0].getsockname()[1])
    config_file = os.path.join(tmp_path, "auth.cfg")
    with open(config_file, "w+", encoding="utf-8") as fd:
        fd.write("""
[auth_jwt_default]
algorithm=HS256
sign=true
client_types=agent,compiler
key=eciwliGyqECVmXtIkNpfVrtBLutZiITZKSKYhogeHMM
expire=0
issuer=https://localhost:8888/
audience=https://localhost:8888/

[auth_jwt_keycloak]
algorithm=RS256
sign=false
client_types=api
issuer=https://localhost:{0}/auth/realms/inmanta
audience=sodev
jwks_uri=http://localhost:{0}/auth/realms/inmanta/protocol/openid-connect/certs
validate_cert=false
""".format(port))

    from inmanta.config import AuthJWTConfig, Config

    Config.load_config(config_file)

    cfg_list = await asyncio.get_event_loop().run_in_executor(
        None, AuthJWTConfig.list)
    assert len(cfg_list) == 2
Exemple #6
0
def install_project(modules_dir, name, config=True):
    subroot = tempfile.mkdtemp()
    coroot = os.path.join(subroot, name)
    subprocess.check_output(["git", "clone", os.path.join(modules_dir, "repos", name)], cwd=subroot, stderr=subprocess.STDOUT)
    os.chdir(coroot)
    os.curdir = coroot
    if config:
        Config.load_config()
    return coroot
Exemple #7
0
def test_for_repo_without_versions(git_modules_dir, modules_repo):
    coroot = os.path.join(git_modules_dir, "noverproject")
    subprocess.check_output([
        "git", "clone",
        os.path.join(git_modules_dir, "repos", "noverproject")
    ],
                            cwd=git_modules_dir,
                            stderr=subprocess.STDOUT)
    os.chdir(coroot)
    Config.load_config()

    ProjectTool().execute("install", [])
Exemple #8
0
def test_bad_checkout(git_modules_dir, modules_repo):
    coroot = os.path.join(git_modules_dir, "badproject")
    subprocess.check_output(
        ["git", "clone",
         os.path.join(git_modules_dir, "repos", "badproject")],
        cwd=git_modules_dir,
        stderr=subprocess.STDOUT)
    os.chdir(coroot)
    Config.load_config()

    with pytest.raises(ModuleLoadingException):
        ProjectTool().execute("install", [])
def test_bad_checkout(modules_dir, modules_repo):
    coroot = os.path.join(modules_dir, "badproject")
    subprocess.check_output(
        ["git", "clone",
         os.path.join(modules_dir, "repos", "badproject")],
        cwd=modules_dir,
        stderr=subprocess.STDOUT)
    os.chdir(coroot)
    os.curdir = coroot
    Config.load_config()

    with pytest.raises(ModuleNotFoundException):
        ModuleTool().execute("install", [])
Exemple #10
0
def test_options():
    configa = Option("test", "a", "markerA", "test a docs")
    configb = Option("test", "B", option_as_default(configa), "test b docs")

    assert "test.a" in configb.get_default_desc()

    Config.load_config()

    assert configb.get() == "markerA"
    configa.set("MA2")
    assert configb.get() == "MA2"
    configb.set("MB2")
    assert configb.get() == "MB2"
Exemple #11
0
def test_bad_dep_checkout(git_modules_dir, modules_repo):
    coroot = os.path.join(git_modules_dir, "baddep")
    subprocess.check_output(
        ["git", "clone",
         os.path.join(git_modules_dir, "repos", "baddep")],
        cwd=git_modules_dir,
        stderr=subprocess.STDOUT)
    os.chdir(coroot)
    Config.load_config()

    with pytest.raises(CompilerException,
                       match="Not all module dependencies have been met"):
        ProjectTool().execute("install", [])
Exemple #12
0
    def test_for_repo_without_versions(self):
        coroot = os.path.join(TestModuleTool.tempdir, "noverproject")
        subprocess.check_output([
            "git", "clone",
            os.path.join(TestModuleTool.tempdir, "repos", "noverproject")
        ],
                                cwd=TestModuleTool.tempdir,
                                stderr=subprocess.STDOUT)
        os.chdir(coroot)
        os.curdir = coroot
        Config.load_config()

        ModuleTool().execute("install", [])
Exemple #13
0
    def test_bad_dep_checkout(self):
        coroot = os.path.join(TestModuleTool.tempdir, "baddep")
        subprocess.check_output([
            "git", "clone",
            os.path.join(TestModuleTool.tempdir, "repos", "baddep")
        ],
                                cwd=TestModuleTool.tempdir,
                                stderr=subprocess.STDOUT)
        os.chdir(coroot)
        os.curdir = coroot
        Config.load_config()

        with pytest.raises(CompilerException):
            ModuleTool().execute("install", [])
Exemple #14
0
async def test_bind_address_ipv4(async_finalizer, client):
    """This test case check if the Inmanta server doesn't bind on another interface than 127.0.0.1 when bind-address is equal
    to 127.0.0.1. Procedure:
        1) Get free port on all interfaces.
        2) Bind that port on a non-loopback interface, so it's not available for the inmanta server anymore.
        3) Start the Inmanta server with bind-address 127.0.0.1. and execute an API call
    """
    @protocol.method(path="/test", operation="POST", client_types=["api"])
    async def test_endpoint():
        pass

    class TestSlice(ServerSlice):
        @protocol.handle(test_endpoint)
        async def test_endpoint_handle(self):
            return 200

    # Select a bind address which is not on the loopback interface
    non_loopback_interfaces = [
        i for i in netifaces.interfaces()
        if i != "lo" and socket.AF_INET in netifaces.ifaddresses(i)
    ]
    bind_iface = "eth0" if "eth0" in non_loopback_interfaces else random.choice(
        non_loopback_interfaces)
    bind_addr = netifaces.ifaddresses(bind_iface)[socket.AF_INET][0]["addr"]

    # Get free port on all interfaces
    sock = netutil.bind_sockets(0, "0.0.0.0", family=socket.AF_INET)[0]
    _addr, free_port = sock.getsockname()
    sock.close()

    # Bind port on non-loopback interface
    sock = netutil.bind_sockets(free_port, bind_addr, family=socket.AF_INET)[0]
    try:
        # Configure server
        Config.load_config()
        Config.set("server", "bind-port", str(free_port))
        Config.set("server", "bind-address", "127.0.0.1")
        Config.set("client_rest_transport", "port", str(free_port))

        # Start server
        rs = Server()
        rs.add_slice(TestSlice("test"))
        await rs.start()
        async_finalizer(rs.stop)

        # Check if server is reachable on loopback interface
        result = await client.test_endpoint()
        assert result.code == 200
    finally:
        sock.close()
Exemple #15
0
def test_dev_checkout(git_modules_dir, modules_repo):
    coroot = os.path.join(git_modules_dir, "devproject")
    subprocess.check_output(
        ["git", "clone",
         os.path.join(git_modules_dir, "repos", "devproject")],
        cwd=git_modules_dir,
        stderr=subprocess.STDOUT)
    os.chdir(coroot)
    Config.load_config()

    ProjectTool().execute("install", [])

    dirname = os.path.join(coroot, "libs", "mod8")
    assert os.path.exists(os.path.join(dirname, "devsignal"))
    assert not os.path.exists(os.path.join(dirname, "mastersignal"))
Exemple #16
0
def configure(unused_tcp_port, database_name, database_port):
    import inmanta.agent.config  # noqa: F401
    import inmanta.server.config  # noqa: F401
    from inmanta.config import Config

    free_port = str(unused_tcp_port)
    Config.load_config()
    Config.set("server", "bind-port", free_port)
    Config.set("agent_rest_transport", "port", free_port)
    Config.set("compiler_rest_transport", "port", free_port)
    Config.set("client_rest_transport", "port", free_port)
    Config.set("cmdline_rest_transport", "port", free_port)
    Config.set("database", "name", database_name)
    Config.set("database", "host", "localhost")
    Config.set("database", "port", str(database_port))
Exemple #17
0
def app(args):
    parser = cmd_parser()

    options, other = parser.parse_known_args(args=args)
    options.other = other

    # Load the configuration
    Config.load_config(options.config_file)

    # start the command
    if not hasattr(options, "func"):
        # show help
        parser.print_usage()
        return

    options.func(options)
Exemple #18
0
    def test_dev_checkout(self):
        coroot = os.path.join(TestModuleTool.tempdir, "devproject")
        subprocess.check_output([
            "git", "clone",
            os.path.join(TestModuleTool.tempdir, "repos", "devproject")
        ],
                                cwd=TestModuleTool.tempdir,
                                stderr=subprocess.STDOUT)
        os.chdir(coroot)
        os.curdir = coroot
        Config.load_config()

        ModuleTool().execute("install", [])

        dirname = os.path.join(coroot, "libs", "mod8")
        assert os.path.exists(os.path.join(dirname, "devsignal"))
        assert not os.path.exists(os.path.join(dirname, "mastersignal"))
Exemple #19
0
def test_module_update_with_install_mode_master(
    tmpdir: py.path.local,
    modules_repo: str,
    kwargs_update_method: Dict[str, str],
    mod2_should_be_updated: bool,
    mod8_should_be_updated: bool,
) -> None:
    # Make a copy of masterproject_multi_mod
    masterproject_multi_mod = tmpdir.join("masterproject_multi_mod")
    clone_repo(modules_repo, "masterproject_multi_mod", tmpdir)
    libs_folder = os.path.join(masterproject_multi_mod, "libs")
    os.mkdir(libs_folder)

    # Set masterproject_multi_mod as current project
    os.chdir(masterproject_multi_mod)
    Config.load_config()

    # Dependencies masterproject_multi_mod
    for mod in ["mod2", "mod8"]:
        # Clone mod in root tmpdir
        clone_repo(modules_repo, mod, tmpdir)

        # Clone mod from root of tmpdir into libs folder of masterproject_multi_mod
        clone_repo(tmpdir, mod, libs_folder)

        # Update module in root of tmpdir by adding an extra file
        file_name_extra_file = "test_file"
        path_mod = os.path.join(tmpdir, mod)
        add_file(path_mod, file_name_extra_file, "test", "Second commit")

        # Assert test_file not present in libs folder of masterproject_multi_mod
        path_extra_file = os.path.join(libs_folder, mod, file_name_extra_file)
        assert not os.path.exists(path_extra_file)

    # Update module(s) of masterproject_multi_mod
    ModuleTool().update(**kwargs_update_method)

    # Assert availability of test_file in masterproject_multi_mod
    extra_file_mod2 = os.path.join(libs_folder, "mod2", file_name_extra_file)
    assert os.path.exists(extra_file_mod2) == mod2_should_be_updated
    extra_file_mod8 = os.path.join(libs_folder, "mod8", file_name_extra_file)
    assert os.path.exists(extra_file_mod8) == mod8_should_be_updated
Exemple #20
0
    def test_install_for_git_failures(self):
        coroot = os.path.join(TestModuleTool.tempdir, "testproject3")
        subprocess.check_output([
            "git", "clone",
            os.path.join(TestModuleTool.tempdir, "repos", "testproject"),
            "testproject3"
        ],
                                cwd=TestModuleTool.tempdir,
                                stderr=subprocess.STDOUT)
        os.chdir(coroot)
        os.curdir = coroot
        Config.load_config()

        gp = module.gitprovider
        module.gitprovider = BadModProvider(
            gp, os.path.join(coroot, "libs", "mod6"))
        try:
            with pytest.raises(ModuleNotFoundException):
                ModuleTool().execute("install", [])
        finally:
            module.gitprovider = gp
def test_bad_setup(modules_dir, modules_repo):
    coroot = os.path.join(modules_dir, "badprojectx")
    subprocess.check_output([
        "git", "clone",
        os.path.join(modules_dir, "repos", "badproject"), coroot
    ],
                            cwd=modules_dir,
                            stderr=subprocess.STDOUT)
    os.chdir(coroot)
    os.curdir = coroot
    Config.load_config()

    mod1 = os.path.join(coroot, "libs", "mod1")
    os.makedirs(mod1)
    subprocess.check_output(
        ["git", "clone",
         os.path.join(modules_dir, "repos", "mod2"), mod1],
        cwd=modules_dir,
        stderr=subprocess.STDOUT)

    with pytest.raises(ModuleNotFoundException):
        ModuleTool().execute("verify", [])
Exemple #22
0
def test_install_for_git_failures(git_modules_dir, modules_repo):
    coroot = os.path.join(git_modules_dir, "testproject3")
    subprocess.check_output(
        [
            "git", "clone",
            os.path.join(git_modules_dir, "repos", "testproject"),
            "testproject3"
        ],
        cwd=git_modules_dir,
        stderr=subprocess.STDOUT,
    )
    os.chdir(coroot)
    Config.load_config()

    gp = module.gitprovider
    module.gitprovider = BadModProvider(gp,
                                        os.path.join(coroot, "libs", "mod6"))
    try:
        with pytest.raises(ModuleLoadingException):
            ProjectTool().execute("install", [])
    finally:
        module.gitprovider = gp
Exemple #23
0
def main():
    Config.load_config()
    cmd()
Exemple #24
0
def main() -> None:
    Config.load_config()
    cmd()
def test_timeout(free_port):

    from inmanta.config import Config
    import inmanta.agent.config  # nopep8
    import inmanta.server.config  # nopep8

    io_loop = IOLoop.current()

    # start server
    Config.load_config()
    Config.set("server_rest_transport", "port", free_port)
    Config.set("agent_rest_transport", "port", free_port)
    Config.set("compiler_rest_transport", "port", free_port)
    Config.set("client_rest_transport", "port", free_port)
    Config.set("cmdline_rest_transport", "port", free_port)
    server = Server("server", io_loop, interval=2)
    server.start()

    env = uuid.uuid4()

    # agent 1
    agent = Agent("agent", io_loop)
    agent.add_end_point_name("agent")
    agent.set_environment(env)
    agent.start()

    @gen.coroutine
    def do_call():
        # wait till up
        yield retry_limited(lambda: len(server._sessions) == 1, 0.1)
        assert len(server._sessions) == 1

        # agent 2
        agent2 = Agent("agent", io_loop)
        agent2.add_end_point_name("agent")
        agent2.set_environment(env)
        agent2.start()

        # wait till up
        yield retry_limited(lambda: len(server._sessions) == 2, 0.1)
        assert len(server._sessions) == 2

        # see if it stays up
        yield(check_sessions(server._sessions.values()))
        yield sleep(2)
        assert len(server._sessions) == 2
        yield(check_sessions(server._sessions.values()))

        # take it down
        agent2.stop()

        # timout
        yield sleep(2)
        # check if down
        assert len(server._sessions) == 1
        print(server._sessions)
        yield(check_sessions(server._sessions.values()))
        assert server.expires == 1
        agent.stop()
        server.stop()

        io_loop.stop()

    io_loop.add_callback(do_call)
    io_loop.add_timeout(io_loop.time() + 2, lambda: io_loop.stop())
    try:
        io_loop.start()
    except KeyboardInterrupt:
        io_loop.stop()
    server.stop()
    agent.stop()
def test_2way_protocol(free_port, logs=False):

    from inmanta.config import Config

    import inmanta.agent.config  # nopep8
    import inmanta.server.config  # nopep8

    if logs:
        # set logging to sensible defaults
        formatter = colorlog.ColoredFormatter(
            "%(log_color)s%(levelname)-8s%(reset)s %(green)s%(name)s %(blue)s%(message)s",
            datefmt=None,
            reset=True,
            log_colors={
                'DEBUG': 'cyan',
                'INFO': 'green',
                'WARNING': 'yellow',
                'ERROR': 'red',
                'CRITICAL': 'red',
            }
        )

        stream = logging.StreamHandler()
        stream.setLevel(logging.DEBUG)

        if hasattr(sys.stdout, 'isatty') and sys.stdout.isatty():
            stream.setFormatter(formatter)

        logging.root.handlers = []
        logging.root.addHandler(stream)
        logging.root.setLevel(logging.DEBUG)

    Config.load_config()
    Config.set("server_rest_transport", "port", free_port)
    Config.set("agent_rest_transport", "port", free_port)
    Config.set("compiler_rest_transport", "port", free_port)
    Config.set("client_rest_transport", "port", free_port)
    Config.set("cmdline_rest_transport", "port", free_port)

    io_loop = IOLoop.current()
    server = Server("server", io_loop)
    server.start()

    agent = Agent("agent", io_loop)
    agent.add_end_point_name("agent")
    agent.set_environment(uuid.uuid4())
    agent.start()

    @gen.coroutine
    def do_call():
        client = protocol.Client("client")
        status = yield client.get_status_x(str(agent.environment))
        assert status.code == 200
        assert "agents" in status.result
        assert len(status.result["agents"]) == 1
        assert status.result["agents"][0]["status"], "ok"
        server.stop()
        io_loop.stop()

    io_loop.add_callback(do_call)
    io_loop.add_timeout(io_loop.time() + 2, lambda: io_loop.stop())
    try:
        io_loop.start()
    except KeyboardInterrupt:
        io_loop.stop()
    server.stop()
    agent.stop()
Exemple #27
0
 def __init__(self):
     self.libs = tempfile.mkdtemp()
     self.env = tempfile.mkdtemp()
     Config.load_config()
     Config.set("config", "environment", str(uuid.uuid4()))
Exemple #28
0
async def test_bind_port(unused_tcp_port, async_finalizer, client, caplog):
    @protocol.method(path="/test", operation="POST", client_types=["api"])
    async def test_endpoint():
        pass

    class TestSlice(ServerSlice):
        @protocol.handle(test_endpoint)
        async def test_endpoint_handle(self):
            return 200

    async def assert_port_bound():
        # Start server
        rs = Server()
        rs.add_slice(TestSlice("test"))
        await rs.start()
        async_finalizer(rs.stop)

        # Check if server is reachable on loopback interface
        result = await client.test_endpoint()
        assert result.code == 200
        await rs.stop()

    deprecation_line_log_line = (
        "The server_rest_transport.port config option is deprecated in favour of the "
        "server.bind-port option.")
    ignoring_log_line = (
        "Ignoring the server_rest_transport.port config option since the new config options "
        "server.bind-port/server.bind-address are used.")

    # Old config option server_rest_transport.port is set
    Config.load_config()
    Config.set("server_rest_transport", "port", str(unused_tcp_port))
    Config.set("client_rest_transport", "port", str(unused_tcp_port))
    caplog.clear()
    await assert_port_bound()
    log_sequence = LogSequence(caplog, allow_errors=False)
    log_sequence.contains("py.warnings", logging.WARNING,
                          deprecation_line_log_line)
    log_sequence.assert_not("py.warnings", logging.WARNING, ignoring_log_line)

    # Old config option server_rest_transport.port and new config option server.bind-port are set together
    Config.load_config()
    Config.set("server_rest_transport", "port", str(unused_tcp_port))
    Config.set("server", "bind-port", str(unused_tcp_port))
    Config.set("client_rest_transport", "port", str(unused_tcp_port))
    caplog.clear()
    await assert_port_bound()
    log_sequence = LogSequence(caplog, allow_errors=False)
    log_sequence.assert_not("py.warnings", logging.WARNING,
                            deprecation_line_log_line)
    log_sequence.contains("py.warnings", logging.WARNING, ignoring_log_line)

    # The new config option server.bind-port is set
    Config.load_config()
    Config.set("server", "bind-port", str(unused_tcp_port))
    Config.set("client_rest_transport", "port", str(unused_tcp_port))
    caplog.clear()
    await assert_port_bound()
    log_sequence = LogSequence(caplog, allow_errors=False)
    log_sequence.assert_not("py.warnings", logging.WARNING,
                            deprecation_line_log_line)
    log_sequence.assert_not("py.warnings", logging.WARNING, ignoring_log_line)
Exemple #29
0
def app() -> None:
    """
    Run the compiler
    """
    # Send logs to stdout
    stream_handler = _get_default_stream_handler()
    logging.root.handlers = []
    logging.root.addHandler(stream_handler)
    logging.root.setLevel(0)

    # do an initial load of known config files to build the libdir path
    Config.load_config()
    parser = cmd_parser()
    options, other = parser.parse_known_args()
    options.other = other

    # Log everything to a log_file if logfile is provided
    if options.log_file:
        watched_file_handler = _get_watched_file_handler(options)
        logging.root.addHandler(watched_file_handler)
        logging.root.removeHandler(stream_handler)
    else:
        if options.timed:
            formatter = _get_log_formatter_for_stream_handler(timed=True)
            stream_handler.setFormatter(formatter)
        log_level = _convert_to_log_level(options.verbose)
        stream_handler.setLevel(log_level)

    logging.captureWarnings(True)

    if options.config_file and not os.path.exists(options.config_file):
        LOGGER.warning("Config file %s doesn't exist", options.config_file)

    # Load the configuration
    Config.load_config(options.config_file, options.config_dir)

    if options.inmanta_version:
        print_versions_installed_components_and_exit()

    if options.warnings is not None:
        Config.set("warnings", "default", options.warnings)

    config = Config.get()
    assert isinstance(config, ConfigParser)
    WarningsManager.apply_config(config["warnings"] if "warnings" in config else None)

    # start the command
    if not hasattr(options, "func"):
        # show help
        parser.print_usage()
        return

    def report(e: BaseException) -> None:
        minus_x_set_top_level_command = options.errors
        minus_x_set_subcommand = hasattr(options, "errors_subcommand") and options.errors_subcommand
        if not minus_x_set_top_level_command and not minus_x_set_subcommand:
            if isinstance(e, CompilerException):
                print(e.format_trace(indent="  "), file=sys.stderr)
            else:
                print(str(e), file=sys.stderr)
        else:
            sys.excepthook(*sys.exc_info())

        if isinstance(e, CompilerException):
            from inmanta.compiler.help.explainer import ExplainerFactory

            helpmsg = ExplainerFactory().explain_and_format(e, plain=not _is_on_tty())
            if helpmsg is not None:
                print(helpmsg)

    try:
        options.func(options)
    except ShowUsageException as e:
        print(e.args[0], file=sys.stderr)
        parser.print_usage()
    except CLIException as e:
        report(e)
        sys.exit(e.exitcode)
    except Exception as e:
        report(e)
        sys.exit(1)
    except KeyboardInterrupt as e:
        report(e)
        sys.exit(1)
    sys.exit(0)
Exemple #30
0
def test_configfile_hierarchy(monkeypatch, tmpdir):
    etc_inmanta_dir = os.path.join(tmpdir, "etc", "inmanta")
    os.makedirs(etc_inmanta_dir, exist_ok=False)

    main_inmanta_cfg_file = os.path.join(etc_inmanta_dir, "inmanta.cfg")

    inmanta_d_dir = os.path.join(etc_inmanta_dir, "inmanta.d")
    os.mkdir(inmanta_d_dir)

    inmanta_d_cfg_file01 = os.path.join(inmanta_d_dir, "01-dbconfig.cfg")
    inmanta_d_cfg_file02 = os.path.join(inmanta_d_dir, "02-dbconfig.cfg")
    inmanta_d_cfg_file_no_cfg_extension = os.path.join(inmanta_d_dir,
                                                       "03-config")

    dot_inmanta_file = os.path.join(tmpdir, ".inmanta")
    dot_inmanta_cfg_file = os.path.join(tmpdir, ".inmanta.cfg")

    min_c_file = os.path.join(tmpdir, "custom.cfg")

    monkeypatch.setenv("INMANTA_SERVER_AUTH", "true")
    monkeypatch.setenv("INMANTA_SERVER_AGENT_TIMEOUT", "60")

    with open(main_inmanta_cfg_file, "w", encoding="utf-8") as f:
        f.write("""
[server]
auth=false
[config]
log-dir=/log
[database]
host=host1
name=db1
port=1234
[influxdb]
host=host1
interval=10
tags=tag1=value1
[dashboard]
path=/some/directory
client-id=test
        """)

    with open(inmanta_d_cfg_file01, "w", encoding="utf-8") as f:
        f.write("""
[database]
host=host2
name=db2
[influxdb]
host=host2
        """)

    with open(inmanta_d_cfg_file02, "w", encoding="utf-8") as f:
        f.write("""
[database]
port=5678
[influxdb]
host=host3
interval=20
        """)

    with open(inmanta_d_cfg_file_no_cfg_extension, "w", encoding="utf-8") as f:
        f.write("""
[database]
port=9999
        """)

    with open(dot_inmanta_file, "w", encoding="utf-8") as f:
        f.write("""
[database]
host=host3
[influxdb]
tags=tag2=value2
[dashboard]
path=/some/other/directory
        """)

    with open(dot_inmanta_cfg_file, "w", encoding="utf-8") as f:
        f.write("""
[dashboard]
path=/directory
client-id=test123
        """)

    with open(min_c_file, "w", encoding="utf-8") as f:
        f.write("""
[dashboard]
client-id=test456
        """)

    os.chdir(tmpdir)
    Config.load_config(min_c_config_file=min_c_file,
                       config_dir=inmanta_d_dir,
                       main_cfg_file=main_inmanta_cfg_file)

    assert Config.get("config", "log-dir") == "/log"
    assert Config.get("database", "host") == "host3"
    assert Config.get("database", "name") == "db2"
    assert Config.get("database", "port") == 5678
    assert Config.get("influxdb", "host") == "host3"
    assert Config.get("influxdb", "interval") == 20
    assert Config.get("influxdb", "tags")["tag2"] == "value2"
    assert Config.get("dashboard", "path") == "/directory"
    assert Config.get("dashboard", "client-id") == "test456"
    assert Config.get("server", "auth")
    assert Config.get("server", "agent-timeout") == 60