Skip to content

Commit 851e4cc

Browse files
author
Takashi Matsuo
committed
set AIRFLOW_HOME in noxfile_config.py
1 parent 4e7dc8b commit 851e4cc

3 files changed

Lines changed: 30 additions & 12 deletions

File tree

composer/blog/noxfile_config.py renamed to composer/blog/gcp-tech-blog/unit-test-dags-cloud-build/noxfile_config.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2021 Google LLC
1+
# Copyright 2020 Google LLC
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -20,13 +20,22 @@
2020
# The source of truth:
2121
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/noxfile_config.py
2222

23+
import tempfile
24+
25+
26+
# Airflow creates a config file at the installation, so we want to set
27+
# `AIRFLOW_HOME` envvar before running pytest.
28+
29+
_tmpdir = tempfile.TemporaryDirectory()
30+
31+
2332
TEST_CONFIG_OVERRIDE = {
2433
# You can opt out from the test for specific Python versions.
2534
# Skipping for Python 3.9 due to numpy compilation failure.
2635
"ignored_versions": ["2.7", "3.9"],
2736
# Old samples are opted out of enforcing Python type hints
2837
# All new samples should feature them
29-
"enforce_type_hints": False,
38+
"enforce_type_hints": True,
3039
# An envvar key for determining the project id to use. Change it
3140
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
3241
# build specific Cloud project. You can also use your own string
@@ -35,5 +44,5 @@
3544
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
3645
# A dictionary you want to inject into your test. Don't put any
3746
# secrets here. These values will override predefined values.
38-
"envs": {},
47+
"envs": {"AIRFLOW_HOME": _tmpdir.name},
3948
}

composer/conftest.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,16 @@
2525
def airflow_database():
2626
import airflow.utils.db
2727

28-
# We use separate directory for local db path per session.
29-
tmp_dir = tempfile.TemporaryDirectory()
30-
os.environ['AIRFLOW_HOME'] = tmp_dir.name
31-
print(f"Setting AIRFLOW_HOME to {tmp_dir.name}")
28+
# We use separate directory for local db path per session
29+
# by setting AIRFLOW_HOME env var, which is done in noxfile_config.py.
30+
31+
assert('AIRFLOW_HOME' in os.environ)
32+
33+
airflow_home = os.environ["AIRFLOW_HOME"]
34+
airflow_db = f"{airflow_home}/airflow.db"
3235

3336
# reset both resets and initializes a new database
3437
airflow.utils.db.resetdb(rbac=None) # this command will change in Airflow 2.0
3538

36-
yield
37-
38-
# cleaning up
39-
tmp_dir.cleanup()
39+
# Making sure we are using a data file there.
40+
assert(os.path.isfile(airflow_db))

composer/workflows/noxfile_config.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@
2020
# The source of truth:
2121
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/noxfile_config.py
2222

23+
import tempfile
24+
25+
26+
# Airflow creates a config file at the installation, so we want to set
27+
# `AIRFLOW_HOME` envvar before running pytest.
28+
29+
_tmpdir = tempfile.TemporaryDirectory()
30+
2331
TEST_CONFIG_OVERRIDE = {
2432
# You can opt out from the test for specific Python versions.
2533
# Skipping for Python 3.9 due to numpy compilation failure.
@@ -35,5 +43,5 @@
3543
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
3644
# A dictionary you want to inject into your test. Don't put any
3745
# secrets here. These values will override predefined values.
38-
"envs": {},
46+
"envs": {"AIRFLOW_HOME": _tmpdir.name},
3947
}

0 commit comments

Comments
 (0)