Skip to content

Commit 73d444e

Browse files
authored
Merge pull request #10 from kilobyteno/check-required-env-vars
2 parents 52c439f + 2c06b84 commit 73d444e

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

tests/test_konfig.py

+37-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import logging
2+
import os
23

34
import pytest
45

5-
from tunsberg.konfig import uvicorn_log_config
6+
from tunsberg.konfig import check_required_env_vars, uvicorn_log_config
67

78

89
class TestUvicornLogConfig:
@@ -55,3 +56,38 @@ def test_custom_log_level(self):
5556
assert config['loggers']['uvicorn']['level'] == logging.getLevelName(log_lvl)
5657
assert config['loggers']['uvicorn.error']['level'] == logging.getLevelName(log_lvl)
5758
assert config['loggers']['uvicorn.access']['level'] == logging.getLevelName(log_lvl)
59+
60+
61+
class TestCheckRequiredEnvVars:
62+
# Set the environment variable for testing
63+
os.environ['RANDOM_ENV_VAR'] = 'random_value'
64+
65+
def test_validate_if_true_in_local_development_env(self):
66+
req_envs = {'RANDOM_ENV_VAR': {'runtime': True, 'build': True}}
67+
check = check_required_env_vars(required_env_vars=req_envs, env='local')
68+
assert check
69+
70+
def test_validate_if_true_in_staging_env(self):
71+
req_envs = {'RANDOM_ENV_VAR': {'runtime': True, 'build': True}}
72+
check = check_required_env_vars(required_env_vars=req_envs, env='staging', live_envs=['staging'])
73+
assert check
74+
75+
def test_validate_if_true_in_prod_env(self):
76+
req_envs = {'RANDOM_ENV_VAR': {'runtime': True, 'build': True}}
77+
check = check_required_env_vars(required_env_vars=req_envs, env='prod', live_envs=['prod'])
78+
assert check
79+
80+
def test_validate_that_it_fails_if_env_var_is_not_set(self):
81+
req_envs = {'SOME_OTHER_RANDOM_ENV_VAR': {'runtime': True, 'build': True}}
82+
with pytest.raises(ValueError):
83+
check_required_env_vars(required_env_vars=req_envs, env='production', live_envs=['production'])
84+
85+
def test_validate_that_it_fails_for_code_build_if_env_var_is_not_set(self):
86+
req_envs = {'SOME_OTHER_RANDOM_ENV_VAR': {'runtime': False, 'build': True}}
87+
with pytest.raises(ValueError):
88+
check_required_env_vars(required_env_vars=req_envs, env='production', live_envs=['production'], code_build=True)
89+
90+
def test_validate_that_it_fails_for_runtime_if_env_var_is_not_set(self):
91+
req_envs = {'SOME_OTHER_RANDOM_ENV_VAR': {'runtime': True, 'build': False}}
92+
with pytest.raises(ValueError):
93+
check_required_env_vars(required_env_vars=req_envs, env='production', live_envs=['production'])

tunsberg/konfig.py

+47
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
from os import getenv
23

34

45
def uvicorn_log_config(
@@ -65,3 +66,49 @@ def uvicorn_log_config(
6566
},
6667
},
6768
}
69+
70+
71+
def check_required_env_vars(required_env_vars: dict, env: str, live_envs: [] or None = None, code_build: bool = False) -> bool or None:
72+
"""
73+
Check if all required environment variables are set based on the current environment and if the code is being built.
74+
75+
Example for required_env_vars:
76+
{'ENV': {'runtime': True, 'build': True},'JWT_PUBLIC_KEY': {'runtime': True, 'build': False}}
77+
78+
Example for env:
79+
'production'
80+
81+
Example for live_envs:
82+
['production', 'prod', 'staging']
83+
84+
:param required_env_vars: Required environment variables
85+
:type required_env_vars: dict
86+
:param env: Current environment
87+
:type env: str
88+
:param live_envs: List of live environments
89+
:type live_envs: [] or None
90+
:param code_build: Whether the code is being built
91+
:type code_build: bool
92+
:return: True if all required environment variables are set
93+
:rtype: bool or None
94+
:raises Exception: If any required environment variable is not set
95+
"""
96+
if live_envs is None:
97+
live_envs = ['production', 'prod']
98+
99+
is_runtime = env in live_envs and not code_build
100+
is_code_build = code_build
101+
102+
# Select environment variables based on current environment (production or build)
103+
req_env_vars = [
104+
env_var for env_var, conditions in required_env_vars.items() if (conditions['runtime'] and is_runtime) or (conditions['build'] and is_code_build)
105+
]
106+
107+
# Validate if all required environment variables are set
108+
missing_vars = [env_var for env_var in req_env_vars if getenv(env_var) is None]
109+
110+
if missing_vars:
111+
missing_vars_str = ', '.join(missing_vars)
112+
raise ValueError(f'Environment Config Error: The following variables are not set: {missing_vars_str}')
113+
114+
return True

0 commit comments

Comments
 (0)