diff options
author | Fu Jinhua <fu.jinhua@zte.com.cn> | 2019-04-22 08:33:08 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2019-04-22 08:33:08 +0000 |
commit | dff817b2353e54abb28f7807e6e352923b3ac1dd (patch) | |
tree | f9519fcb0ecc8b1387545956bed832b05ad70d91 | |
parent | 90662f422e118d17cbe8714a32747532514922da (diff) | |
parent | d06dae69c60312e3d3352d734f172ab4b3cad71b (diff) |
Merge "move json from code to indepandent file"
-rw-r--r-- | lcm/ns/tests/data/create_ns.json | 10 | ||||
-rw-r--r-- | lcm/ns/tests/data/heal_vnf.json | 14 | ||||
-rw-r--r-- | lcm/ns/tests/test_ns_create.py | 61 | ||||
-rw-r--r-- | lcm/pub/utils/fileutil.py | 13 |
4 files changed, 55 insertions, 43 deletions
diff --git a/lcm/ns/tests/data/create_ns.json b/lcm/ns/tests/data/create_ns.json new file mode 100644 index 00000000..ee723075 --- /dev/null +++ b/lcm/ns/tests/data/create_ns.json @@ -0,0 +1,10 @@ +{ + "context": { + "globalCustomerId": "global-customer-id-test1", + "serviceType": "service-type-test1" + }, + "csarId": "default", + "nsName": "ns", + "nsDescription": "description", + "nsdId": "nsdId" +} diff --git a/lcm/ns/tests/data/heal_vnf.json b/lcm/ns/tests/data/heal_vnf.json new file mode 100644 index 00000000..89d116fb --- /dev/null +++ b/lcm/ns/tests/data/heal_vnf.json @@ -0,0 +1,14 @@ +{ + "healVnfData": [{ + "vnfInstanceId": "default", + "cause": "vm is down", + "additionalParams": { + "action": "restartvm", + "actionvminfo": { + "vmid": "33", + "vduid": "", + "vmname": "xgw-smp11" + } + } + }] +} diff --git a/lcm/ns/tests/test_ns_create.py b/lcm/ns/tests/test_ns_create.py index e32d7e48..a2c363e9 100644 --- a/lcm/ns/tests/test_ns_create.py +++ b/lcm/ns/tests/test_ns_create.py @@ -12,25 +12,26 @@ # See the License for the specific language governing permissions and # limitations under the License. import json +import mock +import os import uuid -import mock -from django.test import TestCase, Client +from django.test import TestCase from rest_framework import status from rest_framework.test import APIClient from lcm.ns.biz.ns_create import CreateNSService from lcm.pub.database.models import NSInstModel -from lcm.pub.exceptions import NSLCMException -from lcm.pub.utils import restcall +from lcm.pub.utils import restcall, fileutil class TestNsInstantiate(TestCase): def setUp(self): - self.client = Client() - self.client1 = APIClient() + self.client = APIClient() self.nsd_id = str(uuid.uuid4()) self.ns_package_id = str(uuid.uuid4()) + self.cur_path = os.path.dirname(os.path.abspath(__file__)) + self.create_ns_json = fileutil.read_json_file(self.cur_path + '/data/create_ns.json') def tearDown(self): NSInstModel.objects.all().delete() @@ -44,41 +45,23 @@ class TestNsInstantiate(TestCase): r1_query_nspackage_from_catalog = [0, json.JSONEncoder().encode(nspackage_info), '201'] r2_create_ns_to_aai = [0, json.JSONEncoder().encode({}), '201'] mock_call_req.side_effect = [r1_query_nspackage_from_catalog, r2_create_ns_to_aai] - data = { - "context": { - "globalCustomerId": "global-customer-id-test1", - "serviceType": "service-type-test1" - }, - "csarId": self.nsd_id, - "nsName": "ns", - "nsDescription": "description", - 'nsdId': 'nsdId' - } - response = self.client1.post("/api/nslcm/v1/ns", data=data, format='json') + self.create_ns_json["csarId"] = self.nsd_id + response = self.client.post("/api/nslcm/v1/ns", data=self.create_ns_json, format='json') self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code) @mock.patch.object(CreateNSService, "do_biz") def test_create_ns_empty_data(self, mock_do_biz): mock_do_biz.side_effect = Exception("Exception in CreateNS.") - data = { - 'nsdId': 'nsdId' - } - response = self.client.post("/api/nslcm/v1/ns", data=data) + response = self.client.post("/api/nslcm/v1/ns", data={}) self.assertEqual(response.data["error"], "Exception in CreateNS.") self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) self.assertIn("error", response.data) @mock.patch.object(CreateNSService, "do_biz") def test_create_ns_non_existing_nsd(self, mock_do_biz): - mock_do_biz.side_effect = NSLCMException("nsd not exists.") - new_nsd_id = '1' - data = { - 'csarId': new_nsd_id, - 'nsName': 'ns', - 'nsDescription': 'description', - 'nsdId': 'nsdId' - } - response = self.client.post("/api/nslcm/v1/ns", data=data) + mock_do_biz.side_effect = Exception("nsd not exists.") + self.create_ns_json["csarId"] = "1" + response = self.client.post("/api/nslcm/v1/ns", data=self.create_ns_json, format='json') self.assertEqual(response.data["error"], "nsd not exists.") self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) self.assertIn("error", response.data) @@ -86,12 +69,8 @@ class TestNsInstantiate(TestCase): @mock.patch.object(restcall, 'call_req') def test_create_ns_when_fail_to_get_nsd(self, mock_call_req): mock_call_req.return_value = [1, "Failed to get nsd.", '500'] - data = { - 'csarId': '1', - 'nsName': 'ns', - 'nsDescription': 'description' - } - response = self.client.post("/api/nslcm/v1/ns", data=data) + self.create_ns_json["csarId"] = "1" + response = self.client.post("/api/nslcm/v1/ns", data=self.create_ns_json, format='json') self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) self.assertIn("error", response.data) @@ -102,12 +81,8 @@ class TestNsInstantiate(TestCase): "packageInfo": {} }) mock_call_req.return_value = [0, nspackage_info, '200'] - NSInstModel(id="1", name="ns1").save() - data = { - 'csarId': '1', - 'nsName': 'ns1', - 'nsDescription': 'description' - } - response = self.client.post("/api/nslcm/v1/ns", data=data) + NSInstModel(id="1", name="ns").save() + self.create_ns_json["csarId"] = "1" + response = self.client.post("/api/nslcm/v1/ns", data=self.create_ns_json, format='json') self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) self.assertIn("error", response.data) diff --git a/lcm/pub/utils/fileutil.py b/lcm/pub/utils/fileutil.py index 87cdfd9b..00f28d81 100644 --- a/lcm/pub/utils/fileutil.py +++ b/lcm/pub/utils/fileutil.py @@ -16,6 +16,7 @@ import shutil import logging import traceback import urllib2 +import json logger = logging.getLogger(__name__) @@ -50,3 +51,15 @@ def download_file_from_http(url, local_dir, file_name): logger.error(traceback.format_exc()) logger.error("Failed to download %s to %s.", url, local_file_name) return is_download_ok, local_file_name + + +def read_json_file(file_path): + if os.path.exists(file_path): + try: + with open(file_path, 'r') as json_file: + data = json_file.read() + return json.loads(data) + except: + logger.error(traceback.format_exc()) + logger.error("Failed to parse json file %s." % file_path) + return None |