def test_dataflow_exception(snippetcompiler): snippetcompiler.setup_for_snippet(""" x = 0 x = 1 """, ) Config.set("compiler", "datatrace_enable", "true") try: compiler.do_compile() except DoubleSetException as e: assert e.msg.strip() == ( """ value set twice: old value: 0 set at {dir}/main.cf:2 new value: 1 set at {dir}/main.cf:3 data trace: x ├── 0 │ SET BY `x = 0` │ AT {dir}/main.cf:2 └── 1 SET BY `x = 1` AT {dir}/main.cf:3 """.strip().format( # noqa: W191, E101 dir=snippetcompiler.project_dir))
def compile(self, env, logfile=None, envvars=None): Config.set("config", "environment", env.envid) # reset compiler state # do_compile() apppath = os.path.abspath( os.path.join(__file__, "../../src/inmanta/app.py")) args = [sys.executable, apppath] if logfile is not None: args += ["--log-file", logfile, "--log-file-level", "3", "-v"] else: args += ["-vvv"] args = args + [ "compile", "-e", env.envid, "--server_address", env.connection.server, "--server_port", "8888" ] if env.auth: args += ["--username", "jos", "--password", "raienvnWAVbaerMSZ"] if env.ssl: args += ["--ssl", "--ssl-ca-cert", "/etc/pki/tls/certs/server.crt"] try: env = os.environ.copy() if envvars is not None: for k, v in envvars.items(): env[k] = v subprocess.check_output(args, cwd=self.target, env=env) except CalledProcessError as e: print(e.output) raise e
async def test_agent_timeout(unused_tcp_port, no_tid_check, async_finalizer, postgres_db, database_name): from inmanta.config import Config configure(unused_tcp_port, database_name, postgres_db.port) Config.set("server", "agent-timeout", "1") rs = Server() server = SessionSpy() rs.get_slice(SLICE_SESSION_MANAGER).add_listener(server) rs.add_slice(server) await rs.start() async_finalizer(rs.stop) env = uuid.uuid4() # agent 1 agent = Agent("agent") await agent.add_end_point_name("agent") agent.set_environment(env) await agent.start() async_finalizer(agent.stop) # wait till up await retry_limited(lambda: len(server.get_sessions()) == 1, timeout=10) assert len(server.get_sessions()) == 1 await assert_agent_counter(agent, 1, 0) # agent 2 agent2 = Agent("agent") await agent2.add_end_point_name("agent") agent2.set_environment(env) await agent2.start() async_finalizer(agent2.stop) # wait till up await retry_limited(lambda: len(server.get_sessions()) == 2, timeout=10) assert len(server.get_sessions()) == 2 await assert_agent_counter(agent, 1, 0) await assert_agent_counter(agent2, 1, 0) # see if it stays up await check_sessions(server.get_sessions()) await sleep(1.1) assert len(server.get_sessions()) == 2 await check_sessions(server.get_sessions()) # take it down await agent2.stop() # Timeout=2 # -> 1sec: Wait for agent-timeout # -> 1sec: Wait until session bookkeeping is updated await retry_limited(lambda: len(server.get_sessions()) == 1, timeout=2) print(server.get_sessions()) await check_sessions(server.get_sessions()) assert server.expires == 1 await assert_agent_counter(agent, 1, 0) await assert_agent_counter(agent2, 1, 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
def __init__(self, host: Optional[str], port: Optional[int]) -> None: if host is None: self.host = cmdline_rest_transport.host.get() else: self.host = host Config.set("cmdline_rest_transport", "host", host) if port is None: self.port = cmdline_rest_transport.port.get() else: self.port = port Config.set("cmdline_rest_transport", "port", str(port)) self._client = protocol.SyncClient("cmdline")
async def test_server_timeout(unused_tcp_port, no_tid_check, async_finalizer, postgres_db, database_name): from inmanta.config import Config configure(unused_tcp_port, database_name, postgres_db.port) Config.set("server", "agent-timeout", "1") async def start_server(): rs = Server() server = SessionSpy() rs.get_slice(SLICE_SESSION_MANAGER).add_listener(server) rs.add_slice(server) await rs.start() async_finalizer(rs.stop) return server, rs server, rs = await start_server() env = uuid.uuid4() # agent 1 agent = Agent("agent") await agent.add_end_point_name("agent") agent.set_environment(env) await agent.start() async_finalizer(agent.stop) # wait till up await retry_limited(lambda: len(server.get_sessions()) == 1, 10) assert len(server.get_sessions()) == 1 await assert_agent_counter(agent, 1, 0) await rs.stop() # timeout await sleep(1.1) # check agent disconnected await assert_agent_counter(agent, 1, 1) # recover server, rs = await start_server() await retry_limited(lambda: len(server.get_sessions()) == 1, 10) assert len(server.get_sessions()) == 1 await assert_agent_counter(agent, 2, 1)
def export_to_file( path: Path, expected_error_type: Optional[Type[CompilerException]] = None ) -> CompileData: """ Compiles, exporting to a file, and returns the file contents, loaded as CompileData. """ Config.set("compiler", "export_compile_data", "true") Config.set("compiler", "export_compile_data_file", str(path)) if expected_error_type is None: compiler.do_compile() else: with pytest.raises(CompilerException): compiler.do_compile() with path.open() as f: return CompileData(**json.loads(f.read()))
def export(self, env, logfile, envvars=None): Config.set("config", "environment", env.envid) # reset compiler state # do_compile() app = os.path.abspath( os.path.join(__file__, "../../src/inmanta/app.py")) inmanta_path = [sys.executable, app] args = inmanta_path if logfile is None: logfile = tempfile.mktemp() logfile = os.path.abspath(logfile) args += ["--log-file", logfile, "--log-file-level", "3"] args = args + [ "-v", "export", "-e", env.envid, "--server_address", env.connection.server, "--server_port", "8888" ] if env.auth: args += ["--username", "jos", "--password", "raienvnWAVbaerMSZ"] if env.ssl: args += ["--ssl", "--ssl-ca-cert", "/etc/pki/tls/certs/server.crt"] try: env = os.environ.copy() if envvars is not None: for k, v in envvars.items(): env[k] = v subprocess.check_call(args, cwd=self.target, env=env, stderr=subprocess.STDOUT) out = subprocess.check_output( ["grep", "Committed resources with version", logfile], env=os.environ.copy(), universal_newlines=True) return re.search(r'Committed resources with version ([0-9]+)', out).group(1) except CalledProcessError as e: print(e.output.decode("utf-8")) raise e
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
def __init__(self, host, port, io_loop): self._client = None if io_loop is not None: self._io_loop = io_loop self._own_loop = False else: self._io_loop = IOLoop.current() self._own_loop = True if host is None: self.host = cmdline_rest_transport.host.get() else: self.host = host Config.set("cmdline_rest_transport", "host", host) if port is None: self.port = cmdline_rest_transport.port.get() else: self.port = port Config.set("cmdline_rest_transport", "port", str(port)) self._client = protocol.Client("cmdline")
def test_dataflow_multi_exception(snippetcompiler): snippetcompiler.setup_for_snippet( """ entity A: number n end implement A using std::none x = A() y = A() x.n = y.n y.n = nn nn = mm mm = nn """, ) Config.set("compiler", "datatrace_enable", "true") try: compiler.do_compile() except MultiException as e: assert e.format_trace(indent=" ").strip() == (""" Reported 1 errors error 0: The object __config__::A (instantiated at {dir}/main.cf:9) is not complete: attribute n ({dir}/main.cf:3:12) is not set data trace: attribute n on __config__::A instance SUBTREE for __config__::A instance: CONSTRUCTED BY `A()` AT {dir}/main.cf:9 └── nn SET BY `y.n = nn` AT {dir}/main.cf:12 EQUIVALENT TO {{mm, nn}} DUE TO STATEMENTS: `nn = mm` AT {dir}/main.cf:14 `mm = nn` AT {dir}/main.cf:15 """.strip().format(dir=snippetcompiler.project_dir))
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()
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 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)
def export(options: argparse.Namespace) -> None: if options.environment is not None: Config.set("config", "environment", options.environment) if options.server is not None: Config.set("compiler_rest_transport", "host", options.server) if options.port is not None: Config.set("compiler_rest_transport", "port", options.port) if options.token is not None: Config.set("compiler_rest_transport", "token", options.token) if options.ssl: Config.set("compiler_rest_transport", "ssl", "true") if options.ca_cert is not None: Config.set("compiler_rest_transport", "ssl-ca-cert-file", options.ca_cert) if options.export_compile_data is True: Config.set("compiler", "export_compile_data", "true") if options.export_compile_data_file is not None: Config.set("compiler", "export_compile_data_file", options.export_compile_data_file) compiler_features.read_options_to_config(options) # try to parse the metadata as json. If a normal string, create json for it. if options.metadata is not None and len(options.metadata) > 0: try: metadata = json.loads(options.metadata) except json.decoder.JSONDecodeError: metadata = {"message": options.metadata} else: metadata = {"message": "Manual compile on the CLI by user"} if "cli-user" not in metadata and "USERNAME" in os.environ: metadata["cli-user"] = os.environ["USERNAME"] if "hostname" not in metadata: metadata["hostname"] = socket.gethostname() if "type" not in metadata: metadata["type"] = "manual" module.Project.get(options.main_file) from inmanta.export import Exporter # noqa: H307 exp = None types: Optional[Dict[str, inmanta_type.Type]] scopes: Optional[Namespace] try: (types, scopes) = do_compile() except Exception as e: exp = e types, scopes = (None, None) # Even if the compile failed we might have collected additional data such as unknowns. So # continue the export export = Exporter(options) results = export.run( types, scopes, metadata=metadata, model_export=options.model_export, export_plugin=options.export_plugin ) version = results[0] if exp is not None: raise exp if options.model: modelexporter = ModelExporter(types) with open("testdump.json", "w", encoding="utf-8") as fh: print(yaml.dump(modelexporter.export_all())) json.dump(modelexporter.export_all(), fh) if options.deploy: conn = protocol.SyncClient("compiler") LOGGER.info("Triggering deploy for version %d" % version) tid = cfg_env.get() agent_trigger_method = const.AgentTriggerMethod.get_agent_trigger_method(options.full_deploy) conn.release_version(tid, version, True, agent_trigger_method)
def compile_project(options: argparse.Namespace) -> None: if options.environment is not None: Config.set("config", "environment", options.environment) if options.server is not None: Config.set("compiler_rest_transport", "host", options.server) if options.port is not None: Config.set("compiler_rest_transport", "port", options.port) if options.user is not None: Config.set("compiler_rest_transport", "username", options.user) if options.password is not None: Config.set("compiler_rest_transport", "password", options.password) if options.ssl: Config.set("compiler_rest_transport", "ssl", "true") if options.ca_cert is not None: Config.set("compiler_rest_transport", "ssl-ca-cert-file", options.ca_cert) if options.export_compile_data is True: Config.set("compiler", "export_compile_data", "true") if options.export_compile_data_file is not None: Config.set("compiler", "export_compile_data_file", options.export_compile_data_file) if options.datatrace is True: Config.set("compiler", "datatrace_enable", "true") if options.dataflow_graphic is True: Config.set("compiler", "dataflow_graphic_enable", "true") compiler_features.read_options_to_config(options) module.Project.get(options.main_file) if options.profile: import cProfile import pstats cProfile.runctx("do_compile()", globals(), {}, "run.profile") p = pstats.Stats("run.profile") p.strip_dirs().sort_stats("time").print_stats(20) else: t1 = time.time() do_compile() LOGGER.debug("Compile time: %0.03f seconds", time.time() - t1)
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)
def __init__(self): self.libs = tempfile.mkdtemp() self.env = tempfile.mkdtemp() Config.load_config() Config.set("config", "environment", str(uuid.uuid4()))
def __init__(self, server, auth=False, ssl=False): self.server = server TransportConfig("autotest") Config.set("autotest_rest_transport", "host", self.server) Config.set("autotest_rest_transport", "port", "8888") Config.set("compiler_rest_transport", "host", self.server) Config.set("compiler_rest_transport", "port", "8888") if auth: Config.set("autotest_rest_transport", "username", "jos") Config.set("autotest_rest_transport", "password", "raienvnWAVbaerMSZ") Config.set("compiler_rest_transport", "username", "jos") Config.set("compiler_rest_transport", "password", "raienvnWAVbaerMSZ") if ssl: Config.set("autotest_rest_transport", "ssl", "true") Config.set("compiler_rest_transport", "ssl", "true") Config.set("autotest_rest_transport", "ssl_ca_cert_file", "/etc/pki/tls/certs/server.crt") Config.set("compiler_rest_transport", "ssl_ca_cert_file", "/etc/pki/tls/certs/server.crt") self._client = inmanta.protocol.Client("autotest") self.auth = auth self.ssl = ssl
def export(options): if options.environment is not None: Config.set("config", "environment", options.environment) if options.server is not None: Config.set("compiler_rest_transport", "host", options.server) if options.server is not None: Config.set("compiler_rest_transport", "port", options.port) if options.token is not None: Config.set("compiler_rest_transport", "token", options.token) if options.ssl: Config.set("compiler_rest_transport", "ssl", "true") if options.ca_cert is not None: Config.set("compiler_rest_transport", "ssl-ca-cert-file", options.ca_cert) module.Project.get(options.main_file) from inmanta.export import Exporter # noqa: H307 exp = None try: (types, scopes) = do_compile() except Exception as e: exp = e types, scopes = (None, None) export = Exporter(options) version, _ = export.run(types, scopes) if exp is not None: if not options.errors: print(exp, file=sys.stderr) sys.exit(1) else: raise exp if options.deploy: conn = protocol.Client("compiler") LOGGER.info("Triggering deploy for version %d" % version) tid = cfg_env.get() IOLoop.current().run_sync(lambda: conn.release_version(tid, version, True), 60)
def compile(): self.snippetcompiler.setup_for_snippet(snippet) Config.set("compiler", "datatrace_enable", "true") (self._types, root_ns) = compiler.do_compile() self._namespace = root_ns.get_child("__config__")
def compile_project(options): if options.environment is not None: Config.set("config", "environment", options.environment) if options.server is not None: Config.set("compiler_rest_transport", "host", options.server) if options.port is not None: Config.set("compiler_rest_transport", "port", options.port) if options.user is not None: Config.set("compiler_rest_transport", "username", options.user) if options.password is not None: Config.set("compiler_rest_transport", "password", options.password) if options.ssl: Config.set("compiler_rest_transport", "ssl", "true") if options.ca_cert is not None: Config.set("compiler_rest_transport", "ssl-ca-cert-file", options.ca_cert) module.Project.get(options.main_file) try: if options.profile: import cProfile import pstats result = cProfile.runctx('do_compile()', globals(), {}, "run.profile") p = pstats.Stats('run.profile') p.strip_dirs().sort_stats("time").print_stats(20) else: t1 = time.time() result = do_compile() LOGGER.debug("Compile time: %0.03f seconds", time.time() - t1) return result except CompilerException as e: if not options.errors: print(str(e), file=sys.stderr) sys.exit(1) else: raise e
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))
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()