Add unit test for vnf Grant 19/34419/1
authorfujinhua <fu.jinhua@zte.com.cn>
Wed, 7 Mar 2018 05:41:27 +0000 (13:41 +0800)
committerfujinhua <fu.jinhua@zte.com.cn>
Wed, 7 Mar 2018 05:41:27 +0000 (13:41 +0800)
Change-Id: Ib773e6e57a23ccbcc15d18203c269dda5b8e9ab0
Issue-ID: VFC-779
Signed-off-by: fujinhua <fu.jinhua@zte.com.cn>
lcm/urls.py
lcm/v2/grant_vnf.py
lcm/v2/serializers.py
lcm/v2/tests.py [new file with mode: 0644]

index 97df81e..eb82f08 100644 (file)
@@ -38,6 +38,7 @@ urlpatterns = [
     url(r'^', include('lcm.jobs.urls')),
     url(r'^', include('lcm.workflows.urls')),
     url(r'^', include('lcm.swagger.urls')),
+    url(r'^', include('lcm.v2.urls')),
 ]
 
 # regist to MSB when startup
index 832c781..a3ba37f 100644 (file)
@@ -29,18 +29,7 @@ class GrantVnf(object):
         grant_resp = {
             "id": str(uuid.uuid4()),
             "vnfInstanceId": self.data.get("vnfInstanceId"),
-            "vnfLcmOpOccId": self.data.get("vnfLcmOpOccId"),
-            "_links": {
-                "self": {
-                    "href": "/grants"
-                },
-                "vnfLcmOpOcc": {
-                    "href": self.data.get("_links").get("vnfLcmOpOcc")
-                },
-                "vnfInstance": {
-                    "href": self.data.get("_links").get("vnfInstance")
-                }
-            }
+            "vnfLcmOpOccId": self.data.get("vnfLcmOpOccId")
         }
         logger.debug("grant_resp=%s", grant_resp)
         return grant_resp
index eee7a9c..1ba05a4 100644 (file)
@@ -106,19 +106,20 @@ class PlacementConstraintSerializer(serializers.Serializer):
     )
     resource = ConstraintResourceRefSerializer(
         help_text="References to resources in the constraint rule.",
-        many=True
+        many=True,
+        required=False
     )
 
 
 class VimConstraintSerializer(serializers.Serializer):
     sameResourceGroup = serializers.BooleanField(
         help_text="Set to true when the constraint applies not only to the same VIM connection, but also to the same infrastructure resource group.",
-        required=False,
-        allow_null=True
+        required=False
     )
     resource = ConstraintResourceRefSerializer(
         help_text="References to resources in the constraint rule.",
-        many=True
+        many=True,
+        required=False
     )
 
 
@@ -180,27 +181,33 @@ class GrantRequestSerializer(serializers.Serializer):
     )
     addResources = ResourceDefinitionSerializer(
         help_text="List of resource definitions in the VNFD for resources to be added by the LCM operation.",
-        many=True
+        many=True,
+        required=False
     )
     tempResources = ResourceDefinitionSerializer(
         help_text="List of resource definitions in the VNFD for resources to be temporarily instantiated during the runtime of the LCM operation.",
-        many=True
+        many=True,
+        required=False
     )
     removeResources = ResourceDefinitionSerializer(
         help_text="Provides the definitions of resources to be removed by the LCM operation.",
-        many=True
+        many=True,
+        required=False
     )
     updateResources = ResourceDefinitionSerializer(
         help_text="Provides the definitions of resources to be modified by the LCM operation.",
-        many=True
+        many=True,
+        required=False
     )
     placementConstraints = PlacementConstraintSerializer(
         help_text="Placement constraints that the VNFM may send to the NFVO in order to influence the resource placement decision.",
-        many=True
+        many=True,
+        required=False
     )
     vimConstraints = VimConstraintSerializer(
         help_text="Used by the VNFM to require that multiple resources are managed through the same VIM connection.",
-        many=True
+        many=True,
+        required=False
     )
     additionalParams = serializers.DictField(
         help_text="Additional parameters passed by the VNFM.",
@@ -210,7 +217,7 @@ class GrantRequestSerializer(serializers.Serializer):
     )
     _links = GrantRequestLinksSerializer(
         help_text="Links to resources related to this request.",
-        required=True
+        required=False
     )
 
 
@@ -379,11 +386,13 @@ class VimSoftwareImageSerializer(serializers.Serializer):
 class VimAssetsSerializer(serializers.Serializer):
     computeResourceFlavours = VimComputeResourceFlavourSerializer(
         help_text="Mappings between virtual compute descriptors defined in the VNFD and compute resource flavours managed in the VIM.",
-        many=True
+        many=True,
+        required=False
     )
     softwareImages = VimSoftwareImageSerializer(
         help_text="Mappings between software images defined in the VNFD and software images managed in the VIM.",
-        many=True
+        many=True,
+        required=False
     )
 
 
@@ -436,7 +445,8 @@ class IpOverEthernetAddressDataSerializer(serializers.Serializer):
     )
     ipAddresses = IpAddresseSerializer(
         help_text="List of IP addresses to assign to the CP instance.",
-        many=True
+        many=True,
+        required=False
     )
 
 
@@ -468,7 +478,8 @@ class VnfExtCpConfigSerializer(serializers.Serializer):
     )
     cpProtocolData = CpProtocolDataSerializer(
         help_text="Parameters for configuring the network protocols on the link port that connects the CP to a VL.",
-        many=True
+        many=True,
+        required=False
     )
 
 
