4ded31cebb5d3337b4f168b213fa7ae910c3b777
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / tests / test_create_vnf.py
1 # Copyright 2017 ZTE Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import json
16
17 import mock
18 from django.test import TestCase
19 from rest_framework import status
20 from rest_framework.test import APIClient
21 from lcm.nf.const import vnfpackage_info
22 from lcm.pub.database.models import NfInstModel, JobStatusModel
23 from lcm.pub.utils import restcall
24 from lcm.pub.utils.timeutil import now_time
25 import uuid
26
27
28 class TestNFInstantiate(TestCase):
29     def setUp(self):
30         self.client = APIClient()
31         self.grant_result = {
32             "vimid": 'vimid_1',
33             "tenant": 'tenantname_1'
34         }
35
36     def tearDown(self):
37         pass
38
39     def assert_job_result(self, job_id, job_progress, job_detail):
40         jobs = JobStatusModel.objects.filter(jobid=job_id,
41                                              progress=job_progress,
42                                              descp=job_detail)
43         self.assertEqual(1, len(jobs))
44
45     def test_create_vnf_identifier_when_vnf_is_exist(self):
46         NfInstModel.objects.create(nfinstid='1111',
47                                    nf_name='vFW_01',
48                                    package_id='222',
49                                    version='',
50                                    vendor='',
51                                    netype='',
52                                    vnfd_model='',
53                                    status='NOT_INSTANTIATED',
54                                    nf_desc='vFW in Nanjing TIC Edge',
55                                    vnfdid='111',
56                                    create_time=now_time())
57         data = {
58             "vnfdId": "111",
59             "vnfInstanceName": "vFW_01",
60             "vnfInstanceDescription": "vFW in Nanjing TIC Edge"
61         }
62         response = self.client.post("/api/vnflcm/v1/vnf_instances", data=data, format='json')
63         self.failUnlessEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code)
64         context = json.loads(response.content)
65         self.assertEqual({
66             'detail': 'VNF is already exist.',
67             'status': 500
68         }, context)
69
70     @mock.patch.object(restcall, 'call_req')
71     @mock.patch.object(uuid, 'uuid4')
72     def test_create_vnf_identifier(self, mock_uuid4, mock_call_req):
73         r2_get_vnfpackage_from_catalog = [0, json.JSONEncoder().encode(vnfpackage_info), '200']
74         mock_call_req.return_value = r2_get_vnfpackage_from_catalog
75         mock_uuid4.return_value = "1"
76         data = {
77             "vnfdId": "111",
78             "vnfInstanceName": "vFW_01",
79             "vnfInstanceDescription": "vFW in Nanjing TIC Edge"
80         }
81         response = self.client.post("/api/vnflcm/v1/vnf_instances", data=data, format='json')
82         expect_data = {
83             "id": "1",
84             "vnfProvider": "huawei",
85             "vnfdVersion": "1.0",
86             "vnfPkgId": "111",
87             "instantiationState": "NOT_INSTANTIATED"
88         }
89         self.assertEqual(expect_data, response.data)