Modify gvnfm-vnflcm code and unit tests
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / vnfs / tests / test_vnf_query.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
16 from django.test import TestCase, Client
17 from rest_framework import status
18
19 from lcm.pub.database.models import NfInstModel, StorageInstModel
20
21
22 class ResourceTest(TestCase):
23     def setUp(self):
24         self.client = Client()
25         self.nf_inst_id = "01"
26         NfInstModel.objects.all().delete()
27         self.test_data_single_vnf = {
28             "vnfInstanceId": "1",
29             "vnfInstanceName": "VNF1",
30             "vnfProvider": None,
31             "instantiatedVnfInfo": {
32                 "vnfState": None,
33                 "extCpInfo": [],
34                 "virtualStorageResourceInfo": [
35                     {
36                         "virtualStorageInstanceId": "s02",
37                         "storageResource": {
38                             "resourceId": "resource01",
39                             "vimId": "vim01"
40                         }
41                     }
42                 ],
43                 "extVirtualLink": [],
44                 "vnfcResourceInfo": [],
45                 "monitoringParameters": {},
46                 "vimInfo": [],
47                 "flavourId": None,
48                 "virtualLinkResourceInfo": [],
49                 "scaleStatus": []
50             },
51             "vnfdVersion": None,
52             "onboardedVnfPkgInfoId": None
53         }
54         self.test_data_multi_vnf = [
55             {
56                 "vnfInstanceId": "1",
57                 "vnfInstanceName": "VNF1",
58                 "vnfProvider": None,
59                 "instantiatedVnfInfo": {
60                     "vnfState": None,
61                     "extCpInfo": [],
62                     "virtualStorageResourceInfo": [
63                         {
64                             "virtualStorageInstanceId": "s01",
65                             "storageResource": {
66                                 "resourceId": "resource01",
67                                 "vimId": "vim01"
68                             }
69                         }
70                     ],
71                     "extVirtualLink": [],
72                     "vnfcResourceInfo": [],
73                     "monitoringParameters": {},
74                     "vimInfo": [],
75                     "flavourId": None,
76                     "virtualLinkResourceInfo": [],
77                     "scaleStatus": []
78                 },
79                 "vnfdVersion": None,
80                 "onboardedVnfPkgInfoId": None
81             },
82             {
83                 "vnfInstanceId": "2",
84                 "vnfInstanceName": "VNF2",
85                 "vnfProvider": None,
86                 "instantiatedVnfInfo": {
87                     "vnfState": None,
88                     "extCpInfo": [],
89                     "virtualStorageResourceInfo": [
90                         {
91                             "virtualStorageInstanceId": "s02",
92                             "storageResource": {
93                                 "resourceId": "resource02",
94                                 "vimId": "vim02"
95                             }
96                         }
97                     ],
98                     "extVirtualLink": [],
99                     "vnfcResourceInfo": [],
100                     "monitoringParameters": {},
101                     "vimInfo": [],
102                     "flavourId": None,
103                     "virtualLinkResourceInfo": [],
104                     "scaleStatus": []
105                 },
106                 "vnfdVersion": None,
107                 "onboardedVnfPkgInfoId": None
108             }
109         ]
110
111     def tearDown(self):
112         pass
113
114     def test_get_vnf(self):
115         vnf_inst_id = "1"
116         NfInstModel(nfinstid=vnf_inst_id, nf_name='VNF1').save()
117         StorageInstModel(storageid='s02', vimid='vim01', resouceid='resource01', insttype=1, instid=vnf_inst_id).save()
118         response = self.client.get("/api/vnflcm/v1/vnf_instances/%s" % vnf_inst_id, format='json')
119         self.assertEqual(response.status_code, status.HTTP_200_OK)
120         self.assertEqual(self.test_data_single_vnf, response.data)
121
122     def test_get_vnfs(self):
123         for i in range(1, 3):
124             NfInstModel(nfinstid='%s' % i, nf_name='VNF%s' % i).save()
125             StorageInstModel(storageid='s0%s' % i, vimid='vim0%s' % i, resouceid='resource0%s' % i,
126                              insttype=1, instid='%s' % i).save()
127         response = self.client.get("/api/vnflcm/v1/vnf_instances", format='json')
128         self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
129         self.assertEqual(self.test_data_multi_vnf, response.data)