-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from northpowered/51-test-startup-exceptions
Testing config loader and some startuo events
- Loading branch information
Showing
14 changed files
with
227 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,22 @@ | ||
from pydantic import (BaseModel, ValidationError) | ||
from loguru import logger | ||
import os | ||
|
||
|
||
class BaseSectionModel(BaseModel): | ||
|
||
class Config: | ||
load_failed: bool = False | ||
|
||
def __repr__(self) -> str: | ||
return f"<FastAPIConfigurationSection object at {hex(id(self))}>" | ||
|
||
def load(self, section_data: dict, section_name: str): | ||
try: | ||
return self.parse_obj(section_data) | ||
except KeyError: | ||
logger.error(f'Missed {section_name} section in config file') | ||
os._exit(0) | ||
except ValidationError as ex: | ||
error = ex.errors()[0] | ||
# type: ignore | ||
self.Config.load_failed = True | ||
logger.error( | ||
f"{section_name} | {error.get('loc')[0]} | {error.get('msg')}") | ||
os._exit(0) | ||
f"{section_name} | {error.get('loc')[0]} | {error.get('msg')}" | ||
) | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from .shared import load_config | ||
from configuration.sections import ServerSectionConfiguration | ||
|
||
|
||
def test_conf_load_toml(): | ||
c = load_config('src/config.toml') | ||
assert c | ||
assert not c.Config.load_failed | ||
assert "<FastAPIConfiguration object at" in str(c.__repr__) | ||
|
||
|
||
def test_conf_load_yaml(): | ||
c = load_config('src/config.yaml') | ||
assert c | ||
assert not c.Config.load_failed | ||
assert "<FastAPIConfiguration object at" in str(c.__repr__) | ||
|
||
|
||
def test_conf_load_ini(): | ||
c = load_config('src/config.ini') | ||
assert c | ||
assert not c.Config.load_failed | ||
assert "<FastAPIConfiguration object at" in str(c.__repr__) | ||
|
||
|
||
def test_conf_non_existing_config(): | ||
assert not load_config('non_existing.toml') | ||
|
||
|
||
def test_conf_lost_file_extention(): | ||
assert load_config('non_existing').Config.load_failed, "Cannot catch lost file extention" | ||
|
||
|
||
def test_conf_unknown_file_extention(): | ||
assert load_config('non_existing.boroda').Config.load_failed, "Cannot catch unknown file extention" | ||
|
||
|
||
def test_conf_validation_error(): | ||
bad_data: dict = { | ||
'bind_port': 'bar' | ||
} | ||
section = ServerSectionConfiguration() | ||
assert not section.load(bad_data, 'Server') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
[Main] | ||
application_mode = "dev1" | ||
log_level = "debug" | ||
log_destination = "stdout" | ||
log_in_json = 0 | ||
log_sql = 0 | ||
timezone = +3 | ||
enable_swagger = 1 | ||
swagger_doc_url = "/doc" | ||
swagger_redoc_url = "/redoc" | ||
enable_security = 1 | ||
|
||
[AdminGUI] | ||
admin_enable = 1 | ||
admin_url = "/admin/" | ||
|
||
|
||
[Server] | ||
bind_address = "localhost" | ||
bind_port = 8000 | ||
base_url = "example.com" | ||
|
||
|
||
[Vault] | ||
vault_enable = 1 | ||
vault_host = "localhost" | ||
vault_port = 8200 | ||
vault_disable_tls = 1 | ||
vault_auth_method = "token" | ||
vault_token = "test" | ||
#vault_credentials = | ||
vault_try_to_unseal = 1 | ||
#vault_key_type - json | keys | ||
#json - legacy json file from Vault, created at initialization of Vault instance/cluster | ||
# also can contain root_token string, which will be used to access Vault with TOKEN auth_method | ||
# Priority: | ||
# 1) vault_auth_token from config file | ||
# 2) root_token from json file | ||
#keys - simple txt file with unsealing key portions in base64, line by line | ||
#vault_keyfile_type = | ||
#vault_unseal_keys = | ||
|
||
[Database] | ||
db_driver = "postgresql" | ||
db_host = "127.0.0.1" | ||
db_port = 5432 | ||
db_name = "test" | ||
db_username = "test" | ||
db_password = "test" | ||
|
||
db_vault_enable = 1 | ||
db_vault_role = "testrole" | ||
db_vault_static = 1 | ||
db_vault_storage = "database" | ||
|
||
[Telemetry] | ||
enable = 1 | ||
agent_type = "jaeger" | ||
agent_host = "localhost" | ||
agent_port = 6831 | ||
trace_id_length = 12 | ||
|
||
[Security] | ||
enable_rbac = 1 | ||
login_with_username = 1 | ||
login_with_email = 1 | ||
jwt_algorithm = "HS256" | ||
jwt_ttl = 3600 | ||
jwt_base_secret = "dev-secret-from-configfile" | ||
jwt_base_secret_storage = 'vault' | ||
jwt_base_secret_filename = 'secret.key1' | ||
jwt_base_secret_vault_storage_name = 'kv_test' | ||
jwt_base_secret_vault_secret_name = 'jwt' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
localfilesecret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.