Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use pytest-env to set up API key #3272

Merged
merged 9 commits into from
Nov 6, 2023

Conversation

ngken0995
Copy link
Collaborator

Fixes

Fixes #1382 by @AetherUnbound

Description

  • comment all variables beginning with AIRFLOW_VAR_API_KEY
  • install pytest-env
  • all variables beginning with AIRFLOW_VAR_API_KEY are placed in catalog/pytest.ini

Testing Instructions

Checklist

  • My pull request has a descriptive title (not a vague title likeUpdate index.md).
  • My pull request targets the default branch of the repository (main) or a parent feature branch.
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • [N/A] I added or updated tests for the changes I made (if applicable).
  • [N/A] I added or updated documentation (if applicable).
  • I tried running the project locally and verified that there are no visible errors.
  • [N/A] I ran the DAG documentation generator (if applicable).

Developer Certificate of Origin

Developer Certificate of Origin
Developer Certificate of Origin
Version 1.1

Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.


Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.

@ngken0995 ngken0995 requested a review from a team as a code owner October 30, 2023 17:59
@ngken0995 ngken0995 changed the title 1382 pytest env api key Use pytest-env to set up API key Oct 30, 2023
@openverse-bot openverse-bot added 🟩 priority: low Low priority and doesn't need to be rushed ✨ goal: improvement Improvement to an existing user-facing feature 🤖 aspect: dx Concerns developers' experience with the codebase 🧱 stack: catalog Related to the catalog and Airflow DAGs labels Oct 30, 2023
@ngken0995
Copy link
Collaborator Author

@AetherUnbound CI + CD / Run tests for the catalog (pull_request) is failing. I have been trying to edit the api_key variable name and no luck on pass the test for the catalog.

@AetherUnbound
Copy link
Collaborator

@ngken0995 thanks for taking this on! We don't use a Pipfile for the catalog because Airflow comes with its own constraints file which we use when building the Docker image.

Instead please add the pytest-env package (pinned to the latest version) in the requirements_dev.txt file:

# Note: Unpinned packages have their versions determined by the Airflow constraints file
-r requirements_prod.txt
flaky==3.7.0
ipython
pook==1.1.1
pytest-mock==3.11.1
pytest-raises==0.11
pytest-socket==0.6.0
pytest-sugar==0.9.7
pytest-xdist

I think the CI might be failing because the requirement is missing from that file and in the (currently unused) Pipfile instead!

@AetherUnbound
Copy link
Collaborator

Oh one other note, all of the API_KEY_* variables will need to be prefixed with AIRFLOW_VAR_* like they are in the .env file, otherwise Airflow won't recognize them as Variables (explanation of the environment variable piece is here).

@AetherUnbound
Copy link
Collaborator

Update: I confirmed that with the following changes, pytest-env behaves as expected 😄

diff --git a/catalog/pytest.ini b/catalog/pytest.ini
index 48029dcc4..96cafe324 100644
--- a/catalog/pytest.ini
+++ b/catalog/pytest.ini
@@ -41,14 +41,14 @@ filterwarnings=
 cache_dir=/home/airflow/.cache/pytest
 
 env =
