summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/adapters/test_message_router.py44
-rw-r--r--test/mainapp/test_osdfapp.py57
-rw-r--r--test/test_api_validation.py29
-rw-r--r--test/test_process_placement_opt.py38
4 files changed, 168 insertions, 0 deletions
diff --git a/test/adapters/test_message_router.py b/test/adapters/test_message_router.py
new file mode 100644
index 0000000..2a02dc8
--- /dev/null
+++ b/test/adapters/test_message_router.py
@@ -0,0 +1,44 @@
+import osdf.adapters.dcae.message_router as MR
+import unittest
+
+from osdf.operation.exceptions import MessageBusConfigurationException
+from unittest.mock import patch
+
+
+class TestMessageRouter(unittest.TestCase):
+
+ def test_valid_MR(self):
+ mr = MR.MessageRouterClient(dmaap_url="https://MYHOST:3905")
+
+ def test_valid_MR_with_base_urls(self):
+ base_urls = ["https://MYHOST1:3905/","https://MYHOST2:3905/"]
+ mr = MR.MessageRouterClient(mr_host_base_urls=base_urls, topic="MY-TOPIC")
+
+ def test_invalid_valid_MR_with_base_urls(self):
+ """Topic missing"""
+ base_urls = ["https://MYHOST1:3905/","https://MYHOST2:3905/"]
+ try:
+ mr = MR.MessageRouterClient(mr_host_base_urls=base_urls)
+ except MessageBusConfigurationException:
+ return
+
+ raise Exception("Allows invalid MR configuration") # if it failed to error out
+
+ @patch('osdf.adapters.dcae.message_router.MessageRouterClient.http_request', return_value={})
+ def test_mr_http_request_mocked(self, http_request):
+ mr = MR.MessageRouterClient(dmaap_url="https://MYHOST:3905")
+ mr.http_request = http_request
+ assert mr.get() == {}
+ assert mr.post("Hello") == {}
+
+ def test_mr_http_request_non_existent_host(self):
+ mr = MR.MessageRouterClient(dmaap_url="https://MYHOST:3905")
+ try:
+ mr.get()
+ except:
+ return
+
+ raise Exception("Allows invalid host") # if it failed to error out
+if __name__ == "__main__":
+ unittest.main()
+
diff --git a/test/mainapp/test_osdfapp.py b/test/mainapp/test_osdfapp.py
new file mode 100644
index 0000000..2ffe4f3
--- /dev/null
+++ b/test/mainapp/test_osdfapp.py
@@ -0,0 +1,57 @@
+import osdfapp
+import unittest
+
+from osdf.operation.exceptions import BusinessException
+from requests import Request, RequestException
+from schematics.exceptions import DataError
+from unittest import mock, TestCase
+from unittest.mock import patch
+
+
+class TestOSDFApp(TestCase):
+
+ def setUp(self):
+ self.patcher_g = patch('osdfapp.g', return_value={'request_id':'DUMMY-REQ'})
+ self.Mock_g = self.patcher_g.start()
+ # self.patcher2 = patch('package.module.Class2')
+ # self.MockClass2 = self.patcher2.start()
+
+ def tearDown(self):
+ patch.stopall()
+
+ def dummy_request_exception(self):
+ e = RequestException("Web Request Exception Description")
+ e.response = mock.MagicMock()
+ e.request = Request(method="GET", url="SOME-URL")
+ e.response.status_code = 400
+ e.response.content = "Some request exception occurred"
+ # request().raise_for_status.side_effect = e
+ return e
+
+ def test_handle_business_exception(self):
+ e = BusinessException("Business Exception Description")
+ resp = osdfapp.handle_business_exception(e)
+ assert resp.status_code == 400
+
+ def test_handle_request_exception(self):
+ e = self.dummy_request_exception()
+ resp = osdfapp.handle_request_exception(e)
+ assert resp.status_code == 400
+
+ def test_handle_data_error(self):
+ e = DataError({"A1": "A1 Data Error"})
+ resp = osdfapp.handle_data_error(e)
+ assert resp.status_code == 400
+
+ def test_internal_failure(self):
+ e = Exception("An Internal Error")
+ resp = osdfapp.internal_failure(e)
+ assert resp.status_code == 500
+
+ def test_getOptions_default(self):
+ opts = osdfapp.getOptions(["PROG"]) # ensure nothing breaks
+
+
+if __name__ == "__main__":
+ unittest.main()
+
diff --git a/test/test_api_validation.py b/test/test_api_validation.py
new file mode 100644
index 0000000..5af81e6
--- /dev/null
+++ b/test/test_api_validation.py
@@ -0,0 +1,29 @@
+import json
+import unittest
+
+from osdf.models.api.placementRequest import PlacementAPI
+from osdf.models.api.placementResponse import PlacementResponse
+from schematics.exceptions import ModelValidationError
+
+
+class TestReqValidation(unittest.TestCase):
+
+ def test_req_validation(self):
+ req_file = "./test/placement-tests/request.json"
+ req_json = json.loads(open(req_file).read())
+ self.assertEqual(PlacementAPI(req_json).validate(), None)
+
+ def test_req_failure(self):
+ req_json = {}
+ self.assertRaises(ModelValidationError, lambda: PlacementAPI(req_json).validate())
+
+
+class TestResponseValidation(unittest.TestCase):
+
+ def test_invalid_response(self):
+ resp_json = {}
+ self.assertRaises(ModelValidationError, lambda: PlacementResponse(resp_json).validate())
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/test/test_process_placement_opt.py b/test/test_process_placement_opt.py
new file mode 100644
index 0000000..5d3014b
--- /dev/null
+++ b/test/test_process_placement_opt.py
@@ -0,0 +1,38 @@
+import unittest
+import json
+import yaml
+from osdf.optimizers.placementopt.conductor.remote_opt_processor import process_placement_opt
+from mock import patch
+
+class TestConductorApiBuilder(unittest.TestCase):
+
+ def test_conductor_api_call_builder(self):
+ #main_dir = ".."
+ main_dir = ""
+ conductor_api_template = main_dir + "osdf/templates/conductor_interface.json"
+ parameter_data_file = main_dir + "test/placement-tests/request.json"
+ policy_data_path = main_dir + "test/policy-local-files/"
+ local_config_file = main_dir + "config/common_config.yaml"
+
+ policy_data_files = ["CloudAttributePolicy_vGMuxInfra_1.json",
+ "CloudAttributePolicy_vG_1.json",
+ "DistanceToLocationPolicy_vGMuxInfra_1.json",
+ "DistanceToLocationPolicy_vG_1.json",
+ "InventoryGroup_vGMuxInfra_1.json",
+ "InventoryGroup_vG_1.json",
+ "PlacementOptimizationPolicy.json",
+ "ResourceInstancePolicy_vG_1.json",
+ "VNFPolicy_vGMuxInfra_1.json",
+ "VNFPolicy_vG_1.json",
+ "ZonePolicy_vGMuxInfra_1.json",
+ "ZonePolicy_vG_1.json"]
+ request_json = json.loads(open(parameter_data_file).read())
+ policies = [json.loads(open(policy_data_path + file).read()) for file in policy_data_files]
+ local_config = yaml.load(open(local_config_file))
+ with patch('osdf.optimizers.placementopt.conductor.conductor.request', return_value={"solutionInfo": {"placementInfo": "dummy"}}):
+ templ_string = process_placement_opt(request_json, policies, local_config, [])
+
+
+if __name__ == "__main__":
+ unittest.main()
+