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