Skip to content

Commit

Permalink
[Fixes #30] ProcessBot and ServiceBot tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gioscarda committed Oct 31, 2019
1 parent 75f55e2 commit f811c32
Show file tree
Hide file tree
Showing 10 changed files with 297 additions and 18 deletions.
24 changes: 24 additions & 0 deletions src/wpsremote/mockutils.py
Expand Up @@ -4,6 +4,9 @@
# This code is licensed under the GPL 2.0 license, available at the root
# application directory.

from wpsagent import WPSAgentService, WPSAgentProcess
from wpsremote.resource_monitor import ResourceMonitor

__author__ = "Alessio Fabiani"
__copyright__ = "Copyright 2016 Open Source Geospatial Foundation - all rights reserved"
__license__ = "GPL"
Expand All @@ -24,3 +27,24 @@ def readline(self):
else:
self._lp += 1
return self._lines[self._lp-1]


class MockWPSAgentService(WPSAgentService):

def run(self):
pass


class MockWPSAgentProcess(WPSAgentProcess):

def run(self):
pass


class MockResourceMonitor(ResourceMonitor):
def run(self):
pass


def mock_clean_up_all():
pass
28 changes: 28 additions & 0 deletions src/wpsremote/xmpp_data/test/logger_test.properties
@@ -0,0 +1,28 @@
# (c) 2016 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.

[loggers]
keys=root

[handlers]
keys=consoleHandler

[formatters]
keys=simpleFormatter

[logger_root]
level=DEBUG
handlers=consoleHandler

[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)

[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - [%(levelname)s] %(message)s
datefmt=
4 changes: 2 additions & 2 deletions src/wpsremote/xmpp_data/test/test_remote.config
Expand Up @@ -21,9 +21,9 @@ password = R3m0T3wP5
mucService = conference.%(domain)s
mucServicePassword = geoserver

resource_file_dir = ./xmpp_data/resource_dir
resource_file_dir = ./src/wpsremote/xmpp_data/test/resource_dir

# . Configure this option (along with 'backup_on_wps_execution_shared_dir'
# . Configure this option (along with 'backup_on_wps_execution_shared_dir'
# . on single outputs of 'service.config') in order to make a copy
# . of the results into a shared folder before sending messages to XMPP
# . WARNING: this option takes precedence on "UPLOADER" option
Expand Down
11 changes: 6 additions & 5 deletions src/wpsremote/xmpp_data/test/test_service.config
Expand Up @@ -7,11 +7,12 @@ namespace = default
description = foo service
executable_path = \code\etl
executable_cmd = python %(executable_path)s\etl.py
output_dir = \tmp\oaaOnDemand
output_dir = src\wpsremote\xmpp_data\test\tmp
unique_execution_id = 123
workdir = %(output_dir)s\%(unique_execution_id)s
active = True
max_running_time_seconds = 300
max_running_time_seconds = 10
load_average_scan_minutes = 1
servicePassword = admin

[Input1]
Expand All @@ -22,7 +23,7 @@ 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]
path_list = [%(workdir)s, src\wpsremote\xmpp_data\test\tmp]

[Action1]
class = cmdline
Expand Down Expand Up @@ -70,7 +71,7 @@ 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
json_schema = ./src/wpsremote/xmpp_data/test/asset_schema.json


[Const1]
Expand Down Expand Up @@ -130,7 +131,7 @@ value = %(unique_execution_id)s
[Action10]
class = updateINIfile
input_ref = uniqueId
source_filepath = .\configs\OAAonDemand\geoserverCommands_template.properties
source_filepath = ./src/wpsremote/xmpp_data/test/geoserverCommands_template.properties
target_filepath= %(workdir)s\geoserverCommands.properties
section = DEFAULT
alias = unique_id
Expand Down
1 change: 1 addition & 0 deletions test/continoustest.bat
Expand Up @@ -30,6 +30,7 @@ python test_process_input_parameters.py
python test_computation_job_outputs.py
python test_config_parser.py
python test_bus_independent_messages.py
python test_bot.py
python resource_cleaner.py

IF "%1"=="" GOTO exit
Expand Down
211 changes: 211 additions & 0 deletions test/test_bot.py
@@ -0,0 +1,211 @@
# (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 os
from argparse import Namespace
import pickle
import mock
import datetime
import psutil
from wpsremote.processbot import ProcessBot
from wpsremote.servicebot import ServiceBot
from wpsremote.resource_cleaner import Resource
import wpsremote.resource_monitor as resource_monitor
import wpsremote.configInstance as configInstance
from wpsremote.xmppBus import XMPPBus
from wpsremote.computation_job_inputs import ComputationJobInputs
from wpsremote.computational_job_input_actions import ComputationalJobInputActions
from wpsremote.output_parameters import OutputParameters
from wpsremote.path import path
from wpsremote.mockutils import (
MockWPSAgentService, MockWPSAgentProcess, mock_clean_up_all, MockResourceMonitor
)

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

# due to xmppMessages chdir
os.chdir(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))


