From b7b548585ba34ffe162031b10b797fc1e7187c2e Mon Sep 17 00:00:00 2001 From: Quigley Malcolm Date: Tue, 28 Mar 2023 16:12:40 -0700 Subject: [PATCH 1/4] Add tests for logging jinja2.Undefined objects [CT-2259](https://github.com/dbt-labs/dbt-core/issues/7108) identifies an issue wherein dbt-core 1.0-1.3 raise errors if a jinja2.Undefined object is attempted to be logged. This generally happened in the form of `{{ log(undefined_variable, info=True) }}`. This commit adding this test exists for two reasons 1. Ensure we don't have a regression in this going forward 2. Exist as a commit to be used for backport fixes for dbt-core 1.0-1.3 --- test/unit/test_base_context.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 test/unit/test_base_context.py diff --git a/test/unit/test_base_context.py b/test/unit/test_base_context.py new file mode 100644 index 00000000000..462323de5d7 --- /dev/null +++ b/test/unit/test_base_context.py @@ -0,0 +1,12 @@ +from dbt.context.base import BaseContext +from jinja2.runtime import Undefined + + +class TestBaseContext: + def test_log_jinja_undefined(self): + # regression test for CT-2259 + try: + os.environ["DBT_ENV_SECRET_LOG_TEST"] = "cats_are_cool" + BaseContext.log(msg=Undefined(), info=True) + except Exception as e: + assert False, f"Logging an jinja2.Undefined object raises an exception: {e}" From 53bf368e10e3339db7550e3ba167e1639b0629fc Mon Sep 17 00:00:00 2001 From: Quigley Malcolm Date: Tue, 28 Mar 2023 16:23:14 -0700 Subject: [PATCH 2/4] Add tests for checking `DBT_ENV_SECRET_`s don't break logging [CT-1783](https://github.com/dbt-labs/dbt-core/issues/6568) describes a bug in dbt-core 1.0-1.3 wherein when a `DBT_ENV_SECRET_` all `{{ log("logging stuff", info=True) }}` invocations break. This commit adds a test for this for two reasons: 1. Ensure we don't regress to this behavior going forward 2. Act as a base commit for making the backport fixes to dbt-core 1.0-1.3 --- test/unit/test_base_context.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/unit/test_base_context.py b/test/unit/test_base_context.py index 462323de5d7..0dc2d93ddca 100644 --- a/test/unit/test_base_context.py +++ b/test/unit/test_base_context.py @@ -1,3 +1,5 @@ +import os + from dbt.context.base import BaseContext from jinja2.runtime import Undefined @@ -10,3 +12,11 @@ def test_log_jinja_undefined(self): BaseContext.log(msg=Undefined(), info=True) except Exception as e: assert False, f"Logging an jinja2.Undefined object raises an exception: {e}" + + def test_log_with_dbt_env_secret(self): + # regression test for CT-1783 + try: + os.environ["DBT_ENV_SECRET_LOG_TEST"] = "cats_are_cool" + BaseContext.log({"fact1": "I like cats"}, info=True) + except Exception as e: + assert False, f"Logging while a `DBT_ENV_SECRET` was set raised an exception: {e}" From 021ccfd3419eea32c58730f0f212fc1bb6b27705 Mon Sep 17 00:00:00 2001 From: Quigley Malcolm Date: Fri, 31 Mar 2023 09:55:12 -0700 Subject: [PATCH 3/4] Add changie info for changes --- .changes/unreleased/Fixes-20230331-095428.yaml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changes/unreleased/Fixes-20230331-095428.yaml diff --git a/.changes/unreleased/Fixes-20230331-095428.yaml b/.changes/unreleased/Fixes-20230331-095428.yaml new file mode 100644 index 00000000000..772ce077b7d --- /dev/null +++ b/.changes/unreleased/Fixes-20230331-095428.yaml @@ -0,0 +1,6 @@ +kind: Fixes +body: Improved failed event serialization handling and associated tests +time: 2023-03-31T09:54:28.701963-07:00 +custom: + Author: QMalcolm + Issue: 7113 7108 6568 From 52eadae90f7eb5f8568b23b9d0fd50a8744ee936 Mon Sep 17 00:00:00 2001 From: Quigley Malcolm Date: Wed, 12 Apr 2023 10:59:04 -0700 Subject: [PATCH 4/4] Ensure `log()` calls from jinja ensure the msg is stringified The simplest way to resolve [CT-2259](https://github.com/dbt-labs/dbt-core/issues/7108) and [CT-1783](https://github.com/dbt-labs/dbt-core/issues/6568) in backports to dbt-core < 1.4 is to ensure `msg` in `BaseContext.log()` is stringified. --- core/dbt/context/base.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core/dbt/context/base.py b/core/dbt/context/base.py index c1d5665c2e3..d6f5ece5e0e 100644 --- a/core/dbt/context/base.py +++ b/core/dbt/context/base.py @@ -441,6 +441,10 @@ def log(msg: str, info: bool = False) -> str: {{ log("Running some_macro: " ~ arg1 ~ ", " ~ arg2) }} {% endmacro %}" """ + + if not isinstance(msg, str): + msg = str(msg) + if info: fire_event(MacroEventInfo(msg=msg)) else: