diff options
Diffstat (limited to 'components/pm-subscription-handler/tests')
-rw-r--r-- | components/pm-subscription-handler/tests/data/aai_xnfs.json | 209 | ||||
-rw-r--r-- | components/pm-subscription-handler/tests/data/cbs_data.json | 109 | ||||
-rw-r--r-- | components/pm-subscription-handler/tests/test_aai_service.py | 77 | ||||
-rw-r--r-- | components/pm-subscription-handler/tests/test_pmsh_utils.py | 100 | ||||
-rw-r--r-- | components/pm-subscription-handler/tests/test_subscription.py (renamed from components/pm-subscription-handler/tests/test_logging.py) | 27 |
5 files changed, 516 insertions, 6 deletions
diff --git a/components/pm-subscription-handler/tests/data/aai_xnfs.json b/components/pm-subscription-handler/tests/data/aai_xnfs.json new file mode 100644 index 00000000..78fc6548 --- /dev/null +++ b/components/pm-subscription-handler/tests/data/aai_xnfs.json @@ -0,0 +1,209 @@ +{ + "results": [ + { + "id": "200904", + "node-type": "pnf", + "url": "/aai/v16/network/pnfs/pnf/asd", + "properties": { + "pnf-name": "nokia_123", + "pnf-id": "10cc1039-5098-4b77-9ae9-359d74d7bba6", + "equip-type": "val8", + "equip-vendor": "Nokia", + "equip-model": "val6", + "ipaddress-v4-oam": "10.10.10.25", + "orchestration-status": "Inventoried", + "sw-version": "val7", + "in-maint": false, + "serial-number": "6061ZW3", + "ipaddress-v6-oam": "2001:0db8:0:0:0:0:1428:57ab", + "resource-version": "1572609320819", + "nf-role": "gNB" + } + }, + { + "id": "209096", + "node-type": "pnf", + "url": "/aai/v16/network/pnfs/pnf/pnf201", + "properties": { + "pnf-name": "pnf201", + "pnf-id": "99700432-df00-473d-8ff9-2fb14243417a", + "equip-type": "val8", + "equip-vendor": "Nokia", + "equip-model": "val6", + "ipaddress-v4-oam": "10.10.10.32", + "sw-version": "val7", + "in-maint": false, + "serial-number": "6061ZW3", + "ipaddress-v6-oam": "2001:0db8:0:0:0:0:1428:57ab", + "resource-version": "1573053304574", + "nf-role": "gNB" + } + }, + { + "id": "217288", + "node-type": "pnf", + "url": "/aai/v16/network/pnfs/pnf/pnf202", + "properties": { + "pnf-name": "pnf_33_ericsson", + "pnf-id": "1a9dc493-402c-4d13-b5cc-d555c386860c", + "equip-type": "val8", + "equip-vendor": "Nokia", + "equip-model": "val6", + "ipaddress-v4-oam": "10.10.10.66", + "orchestration-status": "Inventoried", + "sw-version": "val7", + "in-maint": false, + "serial-number": "6061ZW3", + "ipaddress-v6-oam": "2001:0db8:0:0:0:0:1428:57ab", + "resource-version": "1573054784925", + "nf-role": "gNB" + } + }, + { + "id": "262144", + "node-type": "pnf", + "url": "/aai/v16/network/pnfs/pnf/pnf5", + "properties": { + "pnf-name": "pnf5", + "pnf-id": "cd7ee211-2595-49b6-b85c-76eab621d4b4", + "equip-type": "val8", + "equip-vendor": "Nokia", + "equip-model": "val6", + "ipaddress-v4-oam": "10.10.13.26", + "sw-version": "val7", + "in-maint": false, + "serial-number": "6061ZW3", + "ipaddress-v6-oam": "2001:0db8:0:0:0:0:1428:57ab", + "resource-version": "1572543742093", + "nf-role": "gNB" + } + }, + { + "id": "270472", + "node-type": "pnf", + "url": "/aai/v16/network/pnfs/pnf/pnf205", + "properties": { + "pnf-name": "eric_23", + "pnf-id": "d82ca5aa-cf05-407a-bc84-0ebf22cd83be", + "equip-type": "val8", + "equip-vendor": "Ericsson", + "equip-model": "val6", + "ipaddress-v4-oam": "10.40.10.26", + "orchestration-status": "Inventoried", + "sw-version": "val7", + "in-maint": false, + "serial-number": "6061ZW3", + "ipaddress-v6-oam": "2001:0db8:0:0:0:0:1428:57ab", + "resource-version": "1573064215776", + "nf-role": "gNB" + } + }, + { + "id": "278664", + "node-type": "pnf", + "url": "/aai/v16/network/pnfs/pnf/pnf300", + "properties": { + "pnf-name": "huawei_22", + "pnf-id": "eabcfaf7-b7f3-45fb-94e7-e6112fb3e8b8", + "equip-type": "val8", + "equip-vendor": "Huawei", + "equip-model": "val6", + "ipaddress-v4-oam": "30.10.10.26", + "orchestration-status": "Inventoried", + "sw-version": "val7", + "in-maint": false, + "serial-number": "6061ZW3", + "ipaddress-v6-oam": "2001:0db8:0:0:0:0:1428:57ab", + "resource-version": "1573127626866", + "nf-role": "gNB" + } + }, + { + "id": "667696", + "node-type": "pnf", + "url": "/aai/v16/network/pnfs/pnf/pnf303", + "properties": { + "pnf-name": "pnf_1111", + "pnf-id": "5cd35ce5-48a3-47c2-a5ab-787bfa5e3027", + "equip-type": "val8", + "equip-vendor": "Ericsson", + "equip-model": "val6", + "ipaddress-v4-oam": "10.60.10.26", + "sw-version": "val7", + "in-maint": false, + "serial-number": "6061ZW3", + "ipaddress-v6-oam": "2001:0db8:0:0:0:0:1428:57ab", + "resource-version": "1573138552022", + "nf-role": "gNB" + } + }, + { + "id": "671792", + "node-type": "pnf", + "url": "/aai/v16/network/pnfs/pnf/my_pnf_is_good", + "properties": { + "pnf-name": "my_pnf_is_bad", + "pnf-id": "4f043f83-3ddf-49d0-a878-ae6b2f9bd7e5", + "in-maint": false, + "resource-version": "1574701401896" + } + }, + { + "id": "241864", + "node-type": "generic-vnf", + "url": "/aai/v16/network/generic-vnfs/generic-vnf/1083ecbb-f3b3-49da-b5f5-b5962140ee99", + "properties": { + "vnf-id": "1083ecbb-f3b3-49da-b5f5-b5962140ee99", + "vnf-name": "vnf-eric-1", + "vnf-type": "demoVCPEvBNG/vCPE_vbng 5eb5661a-0cb6 0", + "service-id": "2db01c96-4baa-4393-8d79-af8d7bf4698e", + "prov-status": "PREPROV", + "orchestration-status": "Inventoried", + "in-maint": false, + "is-closed-loop-disabled": false, + "resource-version": "1575541662410", + "model-invariant-id": "7129e420-d396-4efb-af02-6b83499b12f8", + "model-version-id": "e80a6ae3-cafd-4d24-850d-e14c084a5ca9", + "model-customization-id": "376dbe87-e9c5-4f2b-80e2-a420b373ee87" + } + }, + { + "id": "241864", + "node-type": "generic-vnf", + "url": "/aai/v16/network/generic-vnfs/generic-vnf/1083ecbb-f3b3-49da-b5f5-b5962140ee99", + "properties": { + "vnf-id": "1083ecbb-f3b3-49da-b5f5-b5962140ee98", + "vnf-name": "vnf-nokia-22", + "vnf-type": "demoVCPEvBNG/vCPE_vbng 5eb5661a-0cb6 0", + "service-id": "2db01c96-4baa-4393-8d79-af8d7bf4698e", + "prov-status": "PREPROV", + "orchestration-status": "Inventoried", + "in-maint": false, + "is-closed-loop-disabled": false, + "resource-version": "1575541662410", + "model-invariant-id": "7129e420-d396-4efb-af02-6b83499b12f8", + "model-version-id": "e80a6ae3-cafd-4d24-850d-e14c084a5ca9", + "model-customization-id": "376dbe87-e9c5-4f2b-80e2-a420b373ee87" + } + }, + { + "id": "303240", + "node-type": "generic-vnf", + "url": "/aai/v16/network/generic-vnfs/generic-vnf/230f339c-3e75-4f6b-8c9c-da4759da8222", + "properties": { + "vnf-id": "230f339c-3e75-4f6b-8c9c-da4759da8222", + "vnf-name": "vcpe_1", + "vnf-type": "demoVCPEInfra/vCPE_infra 0d1234cf-100c 0", + "service-id": "2db01c96-4baa-4393-8d79-af8d7bf4698e", + "prov-status": "PREPROV", + "orchestration-status": "Inventoried", + "in-maint": false, + "is-closed-loop-disabled": false, + "resource-version": "1575542300539", + "model-invariant-id": "c6aef848-e119-4572-9643-0b68da217358", + "model-version-id": "77c1a3d9-422a-4f78-bd8f-f7a357685b25", + "model-customization-id": "1f6a305d-74b6-4b92-b407-cd03284b6432" + } + } + ] +}
\ No newline at end of file diff --git a/components/pm-subscription-handler/tests/data/cbs_data.json b/components/pm-subscription-handler/tests/data/cbs_data.json new file mode 100644 index 00000000..ccc0626d --- /dev/null +++ b/components/pm-subscription-handler/tests/data/cbs_data.json @@ -0,0 +1,109 @@ +{ + "policy":{ + "subscription":{ + "subscriptionName":"ExtraPM-All-gNB-R2B", + "administrativeState":"UNLOCKED", + "fileBasedGP":15, + "fileLocation":"\/pm\/pm.xml", + "nfTypeModelInvariantId":"2829292", + "nfFilter":{ + "swVersions":[ + "1.0.0", + "1.0.1" + ], + "nfNames":[ + "^pnf.*", + "^vnf.*" + ] + }, + "measurementGroups":[ + { + "measurementGroup":{ + "measurementTypes":[ + { + "measurementType":"countera" + }, + { + "measurementType":"counterb" + } + ], + "managedObjectDNsBasic":[ + { + "DN":"dna" + }, + { + "DN":"dnb" + } + ] + } + }, + { + "measurementGroup":{ + "measurementTypes":[ + { + "measurementType":"counterc" + }, + { + "measurementType":"counterd" + } + ], + "managedObjectDNsBasic":[ + { + "DN":"dnc" + }, + { + "DN":"dnd" + } + ] + } + } + ] + } + }, + "config":{ + "aaf_password":"demo123456!", + "aaf_identity":"dcae@dcae.onap.org", + "cert_path":"/opt/app/pm-mapper/etc/certs/cert.pem", + "key_path":"/opt/app/pm-mapper/etc/certs/key.pem", + "streams_subscribes":{ + "aai_subscriber":{ + "type":"message_router", + "dmaap_info":{ + "topic_url":"https://node:30226/events/AAI_EVENT", + "client_role":"org.onap.dcae.aaiSub", + "location":"san-francisco", + "client_id":"1575976809466" + } + }, + "policy_pm_subscriber":{ + "type":"message_router", + "dmaap_info":{ + "topic_url":"https://node:30226/events/org.onap.dmaap.mr.PM_SUBSCRIPTIONS", + "client_role":"org.onap.dcae.pmSubscriber", + "location":"san-francisco", + "client_id":"1575876809456" + } + } + }, + "streams_publishes":{ + "policy_pm_publisher":{ + "type":"message_router", + "dmaap_info":{ + "topic_url":"https://node:30226/events/org.onap.dmaap.mr.PM_SUBSCRIPTIONS", + "client_role":"org.onap.dcae.pmPublisher", + "location":"san-francisco", + "client_id":"1475976809466" + } + }, + "other_publisher":{ + "type":"message_router", + "dmaap_info":{ + "topic_url":"https://node:30226/events/org.onap.dmaap.mr.SOME_OTHER_TOPIC", + "client_role":"org.onap.dcae.pmControlPub", + "location":"san-francisco", + "client_id":"1875976809466" + } + } + } + } +}
\ No newline at end of file diff --git a/components/pm-subscription-handler/tests/test_aai_service.py b/components/pm-subscription-handler/tests/test_aai_service.py new file mode 100644 index 00000000..7b71d3e8 --- /dev/null +++ b/components/pm-subscription-handler/tests/test_aai_service.py @@ -0,0 +1,77 @@ +# ============LICENSE_START=================================================== +# Copyright (C) 2019 Nordix Foundation. +# ============================================================================ +# Licensed under the Apache License, Version 2.0 (the 'License'); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an 'AS IS' BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# ============LICENSE_END===================================================== +import json +import os +import unittest +from test.support import EnvironmentVarGuard +from unittest import mock + +import responses +from requests import Session + +import mod.aai_client as aai_client + + +class AaiClientTestCase(unittest.TestCase): + + def setUp(self): + self.env = EnvironmentVarGuard() + self.env.set('AAI_SERVICE_HOST', '1.2.3.4') + self.env.set('AAI_SERVICE_PORT_AAI_SSL', '8443') + with open(os.path.join(os.path.dirname(__file__), 'data/cbs_data.json'), 'r') as data: + self.cbs_data = json.load(data) + with open(os.path.join(os.path.dirname(__file__), 'data/aai_xnfs.json'), 'r') as data: + self.aai_response_data = data.read() + + @mock.patch.object(Session, 'put') + def test_aai_client_get_pm_sub_data_success(self, mock_session): + mock_session.return_value.status_code = 200 + mock_session.return_value.text = self.aai_response_data + sub, xnfs = aai_client.get_pmsh_subscription_data(self.cbs_data) + self.assertEqual(sub.subscriptionName, 'ExtraPM-All-gNB-R2B') + self.assertEqual(sub.administrativeState, 'UNLOCKED') + self.assertEqual(len(xnfs), 6) + + @mock.patch.object(Session, 'put') + def test_aai_client_get_pm_sub_data_fail(self, mock_session): + mock_session.return_value.status_code = 404 + with mock.patch('aai_client._get_all_aai_xnf_data', return_value=None): + with self.assertRaises(RuntimeError): + aai_client.get_pmsh_subscription_data(self.cbs_data) + + @responses.activate + def test_aai_client_get_all_aai_xnf_data_not_found(self): + responses.add(responses.PUT, + 'https://1.2.3.4:8443/aai/v16/query?format=simple&nodesOnly=true', + json={'error': 'not found'}, status=404) + self.assertIsNone(aai_client._get_all_aai_xnf_data()) + + @responses.activate + def test_aai_client_get_all_aai_xnf_data_success(self): + responses.add(responses.PUT, + 'https://1.2.3.4:8443/aai/v16/query?format=simple&nodesOnly=true', + json={'dummy_data': 'blah_blah'}, status=200) + self.assertIsNotNone(aai_client._get_all_aai_xnf_data()) + + def test_aai_client_get_aai_service_url_fail(self): + self.env.clear() + with self.assertRaises(KeyError): + aai_client._get_aai_service_url() + + def test_aai_client_get_aai_service_url_succses(self): + self.assertEqual('https://1.2.3.4:8443', aai_client._get_aai_service_url()) diff --git a/components/pm-subscription-handler/tests/test_pmsh_utils.py b/components/pm-subscription-handler/tests/test_pmsh_utils.py new file mode 100644 index 00000000..ee79d523 --- /dev/null +++ b/components/pm-subscription-handler/tests/test_pmsh_utils.py @@ -0,0 +1,100 @@ +# ============LICENSE_START=================================================== +# Copyright (C) 2019-2020 Nordix Foundation. +# ============================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# ============LICENSE_END===================================================== +import json +import os +import unittest +from unittest import mock +from unittest.mock import patch + +import responses +from requests import Session + +from mod.pmsh_utils import AppConfig +from mod.subscription import Subscription + + +class PmshUtilsTestCase(unittest.TestCase): + + def setUp(self): + with open(os.path.join(os.path.dirname(__file__), 'data/cbs_data.json'), 'r') as data: + self.cbs_data = json.load(data) + self.app_conf = AppConfig(**self.cbs_data['config']) + self.sub = Subscription(**self.cbs_data['policy']['subscription']) + + def test_utils_get_mr_sub(self): + mr_policy_sub = self.app_conf.get_mr_sub('policy_pm_subscriber') + self.assertTrue(mr_policy_sub.aaf_id, 'dcae@dcae.onap.org') + + def test_utils_get_mr_sub_fails_with_invalid_name(self): + with self.assertRaises(KeyError): + self.app_conf.get_mr_sub('invalid_sub') + + def test_utils_get_mr_pub(self): + mr_policy_pub = self.app_conf.get_mr_pub('policy_pm_publisher') + self.assertTrue(mr_policy_pub.aaf_pass, 'demo123456!') + + def test_utils_get_mr_pub_fails_with_invalid_name(self): + with self.assertRaises(KeyError): + self.app_conf.get_mr_pub('invalid_pub') + + def test_utils_get_cert_data(self): + self.assertTrue(self.app_conf.cert_params, ('/opt/app/pm-mapper/etc/certs/cert.pem', + '/opt/app/pm-mapper/etc/certs/key.pem')) + + @mock.patch.object(Session, 'post') + def test_mr_pub_publish_to_topic_success(self, mock_session): + mock_session.return_value.status_code = 200 + mr_policy_pub = self.app_conf.get_mr_pub('policy_pm_publisher') + with patch('requests.Session.post') as session_post_call: + mr_policy_pub.publish_to_topic({"dummy_val": "43c4ee19-6b8d-4279-a80f-c507850aae47"}) + session_post_call.assert_called_once() + + @responses.activate + def test_mr_pub_publish_to_topic_fail(self): + responses.add(responses.POST, + 'https://node:30226/events/org.onap.dmaap.mr.PM_SUBSCRIPTIONS', + json={'error': 'Client Error'}, status=400) + mr_policy_pub = self.app_conf.get_mr_pub('policy_pm_publisher') + with self.assertRaises(Exception): + mr_policy_pub.publish_to_topic({"dummy_val": "43c4ee19-6b8d-4279-a80f-c507850aae47"}) + + def test_mr_pub_publish_sub_event_data_success(self): + mr_policy_pub = self.app_conf.get_mr_pub('policy_pm_publisher') + with patch('mod.pmsh_utils._MrPub.publish_to_topic') as pub_to_topic_call: + mr_policy_pub.publish_subscription_event_data(self.sub, 'pnf201') + pub_to_topic_call.assert_called_once() + + @responses.activate + def test_mr_sub_get_from_topic_success(self): + responses.add(responses.GET, + 'https://node:30226/events/org.onap.dmaap.mr.PM_SUBSCRIPTIONS/' + 'dcae_pmsh_cg/1?timeout=1000', + json={"dummy_val": "43c4ee19-6b8d-4279-a80f-c507850aae47"}, status=200) + mr_policy_sub = self.app_conf.get_mr_sub('policy_pm_subscriber') + mr_topic_data = mr_policy_sub.get_from_topic(1) + self.assertIsNotNone(mr_topic_data) + + @responses.activate + def test_mr_sub_get_from_topic_fail(self): + responses.add(responses.GET, + 'https://node:30226/events/org.onap.dmaap.mr.PM_SUBSCRIPTIONS/' + 'dcae_pmsh_cg/1?timeout=1000', + json={"dummy_val": "43c4ee19-6b8d-4279-a80f-c507850aae47"}, status=400) + mr_policy_sub = self.app_conf.get_mr_sub('policy_pm_subscriber') + mr_topic_data = mr_policy_sub.get_from_topic(1) + self.assertIsNone(mr_topic_data) diff --git a/components/pm-subscription-handler/tests/test_logging.py b/components/pm-subscription-handler/tests/test_subscription.py index 790aa40c..cbf930fd 100644 --- a/components/pm-subscription-handler/tests/test_logging.py +++ b/components/pm-subscription-handler/tests/test_subscription.py @@ -15,14 +15,29 @@ # # SPDX-License-Identifier: Apache-2.0 # ============LICENSE_END===================================================== - +import json +import os import unittest +from mod.subscription import Subscription, XnfFilter + + +class SubscriptionTestCase(unittest.TestCase): + + def setUp(self): + with open(os.path.join(os.path.dirname(__file__), 'data/cbs_data.json'), 'r') as data: + self.cbs_data = json.load(data) + self.sub = Subscription(**self.cbs_data['policy']['subscription']) + self.xnf_filter = XnfFilter(**self.sub.nfFilter) + + def test_xnf_filter_true(self): + self.assertTrue(self.xnf_filter.is_xnf_in_filter('pnf1')) -class MyTestCase(unittest.TestCase): - def test_something(self): - self.assertEqual(True, True) + def test_xnf_filter_false(self): + self.assertFalse(self.xnf_filter.is_xnf_in_filter('PNF-33')) + def test_sub_measurement_group(self): + self.assertEqual(len(self.sub.measurementGroups), 2) -if __name__ == '__main__': - unittest.main() + def test_sub_file_location(self): + self.assertEqual(self.sub.fileLocation, '/pm/pm.xml') |