-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fixes #30] ProcessBot and ServiceBot tests
- Loading branch information
Showing
9 changed files
with
187 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
# (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 | ||
import pickle | ||
import mock | ||
import datetime | ||
import psutil | ||
from wpsremote.processbot import ProcessBot | ||
from wpsremote.servicebot import ServiceBot | ||
from wpsremote.resource_cleaner import Resource | ||
from wpsremote.resource_monitor import ResourceMonitor | ||
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 | ||
|
||
__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() | ||
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 | ||
self.cleaner = Resource() | ||
|
||
def test_process_bot(self): | ||
process_bot = ProcessBot( | ||
"./src/wpsremote/xmpp_data/test/test_remote.config", | ||
"./src/wpsremote/xmpp_data/test/test_service.config", | ||
self.execute_message | ||
) | ||
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, 300)) | ||
self.assertEqual(process_bot.max_execution_time(), datetime.timedelta(0, 300)) | ||
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("./xmpp_data/resource_dir")) | ||
self.assertEqual(process_bot.get_resource_file_dir(), path("./xmpp_data/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) | ||
self.cleaner.kill_processbot() | ||
self.assertFalse(psutil.pid_exists(self.cleaner._processbot_pid)) | ||
|
||
def test_service_bot(self): | ||
service_bot = ServiceBot( | ||
"./src/wpsremote/xmpp_data/test/test_remote.config", | ||
"./src/wpsremote/xmpp_data/test/test_service.config" | ||
) | ||
self.assertTrue(service_bot._active) | ||
self.assertIsInstance(service_bot._input_parameters_defs, ComputationJobInputs) | ||
self.assertEqual(service_bot._max_running_time, datetime.timedelta(0, 300)) | ||
self.assertEqual(service_bot._output_dir, path('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('./xmpp_data/resource_dir')) | ||
self.assertIsInstance(service_bot._resource_monitor, ResourceMonitor) | ||
self.assertEqual( | ||
service_bot._service_config_file, | ||
'./src/wpsremote/xmpp_data/test/test_service.config' | ||
) | ||
self.assertIsNone(service_bot._wps_execution_shared_dir) | ||
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') | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters