From 0d794019fbdb3049dccb3cc71652735edfdb3fad Mon Sep 17 00:00:00 2001 From: Ethan Lynn Date: Wed, 19 Sep 2018 11:20:42 +0800 Subject: Add UT for infra_workload Add unit tests for infra_workload API Change-Id: I839ce04eb17302402d8296baf1b6bf73a6462b1d Issue-ID: MULTICLOUD-357 Signed-off-by: Ethan Lynn --- vio/vio/swagger/views/workload/views.py | 9 ++-- vio/vio/tests/test_infraworkload_view.py | 74 ++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 vio/vio/tests/test_infraworkload_view.py diff --git a/vio/vio/swagger/views/workload/views.py b/vio/vio/swagger/views/workload/views.py index 84b4057..17b5223 100644 --- a/vio/vio/swagger/views/workload/views.py +++ b/vio/vio/swagger/views/workload/views.py @@ -37,12 +37,11 @@ def handle_directives(body): for attr in dd["attributes"]: params[attr[ "attribute_name"]] = attr["attribute_value"] + for k, v in params.items(): + if k in stack_params_keys: + stack_params[k] = v except KeyError as ex: logger.debug("Error handle directives: %s" % str(ex)) - return {} - for k, v in params.items(): - if k in stack_params_keys: - stack_params[k] = v return body @@ -117,7 +116,7 @@ class GetDelStackViewV1(APIView): try: stack_op = OperateStack.OperateStack(vim_info) stack_op.delete_vim_stack(workload_id) - return Response(status=status.HTTP_202_ACCEPTED) + return Response(status=status.HTTP_204_NO_CONTENT) except Exception as e: if hasattr(e, "http_status"): return Response(data={'error': str(e)}, status=e.http_status) diff --git a/vio/vio/tests/test_infraworkload_view.py b/vio/vio/tests/test_infraworkload_view.py new file mode 100644 index 0000000..74d6951 --- /dev/null +++ b/vio/vio/tests/test_infraworkload_view.py @@ -0,0 +1,74 @@ +# Copyright (c) 2018 VMware, Inc. +# +# 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. + +import mock +import unittest +import json + +from vio.pub.msapi import extsys +from vio.swagger.views.workload import views +from vio.pub.vim.vimapi.heat import OperateStack + + +class TestGetDeleteStackView(unittest.TestCase): + + def setUp(self): + self.view = views.GetDelStackViewV1() + + @mock.patch.object(OperateStack.OperateStack, "get_vim_stack") + @mock.patch.object(extsys, "get_vim_by_id") + def test_get(self, mock_getvim, mock_getstk): + mock_getvim.return_value = {} + stk = mock.Mock(id="stack-id", status="CREATE_COMPLETE") + # stk.to_dict.return_value = { + # "id": "stack-id" + # } + mock_getstk.return_value = stk + resp = self.view.get(mock.Mock(), "vmware", "nova", "stack1") + self.assertEqual(200, resp.status_code) + self.assertEqual("stack-id", resp.data.get('workload_id')) + self.assertEqual("CREATE_COMPLETE", resp.data.get('workload_status')) + + @mock.patch.object(OperateStack.OperateStack, "delete_vim_stack") + @mock.patch.object(extsys, "get_vim_by_id") + def test_delete(self, mock_getvim, mock_delstk): + mock_getvim.return_value = {} + resp = self.view.delete( + mock.Mock(), "vmware", "nova", "stack1") + self.assertEqual(204, resp.status_code) + mock_delstk.assert_called_once() + + +class TestCreateStackView(unittest.TestCase): + + def setUp(self): + self.view = views.CreateStackViewV1() + + @mock.patch.object(OperateStack.OperateStack, "create_vim_stack") + @mock.patch.object(extsys, "get_vim_by_id") + def test_post(self, mock_getvim, mock_createstk): + mock_getvim.return_value = { + "tenant": "tenant-id" + } + stk = mock.Mock() + stk.id = "stack-id" + mock_createstk.return_value = stk + req = mock.Mock() + req.body = json.dumps({ + "template_type": "heat", + "template_data": { + "parameters": {} + } + }) + resp = self.view.post(req, "vmware", "nova") + self.assertEqual(201, resp.status_code) + self.assertEqual("stack-id", resp.data["workload_id"]) -- cgit 1.2.3-korg