-
Notifications
You must be signed in to change notification settings - Fork 2
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 #30 from PDXCapstoneF/bs-blackbox-schema
Add Configuration validation using `schema` library
- Loading branch information
Showing
7 changed files
with
124 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,8 @@ name = "pypi" | |
|
||
[packages] | ||
docopt = "*" | ||
six = "*" | ||
schema = "*" | ||
|
||
[requires] | ||
python_version = "3.6" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,4 @@ | ||
from os.path import dirname, basename, isfile | ||
import glob | ||
modules = glob.glob(dirname(__file__)+"/*.py") | ||
__all__ = [ basename(f)[:-3] for f in modules if isfile(f) and not f.endswith('__init__.py')] |
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,31 @@ | ||
from schema import Schema, And, Or, Optional | ||
|
||
# used for python2 and python3 string types | ||
from six import text_type | ||
|
||
def is_stringy(v): | ||
return type(v) is text_type | ||
|
||
ConfigSchema = Schema({ | ||
Optional("meta"): { | ||
object: object | ||
}, | ||
|
||
"specjbb": { | ||
"run_type": And(is_stringy, | ||
lambda rt: rt.lower() in ["hbir", "hbir_rt", "preset", "loadlevel"]), | ||
"kit_version": is_stringy, | ||
"tag": is_stringy, | ||
"jdk": is_stringy, | ||
"numa_node": int, | ||
"data": is_stringy, | ||
"rt_start": int, | ||
"duration": int, | ||
"t": [int], | ||
"jvm_options": [is_stringy], | ||
}, | ||
|
||
}) | ||
|
||
def validate(unvalidated): | ||
return ConfigSchema.validate(unvalidated) |
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,36 @@ | ||
{ | ||
"run_type": "i made my own run type and its cool", | ||
"meta": { | ||
"creation_date": "2012 23 1 1", | ||
"name": "single run", | ||
"company": "ACME" | ||
}, | ||
|
||
"speccjbb": { | ||
"injectors": 4, | ||
"backends": 2, | ||
"jvm_path": "/path/to/jvm", | ||
"jar_path": "/path/to/specjbb/jar" | ||
}, | ||
|
||
"jvm_options": [ | ||
"option 1", | ||
"option 2", | ||
"option 3" | ||
], | ||
|
||
"props": { | ||
"specjbb.controller.type": "runtype", | ||
"specjbb.controller.rtcurve.start": "0.0", | ||
"specjbb.controller.type": "HBIR" | ||
}, | ||
|
||
"other": { | ||
"stuff": "we might decide to", | ||
"add": "later" | ||
}, | ||
|
||
"extra": { | ||
"options": "we don't use yet" | ||
} | ||
} |
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,21 @@ | ||
from schema import SchemaError | ||
from unittest import TestCase | ||
import json | ||
from src.validate import validate | ||
|
||
class TestConfigValidator(TestCase): | ||
def test_empty_config_does_not_validate(self): | ||
with self.assertRaises(SchemaError): | ||
validate({}) | ||
|
||
def test_example_config_does_validate(self): | ||
with open('example_config.json') as f: | ||
j = json.loads(f.read()) | ||
assert 'specjbb' in j | ||
self.assertTrue(validate(j)) | ||
|
||
def test_invalid_config_does_not_validate(self): | ||
with self.assertRaises(SchemaError): | ||
with open('tests/fixtures/sample_config_invalid.json') as f: | ||
j = json.loads(f.read()) | ||
self.assertFalse(validate(j)) |