class TestBot(unittest.TestCase):

def setUp(self):
os.chdir(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
self.remote_config = configInstance.create(
"./src/wpsremote/xmpp_data/test/test_remote.config"
)
self.xmpp_bus = XMPPBus(self.remote_config, "service_name", "service_name_namespace")
self.xep_0045_plugin = self.xmpp_bus.xmpp.plugin["xep_0045"]
self.from_obj_mock = mock.Mock()
self.from_obj_mock.__class__ = mock.Mock
rooms = {
self.xmpp_bus._get_MUC_JId(): {
"resource": {
"role": "not_visitor"
}
}
}
self.xep_0045_plugin.rooms = rooms
self.from_obj_mock.resource = "resource"
test_msg = pickle.dumps({
"test_message": "test message"
})
msg = {
"type": "normal",
"body": "topic=request&id=123&baseURL=test_base_url&message={}".format(test_msg),
"from": self.from_obj_mock
}
# ExecuteMessage
self.execute_message = self.xmpp_bus.CreateIndipendentMessage(msg)
# cleaner
Resource.clean_up_all = staticmethod(mock_clean_up_all)
self.cleaner = Resource()
# monitor
resource_monitor.ResourceMonitor = MockResourceMonitor
# wps agent args
self.params_file = "./src/wpsremote/xmpp_data/test/test_params.p"
pickle.dump(
self.execute_message,
open(self.params_file, "wb")
)
self.args = Namespace(
executionid=None,
logconf='./src/wpsremote/xmpp_data/test/logger_test.properties',
params=self.params_file,
remoteconfig='./src/wpsremote/xmpp_data/test/test_remote.config',
runtype='service',
serviceconfig='./src/wpsremote/xmpp_data/test/test_service.config'
)

def test_process_bot(self):
wps_agent = MockWPSAgentProcess(self.args)
# test bot
process_bot = wps_agent.create_bot()
self.assertIsInstance(process_bot, ProcessBot)
self.assertTrue(process_bot._active)
self.assertFalse(process_bot._finished)
self.assertIsInstance(process_bot._input_parameters_defs, ComputationJobInputs)
self.assertIsInstance(process_bot._input_params_actions, ComputationalJobInputActions)
self.assertEqual(process_bot._input_values, {"test_message": "test message"})
self.assertEqual(process_bot._max_running_time, datetime.timedelta(0, 10))
self.assertEqual(process_bot.max_execution_time(), datetime.timedelta(0, 10))
self.assertIsInstance(process_bot._output_parameters_defs, OutputParameters)
self.assertEqual(process_bot._remote_wps_baseurl, "test_base_url")
self.assertEqual(
process_bot._resource_file_dir,
path("./src/wpsremote/xmpp_data/test/resource_dir")
)
self.assertEqual(
process_bot.get_resource_file_dir(),
path("./src/wpsremote/xmpp_data/test/resource_dir")
)
self.assertEqual(len(process_bot._stdout_action), 6)
for stdout_action in process_bot._stdout_action:
self.assertIn(stdout_action, ['ignore', 'progress', 'log', 'log', 'log', 'abort'])
self.assertEqual(len(process_bot._stdout_parser), 6)
for stdout_parser in process_bot._stdout_parser:
self.assertIn(stdout_parser, [
'.*\\[DEBUG\\](.*)',
'.*\\[INFO\\] ProgressInfo\\:([-+]?[0-9]*\\.?[0-9]*)\\%',
'.*\\[(INFO)\\](.*)',
'.*\\[(WARN)\\](.*)',
'.*\\[(ERROR)\\](.*)',
'.*\\[(CRITICAL)\\](.*)'
])
self.assertEqual(process_bot._uniqueExeId, "123")
self.assertEqual(process_bot._uploader, None)
self.assertEqual(process_bot._wps_execution_shared_dir, None)
self.assertEqual(process_bot.get_wps_execution_shared_dir(), None)
self.assertEqual(process_bot.description, "foo service")
self.assertEqual(process_bot.namespace, "default")
self.assertEqual(process_bot.service, "Service")
self.assertEqual(
process_bot.workdir(),
process_bot._output_dir + "/" + process_bot._uniqueExeId
)
# handle_finish
finish_msg = {
"type": "normal",
"body": "topic=finish",
"from": self.from_obj_mock
}
finished_message = self.xmpp_bus.CreateIndipendentMessage(finish_msg)
with self.assertRaises(SystemExit) as cm_finish:
process_bot.handle_finish(finished_message)
self.assertEqual(cm_finish.exception.code, 0)
# handle_abort
abort_msg = {
"type": "normal",
"body": "topic=abort",
"from": self.from_obj_mock
}
abort_message = self.xmpp_bus.CreateIndipendentMessage(abort_msg)
with self.assertRaises(SystemExit) as cm_abort:
process_bot.handle_abort(abort_message)
self.assertEqual(cm_abort.exception.code, -1)
with self.assertRaises(SystemExit):
process_bot.exit(0)
# kill process
self.cleaner.kill_processbot()
self.assertFalse(psutil.pid_exists(self.cleaner._processbot_pid))

def test_service_bot(self):
wps_agent = MockWPSAgentService(self.args)
# test bot
service_bot = wps_agent.create_bot()
self.assertIsInstance(service_bot, ServiceBot)
self.assertTrue(service_bot._active)
self.assertIsInstance(service_bot._input_parameters_defs, ComputationJobInputs)
self.assertEqual(service_bot._max_running_time, datetime.timedelta(0, 10))
self.assertEqual(
service_bot._output_dir,
path('src\\wpsremote\\xmpp_data\\test\\tmp')
)
self.assertIsInstance(service_bot._output_parameters_defs, OutputParameters)
self.assertFalse(service_bot._process_blacklist)
self.assertTrue(service_bot._redirect_process_stdout_to_logger)
self.assertEqual(
service_bot._remote_config_filepath,
'./src/wpsremote/xmpp_data/test/test_remote.config'
)
self.assertIsNone(service_bot._remote_wps_endpoint)
self.assertEqual(
service_bot._resource_file_dir,
path('./src/wpsremote/xmpp_data/test/resource_dir')
)
self.assertEqual(
service_bot.get_resource_file_dir(),
path("./src/wpsremote/xmpp_data/test/resource_dir")
)
self.assertIsInstance(service_bot._resource_monitor, MockResourceMonitor)
self.assertEqual(
service_bot._service_config_file,
'./src/wpsremote/xmpp_data/test/test_service.config'
)
self.assertIsNone(service_bot._wps_execution_shared_dir)
self.assertEqual(
service_bot.max_execution_time(),
datetime.timedelta(0, 10)
)
self.assertEqual(
service_bot.get_wps_execution_shared_dir(),
None
)
self.assertIsNotNone(service_bot.bus)
self.assertEqual(service_bot.description, 'foo service')
self.assertEqual(service_bot.namespace, 'default')
self.assertEqual(service_bot.running_process, {})
self.assertEqual(service_bot.service, 'Service')
# removing tmp files
params_file_path = path(self.params_file)
params_file_path.remove()


if __name__ == '__main__':
unittest.main()

0 comments on commit f811c32

Please sign in to comment.