Skip to content

Commit 9f3c1ae

Browse files
bugraoz93potiuk
authored andcommitted
Fix Variable and KubernetesJobOperator Tests for Database Isolation Tests (#41370)
Fixing remaining Variable tests for db isolation mode, also fixing secret backend haven't called from EnvironmentVariablesBackend, Metastore and custom ones. This caused side effect to move the Variable.get() method to internal API (cherry picked from commit c98d1a1)
1 parent 2740c1c commit 9f3c1ae

File tree

3 files changed

+32
-22
lines changed

3 files changed

+32
-22
lines changed

airflow/models/variable.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,13 @@ def setdefault(cls, key, default, description=None, deserialize_json=False):
110110
:param description: Default value to set Description of the Variable
111111
:param deserialize_json: Store this as a JSON encoded value in the DB
112112
and un-encode it when retrieving a value
113+
:param session: Session
113114
:return: Mixed
114115
"""
115116
obj = Variable.get(key, default_var=None, deserialize_json=deserialize_json)
116117
if obj is None:
117118
if default is not None:
118-
Variable.set(key, default, description=description, serialize_json=deserialize_json)
119+
Variable.set(key=key, value=default, description=description, serialize_json=deserialize_json)
119120
return default
120121
else:
121122
raise ValueError("Default Value must be set")
@@ -170,9 +171,10 @@ def set(
170171
:param value: Value to set for the Variable
171172
:param description: Description of the Variable
172173
:param serialize_json: Serialize the value to a JSON string
174+
:param session: Session
173175
"""
174176
# check if the secret exists in the custom secrets' backend.
175-
Variable.check_for_write_conflict(key)
177+
Variable.check_for_write_conflict(key=key)
176178
if serialize_json:
177179
stored_value = json.dumps(value, indent=2)
178180
else:
@@ -201,16 +203,19 @@ def update(
201203
:param key: Variable Key
202204
:param value: Value to set for the Variable
203205
:param serialize_json: Serialize the value to a JSON string
206+
:param session: Session
204207
"""
205-
Variable.check_for_write_conflict(key)
208+
Variable.check_for_write_conflict(key=key)
206209

207210
if Variable.get_variable_from_secrets(key=key) is None:
208211
raise KeyError(f"Variable {key} does not exist")
209212
obj = session.scalar(select(Variable).where(Variable.key == key))
210213
if obj is None:
211214
raise AttributeError(f"Variable {key} does not exist in the Database and cannot be updated.")
212215

213-
Variable.set(key, value, description=obj.description, serialize_json=serialize_json)
216+
Variable.set(
217+
key=key, value=value, description=obj.description, serialize_json=serialize_json, session=session
218+
)
214219

215220
@staticmethod
216221
@provide_session

tests/models/test_variable.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def setup_test_cases(self):
4747
db.clear_db_variables()
4848
crypto._fernet = None
4949

50-
@conf_vars({("core", "fernet_key"): ""})
50+
@conf_vars({("core", "fernet_key"): "", ("core", "unit_test_mode"): "True"})
5151
def test_variable_no_encryption(self, session):
5252
"""
5353
Test variables without encryption
@@ -100,12 +100,13 @@ def test_variable_set_get_round_trip(self):
100100
Variable.set("tested_var_set_id", "Monday morning breakfast")
101101
assert "Monday morning breakfast" == Variable.get("tested_var_set_id")
102102

103+
@pytest.mark.skip_if_database_isolation_mode # Does not work in db isolation mode
103104
def test_variable_set_with_env_variable(self, caplog, session):
104105
caplog.set_level(logging.WARNING, logger=variable.log.name)
105106
Variable.set(key="key", value="db-value", session=session)
106107
with mock.patch.dict("os.environ", AIRFLOW_VAR_KEY="env-value"):
107108
# setting value while shadowed by an env variable will generate a warning
108-
Variable.set("key", "new-db-value")
109+
Variable.set(key="key", value="new-db-value", session=session)
109110
# value set above is not returned because the env variable value takes priority
110111
assert "env-value" == Variable.get("key")
111112
# invalidate the cache to re-evaluate value
@@ -120,6 +121,7 @@ def test_variable_set_with_env_variable(self, caplog, session):
120121
"EnvironmentVariablesBackend"
121122
)
122123

124+
@pytest.mark.skip_if_database_isolation_mode # Does not work in db isolation mode
123125
@mock.patch("airflow.models.variable.ensure_secrets_loaded")
124126
def test_variable_set_with_extra_secret_backend(self, mock_ensure_secrets, caplog, session):
125127
caplog.set_level(logging.WARNING, logger=variable.log.name)
@@ -137,11 +139,11 @@ def test_variable_set_with_extra_secret_backend(self, mock_ensure_secrets, caplo
137139
"will be updated, but to read it you have to delete the conflicting variable from "
138140
"MockSecretsBackend"
139141
)
140-
Variable.delete("key")
142+
Variable.delete(key="key", session=session)
141143

142144
def test_variable_set_get_round_trip_json(self):
143145
value = {"a": 17, "b": 47}
144-
Variable.set("tested_var_set_id", value, serialize_json=True)
146+
Variable.set(key="tested_var_set_id", value=value, serialize_json=True)
145147
assert value == Variable.get("tested_var_set_id", deserialize_json=True)
146148

147149
def test_variable_update(self, session):
@@ -184,9 +186,9 @@ def test_get_non_existing_var_should_raise_key_error(self):
184186
with pytest.raises(KeyError):
185187
Variable.get("thisIdDoesNotExist")
186188

187-
def test_update_non_existing_var_should_raise_key_error(self):
189+
def test_update_non_existing_var_should_raise_key_error(self, session):
188190
with pytest.raises(KeyError):
189-
Variable.update("thisIdDoesNotExist", "value")
191+
Variable.update(key="thisIdDoesNotExist", value="value", session=session)
190192

191193
def test_get_non_existing_var_with_none_default_should_return_none(self):
192194
assert Variable.get("thisIdDoesNotExist", default_var=None) is None
@@ -197,42 +199,45 @@ def test_get_non_existing_var_should_not_deserialize_json_default(self):
197199
"thisIdDoesNotExist", default_var=default_value, deserialize_json=True
198200
)
199201

