Skip to content
Snippets Groups Projects
Commit dd7d050e authored by Alexander Schlemmer's avatar Alexander Schlemmer
Browse files

ENH: validation of configuration schema including unit tests

parent 3107817c
Branches
Tags
2 merge requests!33MAINT: change arguments of create_user,!21ENH: add json schema validation for config files
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
import os import os
import yaml import yaml
from jsonschema import validate
try: try:
# python2 # python2
...@@ -48,7 +49,9 @@ def configure(inifile): ...@@ -48,7 +49,9 @@ def configure(inifile):
_pycaosdbconf = None _pycaosdbconf = None
if _pycaosdbconf is None: if _pycaosdbconf is None:
_reset_config() _reset_config()
return _pycaosdbconf.read(inifile) read_config = _pycaosdbconf.read(inifile)
validate_yaml_schema(config_to_yaml(_pycaosdbconf))
return read_config
def get_config(): def get_config():
return _pycaosdbconf return _pycaosdbconf
...@@ -59,13 +62,18 @@ def config_to_yaml(config): ...@@ -59,13 +62,18 @@ def config_to_yaml(config):
for s in config.sections(): for s in config.sections():
valobj[s] = {} valobj[s] = {}
for key, value in config[s].items(): for key, value in config[s].items():
# TODO: Can the type be inferred from the config object?
if key in ["timeout", "debug"]:
valobj[s][key] = int(value)
elif key in ["ssl_insecure"]:
valobj[s][key] = bool(value)
else:
valobj[s][key] = value valobj[s][key] = value
return valobj return valobj
def validate(valobj): def validate_yaml_schema(valobj):
print(__file__)
with open(os.path.join(os.path.dirname(__file__), "schema-pycaosdb-ini.yml")) as f: with open(os.path.join(os.path.dirname(__file__), "schema-pycaosdb-ini.yml")) as f:
schema = yaml.load(f, Loader=yaml.SafeLoader) schema = yaml.load(f, Loader=yaml.SafeLoader)
validate(instance=valobj, schema=schema) validate(instance=valobj, schema=schema["schema-pycaosdb-ini"])
schema-pycaosdb-ini:
type: object
additionalProperties: false
properties:
Container: Container:
additionalProperties: false additionalProperties: false
properties: properties:
debug: debug:
default: 0
type: integer type: integer
enum: [0, 1, 2]
Connection: Connection:
description: Settings for the connection to the CaosDB server description: Settings for the connection to the CaosDB server
additionalProperties: false additionalProperties: false
...@@ -12,8 +18,6 @@ Connection: ...@@ -12,8 +18,6 @@ Connection:
type: string type: string
pattern: https://[-a-zA-Z0-9\.]+(:[0-9]+)?/ pattern: https://[-a-zA-Z0-9\.]+(:[0-9]+)?/
examples: [https://demo.indiscale.com/, https://localhost:10443/] examples: [https://demo.indiscale.com/, https://localhost:10443/]
debug:
type: integer
username: username:
type: string type: string
description: User name used for authentication with the server description: User name used for authentication with the server
...@@ -23,6 +27,8 @@ Connection: ...@@ -23,6 +27,8 @@ Connection:
type: string type: string
default: input default: input
enum: [input, plain, pass, keyring] enum: [input, plain, pass, keyring]
password_identifier:
type: string
password: password:
type: string type: string
examples: [caosdb] examples: [caosdb]
...@@ -43,6 +49,7 @@ Connection: ...@@ -43,6 +49,7 @@ Connection:
debug: debug:
default: 0 default: 0
type: integer type: integer
enum: [0, 1, 2]
description: The debug key allows control the verbosity. Set it to 1 or 2 in case you want to see debugging output or if you want to learn more about the internals of the protocol. 0 disables debugging output. description: The debug key allows control the verbosity. Set it to 1 or 2 in case you want to see debugging output or if you want to learn more about the internals of the protocol. 0 disables debugging output.
socket_proxy: socket_proxy:
examples: [localhost:12345] examples: [localhost:12345]
...@@ -78,9 +85,3 @@ Connection: ...@@ -78,9 +85,3 @@ Connection:
const: keyring const: keyring
then: then:
required: [url, username] required: [url, username]
[Connection]
url=https://demo.indiscale.com
username=admin
password=caosdb
password_method=plain
cacert=/etc/ssl/cert.pem
timeout=10000
debug=0
[Container]
debug=0
\ No newline at end of file
[Connection]
cacert=/very/long/path/to/self/signed/pem/file/caosdb.ca.pem
url=https://hostname:8833/playground
username=username
password_method=pass
[Connection]
url=https://0.0.0.0/
username=username
password_identifier=SECTION/SUBSECTION/identifier
password_method=pass
cacert=/etc/ssl/cert.pem
ssl_insecure=true
timeout=10000
debug=9
[connection]
ssl_insecure=true
url=https://localhost:10443/
password=caosdb
username=admin
password_method=plain
timeout=10000
debug=0
[Container]
debug=0
[Connection]
ssl_insecure=true
url=https://localhost:10443/
password=caosdb
username=admin
password_method=plain
timeout=10000
debug=0
key=bla
\ No newline at end of file
[Connection]
url=https://demo.indiscale.com/
username=admin
password=caosdb
password_method=plain
cacert=/etc/ssl/cert.pem
timeout=10000
debug=0
[Container]
debug=0
\ No newline at end of file
[Connection]
cacert=/very/long/path/to/self/signed/pem/file/caosdb.ca.pem
url=https://hostname:8833/playground
password_identifier=SECTION/caosdb
username=username
password_method=pass
[Connection]
url=https://0.0.0.0/
username=username
password_identifier=SECTION/SUBSECTION/identifier
password_method=pass
cacert=/etc/ssl/cert.pem
ssl_insecure=true
timeout=10000
debug=0
[Connection]
ssl_insecure=true
url=https://localhost:10443/
password=caosdb
username=admin
password_method=plain
timeout=10000
debug=0
[Container]
debug=0
...@@ -2,8 +2,26 @@ ...@@ -2,8 +2,26 @@
# Test configuration schema # Test configuration schema
# A. Schlemmer, 01/2021 # A. Schlemmer, 01/2021
import yaml
from jsonschema import validate
from jsonschema.exceptions import ValidationError
from pytest import raises
from glob import glob
import os
from caosdb.configuration import config_to_yaml, validate_yaml_schema
from configparser import ConfigParser
def test_config_files(): def test_config_files():
pass for fn in glob(os.path.join(os.path.dirname(__file__), "test_configs", "*.ini")):
c = ConfigParser()
c.read(fn)
validate_yaml_schema(config_to_yaml(c))
def test_broken_config_files(): def test_broken_config_files():
pass for fn in glob(os.path.join(os.path.dirname(__file__), "broken_configs", "*.ini")):
print(fn)
with raises(ValidationError):
c = ConfigParser()
c.read(fn)
validate_yaml_schema(config_to_yaml(c))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment