aboutsummaryrefslogtreecommitdiffstats
path: root/lcm/ns_sfcs/tests/test_sfc_instance.py
blob: f4cf7689a1c4cf573eddfd17929b780e1cb13724 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Copyright 2018 ZTE Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import copy
from rest_framework import status
from django.test import Client
from django.test import TestCase
from lcm.pub.database.models import FPInstModel, VNFFGInstModel
from lcm.ns_sfcs.tests.test_data import nsd_model


class TestSfcInstance(TestCase):
    def setUp(self):
        self.client = Client()
        self.data = {
            "nsInstanceId": "ns_inst_1",
            "context": json.dumps(nsd_model),
            "fpindex": "fpd_1",
            "sdnControllerId": "sdnControllerId_1"
        }
        VNFFGInstModel.objects.all().delete()
        FPInstModel.objects.all().delete()
        VNFFGInstModel(vnffgdid="vnffg_id1", vnffginstid="vnffg_inst_1", nsinstid="ns_inst_1", endpointnumber=2,
                       vllist="vlinst1", cplist="cp1", vnflist="vnf1,vnf2", fplist="fp1").save()

    def tearDown(self):
        VNFFGInstModel.objects.all().delete()
        FPInstModel.objects.all().delete()

    def test_sfc_instance_success(self):
        resp = self.client.post("/api/nslcm/v1/ns/sfc_instance", self.data, format="json")

        self.assertEqual(resp.status_code, status.HTTP_200_OK)
        vnffg = VNFFGInstModel.objects.get(vnffginstid="vnffg_inst_1")
        self.assertEqual(vnffg.fplist, "fp1," + resp.data["fpinstid"])
        ret = FPInstModel.objects.get(fpinstid=resp.data["fpinstid"])
        self.assertIsNotNone(ret)

    def test_sfc_instance_when_request_data_is_not_valid(self):
        data = {}
        resp = self.client.post("/api/nslcm/v1/ns/sfc_instance", data, format="json")
        self.assertEqual(resp.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)

    def test_sfc_instance_when_error_request_data(self):
        data = copy.deepcopy(self.data)
        data.pop("fpindex")
        resp = self.client.post("/api/nslcm/v1/ns/sfc_instance", data, format="json")

        self.assertEqual(resp.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)

    def test_sfc_instance_when_no_fp_model(self):
        data = copy.deepcopy(self.data)
        data["context"] = json.dumps({"fps": []})
        resp = self.client.post("/api/nslcm/v1/ns/sfc_instance", data, format="json")

        self.assertEqual(resp.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)