@@ -479,7 +490,8 @@ class VnfExtCpDataSerializer(serializers.Serializer):
     )
     cpConfig = VnfExtCpConfigSerializer(
         help_text="List of instance data that need to be configured on the CP instances created from the respective CPD.",
-        many=True
+        many=True,
+        required=False
     )
 
 
@@ -517,11 +529,13 @@ class ExtVirtualLinkDataSerializer(serializers.Serializer):
     )
     extCps = VnfExtCpDataSerializer(
         help_text="External CPs of the VNF to be connected to this external VL.",
-        many=True
+        many=True,
+        required=False
     )
     extLinkPorts = ExtLinkPortDataSerializer(
         help_text="Externally provided link ports to be used to connect external connection points to this external VL.",
-        many=True
+        many=True,
+        required=False
     )
 
 
@@ -583,15 +597,18 @@ class GrantSerializer(serializers.Serializer):
     )
     vimConnections = VimConnectionInfoSerializer(
         help_text="Provides information regarding VIM connections that are approved to be used by the VNFM to allocate resources.",
-        many=True
+        many=True,
+        required=False
     )
     zones = ZoneInfoSerializer(
         help_text="Identifies resource zones where the resources are approved to be allocated by the VNFM.",
-        many=True
+        many=True,
+        required=False
     )
     zoneGroups = ZoneGroupInfoSerializer(
         help_text="Information about groups of resource zones.",
-        many=True
+        many=True,
+        required=False
     )
     computeReservationId = serializers.CharField(
         help_text="Information that identifies a reservation applicable to the compute resource requirements.",
@@ -613,19 +630,23 @@ class GrantSerializer(serializers.Serializer):
     )
     addResources = GrantInfoSerializer(
         help_text="List of resources that are approved to be added.",
-        many=True
+        many=True,
+        required=False
     )
     tempResources = GrantInfoSerializer(
         help_text="List of resources that are approved to be temporarily instantiated during the runtime of the lifecycle operation.",
-        many=True
+        many=True,
+        required=False
     )
     removeResources = GrantInfoSerializer(
         help_text="List of resources that are approved to be removed.",
-        many=True
+        many=True,
+        required=False
     )
     updateResources = GrantInfoSerializer(
         help_text="List of resources that are approved to be modified.",
-        many=True
+        many=True,
+        required=False
     )
     vimAssets = VimAssetsSerializer(
         help_text="Information about assets for the VNF that are managed by the NFVO in the VIM.",
@@ -634,13 +655,15 @@ class GrantSerializer(serializers.Serializer):
     )
     extVirtualLinks = ExtVirtualLinkDataSerializer(
         help_text="Information about external VLs to connect the VNF to.",
-        many=True
+        many=True,
+        required=False
     )
     extManagedVirtualLinks = ExtManagedVirtualLinkDataSerializer(
         help_text="Information about internal VLs that are managed by other entities than the VNFM.",
-        many=True
+        many=True,
+        required=False
     )
     _links = GrantLinksSerializer(
         help_text="Links to resources related to this resource.",
-        required=True
+        required=False
     )
diff --git a/lcm/v2/tests.py b/lcm/v2/tests.py
new file mode 100644 (file)
index 0000000..3de67df
--- /dev/null
@@ -0,0 +1,96 @@
+# 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 unittest
+import json
+from django.test import Client
+from rest_framework import status
+
+
+class VnfGrantViewTest(unittest.TestCase):
+    def setUp(self):
+        self.client = Client()
+
+    def tearDown(self):
+        pass
+
+    def test_grant_vnf_normal(self):
+        data = {
+            "vnfInstanceId": "1",
+            "vnfLcmOpOccId": "2",
+            "vnfdId": "3",
+            "flavourId": "4",
+            "operation": "INSTANTIATE",
+            "isAutomaticInvocation": True,
+            "instantiationLevelId": "5",
+            "addResources": [
+                {
+                    "id": "1",
+                    "type": "COMPUTE",
+                    "vduId": "2",
+                    "resourceTemplateId": "3",
+                    "resource": {
+                        "vimConnectionId": "4",
+                        "resourceProviderId": "5",
+                        "resourceId": "6",
+                        "vimLevelResourceType": "7"
+                    }
+                }
+            ],
+            "placementConstraints": [
+                {
+                    "affinityOrAntiAffinity": "AFFINITY",
+                    "scope": "NFVI_POP",
+                    "resource": [
+                        {
+                            "idType": "RES_MGMT",
+                            "resourceId": "1",
+                            "vimConnectionId": "2",
+                            "resourceProviderId": "3"
+                        }
+                    ]
+                }
+            ],
+            "vimConstraints": [
+                {
+                    "sameResourceGroup": True,
+                    "resource": [
+                        {
+                            "idType": "RES_MGMT",
+                            "resourceId": "1",
+                            "vimConnectionId": "2",
+                            "resourceProviderId": "3"
+                        }
+                    ]
+                }
+            ],
+            "additionalParams": {},
+            "_links": {
+                "vnfLcmOpOcc": {
+                    "href": "1"
+                },
+                "vnfInstance": {
+                    "href": "2"
+                }
+            }
+        }
+        response = self.client.post("/api/nslcm/v2/grants", data=data, format='json')
+        self.assertEqual(status.HTTP_201_CREATED, response.status_code, response.content)
+        resp_data = json.loads(response.content)
+        expect_resp_data = {
+            "id": resp_data.get("id"),
+            "vnfInstanceId": "1",
+            "vnfLcmOpOccId": "2"
+        }
+        self.assertEqual(expect_resp_data, resp_data)