200-
def test_variable_setdefault_round_trip(self):
202+
@pytest.mark.skip_if_database_isolation_mode # Does not work in db isolation mode
203+
def test_variable_setdefault_round_trip(self, session):
201204
key = "tested_var_setdefault_1_id"
202205
value = "Monday morning breakfast in Paris"
203-
Variable.setdefault(key, value)
206+
Variable.setdefault(key=key, default=value)
204207
assert value == Variable.get(key)
205208

206-
def test_variable_setdefault_round_trip_json(self):
209+
@pytest.mark.skip_if_database_isolation_mode # Does not work in db isolation mode
210+
def test_variable_setdefault_round_trip_json(self, session):
207211
key = "tested_var_setdefault_2_id"
208212
value = {"city": "Paris", "Happiness": True}
209-
Variable.setdefault(key, value, deserialize_json=True)
213+
Variable.setdefault(key=key, default=value, deserialize_json=True)
210214
assert value == Variable.get(key, deserialize_json=True)
211215

216+
@pytest.mark.skip_if_database_isolation_mode # Does not work in db isolation mode
212217
def test_variable_setdefault_existing_json(self, session):
213218
key = "tested_var_setdefault_2_id"
214219
value = {"city": "Paris", "Happiness": True}
215220
Variable.set(key=key, value=value, serialize_json=True, session=session)
216-
val = Variable.setdefault(key, value, deserialize_json=True)
221+
val = Variable.setdefault(key=key, default=value, deserialize_json=True)
217222
# Check the returned value, and the stored value are handled correctly.
218223
assert value == val
219224
assert value == Variable.get(key, deserialize_json=True)
220225

221-
def test_variable_delete(self):
226+
def test_variable_delete(self, session):
222227
key = "tested_var_delete"
223228
value = "to be deleted"
224229

225230
# No-op if the variable doesn't exist
226-
Variable.delete(key)
231+
Variable.delete(key=key, session=session)
227232
with pytest.raises(KeyError):
228233
Variable.get(key)
229234

230235
# Set the variable
231-
Variable.set(key, value)
236+
Variable.set(key=key, value=value, session=session)
232237
assert value == Variable.get(key)
233238

234239
# Delete the variable
235-
Variable.delete(key)
240+
Variable.delete(key=key, session=session)
236241
with pytest.raises(KeyError):
237242
Variable.get(key)
238243

@@ -276,15 +281,15 @@ def test_caching_caches(self, mock_ensure_secrets: mock.Mock):
276281
mock_backend.get_variable.assert_called_once() # second call was not made because of cache
277282
assert first == second
278283

279-
def test_cache_invalidation_on_set(self):
284+
def test_cache_invalidation_on_set(self, session):
280285
with mock.patch.dict("os.environ", AIRFLOW_VAR_KEY="from_env"):
281286
a = Variable.get("key") # value is saved in cache
282287
with mock.patch.dict("os.environ", AIRFLOW_VAR_KEY="from_env_two"):
283288
b = Variable.get("key") # value from cache is used
284289
assert a == b
285290

286291
# setting a new value invalidates the cache
287-
Variable.set("key", "new_value")
292+
Variable.set(key="key", value="new_value", session=session)
288293

289294
c = Variable.get("key") # cache should not be used
290295

@@ -312,7 +317,6 @@ def test_masking_only_secret_values(variable_value, deserialize_json, expected_m
312317
)
313318
session.add(var)
314319
session.flush()
315-
316320
# Make sure we re-load it, not just get the cached object back
317321
session.expunge(var)
318322
_secrets_masker().patterns = set()

tests/providers/cncf/kubernetes/operators/test_job.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ def test_templates(self, create_task_instance_of_operator, session):
116116
cmds="{{ dag.dag_id }}",
117117
image="{{ dag.dag_id }}",
118118
annotations={"dag-id": "{{ dag.dag_id }}"},
119+
session=session,
119120
)
120121

121122
session.add(ti)

0 commit comments

Comments
 (0)