|
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | 15 | import collections |
| 16 | +import copy |
| 17 | +import sys |
16 | 18 | import tempfile |
17 | 19 |
|
18 | 20 | import urllib.parse as urlparse |
|
44 | 46 | ) |
45 | 47 |
|
46 | 48 |
|
| 49 | +SKIP_IF_NOT_PY311 = pytest.mark.skipif(sys.version_info < (3, 11), reason="TOML not in the standard library.") |
| 50 | + |
| 51 | + |
| 52 | +@pytest.fixture(scope="function") |
| 53 | +def collector_available_fixture(): |
| 54 | + # Disable fixture that requires real application to exist for this file |
| 55 | + pass |
| 56 | + |
| 57 | + |
| 58 | +@pytest.fixture(scope="module", autouse=True) |
| 59 | +def restore_settings_fixture(): |
| 60 | + # Backup settings from before this test file runs |
| 61 | + original_settings = global_settings() |
| 62 | + backup = copy.deepcopy(original_settings.__dict__) |
| 63 | + |
| 64 | + # Run tests |
| 65 | + yield |
| 66 | + |
| 67 | + # Restore settings after tests run |
| 68 | + original_settings.__dict__.clear() |
| 69 | + original_settings.__dict__.update(backup) |
| 70 | + |
| 71 | + |
47 | 72 | def function_to_trace(): |
48 | 73 | pass |
49 | 74 |
|
@@ -949,6 +974,73 @@ def test_initialize_developer_mode(section, expect_error, logger): |
949 | 974 | assert "CONFIGURATION ERROR" not in logger.caplog.records |
950 | 975 |
|
951 | 976 |
|
| 977 | +newrelic_toml_contents = b""" |
| 978 | +[tool.newrelic] |
| 979 | +app_name = "test11" |
| 980 | +monitor_mode = true |
| 981 | +
|
| 982 | +[tool.newrelic.env.development] |
| 983 | +app_name = "test11 (Development)" |
| 984 | +
|
| 985 | +[tool.newrelic.env.production] |
| 986 | +app_name = "test11 (Production)" |
| 987 | +log_level = "error" |
| 988 | +
|
| 989 | +[tool.newrelic.env.production.distributed_tracing] |
| 990 | +enabled = false |
| 991 | +
|
| 992 | +[tool.newrelic.error_collector] |
| 993 | +enabled = true |
| 994 | +ignore_errors = ["module:name1", "module:name"] |
| 995 | +
|
| 996 | +[tool.newrelic.transaction_tracer] |
| 997 | +enabled = true |
| 998 | +
|
| 999 | +[tool.newrelic.import-hook.django] |
| 1000 | +"instrumentation.scripts.django_admin" = ["stuff", "stuff2"] |
| 1001 | +""" |
| 1002 | + |
| 1003 | + |
| 1004 | +@SKIP_IF_NOT_PY311 |
| 1005 | +def test_toml_parse_development(): |
| 1006 | + settings = global_settings() |
| 1007 | + _reset_configuration_done() |
| 1008 | + _reset_config_parser() |
| 1009 | + _reset_instrumentation_done() |
| 1010 | + |
| 1011 | + with tempfile.NamedTemporaryFile(suffix=".toml") as f: |
| 1012 | + f.write(newrelic_toml_contents) |
| 1013 | + f.seek(0) |
| 1014 | + |
| 1015 | + initialize(config_file=f.name, environment="development") |
| 1016 | + value = fetch_config_setting(settings, "app_name") |
| 1017 | + assert value != "test11" |
| 1018 | + value = fetch_config_setting(settings, "monitor_mode") |
| 1019 | + assert value is True |
| 1020 | + value = fetch_config_setting(settings, "error_collector") |
| 1021 | + assert value.enabled is True |
| 1022 | + assert value.ignore_classes[0] == "module:name1" |
| 1023 | + assert value.ignore_classes[1] == "module:name" |
| 1024 | + |
| 1025 | + |
| 1026 | +@SKIP_IF_NOT_PY311 |
| 1027 | +def test_toml_parse_production(): |
| 1028 | + settings = global_settings() |
| 1029 | + _reset_configuration_done() |
| 1030 | + _reset_config_parser() |
| 1031 | + _reset_instrumentation_done() |
| 1032 | + |
| 1033 | + with tempfile.NamedTemporaryFile(suffix=".toml") as f: |
| 1034 | + f.write(newrelic_toml_contents) |
| 1035 | + f.seek(0) |
| 1036 | + |
| 1037 | + initialize(config_file=f.name, environment="production") |
| 1038 | + value = fetch_config_setting(settings, "app_name") |
| 1039 | + assert value == "test11 (Production)" |
| 1040 | + value = fetch_config_setting(settings, "distributed_tracing") |
| 1041 | + assert value.enabled is False |
| 1042 | + |
| 1043 | + |
952 | 1044 | @pytest.fixture |
953 | 1045 | def caplog_handler(): |
954 | 1046 | class CaplogHandler(logging.StreamHandler): |
|
0 commit comments