Skip to content

Commit

Permalink
[Fixes #26] configparser tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gioscarda committed Oct 23, 2019
1 parent b1b35f3 commit 3eb8b9f
Show file tree
Hide file tree
Showing 4 changed files with 349 additions and 0 deletions.
151 changes: 151 additions & 0 deletions src/wpsremote/xmpp_data/test/test_service.config
@@ -0,0 +1,151 @@
# This is a inni file to be read with python ConfigParser (https://docs.python.org/2/library/configparser.html)
# Is possible to reference another variable in the ini file using the format %(<variable name>)s

[DEFAULT]
service = Service
namespace = default
description = foo service
executable_path = \code\etl
executable_cmd = python %(executable_path)s\etl.py
output_dir = \tmp\oaaOnDemand
unique_execution_id = 123
workdir = %(output_dir)s\%(unique_execution_id)s
active = True
max_running_time_seconds = 300
servicePassword = admin

[Input1]
class = param
name = referenceDate
type = string
description = Start date of optmizier
list = [item_0, item_1, item_2]
int_list = [0, 1, 2, 3]
float_list = [0.12, 1.6, 2.55, 3.4]
path_list = [%(workdir)s, \tmp\oaaOnDemand]

[Action1]
class = cmdline
input_ref = referenceDate
alias = d
template = -name value

[Input2]
class = param
name = numberOfEvaluations
type = int
description = Number of optimizer iterations

[Action2]
class = updateJSONfile
input_ref = numberOfEvaluations
source_filepath = .\configs\OAAonDemand\CMREOAA_MainConfigFile_template.json
target_filepath = %(workdir)s\config.json
json_path_expr = ['Config']['nEvaluations']

[Input3]
class = param
name = timeHorizon
type = int
description = Optimizer time horizon in seconds

[Action3]
class = updateJSONfile
input_ref = timeHorizon
source_filepath = .\configs\OAAonDemand\CMREOAA_MainConfigFile_template.json
target_filepath = %(workdir)s\config.json
json_path_expr = ['Config']['timeHorizon']


[Input4]
class = param
name = asset
type = application/json
description = Assets configuration
min = 1
max = 10

[Action4]
class = createJSONfile
input_ref = asset
target_filepath = %(workdir)s\asset_${json_path_expr}.json
json_path_expr = ['Asset']['id']
json_schema = .\configs\OAAonDemand\asset_schema.json


[Const1]
class = const
name = workdir
type = string
description = Optimal Asset Allocator process sandbox working directory
value = %(workdir)s

[Action5]
class = cmdline
input_ref = workdir
alias = w
template = -name value


[Const2]
class = const
name = etlExecutionType
type = string
description = identifies the type of execution performed by etl.py script
value = oaaOnDemand

[Action6]
class = cmdline
input_ref = etlExecutionType
template = value


[Action7]
class = copyfile
source_filepath = .\configs\OAAonDemand\oaaOnDemand_template.properties
target_filepath= %(workdir)s\oaaOnDemand.properties


[Action8]
class = updateINIfile
input_ref = timeHorizon
source_filepath = .\configs\OAAonDemand\wcsOAAonDemand_template.properties
target_filepath= %(workdir)s\wcsOAAonDemand.properties
section = DEFAULT
alias = time_horizont_sec

[Action9]
class = copyfile
source_filepath = .\configs\OAAonDemand\OAAonDemandWrapper_template.properties
target_filepath= %(workdir)s\OAAonDemandWrapper.properties


[Const3]
class = const
name = uniqueId
type = string
description = unique id identifing the run
value = %(unique_execution_id)s

[Action10]
class = updateINIfile
input_ref = uniqueId
source_filepath = .\configs\OAAonDemand\geoserverCommands_template.properties
target_filepath= %(workdir)s\geoserverCommands.properties
section = DEFAULT
alias = unique_id


[Output]
name = result
type = string
description = xml OAA output file
filepath = %(workdir)s\geoserverLoadOAAOutput.xml




[Logging]
#note the order
stdout_parser = [.*\[DEBUG\](.*), .*\[INFO\] ProgressInfo\:([-+]?[0-9]*\.?[0-9]*)\%, .*\[(INFO)\](.*), .*\[(WARN)\](.*), .*\[(ERROR)\](.*), .*\[(CRITICAL)\](.*)]
stdout_action = [ignore, progress, log, log, log, abort]
1 change: 1 addition & 0 deletions test/continoustest.bat
Expand Up @@ -28,6 +28,7 @@ pylint -r n --disable=C --disable=W0141 test_computation_job_outputs.py
python test_computation_job_inputs.py
python test_process_input_parameters.py
python test_computation_job_outputs.py
python test_config_parser.py
python resource_cleaner.py

IF "%1"=="" GOTO exit
Expand Down
194 changes: 194 additions & 0 deletions test/test_config_parser.py
@@ -0,0 +1,194 @@
# (c) 2019 Open Source Geospatial Foundation - all rights reserved
# (c) 2014 - 2015 Centre for Maritime Research and Experimentation (CMRE)
# (c) 2013 - 2014 German Aerospace Center (DLR)
# This code is licensed under the GPL 2.0 license, available at the root
# application directory.

import unittest
import wpsremote.ConfigParser as ConfigParser
from wpsremote.ConfigParser import DuplicateSectionError
import wpsremote.configInstance as configInstance
import wpsremote.path as path

__author__ = "Alessio Fabiani"
__copyright__ = "Copyright 2019 Open Source Geospatial Foundation - all rights reserved"
__license__ = "GPL"


class TestConfigParser(unittest.TestCase):

DEFAULT_ITEMS = [
('service', 'Service'), ('namespace', 'default'), ('description', 'foo service'),
('executable_path', '\\code\\etl'), ('executable_cmd', 'python %(executable_path)s\\etl.py'),
('output_dir', '\\tmp\\oaaOnDemand'), ('unique_execution_id', '123'),
('workdir', '%(output_dir)s\\%(unique_execution_id)s'), ('active', 'True'),
('max_running_time_seconds', '300'), ('servicepassword', 'admin')
]
CONFIG_SECTIONS = [
'Input1', 'Action1', 'Input2', 'Action2', 'Input3', 'Action3', 'Input4', 'Action4', 'Const1', 'Action5',
'Const2', 'Action6', 'Action7', 'Action8', 'Action9', 'Const3', 'Action10', 'Output', 'Logging'
]

def test_create_config_parser(self):
cp = configInstance.create("./src/wpsremote/xmpp_data/test/test_service.config")
self.assertIsNotNone(cp)
self.assertIsInstance(cp, ConfigParser.ConfigParser)

def test_sections(self):
cp = configInstance.create("./src/wpsremote/xmpp_data/test/test_service.config")
sections = cp.sections()
self.assertIsNotNone(sections)
for s in sections:
self.assertIn(s, TestConfigParser.CONFIG_SECTIONS)

def test_defaults(self):
cp = configInstance.create("./src/wpsremote/xmpp_data/test/test_service.config")
default_section = cp.defaults()
self.assertIsNotNone(default_section)
self.assertIsNotNone(default_section.items())
for d in default_section.items():
self.assertIn(d, TestConfigParser.DEFAULT_ITEMS)

def test_add_section(self):
cp = configInstance.create("./src/wpsremote/xmpp_data/test/test_service.config")
self.assertRaises(DuplicateSectionError, lambda: cp.add_section("Input1"))
self.assertNotIn("Input5", cp.sections())
cp.add_section("Input5")
self.assertIn("Input5", cp.sections())

def test_has_section(self):
cp = configInstance.create("./src/wpsremote/xmpp_data/test/test_service.config")
self.assertTrue(cp.has_section("Input1"))

def test_options(self):
options = [
'class', 'input_ref', 'alias', 'template', 'service', 'namespace', 'description',
'executable_path', 'executable_cmd', 'output_dir', 'unique_execution_id', 'workdir',
'active', 'max_running_time_seconds', 'servicepassword'
]
cp = configInstance.create("./src/wpsremote/xmpp_data/test/test_service.config")
opts = cp.options("Action1")
for o in opts:
self.assertIn(o, options)

def test_read(self):
filenames = ["./src/wpsremote/xmpp_data/test/service.config"]
cp = configInstance.create("./src/wpsremote/xmpp_data/test/test_service.config")
fn = cp.read("./src/wpsremote/xmpp_data/test/service.config")
self.assertEqual(fn, filenames)

def test_readfp(self):
cp = configInstance.create("./src/wpsremote/xmpp_data/test/test_service.config")
fp = open("./src/wpsremote/xmpp_data/test/service.config")
cp.readfp(fp)
fp.close()
default_section = cp.defaults()
for d in default_section.items():
if d == ('servicePassword', 'admin'):
self.assertNotIn(d, TestConfigParser.DEFAULT_ITEMS)

def test_get(self):
cp = configInstance.create("./src/wpsremote/xmpp_data/test/test_service.config")
self.assertEqual("admin", cp.get("DEFAULT", "servicePassword"))

def test_items(self):
items_list = [
('service', 'Service'), ('namespace', 'default'), ('description', 'foo service'),
('executable_path', '\\code\\etl'), ('executable_cmd', 'python \\code\\etl\\etl.py'),
('output_dir', '\\tmp\\oaaOnDemand'), ('unique_execution_id', '123'),
('workdir', '\\tmp\\oaaOnDemand\\123'), ('active', 'True'), ('max_running_time_seconds', '300'),
('servicepassword', 'admin'), ('class', 'updateJSONfile'), ('input_ref', 'timeHorizon'),
('source_filepath', '.\\configs\\OAAonDemand\\CMREOAA_MainConfigFile_template.json'),
('target_filepath', '\\tmp\\oaaOnDemand\\123\\config.json'),
('json_path_expr', "['Config']['timeHorizon']")
]
cp = configInstance.create("./src/wpsremote/xmpp_data/test/test_service.config")
for i in cp.items("Action3"):
self.assertIn(i, items_list)

def test_getboolean(self):
cp = configInstance.create("./src/wpsremote/xmpp_data/test/test_service.config")
self.assertIs(True, cp.getboolean("DEFAULT", "active"))

def test_has_option(self):
cp = configInstance.create("./src/wpsremote/xmpp_data/test/test_service.config")
self.assertTrue(cp.has_option("DEFAULT", "active"))

def test_set(self):
cp = configInstance.create("./src/wpsremote/xmpp_data/test/test_service.config")
self.assertTrue(cp.getboolean("DEFAULT", "active"))
cp.set("DEFAULT", "active", "False")
self.assertFalse(cp.getboolean("DEFAULT", "active"))

def test_remove_option(self):
cp = configInstance.create("./src/wpsremote/xmpp_data/test/test_service.config")
self.assertTrue(cp.has_option("Action1", "input_ref"))
cp.remove_option("Action1", "input_ref")
self.assertFalse(cp.has_option("Action1", "input_ref"))

def test_remove_section(self):
cp = configInstance.create("./src/wpsremote/xmpp_data/test/test_service.config")
self.assertTrue(cp.has_section("Action1"))
cp.remove_section("Action1")
self.assertFalse(cp.has_section("Action1"))

def test_items_without_defaults(self):
cp = configInstance.create("./src/wpsremote/xmpp_data/test/test_service.config")
for section in TestConfigParser.CONFIG_SECTIONS:
items = configInstance.items_without_defaults(cp, section, raw=False)
for i in items:
self.assertNotIn(i, TestConfigParser.DEFAULT_ITEMS)

def test_get_list_impl(self):
list_items = ['item_0', 'item_1', 'item_2']
cp = configInstance.create("./src/wpsremote/xmpp_data/test/test_service.config")
items = configInstance.get_list_impl(cp, "Input1", "list")
for i in items:
self.assertIn(i, list_items)

def test_get_list_list_impl(self):
list_list = [['item_0'], ['item_1'], ['item_2']]
cp = configInstance.create("./src/wpsremote/xmpp_data/test/test_service.config")
items = configInstance.get_list_list_impl(cp, "Input1", "list")
for i in items:
self.assertIsInstance(i, list)
self.assertIn(i, list_list)

def test_get_list_int_impl(self):
int_list = [0, 1, 2, 3]
cp = configInstance.create("./src/wpsremote/xmpp_data/test/test_service.config")
items = configInstance.get_list_int_impl(cp, "Input1", "int_list")
for i in items:
self.assertIsInstance(i, int)
self.assertIn(i, int_list)

def test_get_list_float_impl(self):
float_list = [0.12, 1.6, 2.55, 3.4]
cp = configInstance.create("./src/wpsremote/xmpp_data/test/test_service.config")
items = configInstance.get_list_float_impl(cp, "Input1", "float_list")
for i in items:
self.assertIsInstance(i, float)
self.assertIn(i, float_list)

def test_get_list_path_impl(self):
path_list = ['\\tmp\\oaaOnDemand\\123', '\\tmp\\oaaOnDemand']
cp = configInstance.create("./src/wpsremote/xmpp_data/test/test_service.config")
items = configInstance.get_list_path_impl(cp, "Input1", "path_list")
for i in items:
self.assertIsInstance(i, path.path)
self.assertIn(i, path_list)

def test_get_password(self):
cp = configInstance.create("./src/wpsremote/xmpp_data/test/test_service.config")
psw = configInstance.get_password(cp, "DEFAULT", "servicePassword")
self.assertEqual("admin", psw)

def test_get_path(self):
cp = configInstance.create("./src/wpsremote/xmpp_data/test/test_service.config")
item = configInstance.get_path(cp, "DEFAULT", "output_dir")
self.assertIsInstance(item, path.path)
self.assertEqual("\\tmp\\oaaOnDemand", item)


if __name__ == '__main__':
unittest.main()
3 changes: 3 additions & 0 deletions test_suite.sh
Expand Up @@ -10,3 +10,6 @@ coverage run --source=src test/test_process_input_parameters.py
echo "Running... test_computation_job_outputs"
coverage run --source=src test/test_computation_job_outputs.py

echo "Running... test_config_parser"
coverage run --source=src test/test_config_parser.py

0 comments on commit 3eb8b9f

Please sign in to comment.