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

Config refactor #96

Merged
merged 3 commits into from
Sep 12, 2023
Merged

Config refactor #96

merged 3 commits into from
Sep 12, 2023

Conversation

ehanson8
Copy link
Contributor

Helpful background context

Adding a Config class to this repo to facilitate discussion about we can ultimately add to the Python template repos. Let's refine this!

What are the relevant tickets?

Requires Database Migrations?

NO

Includes new or updated dependencies?

YES

Why these changes are being introduced:
* Adding a Config class simplifies the loading of config values from env variables and allows for the removal of the click options

How this addresses that need:
* Add Config class with load_config_variables method and corresponding unit tests
* Refactor deposit and listen CLI commands with new Config class
* Remove all click options from CLI commands and update corresponding CLI tests
* Update _aws_credentials fixtures to _test_env fixture with all required env variables

Side effects of this change:
* None

Relevant ticket(s):
* https://mitlibraries.atlassian.net/browse/IN-898
Copy link

@ghukill ghukill left a comment

Choose a reason for hiding this comment

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

Looking really good and is functional as-is!

I left some thoughts and suggestions, that might help colocate the list of required env vars and remove the need for attr-defined linting skips. Understanding this PR to the first of a standardization of configurations / env vars, so just thinking broadly about patterns, even those that may not immediately benefit this project.

awd/config.py Outdated Show resolved Hide resolved
awd/config.py Outdated Show resolved Hide resolved
awd/config.py Outdated Show resolved Hide resolved
awd/cli.py Outdated Show resolved Hide resolved
awd/cli.py Show resolved Hide resolved
Comment on lines 28 to 45
def _test_env():
os.environ["AWS_ACCESS_KEY_ID"] = "testing"
os.environ["AWS_DEFAULT_REGION"] = "us-east-1"
os.environ["AWS_SECRET_ACCESS_KEY"] = "testing" # noqa: S105
os.environ["WORKSPACE"] = "test"
os.environ["LOG_LEVEL"] = "INFO"
os.environ["DOI_TABLE"] = "wiley-test"
os.environ["METADATA_URL"] = "http://example.com/works/"
os.environ["CONTENT_URL"] = "http://example.com/doi/"
os.environ["BUCKET"] = "awd"
os.environ["SQS_BASE_URL"] = "https://queue.amazonaws.com/123456789012/"
os.environ["SQS_INPUT_QUEUE"] = "mock-input-queue"
os.environ["SQS_OUTPUT_QUEUE"] = "mock-output-queue"
os.environ["COLLECTION_HANDLE"] = "123.4/5678"
os.environ["LOG_SOURCE_EMAIL"] = "noreply@example.com"
os.environ["LOG_RECIPIENT_EMAIL"] = "mock@mock.mock"
os.environ["RETRY_THRESHOLD"] = "10"
os.environ["SENTRY_DSN"] = "None"
Copy link

Choose a reason for hiding this comment

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

Genuinely curious others' thoughts, but we could start to standardize around using monkeypatch (https://docs.pytest.org/en/7.1.x/how-to/monkeypatch.html) for environment variables:

@pytest.fixture(autouse=True)
def _test_env(monkeypatch):
    monkeypatch.setenv("AWS_ACCESS_KEY_ID", "testing")
    monkeypatch.setenv("AWS_DEFAULT_REGION", "us-east-1")
    monkeypatch.setenv("AWS_SECRET_ACCESS_KEY", "testing")  # noqa: S105
    monkeypatch.setenv("WORKSPACE", "test")
    monkeypatch.setenv("LOG_LEVEL", "INFO")
    monkeypatch.setenv("DOI_TABLE", "wiley-test")
    monkeypatch.setenv("METADATA_URL", "http://example.com/works/")
    monkeypatch.setenv("CONTENT_URL", "http://example.com/doi/")
    monkeypatch.setenv("BUCKET", "awd")
    monkeypatch.setenv("SQS_BASE_URL", "https://queue.amazonaws.com/123456789012/")
    monkeypatch.setenv("SQS_INPUT_QUEUE", "mock-input-queue")
    monkeypatch.setenv("SQS_OUTPUT_QUEUE", "mock-output-queue")
    monkeypatch.setenv("COLLECTION_HANDLE", "123.4/5678")
    monkeypatch.setenv("LOG_SOURCE_EMAIL", "noreply@example.com")
    monkeypatch.setenv("LOG_RECIPIENT_EMAIL", "mock@mock.mock")
    monkeypatch.setenv("RETRY_THRESHOLD", "10")
    monkeypatch.setenv("SENTRY_DSN", "None")

While the effect is the same in this situation, the fact that it rolls back those changes after the test completes could be helpful when we do want that behavior. For example, if we move into integration tests where actual OS env vars are needed / wanted.

One example is monkeypatch.delenv() which will delete an env var for that test, but rollback after test is complete, which could be helpful. Otherwise, we'd be manipulating the os.environ[] and have to "reset" something for a downstream test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have no concerns about that, your thoughts @jonavellecuerdo?

tests/test_config.py Show resolved Hide resolved
* Shift list of required env variables to Config class attribute and add attribute definitions
* Rename load_config_variables method to load_environment_variables
* Remove #type: ignore comments as they are no longer necessary
* Replace os.environ calls with monkeypatch.setenv in _test_env fixture
Copy link

@ghukill ghukill left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks for moving this updated config work ahead.

@ehanson8 ehanson8 merged commit f99d787 into main Sep 12, 2023
3 checks passed
@ehanson8 ehanson8 deleted the config-refactor branch September 12, 2023 17:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants