Skip to content

Commit ca9cd09

Browse files
Takashi Matsuoleahecole
andauthored
fix(composer): use separate airflow db file per session (GoogleCloudPlatform#5515)
* fix(composer): use separate AIRFLOW_HOME per session fixes GoogleCloudPlatform#5482 * add a commit for triggering builds in blog and workflow subdirs. * set AIRFLOW_HOME in noxfile_config.py * fix errors in noxfile_config.py Co-authored-by: Leah E. Cole <6719667+leahecole@users.noreply.github.com>
1 parent 650a869 commit ca9cd09

5 files changed

Lines changed: 36 additions & 2 deletions

File tree

composer/blog/gcp-tech-blog/unit-test-dags-cloud-build/dags/example_dag_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222

2323
@pytest.fixture(autouse=True, scope="function")
24+
# The fixture `airflow_database` lives in composer/conftest.py.
2425
def set_variables(airflow_database):
2526
models.Variable.set('gcp_project', PROJECT_ID)
2627
yield

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@
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.
@@ -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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
# limitations under the License.
1414

1515

16+
import os
17+
import tempfile
18+
1619
import pytest
1720

1821

@@ -21,5 +24,17 @@
2124
@pytest.fixture(scope="session")
2225
def airflow_database():
2326
import airflow.utils.db
27+
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"
35+
2436
# reset both resets and initializes a new database
2537
airflow.utils.db.resetdb(rbac=None) # this command will change in Airflow 2.0
38+
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
}

composer/workflows/quickstart_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020

2121
@pytest.fixture(autouse=True, scope="function")
22+
# The fixture `airflow_database` lives in composer/conftest.py.
2223
def set_variables(airflow_database):
2324

2425
models.Variable.set('gcs_bucket', 'example_bucket')

0 commit comments

Comments
 (0)