-    API_KEY_BROOKLYN_MUSEUM=apikey
-    API_KEY_DATA_GOV=apikey
-    API_KEY_EUROPEANA=apikey
-    API_KEY_FLICKR=apikey
-    API_KEY_FREESOUND=apikey
-    API_KEY_JAMENDO=apikey
-    API_KEY_JUSTTAKEITFREE=apikey
-    API_KEY_NYPL=apikey
-    API_KEY_RAWPIXEL=apikey
-    API_KEY_THINGIVERSE=apikey
-    API_KEY_WALTERS_ART_MUSEUM=apikey
+    AIRFLOW_VAR_API_KEY_BROOKLYN_MUSEUM=apikey
+    AIRFLOW_VAR_API_KEY_DATA_GOV=apikey
+    AIRFLOW_VAR_API_KEY_EUROPEANA=apikey
+    AIRFLOW_VAR_API_KEY_FLICKR=apikey
+    AIRFLOW_VAR_API_KEY_FREESOUND=apikey
+    AIRFLOW_VAR_API_KEY_JAMENDO=apikey
+    AIRFLOW_VAR_API_KEY_JUSTTAKEITFREE=apikey
+    AIRFLOW_VAR_API_KEY_NYPL=apikey
+    AIRFLOW_VAR_API_KEY_RAWPIXEL=apikey
+    AIRFLOW_VAR_API_KEY_THINGIVERSE=apikey
+    AIRFLOW_VAR_API_KEY_WALTERS_ART_MUSEUM=apikey
diff --git a/catalog/requirements_dev.txt b/catalog/requirements_dev.txt
index d1e577fa5..00821c9c0 100644
--- a/catalog/requirements_dev.txt
+++ b/catalog/requirements_dev.txt
@@ -10,3 +10,4 @@ pytest-raises==0.11
 pytest-socket==0.6.0
 pytest-sugar==0.9.7
 pytest-xdist
+pytest-env

@ngken0995
Copy link
Collaborator Author

@AetherUnbound Thank you, I will always check where to install new dependencies.

Copy link
Collaborator

@AetherUnbound AetherUnbound left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes look great, and runs great locally when I comment out my API keys! I have one note on the env template, otherwise this is good to go 😄

Comment on lines 29 to 39
#AIRFLOW_VAR_API_KEY_BROOKLYN_MUSEUM=not_set
#AIRFLOW_VAR_API_KEY_DATA_GOV=not_set
#AIRFLOW_VAR_API_KEY_EUROPEANA=not_set
#AIRFLOW_VAR_API_KEY_FLICKR=not_set
#AIRFLOW_VAR_API_KEY_FREESOUND=not_set
#AIRFLOW_VAR_API_KEY_JAMENDO=not_set
#AIRFLOW_VAR_API_KEY_JUSTTAKEITFREE=not_set
#AIRFLOW_VAR_API_KEY_NYPL=not_set
#AIRFLOW_VAR_API_KEY_RAWPIXEL=not_set
#AIRFLOW_VAR_API_KEY_THINGIVERSE=not_set
#AIRFLOW_VAR_API_KEY_WALTERS_ART_MUSEUM=not_set
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

**I actually think we can do away with most of these, maybe leaving one as an example!

Suggested change
#AIRFLOW_VAR_API_KEY_BROOKLYN_MUSEUM=not_set
#AIRFLOW_VAR_API_KEY_DATA_GOV=not_set
#AIRFLOW_VAR_API_KEY_EUROPEANA=not_set
#AIRFLOW_VAR_API_KEY_FLICKR=not_set
#AIRFLOW_VAR_API_KEY_FREESOUND=not_set
#AIRFLOW_VAR_API_KEY_JAMENDO=not_set
#AIRFLOW_VAR_API_KEY_JUSTTAKEITFREE=not_set
#AIRFLOW_VAR_API_KEY_NYPL=not_set
#AIRFLOW_VAR_API_KEY_RAWPIXEL=not_set
#AIRFLOW_VAR_API_KEY_THINGIVERSE=not_set
#AIRFLOW_VAR_API_KEY_WALTERS_ART_MUSEUM=not_set
# Example:
# AIRFLOW_VAR_API_KEY_BROOKLYN_MUSEUM=apikey

Copy link
Contributor

@obulat obulat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, nice to simplify the variable creation, @ngken0995!

@obulat obulat merged commit 910cc68 into WordPress:main Nov 6, 2023
42 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖 aspect: dx Concerns developers' experience with the codebase ✨ goal: improvement Improvement to an existing user-facing feature 🟩 priority: low Low priority and doesn't need to be rushed 🧱 stack: catalog Related to the catalog and Airflow DAGs
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Use pytest-env to set up API key shims for testing
4 participants