The .env file with the utility function configures environment variables during local development. This allows developers to customize their local environment without affecting the production settings.
Changes
Two files added: env_manager.py (Location: app/services/env_manager.py) and test_env_manager.py (Location: app/services/tests/test_env_manager.py)
Changes made to the ./local-start.sh to incorporate the utility function
Changes made to the .gitignore to ignore the folder keys/
Change made in the requirements.txt, fastapi changed to fastapi[standard]
Testing
The new feature was tested using the following unit tests and integration tests. Refer to the documentation for more details.
I) Unit Tests
‘Test_env_variable_from_env_file’: this test verifies that the ‘get_env_variable’ function correctly retrieves environment variables from a ‘.env’ file.
‘test_env_variable _from_google_secrets’: this test checks that the ‘get_env_variable’ function correctly retrieve environment variables from Google Cloud’s Secret Manager when the variable is not found in the ‘.env’ file.
Steps to run the unit tests:
Execute the test command: pytest app/services/tests/test_env_manager.py
Review the test output: after running the command, you’ll see output indicating whether the tests passed or failed.
Expected Output: Upon successful execution, you should see the following output in terminal:
II) Integration Tests
The integration test ensures that the ‘get_env_variable’ function works correctly when integrated into the broader application environment. This test verifies that environment variables are correctly loaded from both the ‘.env’ file and Google Cloud’s Secret Manager, and that the application starts with the correct configuration.
Steps to run the integration test:
Run the Startup Script:
./local-start.sh
The script begins by setting up local environment variables required by the application.
It authenticates the Google Cloud project using the service account credentials specified in the ‘local-auth.json’ file.
The ‘get_env_variable’ function is called to fetch environment variables, ‘GOOGLE_API_KEY’, ‘ENV_TYPE’,and ‘PROJECT_ID’. These variables are either retrieved from the local ‘.env’ file or from Google Cloud Secret Manager if not found locally.
Expected Output: Upon successful execution, you should see the following output in terminal:
Results
The new feature passed the above-mentioned tests and performed its required function without breaking the existing codebase.
Notes
The utility function ‘get_env_varible’ is designed to streamline the process of retrieving environment variables from both the .env file (for local development) and Google Cloud Secrets Manager (for production).
This function attempts to retrieve environment variables in a secure and reliable manner. It first attempts to retrieve the environment variable from a local .env file. If the variable is not found there, the function then tries to retrieve it from Google Cloud Secret Manager.
Screenshots
The utility function with relevant error handling performed.
Summary
The .env file with the utility function configures environment variables during local development. This allows developers to customize their local environment without affecting the production settings.
Changes
Two files added:
env_manager.py
(Location: app/services/env_manager.py) andtest_env_manager.py
(Location: app/services/tests/test_env_manager.py)Changes made to the
./local-start.sh
to incorporate the utility functionChanges made to the
.gitignore
to ignore the folder keys/Change made in the
requirements.txt
, fastapi changed to fastapi[standard]Testing
The new feature was tested using the following unit tests and integration tests. Refer to the documentation for more details.
I) Unit Tests
Steps to run the unit tests:
Execute the test command:
pytest app/services/tests/test_env_manager.py
Review the test output: after running the command, you’ll see output indicating whether the tests passed or failed. Expected Output: Upon successful execution, you should see the following output in terminal:II) Integration Tests
Steps to run the integration test: Run the Startup Script:
The script begins by setting up local environment variables required by the application. It authenticates the Google Cloud project using the service account credentials specified in the ‘local-auth.json’ file. The ‘get_env_variable’ function is called to fetch environment variables, ‘GOOGLE_API_KEY’, ‘ENV_TYPE’,and ‘PROJECT_ID’. These variables are either retrieved from the local ‘.env’ file or from Google Cloud Secret Manager if not found locally. Expected Output: Upon successful execution, you should see the following output in terminal:
Results
The new feature passed the above-mentioned tests and performed its required function without breaking the existing codebase.
Notes
Screenshots
The utility function with relevant error handling performed.