Commit 9cf201a5 authored by Jelte Jansen's avatar Jelte Jansen
Browse files

addressed comments from trac review


git-svn-id: svn://bind10.isc.org/svn/bind10/branches/trac284@2532 e5f2f494-b856-4b98-b285-d166d9295462
parent 6fe33b95
......@@ -17,6 +17,7 @@
import unittest
import socket
import tempfile
import sys
from cmdctl import *
SPEC_FILE_PATH = '..' + os.sep
......@@ -279,7 +280,6 @@ class TestSecureHTTPRequestHandler(unittest.TestCase):
rcode, reply = self.handler._handle_post_request()
self.assertEqual(http.client.BAD_REQUEST, rcode)
import sys
class MyCommandControl(CommandControl):
def _get_modules_specification(self):
return {}
......
{
"module_spec": {
"module_name": "Spec28",
'commands': [ ]
}
}
......@@ -22,7 +22,6 @@
import isc
import signal
import ast
import pprint
import os
import copy
import tempfile
......@@ -94,8 +93,6 @@ class ConfigManagerData:
dir=self.data_path,
delete=False)
filename = file.name
#pp = pprint.PrettyPrinter(indent=4)
#s = pp.pformat(self.data)
file.write(json.dumps(self.data))
file.write("\n")
file.close()
......
......@@ -22,6 +22,7 @@
"""
import json
import sys
import isc.cc.data
......@@ -34,27 +35,29 @@ class ModuleSpecError(Exception):
file"""
pass
import sys
def module_spec_from_file(spec_file, check = True):
"""Returns a ModuleSpec object defined by the file at spec_file.
If check is True, the contents are verified. If there is an error
in those contents, a ModuleSpecError is raised."""
in those contents, a ModuleSpecError is raised.
A ModuleSpecError is also raised if the file cannot be read, or
if it is not valid JSON."""
module_spec = None
if hasattr(spec_file, 'read'):
json_str = spec_file.read(-1)
module_spec = json.loads(json_str)
elif type(spec_file) == str:
file = open(spec_file)
json_str = file.read(-1)
try:
try:
if hasattr(spec_file, 'read'):
json_str = spec_file.read()
module_spec = json.loads(json_str)
except Exception as err:
module_spec = {}
file.close()
else:
raise ModuleSpecError("spec_file not a str or file-like object")
elif type(spec_file) == str:
file = open(spec_file)
json_str = file.read(-1)
module_spec = json.loads(json_str)
file.close()
else:
raise ModuleSpecError("spec_file not a str or file-like object")
except ValueError as ve:
raise ModuleSpecError("JSON parse error: " + str(ve))
except IOError as ioe:
raise ModuleSpecError("JSON read error: " + str(ioe))
if 'module_spec' not in module_spec:
raise ModuleSpecError("Data definition has no module_spec element")
......
......@@ -54,6 +54,13 @@ class TestModuleSpec(unittest.TestCase):
def test_open_bad_file_obj(self):
self.assertRaises(ModuleSpecError, isc.config.module_spec_from_file, 1)
# contains single quotes which json parser does not accept
self.assertRaises(ModuleSpecError, isc.config.module_spec_from_file, self.spec_file("spec28.spec"), False)
my_spec_file = open(self.spec_file("spec28.spec"))
self.assertRaises(ModuleSpecError, isc.config.module_spec_from_file, my_spec_file, False)
self.assertRaises(ModuleSpecError, isc.config.module_spec_from_file, self.spec_file("does_not_exist"), False)
def test_bad_specfiles(self):
self.assertRaises(ModuleSpecError, self.read_spec_file, "spec3.spec")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment