import unittest
import json
+import mock
from django.test import Client
from rest_framework import status
from mgr.pub.database.models import VnfRegModel
+from mgr.pub.utils import restcall
class VnfRegTest(unittest.TestCase):
def setUp(self):
"username": "admin1",
"password": "admin1234"
}
+ self.vnfconfig = {
+ "vnfInstanceId": "1",
+ "vnfConfigurationData": {
+ "cp": [
+ {
+ "cpId": "cp-1",
+ "cpdId": "cpd-a",
+ "cpAddress": [
+ {
+ "addresses": [
+ {
+ "addressType": "MAC",
+ "l2AddressData": "00:f3:43:20:a2:a3"
+ },
+ {
+ "addressType": "IP",
+ "l3AddressData": {
+ "iPAddressType": "IPv4",
+ "iPAddress": "192.168.104.2"
+ }
+ }
+ ],
+ "useDynamicAddress": "FALSE"
+ }
+ ]
+ }
+ ],
+ "vnfSpecificData": {
+ "autoScalable": "FALSE",
+ "autoHealable": "FALSE"
+ }
+ },
+ "vnfcConfigurationData": {
+ "vnfcId": "vnfc-1",
+ "cp": [
+ {
+ "cpId": "cp-11",
+ "cpdId": "cpd-1a",
+ "cpAddress": [
+ {
+ "addresses": [
+ {
+ "addressType": "MAC",
+ "l2AddressData": "00:f3:43:21:a2:a3"
+ },
+ {
+ "addressType": "IP",
+ "l3AddressData": {
+ "iPAddressType": "IPv4",
+ "iPAddress": "192.168.105.2"
+ }
+ }
+ ],
+ "useDynamicAddress": "FALSE"
+ }
+ ]
+ }
+ ],
+ "vnfcSpecificData": {}
+ }
+ }
def tearDown(self):
pass
self.assertEqual(status.HTTP_404_NOT_FOUND, response.status_code, response.content)
self.assertEqual({'error': "Vnf(1) does not exist."}, json.loads(response.content))
- def test_url(self):
- pass
- #resp_data = json.loads(response.content)
- #self.assertEqual({"status": "active"}, resp_data)
+ @mock.patch.object(restcall, 'call_req')
+ def test_vnf_config_normal(self, mock_call_req):
+ mock_call_req.return_value = [0, "", '204']
+ self.client.post("/openoapi/vnfmgr/v1/vnfs", self.vnfInst1, format='json')
+ response = self.client.post("/openoapi/vnfmgr/v1/configuration", self.vnfconfig, format='json')
+ self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.content)
+
+ def test_vnf_config_when_not_exist(self):
+ response = self.client.post("/openoapi/vnfmgr/v1/configuration", self.vnfconfig, format='json')
+ self.assertEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code, response.content)
+ self.assertEqual({'error': "Vnf(1) does not exist."}, json.loads(response.content))
# limitations under the License.
import logging
+import json
from rest_framework import status
from rest_framework.decorators import api_view
from mgr.pub.utils.values import ignore_case_get
from mgr.pub.utils.syscomm import fun_name
from mgr.pub.database.models import VnfRegModel
+from mgr.pub.utils import restcall
logger = logging.getLogger(__name__)
@api_view(http_method_names=['POST'])
def vnf_config(request, *args, **kwargs):
logger.info("Enter %s, data is %s", fun_name(), request.data)
+ vnf_inst_id = ignore_case_get(request.data, "vnfInstanceId")
+ try:
+ vnf = VnfRegModel.objects.filter(id=vnf_inst_id)
+ if not vnf:
+ raise Exception("Vnf(%s) does not exist." % vnf_inst_id)
+ ret = restcall.call_req(
+ base_url="http://%s:%s/" % (vnf[0].ip, vnf[0].port),
+ user=vnf[0].username,
+ passwd=vnf[0].password,
+ auth_type=restcall.rest_no_auth,
+ resource="v1/vnfconfig",
+ method="POST",
+ content=json.dumps(request.data))
+ if ret[0] != 0:
+ raise Exception("Failed to config Vnf(%s): %s" % (vnf_inst_id, ret[1]))
+ except Exception as e:
+ return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+ return Response(data={}, status=status.HTTP_202_ACCEPTED)
\ No newline at end of file