def test__update_env_no_old_value(monkeypatch: MonkeyPatch, default: str, value: List[str], result: str) -> None: """Values are appended to default value.""" monkeypatch.delenv("DUMMY_VAR", raising=False) prerun._update_env("DUMMY_VAR", value, default) assert os.environ["DUMMY_VAR"] == result
def test__update_env_no_old_value_no_default_no_value( monkeypatch: MonkeyPatch) -> None: """Make sure empty value does not touch environment.""" monkeypatch.delenv("DUMMY_VAR", raising=False) prerun._update_env("DUMMY_VAR", []) assert "DUMMY_VAR" not in os.environ
def test_default_xdg_config_home(monkeypatch: MonkeyPatch, tmp_path: Path): home_dir = tmp_path monkeypatch.delenv(ENV_HTTPIE_CONFIG_DIR, raising=False) monkeypatch.delenv(ENV_XDG_CONFIG_HOME, raising=False) monkeypatch.setenv('HOME', str(home_dir)) expected_config_dir = (home_dir / DEFAULT_RELATIVE_XDG_CONFIG_HOME / DEFAULT_CONFIG_DIRNAME) assert get_default_config_dir() == expected_config_dir
def test_null(monkeypatch: MonkeyPatch) -> None: """Test when CI environment variables aren't available.""" # Ensure the triggering environment variable from GitHub Actions isn't set monkeypatch.delenv("GITHUB_ACTIONS", raising=False) ci_metadata = CiMetadata.create() assert ci_metadata.platform == "null"
def test_explicit_xdg_config_home(monkeypatch: MonkeyPatch, tmp_path: Path): home_dir = tmp_path monkeypatch.delenv(ENV_HTTPIE_CONFIG_DIR, raising=False) monkeypatch.setenv('HOME', str(home_dir)) custom_xdg_config_home = home_dir / 'custom_xdg_config_home' monkeypatch.setenv(ENV_XDG_CONFIG_HOME, str(custom_xdg_config_home)) expected_config_dir = custom_xdg_config_home / DEFAULT_CONFIG_DIRNAME assert get_default_config_dir() == expected_config_dir
def test__update_env_no_old_value_no_default(monkeypatch: MonkeyPatch, value: List[str], result: str) -> None: """Values are concatenated using : as the separator.""" monkeypatch.delenv("DUMMY_VAR", raising=False) prerun._update_env("DUMMY_VAR", value) assert os.environ["DUMMY_VAR"] == result
def test_settings(mock_service_envs: None, monkeypatch: MonkeyPatch): monkeypatch.delenv("DASK_START_AS_SCHEDULER") settings = Settings.create_from_envs() assert settings.as_worker() monkeypatch.setenv("DASK_START_AS_SCHEDULER", "1") settings = Settings.create_from_envs() assert settings.as_scheduler()
def test_cache_reportheader(env, pytester: Pytester, monkeypatch: MonkeyPatch) -> None: pytester.makepyfile("""def test_foo(): pass""") if env: monkeypatch.setenv(*env) expected = os.path.join(env[1], ".pytest_cache") else: monkeypatch.delenv("TOX_ENV_DIR", raising=False) expected = ".pytest_cache" result = pytester.runpytest("-v") result.stdout.fnmatch_lines(["cachedir: %s" % expected])
def set_or_delete_postgresql_schema_env_var(monkeypatch: MonkeyPatch, value: Optional[Text]) -> None: """Set `POSTGRESQL_SCHEMA` environment variable using `MonkeyPatch`. Args: monkeypatch: Instance of `MonkeyPatch` to use for patching. value: Value of the `POSTGRESQL_SCHEMA` environment variable to set. """ if value is None: monkeypatch.delenv(POSTGRESQL_SCHEMA, raising=False) else: monkeypatch.setenv(POSTGRESQL_SCHEMA, value)
def test_platform_non_linux(monkeypatch: MonkeyPatch) -> None: from platformdirs import unix try: with monkeypatch.context() as context: context.setattr(sys, "platform", "magic") monkeypatch.delenv("XDG_RUNTIME_DIR", raising=False) importlib.reload(unix) with pytest.raises(RuntimeError, match="should only be used on Linux"): unix.Unix().user_runtime_dir finally: importlib.reload(unix)
def test_pika_message_property_app_id(monkeypatch: MonkeyPatch): pika_processor = PikaMessageProcessor(TEST_CONNECTION_PARAMETERS, queues=None, get_message=lambda: ("", None)) # unset RASA_ENVIRONMENT env var results in empty App ID monkeypatch.delenv("RASA_ENVIRONMENT", raising=False) assert not pika_processor._get_message_properties().app_id # setting it to some value results in that value as the App ID rasa_environment = "some-test-environment" monkeypatch.setenv("RASA_ENVIRONMENT", rasa_environment) assert pika_processor._get_message_properties().app_id == rasa_environment
def test_android_active(monkeypatch: MonkeyPatch, root: str | None, data: str | None) -> None: for env_var, value in {"ANDROID_DATA": data, "ANDROID_ROOT": root}.items(): if value is None: monkeypatch.delenv(env_var, raising=False) else: monkeypatch.setenv(env_var, value) expected = root == "/system" and data == "/data" if expected: assert platformdirs._set_platform_dir_class() is Android else: assert platformdirs._set_platform_dir_class() is not Android
def test_pika_message_property_app_id(monkeypatch: MonkeyPatch): # patch PikaProducer so it doesn't try to connect to RabbitMQ on init with patch.object(PikaEventBroker, "_run_pika", lambda _: None): pika_producer = PikaEventBroker("", "", "") # unset RASA_ENVIRONMENT env var results in empty App ID monkeypatch.delenv("RASA_ENVIRONMENT", raising=False) assert not pika_producer._message_properties.app_id # setting it to some value results in that value as the App ID rasa_environment = "some-test-environment" monkeypatch.setenv("RASA_ENVIRONMENT", rasa_environment) assert pika_producer._message_properties.app_id == rasa_environment
def set_legacy_for_tests(monkeypatch: MonkeyPatch) -> None: """Maintain test suite viability until we migrate all tests to be forward-compatible""" import structlog_sentry_logger # pylint: disable=import-outside-toplevel for env_var in [ # pylint:disable=protected-access *structlog_sentry_logger._feature_flags. _ENV_VARS_REQUIRED_BY_LIBRARY.values(), *structlog_sentry_logger._feature_flags. _CLOUD_ENV_INFERENCE_ENV_VARS, # pylint:enable=protected-access ]: monkeypatch.delenv(env_var, raising=False) monkeypatch.setenv("_STRUCTLOG_SENTRY_LOGGER_STDLIB_BASED_LOGGER_MODE_ON", "ANY_VALUE")
def test_missing_env_tls_var_raises_exception_for_deployed_host( self, tls_env_var: str, tmp_path: Path, monkeypatch: MonkeyPatch) -> None: monkeypatch.delenv(tls_env_var, raising=False) cert_key_pem = tmp_path / DP3_CERT_KEY_PEM_FILENAME with mock.patch("utils.auth.DP3_CERT_KEY_PEM", str(cert_key_pem)): with pytest.raises( ImplementationError, match= "Cannot run load testing in a deployed environment without the matching certificate and key.", ): set_up_certs(env=MilMoveEnv.DP3) assert len(list(tmp_path.iterdir())) == 0 assert not cert_key_pem.exists()
def test_delenv(): name = "xyz1234" assert name not in os.environ monkeypatch = MonkeyPatch() pytest.raises(KeyError, "monkeypatch.delenv(%r, raising=True)" % name) monkeypatch.delenv(name, raising=False) monkeypatch.undo() os.environ[name] = "1" try: monkeypatch = MonkeyPatch() monkeypatch.delenv(name) assert name not in os.environ monkeypatch.setenv(name, "3") assert os.environ[name] == "3" monkeypatch.undo() assert os.environ[name] == "1" finally: if name in os.environ: del os.environ[name]
def test_env_and_debug_environments( quart_env: Optional[str], quart_debug: Optional[bool], expected_env: bool, expected_debug: bool, monkeypatch: MonkeyPatch, ) -> None: if quart_env is None: monkeypatch.delenv("QUART_ENV", raising=False) else: monkeypatch.setenv("QUART_ENV", quart_env) if quart_debug is None: monkeypatch.delenv("QUART_DEBUG", raising=False) else: monkeypatch.setenv("QUART_DEBUG", str(quart_debug)) app = Quart(__name__) assert app.env == expected_env assert app.debug is expected_debug
def mock_env( monkeypatch: MonkeyPatch, network_name: str, dev_features_enabled: str, rabbit_service: RabbitConfig, ) -> None: # Works as below line in docker.compose.yml # ${DOCKER_REGISTRY:-itisfoundation}/dynamic-sidecar:${DOCKER_IMAGE_TAG:-latest} registry = os.environ.get("DOCKER_REGISTRY", "local") image_tag = os.environ.get("DOCKER_IMAGE_TAG", "production") image_name = f"{registry}/dynamic-sidecar:{image_tag}" logger.warning("Patching to: DYNAMIC_SIDECAR_IMAGE=%s", image_name) monkeypatch.setenv("DYNAMIC_SIDECAR_IMAGE", image_name) monkeypatch.setenv("TRAEFIK_SIMCORE_ZONE", "test_traefik_zone") monkeypatch.setenv("SWARM_STACK_NAME", "test_swarm_name") monkeypatch.setenv("SC_BOOT_MODE", "production") monkeypatch.setenv("DYNAMIC_SIDECAR_EXPOSE_PORT", "true") monkeypatch.setenv("PROXY_EXPOSE_PORT", "true") monkeypatch.setenv("SIMCORE_SERVICES_NETWORK_NAME", network_name) monkeypatch.delenv("DYNAMIC_SIDECAR_MOUNT_PATH_DEV", raising=False) monkeypatch.setenv("DIRECTOR_V2_DYNAMIC_SCHEDULER_ENABLED", "true") monkeypatch.setenv("DYNAMIC_SIDECAR_TRAEFIK_ACCESS_LOG", "true") monkeypatch.setenv("DYNAMIC_SIDECAR_TRAEFIK_LOGLEVEL", "debug") # patch host for dynamic-sidecar, not reachable via localhost # the dynamic-sidecar (running inside a container) will use # this address to reach the rabbit service monkeypatch.setenv("RABBIT_HOST", f"{get_ip()}") monkeypatch.setenv("POSTGRES_HOST", f"{get_ip()}") monkeypatch.setenv("R_CLONE_S3_PROVIDER", "MINIO") monkeypatch.setenv("DIRECTOR_V2_DEV_FEATURES_ENABLED", dev_features_enabled) monkeypatch.setenv("DIRECTOR_V2_TRACING", "null")
def test_structlog_logger( caplog: LogCaptureFixture, monkeypatch: MonkeyPatch, random_log_msgs: List[uuid.UUID], is_sentry_integration_mode_requested: bool, ) -> None: tests.utils.enable_sentry_integration_mode(monkeypatch) if not is_sentry_integration_mode_requested: monkeypatch.delenv( "STRUCTLOG_SENTRY_LOGGER_CLOUD_SENTRY_INTEGRATION_MODE_ON", raising=False, ) logger = structlog_sentry_logger.get_logger() for log_msg in random_log_msgs: logger.debug(log_msg) assert caplog.records structlogged_records = [ record for record in caplog.records if isinstance(record.msg, dict) ] module_name = structlog_sentry_logger.get_namespaced_module_name( __file__) for record, log_msg in zip(structlogged_records, random_log_msgs): log = record.msg assert log["level"] == "debug" == record.levelname.lower( ) # type: ignore[index] assert (log["logger"] == logger.name == record.name == module_name == __name__ # type: ignore[index] ) assert log["event"] == log_msg # type: ignore[index] if is_sentry_integration_mode_requested: assert log["sentry"] == "skipped" # type: ignore[index] else: assert "sentry" not in log assert "timestamp" in log
def setup(self, monkeypatch: MonkeyPatch) -> None: structlog.reset_defaults() monkeypatch.delenv( "STRUCTLOG_SENTRY_LOGGER_LOCAL_DEVELOPMENT_LOGGING_MODE_ON", raising=False) monkeypatch.delenv( "_STRUCTLOG_SENTRY_LOGGER_STDLIB_BASED_LOGGER_MODE_ON", raising=False) for env_var in self.cloud_logging_compatibility_mode_env_vars: monkeypatch.delenv(env_var, raising=False)
def delete_all_cloud_logging_compatibility_mode_env_vars_from_environment( monkeypatch: MonkeyPatch, ) -> None: for env_var in TestCloudLogging.cloud_logging_compatibility_mode_env_vars: monkeypatch.delenv(env_var, raising=False)
def test_not_in_ci_if_not_in_ci(monkeypatch: MonkeyPatch): for env in telemetry.CI_ENVIRONMENT_TELL: monkeypatch.delenv(env, raising=False) assert not telemetry.in_continuous_integration()
def delete_env_vars(monkeypatch: MonkeyPatch) -> None: for key in os.environ.keys(): if key.startswith("SAPPORO"): monkeypatch.delenv(key)
class PluginContextTest(unittest.TestCase): def setUp(self): self.monkeypatch = MonkeyPatch() pass def tearDown(self): self.monkeypatch.undo() @unittest.skipIf(sys.version_info.major < 3, "Plugin Manager is Python 3 only") def test_no_plugins(self): self.monkeypatch.delenv("ERT_SITE_CONFIG", raising=False) with ErtPluginContext(plugins=[]) as c: with self.assertRaises(KeyError): os.environ["ECL100_SITE_CONFIG"] with self.assertRaises(KeyError): os.environ["ECL300_SITE_CONFIG"] with self.assertRaises(KeyError): os.environ["FLOW_SITE_CONFIG"] with self.assertRaises(KeyError): os.environ["RMS_SITE_CONFIG"] self.assertTrue(os.path.isfile(os.environ["ERT_SITE_CONFIG"])) with open(os.environ["ERT_SITE_CONFIG"]) as f: self.assertEqual(f.read(), c.plugin_manager.get_site_config_content()) path = os.environ["ERT_SITE_CONFIG"] with self.assertRaises(KeyError): os.environ["ERT_SITE_CONFIG"] self.assertFalse(os.path.isfile(path)) @unittest.skipIf(sys.version_info.major < 3, "Plugin Manager is Python 3 only") def test_with_plugins(self): self.monkeypatch.delenv("ERT_SITE_CONFIG", raising=False) with ErtPluginContext(plugins=[dummy_plugins]) as c: with self.assertRaises(KeyError): os.environ["ECL100_SITE_CONFIG"] with self.assertRaises(KeyError): os.environ["ECL300_SITE_CONFIG"] with self.assertRaises(KeyError): os.environ["FLOW_SITE_CONFIG"] with self.assertRaises(KeyError): os.environ["RMS_SITE_CONFIG"] self.assertTrue(os.path.isfile(os.environ["ERT_SITE_CONFIG"])) with open(os.environ["ERT_SITE_CONFIG"]) as f: self.assertEqual(f.read(), c.plugin_manager.get_site_config_content()) path = os.environ["ERT_SITE_CONFIG"] with self.assertRaises(KeyError): os.environ["ERT_SITE_CONFIG"] self.assertFalse(os.path.isfile(path)) @unittest.skipIf(sys.version_info.major < 3, "Plugin Manager is Python 3 only") def test_already_set(self): for var in env_vars: self.monkeypatch.setenv(var, "TEST") with ErtPluginContext(plugins=[dummy_plugins]) as c: for var in env_vars: self.assertEqual("TEST", os.environ[var]) for var in env_vars: self.assertEqual("TEST", os.environ[var]) @unittest.skipIf( sys.version_info.major > 2, "Skipping Plugin Manager Python 2 test" ) def test_plugin_context_python_2(self): with ErtPluginContext(plugins=[]): for var in env_vars: with self.assertRaises(KeyError): os.environ[var]
def test_expand_path(path: str, expected: str, monkeypatch: MonkeyPatch): monkeypatch.setenv('FOO', 'foo/bar') monkeypatch.delenv('NON_EXIST', raising=False) assert expand_path(path) == expected
class PluginContextTest(unittest.TestCase): def setUp(self): self.monkeypatch = MonkeyPatch() pass def tearDown(self): self.monkeypatch.undo() @unittest.skipIf(sys.version_info.major < 3, "Plugin Manager is Python 3 only") def test_no_plugins(self): self.monkeypatch.delenv("ERT_SITE_CONFIG", raising=False) with ErtPluginContext(plugins=[]) as c: with self.assertRaises(KeyError): os.environ["ECL100_SITE_CONFIG"] with self.assertRaises(KeyError): os.environ["ECL300_SITE_CONFIG"] with self.assertRaises(KeyError): os.environ["FLOW_SITE_CONFIG"] with self.assertRaises(KeyError): os.environ["RMS_SITE_CONFIG"] self.assertTrue(os.path.isfile(os.environ["ERT_SITE_CONFIG"])) with open(os.environ["ERT_SITE_CONFIG"]) as f: self.assertEqual(f.read(), c.plugin_manager.get_site_config_content()) path = os.environ["ERT_SITE_CONFIG"] with self.assertRaises(KeyError): os.environ["ERT_SITE_CONFIG"] self.assertFalse(os.path.isfile(path)) @unittest.skipIf(sys.version_info.major < 3, "Plugin Manager is Python 3 only") def test_with_plugins(self): self.monkeypatch.delenv("ERT_SITE_CONFIG", raising=False) # We are comparing two function calls, both of which generate a tmpdir, this makes # sure that the same tmpdir is called on both occasions. self.monkeypatch.setattr(tempfile, "mkdtemp", Mock(return_value=tempfile.mkdtemp())) with ErtPluginContext(plugins=[dummy_plugins]) as c: with self.assertRaises(KeyError): os.environ["ECL100_SITE_CONFIG"] with self.assertRaises(KeyError): os.environ["ECL300_SITE_CONFIG"] with self.assertRaises(KeyError): os.environ["FLOW_SITE_CONFIG"] with self.assertRaises(KeyError): os.environ["RMS_SITE_CONFIG"] self.assertTrue(os.path.isfile(os.environ["ERT_SITE_CONFIG"])) with open(os.environ["ERT_SITE_CONFIG"]) as f: self.assertEqual(f.read(), c.plugin_manager.get_site_config_content()) path = os.environ["ERT_SITE_CONFIG"] with self.assertRaises(KeyError): os.environ["ERT_SITE_CONFIG"] self.assertFalse(os.path.isfile(path)) @unittest.skipIf(sys.version_info.major < 3, "Plugin Manager is Python 3 only") def test_already_set(self): for var in env_vars: self.monkeypatch.setenv(var, "TEST") with ErtPluginContext(plugins=[dummy_plugins]) as c: for var in env_vars: self.assertEqual("TEST", os.environ[var]) for var in env_vars: self.assertEqual("TEST", os.environ[var]) @unittest.skipIf(sys.version_info.major > 2, "Skipping Plugin Manager Python 2 test") def test_plugin_context_python_2(self): with ErtPluginContext(plugins=[]): for var in env_vars: with self.assertRaises(KeyError): os.environ[var]
def test_windows_config_dir(monkeypatch: MonkeyPatch): monkeypatch.delenv(ENV_HTTPIE_CONFIG_DIR, raising=False) assert get_default_config_dir() == DEFAULT_WINDOWS_CONFIG_DIR
def test_terminalwriter_width_bogus(monkeypatch: MonkeyPatch) -> None: monkeypatch.setattr(shutil, "get_terminal_size", mock.Mock(return_value=(10, 10))) monkeypatch.delenv("COLUMNS", raising=False) tw = terminalwriter.TerminalWriter() assert tw.fullwidth == 80
def test_terminal_width_COLUMNS(monkeypatch: MonkeyPatch) -> None: monkeypatch.setenv("COLUMNS", "42") assert terminalwriter.get_terminal_width() == 42 monkeypatch.delenv("COLUMNS", raising=False)
def test_pika_message_property_app_id_without_env_set(monkeypatch: MonkeyPatch): # unset RASA_ENVIRONMENT env var results in empty App ID monkeypatch.delenv("RASA_ENVIRONMENT", raising=False) pika_broker = PikaEventBroker("some host", "username", "password") assert not pika_broker._message({}, None).app_id