diff options
| author | Dolph Mathews <dolph.mathews@gmail.com> | 2011-11-30 14:26:06 -0600 |
|---|---|---|
| committer | Dolph Mathews <dolph.mathews@gmail.com> | 2011-11-30 15:00:06 -0600 |
| commit | 8f21b2c1efe4020aabfa5388ce1cd5eabf85cef5 (patch) | |
| tree | a3f2adac1c46dbde937892deafb7e4929e672657 | |
| parent | 7d2956d0b5b5e590733a0e88a0cb4ea1f57caa11 (diff) | |
Added JSON validator; fixed samples (bug 898353)
- Fixed broken samples
- Added command to validate all included *.json files
./run_tests.sh -j
Change-Id: I06839e22a4408e7ff4b6f5b8ebd29a73fcc809cf
| -rw-r--r-- | keystone/content/common/samples/endpointTemplateWithOnlyId.json | 4 | ||||
| -rw-r--r-- | keystone/content/common/samples/roles.json | 4 | ||||
| -rwxr-xr-x | run_tests.sh | 14 | ||||
| -rw-r--r-- | tools/validate_json.py | 84 |
4 files changed, 102 insertions, 4 deletions
diff --git a/keystone/content/common/samples/endpointTemplateWithOnlyId.json b/keystone/content/common/samples/endpointTemplateWithOnlyId.json index 0f2da91f..869bc5fb 100644 --- a/keystone/content/common/samples/endpointTemplateWithOnlyId.json +++ b/keystone/content/common/samples/endpointTemplateWithOnlyId.json @@ -1,5 +1,5 @@ { "OS-KSCATALOG:endpointTemplate":{ - "id":1, + "id":1 } -}
\ No newline at end of file +} diff --git a/keystone/content/common/samples/roles.json b/keystone/content/common/samples/roles.json index ac4c9cd5..6d1e6fb4 100644 --- a/keystone/content/common/samples/roles.json +++ b/keystone/content/common/samples/roles.json @@ -2,8 +2,8 @@ "roles":[{ "id":"123", "name":"compute:admin", - "description":"Nova Administrator", + "description":"Nova Administrator" } ], "roles_links":[] -}
\ No newline at end of file +} diff --git a/run_tests.sh b/run_tests.sh index fba85455..04a4a255 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -15,6 +15,7 @@ function usage { echo " Note: you might need to 'sudo' this since it pip installs into the vitual environment" echo " -p, --pep8 Just run pep8" echo " -l, --pylint Just run pylint" + echo " -j, --json Just validate JSON" echo " -c, --coverage Generate coverage report" echo " -h, --help Print this usage message" echo " --hide-elapsed Don't print the elapsed time for each test along with slow test list" @@ -43,6 +44,7 @@ function process_option { -f|--force) force=1;; -p|--pep8) just_pep8=1;; -l|--pylint) just_pylint=1;; + -j|--json) just_json=1;; -c|--coverage) coverage=1;; -*) addlopts="$addlopts $1";; *) addlargs="$addlargs $1" @@ -61,6 +63,7 @@ wrapper="" just_pep8=0 no_pep8=0 just_pylint=0 +just_json=0 coverage=0 for arg in "$@"; do @@ -144,6 +147,12 @@ function run_pylint { echo "Run 'pylint $PYLINT_OPTIONS $PYLINT_INCLUDE' for a full report." } +function validate_json { + echo "Validating JSON..." + python tools/validate_json.py +} + + # Delete old coverage data from previous runs if [ $coverage -eq 1 ]; then ${wrapper} coverage erase @@ -159,6 +168,11 @@ if [ $just_pylint -eq 1 ]; then exit fi +if [ $just_json -eq 1 ]; then + validate_json + exit +fi + run_tests diff --git a/tools/validate_json.py b/tools/validate_json.py new file mode 100644 index 00000000..8a8a3df1 --- /dev/null +++ b/tools/validate_json.py @@ -0,0 +1,84 @@ +""" +Searches the given path for JSON files, and validates their contents. +""" + +import argparse +import errno +import json +import logging +import os +import re + + +# Configure logging +logging.basicConfig(format='%(levelname)s: %(message)s') + +# Configure commandlineability +parser = argparse.ArgumentParser(description=__doc__) +parser.add_argument('-p --path', type=str, required=False, + default='.', help='the path to search for JSON files', + dest='path') +parser.add_argument('-r --regexp', type=str, required=False, + default='.json$', help='the regex to look for', + dest='regexp') +args = parser.parse_args() + + +def main(): + files = find_matching_files(args.path, args.regexp) + + results = True + for path in files: + results &= validate_json(path) + + # Invert our test results to produce a status code + exit(not results) + + +def validate_json(path): + """Open a file and validate it's contents as JSON""" + try: + contents = read_file(path) + except: + logging.warning('Unable to open: %s' % path) + return False + + try: + json.loads(contents) + except: + logging.error('Unable to parse: %s' % path) + return False + + return True + + +def find_matching_files(path, pattern): + """Search the given path for files matching the given pattern""" + + regex = re.compile(pattern) + + json_files = [] + for root, dirs, files in os.walk(path): + for name in files: + if regex.search(name): + full_name = os.path.join(root, name) + json_files.append(full_name) + return json_files + + +def read_file(path): + """Attempt to read a file safely""" + try: + fp = open(path) + except IOError as e: + if e.errno == errno.EACCES: + # permission error + return False + raise + else: + with fp: + return fp.read() + + +if __name__ == "__main__": + main() |
