490a99946614e4529773f448b57d05c7f57b7f7d
[vfc/nfvo/lcm.git] / lcm / ns_sfcs / tests / test_create_port_pair_group.py
1 # Copyright 2016 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 import mock
15 import json
16 from test_data import nsd_model, vnfd_model_dict1, vnfd_model_dict2
17 from rest_framework import status
18 from lcm.pub.utils import restcall
19 from lcm.pub.database.models import FPInstModel, NfInstModel
20 from django.test import Client
21 from django.test import TestCase
22
23
24 class TestSfc(TestCase):
25     def setUp(self):
26         self.client = Client()
27         FPInstModel.objects.all().delete()
28         NfInstModel.objects.all().delete()
29         NfInstModel(
30             nfinstid="vnf_inst_1",
31             ns_inst_id="ns_inst_1",
32             vnf_id="vnf_1",
33             vnfd_model=json.dumps(vnfd_model_dict1)).save()
34         NfInstModel(
35             nfinstid="vnf_inst_2",
36             vnf_id="vnf_2",
37             ns_inst_id="ns_inst_1",
38             vnfd_model=json.dumps(vnfd_model_dict2)).save()
39         FPInstModel(
40             fpid="fpd_1",
41             fpinstid="fp_inst_1",
42             nsinstid="ns_inst_1",
43             vnffginstid="vnffg_inst_1",
44             policyinfo=[{
45                 "type": "ACL",
46                 "criteria": {
47                     "dest_port_range": [80, 1024],
48                     "source_port_range": [80, 1024],
49                     "ip_protocol": "tcp",
50                     "dest_ip_range": ["192.168.1.2", "192.168.1.100"],
51                     "source_ip_range": ["192.168.1.2", "192.168.1.100"],
52                     "dscp": 100,
53                 }
54             }],
55             status="enabled",
56             sdncontrollerid="sdn_controller_1"
57         ).save()
58
59     def tearDown(self):
60         FPInstModel.objects.all().delete()
61         NfInstModel.objects.all().delete()
62
63     @mock.patch.object(restcall, 'call_req')
64     def test_create_port_pair_group_success(self, mock_call_req):
65         data = {
66             "nsinstanceid": "ns_inst_1",
67             "fpinstid": "fp_inst_1",
68             "context": json.dumps(nsd_model)
69         }
70         mock_vals = {
71             "/external-system/esr-thirdparty-sdnc-list/esr-thirdparty-sdnc/sdn_controller_1?depth=all":
72                 [0, json.JSONEncoder().encode({
73                     "thirdparty-sdnc-id": "1",
74                     "esr-system-info-list": {
75                         "esr-system-info": [{
76                             "service-url": "url_1",
77                             "thirdparty-sdnc-id": "1",
78                             "user-name": "aa",
79                             "password": "123",
80                             "vendor": "zte",
81                             "version": "v1.0",
82                             "protocal": "http",
83                             "product-name": "bbb",
84                             "type": "11"
85                         }]
86                     }
87                 }), '200'],
88             "/api/ztesdncdriver/v1/createportpair":
89                 [0, json.JSONEncoder().encode({"id": "createportpair_id"}), '200'],
90             "/api/ztesdncdriver/v1/createportpairgroup":
91                 [0, json.JSONEncoder().encode({"id": "createportpairgroup_id"}), '200'],
92             "/api/microservices/v1/services":
93                 [0, None, '200']
94         }
95
96         def side_effect(*args):
97             return mock_vals[args[4]]
98
99         mock_call_req.side_effect = side_effect
100         resp = self.client.post("/api/nslcm/v1/ns/create_port_pair_group", data)
101         rest = json.loads(FPInstModel.objects.get(fpinstid="fp_inst_1").portpairgroups)[0]
102         self.assertEqual(resp.status_code, status.HTTP_200_OK)
103         self.assertEqual("createportpairgroup_id", rest["groupid"])