From d5fe88f828868ed3e9e1459fa93d37d2b83b2b17 Mon Sep 17 00:00:00 2001 From: gioscarda Date: Fri, 25 Oct 2019 14:16:17 +0200 Subject: [PATCH] [Fixes #28] Add BusIndependentMessage tests --- requirements.txt | 1 + src/wpsremote/xmpp_data/test/test_file | 1 + .../xmpp_data/test/test_remote.config | 63 ++++ test/continoustest.bat | 1 + test/test_bus_independent_messages.py | 285 ++++++++++++++++++ test/test_computation_job_outputs.py | 6 +- test_suite.sh | 3 + 7 files changed, 357 insertions(+), 3 deletions(-) create mode 100644 src/wpsremote/xmpp_data/test/test_remote.config create mode 100644 test/test_bus_independent_messages.py diff --git a/requirements.txt b/requirements.txt index 7898310..2d0bbc8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -39,3 +39,4 @@ twine==1.15.0 urllib3==1.25.6 webencodings==0.5.1 wrapt==1.10.10 +mock==3.0.5 diff --git a/src/wpsremote/xmpp_data/test/test_file b/src/wpsremote/xmpp_data/test/test_file index e69de29..08cf610 100644 --- a/src/wpsremote/xmpp_data/test/test_file +++ b/src/wpsremote/xmpp_data/test/test_file @@ -0,0 +1 @@ +test content \ No newline at end of file diff --git a/src/wpsremote/xmpp_data/test/test_remote.config b/src/wpsremote/xmpp_data/test/test_remote.config new file mode 100644 index 0000000..7ebd4fa --- /dev/null +++ b/src/wpsremote/xmpp_data/test/test_remote.config @@ -0,0 +1,63 @@ +# (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. + +[DEFAULT] + +bus_class_name = xmppBus.XMPPBus + +port = 5223 +address = 127.0.0.1 +domain = geoserver.org + +# . Those are the connection parameters to the XMPP Server. +# . The user must exists on the Server and its name must be +# . equal to the service name. +user = default.GdalContour +password = R3m0T3wP5 + +mucService = conference.%(domain)s +mucServicePassword = geoserver + +resource_file_dir = ./xmpp_data/resource_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 +# wps_execution_shared_dir = /share/xmpp_data/share + +# . This section is used to configure the uploader class and connection +# . parameters. +# . This is necessary in order to let the 'upload_data' option work on +# . single outputs of 'service.config' +[UPLOADER] +# There are different implementations of the FTP Uploader available right now: +# . Plain standard FTP Protocol (based on ftplib) +# ftpUpload.FtpUpload +# . FTP over TLS (FTPS) Protocol (based on ftplib) +# ftpsUpload.FtpsUpload +# . S-FTP Protocol (based on paramiko Python lib) +# sftpUpload.SFtpUpload +#uploader_class_name = ftpUpload.FtpUpload +#uploader_host = ftp.: +#uploader_username = +#uploader_password = + +# . "encryptor" you can use encrypted passwords with a private/public key couple +# +# . To generate a new private key use the following command: +# openssl genrsa -out myTestKey.pem -passout pass:"f00bar" -des3 2048 +# +# . To generate a new public key use the following command: +# openssl rsa -pubout -in myTestKey.pem -passin pass:"f00bar" -out myTestKey.pub +# +# . To encrypt your password use the following utility +# python encrypt.py password path/to/rsakey.pub passphrase +# +# . To double check the password is correct use the following utility +# python decrypt.py password path/to/rsakey.pem passphrase +uploader_private_rsa_key = ./xmpp_data/ssl/myTestKey.pem +uploader_passphrase = f00bar diff --git a/test/continoustest.bat b/test/continoustest.bat index 709460b..1161d73 100644 --- a/test/continoustest.bat +++ b/test/continoustest.bat @@ -29,6 +29,7 @@ python test_computation_job_inputs.py python test_process_input_parameters.py python test_computation_job_outputs.py python test_config_parser.py +python test_bus_independent_messages.py python resource_cleaner.py IF "%1"=="" GOTO exit diff --git a/test/test_bus_independent_messages.py b/test/test_bus_independent_messages.py new file mode 100644 index 0000000..f89bb9d --- /dev/null +++ b/test/test_bus_independent_messages.py @@ -0,0 +1,285 @@ +# (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 os +import unittest +import mock +import pickle +import wpsremote.configInstance as configInstance +from wpsremote.busIndipendentMessages import ( + RegisterMessage, ProgressMessage, LogMessage, CompletedMessage, ErrorMessage, AbortMessage, + LoadAverageMessage, InviteMessage, GetLoadAverageMessage, ExecuteMessage, FinishMessage +) +from wpsremote.xmppBus import XMPPBus +from wpsremote.xmppMessages import ( + XMPPRegisterMessage, XMPPProgressMessage, XMPPLogMessage, + XMPPCompletedMessage, XMPPErrorMessage, XMPPLoadAverageMessage +) + +__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 TestBusIndependentMessages(unittest.TestCase): + + def setUp(self): + 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() + + def test_invite_message(self): + rooms = { + self.xmpp_bus._get_MUC_JId(): { + "resource": { + "role": "not_visitor" + } + } + } + self.xep_0045_plugin.rooms = rooms + self.from_obj_mock.resource = "resource" + msg = { + "type": "normal", + "body": "topic=invite", + "from": self.from_obj_mock + } + # InviteMessage + message = self.xmpp_bus.CreateIndipendentMessage(msg) + self.assertIsInstance(message, InviteMessage) + self.assertEqual(message.originator(), self.from_obj_mock) + + def test_convert_register_message(self): + input_DLR = [ + ( + 'Input1', + '{' + '"class": param, ' + '"name": "interval", ' + '"title": Elevation Interval, ' + '"type": application/json, ' + '"description": Elevation interval between contours, ' + '"min": 1, ' + '"max": 1, ' + '"default": 200' + '}' + ) + ] + output_DLR = [ + ( + 'result1', + '{' + '"publish_layer_name": null, ' + '"description": "WPS Resource Plain Text", ' + '"title": null, ' + '"output_mime_type": null, ' + '"publish_default_style": null, ' + '"publish_as_layer": null, ' + '"type": "string", ' + '"publish_target_workspace": null' + '}' + ) + ] + # RegisterMessage + r_msg = RegisterMessage( + originator=self.from_obj_mock, + service="test_service", + namespace="test_namespace", + descritpion="test_description", + par=input_DLR, + output=output_DLR + ) + c_msg = self.xmpp_bus.Convert(r_msg) + self.assertIsInstance(c_msg, XMPPRegisterMessage) + self.assertEqual(c_msg.title(), "test_namespace.test_service") + + def test_execute_message(self): + rooms = { + self.xmpp_bus._get_MUC_JId(): { + "resource": { + "role": "not_visitor" + } + } + } + self.xep_0045_plugin.rooms = rooms + from_obj_mock = mock.Mock() + from_obj_mock.resource = "resource" + test_msg = pickle.dumps("test_message") + msg = { + "type": "normal", + "body": "topic=request&id=123&baseURL=test_base_url&message={}".format(test_msg), + "from": from_obj_mock + } + # ExecuteMessage + message = self.xmpp_bus.CreateIndipendentMessage(msg) + self.assertIsInstance(message, ExecuteMessage) + + def test_progress_message(self): + # ProgressMessage + r_msg = ProgressMessage(self.from_obj_mock, 1.34) + c_msg = self.xmpp_bus.Convert(r_msg) + self.assertIsInstance(c_msg, XMPPProgressMessage) + self.assertEqual(c_msg.originator, self.from_obj_mock) + self.assertEqual(c_msg.progress, 1.34) + self.assertEqual(c_msg.xmppChannel, self.xmpp_bus) + + def test_covert_log_message(self): + # LogMessage + r_msg = LogMessage(self.from_obj_mock, "INFO", "test_message_text") + c_msg = self.xmpp_bus.Convert(r_msg) + self.assertIsInstance(c_msg, XMPPLogMessage) + self.assertEqual(c_msg.originator, self.from_obj_mock) + self.assertEqual(c_msg.level, "INFO") + self.assertEqual(c_msg.msg, "test_message_text") + + def test_convert_completed_message(self): + outputs = { + 'result1': { + "publish_layer_name": None, + "description": "WPS Resource Plain Text", + "title": None, + "output_mime_type": None, + "publish_default_style": None, + "publish_as_layer": None, + "type": "string", + "publish_target_workspace": None + } + } + # CompletedMessage + r_msg = CompletedMessage(self.from_obj_mock, "test_base_url", outputs) + c_msg = self.xmpp_bus.Convert(r_msg) + self.assertIsInstance(c_msg, XMPPCompletedMessage) + self.assertEqual(c_msg.originator, self.from_obj_mock) + self.assertEqual(c_msg._base_url, "test_base_url") + self.assertEqual(c_msg._outputs, outputs) + + def test_finish_message(self): + rooms = { + self.xmpp_bus._get_MUC_JId(): { + "resource": { + "role": "not_visitor" + } + } + } + self.xep_0045_plugin.rooms = rooms + self.from_obj_mock.resource = "resource" + msg = { + "type": "normal", + "body": "topic=finish", + "from": self.from_obj_mock + } + # FinishMessage + message = self.xmpp_bus.CreateIndipendentMessage(msg) + self.assertIsInstance(message, FinishMessage) + + def test_convert_error_message(self): + # ErrorMessage + r_msg = ErrorMessage(self.from_obj_mock, "test_error_message_text") + c_msg = self.xmpp_bus.Convert(r_msg) + self.assertIsInstance(c_msg, XMPPErrorMessage) + self.assertEqual(c_msg.originator, self.from_obj_mock) + self.assertEqual(c_msg.xmppChannel, self.xmpp_bus) + self.assertEqual(c_msg.id, self.xmpp_bus.id) + self.assertEqual(c_msg.msg, "test_error_message_text") + + def test_abort_message(self): + rooms = { + self.xmpp_bus._get_MUC_JId(): { + "resource": { + "role": "not_visitor" + } + } + } + self.xep_0045_plugin.rooms = rooms + self.from_obj_mock.resource = "resource" + msg = { + "type": "normal", + "body": "topic=abort", + "from": self.from_obj_mock + } + # AbortMessage + message = self.xmpp_bus.CreateIndipendentMessage(msg) + self.assertIsInstance(message, AbortMessage) + + def test_get_load_average_message(self): + rooms = { + self.xmpp_bus._get_MUC_JId(): { + "resource": { + "role": "not_visitor" + } + } + } + self.xep_0045_plugin.rooms = rooms + self.from_obj_mock.resource = "resource" + msg = { + "type": "normal", + "body": "topic=getloadavg", + "from": self.from_obj_mock + } + # GetLoadAverageMessage + message = self.xmpp_bus.CreateIndipendentMessage(msg) + self.assertIsInstance(message, GetLoadAverageMessage) + self.assertEqual(message.originator(), self.from_obj_mock) + + def test_covert_load_average_message(self): + outputs = { + 'result1': { + "publish_layer_name": None, + "description": "WPS Resource Plain Text", + "title": None, + "output_mime_type": None, + "publish_default_style": None, + "publish_as_layer": None, + "type": "string", + "publish_target_workspace": None + } + } + # LoadAverageMessage + r_msg = LoadAverageMessage(self.from_obj_mock, outputs) + c_msg = self.xmpp_bus.Convert(r_msg) + self.assertIsInstance(c_msg, XMPPLoadAverageMessage) + self.assertEqual(c_msg.originator, self.from_obj_mock) + self.assertEqual(c_msg._outputs, outputs) + + def test_clone_for_process(self): + clone = self.xmpp_bus.clone_for_process("not_master") + self.assertEqual(clone.config, self.xmpp_bus.config) + self.assertEqual(clone.address, self.xmpp_bus.address) + self.assertEqual(clone.domain, self.xmpp_bus.domain) + self.assertEqual(clone.MUC_name, self.xmpp_bus.MUC_name) + self.assertEqual(clone.username, self.xmpp_bus.username) + self.assertEqual(clone.password, self.xmpp_bus.password) + self.assertEqual(clone.nameSpacePassword, self.xmpp_bus.nameSpacePassword) + self.assertEqual(clone._service_name, self.xmpp_bus._service_name) + self.assertEqual(clone._service_name_namespace, self.xmpp_bus._service_name_namespace) + self.assertEqual( + clone._fully_qualified_service_name, + self.xmpp_bus._fully_qualified_service_name + ) + + def test_get_MUC_JId(self): + self.assertEqual( + "service_name_namespace@conference.geoserver.org", + self.xmpp_bus._get_MUC_JId() + ) + + def test_get_fully_qualified_service_name(self): + self.assertEqual( + "service_name_namespace.service_name", + self.xmpp_bus.get_fully_qualified_service_name() + ) + + def test_state(self): + self.assertEqual("disconnected", self.xmpp_bus.state()) + + +if __name__ == '__main__': + unittest.main() diff --git a/test/test_computation_job_outputs.py b/test/test_computation_job_outputs.py index 7dbf9c7..af6c5f2 100644 --- a/test/test_computation_job_outputs.py +++ b/test/test_computation_job_outputs.py @@ -140,7 +140,7 @@ def test_string_output_type_from_ini(self): output_section = config.items('Output1') op = OutputParameters.create_from_config({'Output1': output_section}) self.assertEqual(self.result1_DLR, op.as_DLR_protocol()) - self.assertEqual([''], op.get_values()) + self.assertEqual(['test content'], op.get_values()) # OutputFileParameter ofp = op._params['result1'] self.assertIsInstance(ofp, OutputFileParameter) @@ -155,7 +155,7 @@ def test_string_output_type_from_ini(self): self.assertEqual("textual", ofp.get_type()) self.assertIn(ofp.as_json_string(), self.result1_DLR[0][1]) self.assertFalse(ofp.is_publish_as_layer()) - self.assertEqual('', ofp.get_value()) + self.assertEqual('test content', ofp.get_value()) def test_image_geotiff_output_type_from_ini(self): ini_text = '''[Output2] @@ -374,7 +374,7 @@ def test_string_output_type(self): self.assertEqual("textual", ofp.get_type()) self.assertIn(ofp.as_json_string(), self.result1_DLR[0][1]) self.assertFalse(ofp.is_publish_as_layer()) - self.assertEqual('', ofp.get_value()) + self.assertEqual('test content', ofp.get_value()) def test_image_geotiff_output_type(self): rfp = RawFileParameter( diff --git a/test_suite.sh b/test_suite.sh index 1de3e7b..5a8213d 100644 --- a/test_suite.sh +++ b/test_suite.sh @@ -13,3 +13,6 @@ coverage run --source=src test/test_computation_job_outputs.py echo "Running... test_config_parser" coverage run --source=src test/test_config_parser.py +echo "Running... test_bus_independent_messages" +coverage run --source=src test/test_bus_independent_messages.py +