Tested readConfig and validateConfig

This commit is contained in:
Anton Beloglazov
2012-08-13 15:24:01 +10:00
parent e0a3468c9e
commit 5390d7b1bc
2 changed files with 75 additions and 8 deletions

View File

@@ -16,6 +16,7 @@
"""
from contracts import contract
import os
import ConfigParser
@@ -26,11 +27,57 @@ DEFAILT_CONFIG_PATH = os.path.join(os.path.dirname(__file__), '..', 'neat.conf')
#CONFIG_PATH = "/etc/neat/neat.conf"
CONFIG_PATH = os.path.join(os.path.dirname(__file__), '..', 'neat.conf')
REQUIRED_FIELDS = [
'sql_connection',
'admin_tenant_name',
'admin_user',
'admin_password',
'global_manager_host',
'global_manager_port',
'local_data_directory',
'local_manager_interval',
'data_collector_interval',
'data_collector_data_length',
'compute_user',
'compute_password',
'sleep_command',
'algorithm_underload_detection',
'algorithm_overload_detection',
'algorithm_vm_selection',
'algorithm_vm_placement',
]
def readConfig():
"""Read the configuration files and return a dict
@contract
def readConfig(paths):
""" Read the configuration files and return the options.
:param paths: A list of required configuration file paths.
:type paths: list(str)
:return: A dictionary of the configuration options.
:rtype: dict(str: *)
"""
configParser = ConfigParser.ConfigParser()
configParser.readfp(open(DEFAILT_CONFIG_PATH))
configParser.read(CONFIG_PATH)
return dict(configParser.items("DEFAULT"))
@contract
def validateConfig(config, required_fields):
""" Check that the config contains all the required fields.
:param config: A config dictionary to check.
:type config: dict(str: *)
:param required_fields: A list of required fields.
:type required_fields: list(str)
:return: Whether the config is valid.
:rtype: bool
"""
for field in required_fields:
if not field in config:
return False
return True

View File

@@ -13,15 +13,35 @@
# limitations under the License.
from pyqcy import *
from neat.config import readConfig
from neat.config import *
class Config(TestCase):
@qc
def addition_actually_works(
x=int_(min=0), y=int_(min=0)
def read_default_config():
config = readConfig([DEFAILT_CONFIG_PATH])
assert validateConfig(config, REQUIRED_FIELDS)
@qc
def read_config():
config = readConfig([DEFAILT_CONFIG_PATH, CONFIG_PATH])
assert validateConfig(config, REQUIRED_FIELDS)
@qc
def validate_valid_config(
x=list_(of=str_(of='abc123_', max_length=20), min_length=0, max_length=10)
):
the_sum = x + y
assert the_sum >= x and the_sum >= y
test_config = dict(zip(x, x))
assert validateConfig(test_config, x)
@qc
def validate_invalid_config(
x=list_(of=str_(of='abc123_', max_length=20), min_length=0, max_length=10),
y=list_(of=str_(of='abc123_', max_length=20), min_length=0, max_length=10)
):
test_config = dict(zip(x, x))
if not y:
assert validateConfig(test_config, y)
else:
assert not validateConfig(test_config, y)