Modify vfc-vnflcm license head
[vfc/gvnfm/vnflcm.git] / lcm / lcm / v2 / tests / test_vnf_create.py
1 # Copyright 2018 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
22 from lcm.nf.const import vnfpackage_info
23 from lcm.pub.database.models import NfInstModel, JobStatusModel
24 from lcm.pub.utils import restcall
25 from lcm.pub.utils.timeutil import now_time
26
27
28 class TestNFInstantiate(TestCase):
29     def setUp(self):
30         self.client = APIClient()
31         self.grant_result = {
32             "vim": {
33                 "vimid": 'vimid_1',
34                 "accessinfo": {
35                     "tenant": 'tenantname_1'
36                 }
37             }
38         }
39
40     def tearDown(self):
41         pass
42
43     def assert_job_result(self, job_id, job_progress, job_detail):
44         jobs = JobStatusModel.objects.filter(jobid=job_id,
45                                              progress=job_progress,
46                                              descp=job_detail)
47         self.assertEqual(1, len(jobs))
48
49     def test_create_vnf_identifier_when_vnf_is_exist(self):
50         NfInstModel.objects.create(nfinstid='1111',
51                                    nf_name='vFW_01',
52                                    package_id='222',
53                                    version='',
54                                    vendor='',
55                                    netype='',
56                                    vnfd_model='',
57                                    status='NOT_INSTANTIATED',
58                                    nf_desc='vFW in Nanjing TIC Edge',
59                                    vnfdid='111',
60                                    create_time=now_time())
61         data = {
62             "vnfdId": "111",
63             "vnfInstanceName": "vFW_01",
64             "vnfInstanceDescription": "vFW in Nanjing TIC Edge"
65         }
66         response = self.client.post("/api/vnflcm/v2/vnf_instances", data=data, format='json')
67         self.failUnlessEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code)
68         context = json.loads(response.content)
69         self.assertEqual({'error': 'VNF is already exist.'}, context)
70
71     @mock.patch.object(restcall, 'call_req')
72     def test_create_vnf_identifier(self, mock_call_req):
73         r2_get_vnfpackage_from_catalog = [0, json.JSONEncoder().encode(vnfpackage_info), '200']
74         mock_call_req.side_effect = [r2_get_vnfpackage_from_catalog]
75         data = {
76             "vnfdId": "111",
77             "vnfInstanceName": "vFW_01",
78             "vnfInstanceDescription": "vFW in Nanjing TIC Edge"
79         }
80         response = self.client.post("/api/vnflcm/v2/vnf_instances", data=data, format='json')
81         self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code)
82         context = json.loads(response.content)
83         self.assertEqual(context['vnfInstanceName'], "vFW_01")
84         self.assertTrue(NfInstModel.objects.filter(nfinstid=context['id']).exists())