def test_get_config_value_from_python(): ScoutConfig.set(socket_path="/set/from/python") config = ScoutConfig() try: assert config.value("socket_path") == "/set/from/python" finally: ScoutConfig.reset_all()
def test_core_agent_permissions_custom(): ScoutConfig.set(core_agent_permissions=770) config = ScoutConfig() try: assert 0o770 == config.core_agent_permissions() finally: ScoutConfig.reset_all()
def test_list_conversion_from_python(original, converted): ScoutConfig.set(disabled_instruments=original) config = ScoutConfig() try: assert config.value("disabled_instruments") == converted finally: ScoutConfig.reset_all()
def test_core_agent_permissions_invalid_uses_default(): ScoutConfig.set(core_agent_permissions="THIS IS INVALID") config = ScoutConfig() try: assert 0o700 == config.core_agent_permissions() finally: ScoutConfig.reset_all()
def test_boolean_conversion_from_python(original, converted): ScoutConfig.set(monitor=original) config = ScoutConfig() try: assert config.value("monitor") is converted finally: ScoutConfig.reset_all()
def test_float_conversion_from_python(original, converted): ScoutConfig.set(shutdown_timeout_seconds=original) config = ScoutConfig() try: assert config.value("shutdown_timeout_seconds") == converted finally: ScoutConfig.reset_all()
def test_ignore(): ScoutConfig.set(ignore=["/health"]) assert ignore_path("/health") assert ignore_path("/health/foo") assert not ignore_path("/users") ScoutConfig.reset_all()
def install(cls): configs = {} configs['application_root'] = settings.BASE_DIR for name in filter(lambda x: x.startswith('SCOUT_'), dir(settings)): value = getattr(settings, name) clean_name = name.replace('SCOUT_', '').lower() configs[clean_name] = value ScoutConfig.set(**configs)
def test_env_outranks_python(): os.environ["SCOUT_SOCKET_PATH"] = "/set/in/env" ScoutConfig.set(socket_path="/set/from/python") config = ScoutConfig() try: assert config.value("socket_path") == "/set/in/env" finally: del os.environ["SCOUT_SOCKET_PATH"] ScoutConfig.reset_all()
def set_config_from_bottle(self, app): scout_config = ScoutConfig() bottle_configs = {} for k in scout_config.known_keys(): value = app.config.get("scout.{}".format(k)) if value is not None and value != "": bottle_configs[k] = value scout_config.set(**bottle_configs) return scout_config
def copy_config(self): configs = {} if getattr(settings, "BASE_DIR", None) is not None: configs["application_root"] = settings.BASE_DIR for name in dir(settings): if name.startswith("SCOUT_"): value = getattr(settings, name) clean_name = name.replace("SCOUT_", "").lower() configs[clean_name] = value ScoutConfig.set(**configs)
def test_ignore_multiple_prefixes(): ScoutConfig.set(ignore=["/health", "/api"]) assert ignore_path("/health") assert ignore_path("/health/foo") assert ignore_path("/api") assert ignore_path("/api/foo") assert not ignore_path("/users") ScoutConfig.reset_all()
def test_no_launch(logger, core_agent_manager): ScoutConfig.set(core_agent_launch=False) try: assert not core_agent_manager.launch() assert not is_running(core_agent_manager) logger.debug.assert_called_with( "Not attempting to launch Core Agent due to 'core_agent_launch' setting." ) finally: ScoutConfig.set(core_agent_launch=True)
def test_override_triple(): ScoutConfig.set(core_agent_triple="unknown-linux-musl") config = ScoutConfig() try: assert re.match( r"scout_apm_core-v.*-unknown-linux-musl", config.value("core_agent_full_name"), ) finally: ScoutConfig.reset_all()
def test_no_verify(logger, core_agent_manager): ScoutConfig.set(core_agent_download=False) try: assert not core_agent_manager.launch() assert not is_running(core_agent_manager) logger.debug.assert_called_with( "Not attempting to download Core Agent " "due to 'core_agent_download' setting.") finally: ScoutConfig.set(core_agent_download=True)
def test_log_config(): # Include configs in various layers to exercise all code paths. os.environ["SCOUT_CORE_AGENT_DOWNLOAD"] = "False" ScoutConfig.set(core_agent_launch=False) config = ScoutConfig() try: # Logging the config doesn't crash. config.log() finally: del os.environ["SCOUT_CORE_AGENT_DOWNLOAD"] ScoutConfig.reset_all()
def includeme(config): configs = {} pyramid_config = config.get_settings() for name in filter(lambda x: x.startswith('SCOUT_'), pyramid_config): value = pyramid_config[name] clean_name = name.replace('SCOUT_', '').lower() configs[clean_name] = value ScoutConfig.set(**configs) if scout_apm.core.install(): config.add_tween('scout_apm.pyramid.instruments')
def test_override_triple(caplog): triple = "unknown-unknown-linux-musl" ScoutConfig.set(core_agent_triple=triple) config = ScoutConfig() try: full_name = config.value("core_agent_full_name") finally: ScoutConfig.reset_all() assert full_name.endswith(triple) assert caplog.record_tuples == []
def extract_flask_settings(self): """ Copies SCOUT_* settings in the app into Scout's config lookup """ configs = {} configs['application_root'] = self.app.instance_path for name in filter(lambda x: x.startswith('SCOUT_'), current_app.config): value = current_app.config[name] clean_name = name.replace('SCOUT_', '').lower() configs[clean_name] = value ScoutConfig.set(**configs)
def extract_flask_settings(self): """ Copies SCOUT_* settings in the app into Scout's config lookup """ configs = {} configs["application_root"] = self.app.instance_path for name in current_app.config: if name.startswith("SCOUT_"): value = current_app.config[name] clean_name = name.replace("SCOUT_", "").lower() configs[clean_name] = value ScoutConfig.set(**configs)
def includeme(config): configs = {} pyramid_config = config.get_settings() for name in pyramid_config: if name.startswith("SCOUT_"): value = pyramid_config[name] clean_name = name.replace("SCOUT_", "").lower() configs[clean_name] = value ScoutConfig.set(**configs) if scout_apm.core.install(): config.add_tween("scout_apm.pyramid.instruments")
def test_override_triple_invalid(recwarn): bad_triple = "badtriple" ScoutConfig.set(core_agent_triple=bad_triple) config = ScoutConfig() try: full_name = config.value("core_agent_full_name") finally: ScoutConfig.reset_all() assert full_name.endswith(bad_triple) assert len(recwarn) == 1 warning = recwarn.pop(Warning) assert str(warning.message) == "Invalid value for core_agent_triple: badtriple"
def core_agent_manager(core_agent_dir): # Shorten path to socket to prevent core-agent from failing with: # Error opening listener on socket: Custom { kind: InvalidInput, # error: StringError("path must be shorter than SUN_LEN") } socket_path = "{}/test.sock".format(core_agent_dir) ScoutConfig.set(core_agent_dir=core_agent_dir, socket_path=socket_path) AgentContext.build() core_agent_manager = CoreAgentManager() try: yield core_agent_manager finally: assert not is_running(core_agent_manager) ScoutConfig.reset_all()
def test_get_derived_config_value(): ScoutConfig.set(core_agent_version="v1.1.8") config = ScoutConfig() try: assert re.match( r"/tmp/scout_apm_core/scout_apm_core" r"-v1\.1\.8" r"-(x86_64|i686|unknown)" r"-(unknown-linux-gnu|apple-darwin|unknown)" r"/core-agent\.sock", config.value("socket_path"), ) finally: ScoutConfig.reset_all()
def test_override_triple_invalid(caplog): bad_triple = "badtriple" ScoutConfig.set(core_agent_triple=bad_triple) config = ScoutConfig() try: full_name = config.value("core_agent_full_name") finally: ScoutConfig.reset_all() assert full_name.endswith(bad_triple) assert caplog.record_tuples == [( "scout_apm.core.config", logging.WARNING, "Invalid value for core_agent_triple: badtriple", )]
def on_setting_changed(self, setting, **kwargs): if setting == "BASE_DIR": scout_name = "application_root" elif setting.startswith("SCOUT_"): scout_name = setting.replace("SCOUT_", "").lower() else: return try: value = getattr(settings, setting) except AttributeError: # It was removed ScoutConfig.unset(scout_name) else: ScoutConfig.set(**{scout_name: value})
def test_no_launch(caplog, core_agent_manager): caplog.set_level(logging.DEBUG) ScoutConfig.set(core_agent_launch=False) try: result = core_agent_manager.launch() finally: ScoutConfig.set(core_agent_launch=True) assert not result assert not is_running(core_agent_manager) assert caplog.record_tuples == [( "scout_apm.core.core_agent_manager", logging.DEBUG, ("Not attempting to launch Core Agent due to 'core_agent_launch' " + "setting."), )]
def test_no_verify(caplog, core_agent_manager): caplog.set_level(logging.DEBUG) ScoutConfig.set(core_agent_download=False) try: result = core_agent_manager.launch() finally: ScoutConfig.set(core_agent_download=True) assert not result assert not is_running(core_agent_manager) assert ( "scout_apm.core.core_agent_manager", logging.DEBUG, ("Not attempting to download Core Agent due to " + "'core_agent_download' setting."), ) in caplog.record_tuples
def test_install_all_installs_only_enabled_instruments(): # Disable all instruments except the last one. ScoutConfig.set( disabled_instruments=InstrumentManager.DEFAULT_INSTRUMENTS[:-1]) AgentContext.build() try: with patch( "scout_apm.core.instrument_manager.InstrumentManager.install" ) as install: # Only the last instrument is installed. InstrumentManager().install_all() install.assert_called_once_with("{}.{}".format( InstrumentManager.INSTRUMENT_NAMESPACE, InstrumentManager.DEFAULT_INSTRUMENTS[-1], )) finally: ScoutConfig.reset_all()
def test_log_config(caplog): # Include configs in various layers to exercise all code paths. os.environ["SCOUT_CORE_AGENT_DOWNLOAD"] = "False" ScoutConfig.set(core_agent_launch=False, key="abcdefghij") config = ScoutConfig() try: # Logging the config doesn't crash. config.log() finally: del os.environ["SCOUT_CORE_AGENT_DOWNLOAD"] ScoutConfig.reset_all() assert caplog.record_tuples[0] == ( "scout_apm.core.config", logging.DEBUG, "Configuration Loaded:", ) assert ( "scout_apm.core.config", logging.DEBUG, "Python : core_agent_launch = False", ) in caplog.record_tuples assert "abcdefghij" not in pprint.pformat(caplog.record_tuples)