Commit 15c1f8bc authored by Jelte Jansen's avatar Jelte Jansen
Browse files

tests for ConfigManagerData



git-svn-id: svn://bind10.isc.org/svn/bind10/branches/jelte-configuration@747 e5f2f494-b856-4b98-b285-d166d9295462
parent 03515c98
......@@ -5,21 +5,40 @@ import pprint
import os
from isc.cc import data
class ConfigManagerDataReadError(Exception):
pass
class ConfigManagerDataEmpty(Exception):
pass
class ConfigManagerData:
CONFIG_VERSION = 1
def __init__(self, data_path):
def __init__(self, data_path, file_name = "b10-config.db"):
"""Initialize the data for the configuration manager, and
set the version and path for the data store. Initializing
this does not yet read the database, a call to
read_from_file is needed for that."""
self.data = {}
self.data['version'] = ConfigManagerData.CONFIG_VERSION
self.data_path = data_path
self.db_filename = data_path + "/b10-config.db"
self.db_filename = data_path + os.sep + file_name
def set_data_definition(self, module_name, module_data_definition):
self.zones[module_name] = module_data_definition
"""Set the data definition for the given module name."""
#self.zones[module_name] = module_data_definition
self.data_definitions[module_name] = module_data_definition
def read_from_file(data_path):
config = ConfigManagerData(data_path)
def read_from_file(data_path, file_name = "b10-config.db"):
"""Read the current configuration found in the file at
data_path. If the file does not exist, a
ConfigManagerDataEmpty exception is raised. If there is a
parse error, or if the data in the file has the wrong
version, a ConfigManagerDataReadError is raised. In the first
case, it is probably safe to log and ignore. In the case of
the second exception, the best way is probably to report the
error and stop loading the system."""
config = ConfigManagerData(data_path, file_name)
try:
file = open(config.db_filename, 'r')
file_config = ast.literal_eval(file.read())
......@@ -27,19 +46,20 @@ class ConfigManagerData:
file_config['version'] == ConfigManagerData.CONFIG_VERSION:
config.data = file_config
else:
# of course we can put in a migration path here for old data
print("[bind-cfgd] Old version of data found, starting with empty configuration")
# We can put in a migration path here for old data
raise ConfigManagerDataReadError("[bind-cfgd] Old version of data found")
file.close()
except IOError as ioe:
print("No config file found, starting with empty config")
except EOFError as eofe:
print("Config file empty, starting with empty config")
raise ConfigManagerDataEmpty("No config file found")
except:
print("Config file unreadable, starting with empty config")
raise ConfigManagerDataReadError("Config file unreadable")
return config
def write_to_file(self):
def write_to_file(self, output_file_name = None):
"""Writes the current configuration data to a file. If
output_file_name is not specified, the file used in
read_from_file is used."""
try:
tmp_filename = self.db_filename + ".tmp"
file = open(tmp_filename, 'w');
......@@ -52,6 +72,13 @@ class ConfigManagerData:
except IOError as ioe:
print("Unable to write config file; configuration not stored")
def __eq__(self, other):
"""Returns True if the data contained is equal. data_path and
db_filename may be different."""
if type(other) != type(self):
return False
return self.data == other.data
class ConfigManager:
def __init__(self, data_path):
self.commands = {}
......
# Copyright (C) 2009 Internet Systems Consortium.
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
# DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
# INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
# FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# Tests for the configuration manager module
#
import unittest
import os
from isc.config.cfgmgr import *
class TestConfigManagerData(unittest.TestCase):
def setUp(self):
self.data_path = os.environ['CONFIG_TESTDATA_PATH']
self.config_manager_data = ConfigManagerData(self.data_path)
self.assert_(self.config_manager_data)
def test_init(self):
self.assertEqual(self.config_manager_data.data['version'],
ConfigManagerData.CONFIG_VERSION)
self.assertEqual(self.config_manager_data.data_path,
self.data_path)
self.assertEqual(self.config_manager_data.db_filename,
self.data_path + os.sep + "b10-config.db")
def test_set_data_definition(self):
pass
def test_read_from_file(self):
ConfigManagerData.read_from_file(self.data_path)
self.assertRaises(ConfigManagerDataEmpty,
ConfigManagerData.read_from_file,
"doesnotexist")
self.assertRaises(ConfigManagerDataReadError,
ConfigManagerData.read_from_file,
self.data_path, "b10-config-bad1.db")
self.assertRaises(ConfigManagerDataReadError,
ConfigManagerData.read_from_file,
self.data_path, "b10-config-bad2.db")
self.assertRaises(ConfigManagerDataReadError,
ConfigManagerData.read_from_file,
self.data_path, "b10-config-bad3.db")
def test_write_to_file(self):
output_file_name = "b10-config-write-test";
self.config_manager_data.write_to_file(output_file_name)
new_config = ConfigManagerData(self.data_path, output_file_name)
self.assertEqual(self.config_manager_data, new_config)
class TestConfigManager:
def setUp(self):
pass
def test_init(self):
pass
def test_set_config(self):
pass
def test_remove_config(self):
pass
def test_write_config(self):
pass
def test_handle_msg(self):
pass
def test_run(self):
pass
if __name__ == '__main__':
if not 'CONFIG_TESTDATA_PATH' in os.environ:
print("You need to set the environment variable CONFIG_TESTDATA_PATH to point to the directory containing the test data files")
exit(1)
unittest.main()
......@@ -89,4 +89,7 @@ class TestDataDefinition(unittest.TestCase):
self.assertEqual(False, self.validate_data("spec22.spec", "data22_8.data"))
if __name__ == '__main__':
if not 'CONFIG_TESTDATA_PATH' in os.environ:
print("You need to set the environment variable CONFIG_TESTDATA_PATH to point to the directory containing the test data files")
exit(1)
unittest.main()
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