71ba9116c50e1754672463c7901768e071d5c1e2
[multicloud/framework.git] / multivimbroker / multivimbroker / tests / test_v1_check_capacity.py
1 # Copyright (c) 2019 Wind River Systems, Inc.
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
15 import mock
16 import unittest
17
18 from rest_framework import status
19
20 from multivimbroker.forwarder.views import APIv1CheckCapacity
21
22
23 class V1CheckCapacityTest(unittest.TestCase):
24
25     def setUp(self):
26         self.view = APIv1CheckCapacity()
27         super(V1CheckCapacityTest, self).setUp()
28
29     def tearDown(self):
30         pass
31
32     def test_check_capacity_success(self):
33         req = mock.Mock()
34         req.body = """
35         {
36             "vCPU": 1,
37             "Memory": 1,
38             "Storage": 500,
39             "VIMs": [{"cloud-owner": "openstack",
40              "cloud-region-id": "RegionOne"}]
41         }"""
42         req.get_full_path.return_value = ("http://msb.onap.org/api/multicloud"
43                                           "/v1/check_vim_capacity")
44         req.META.items.return_value = [("Project", "projectname1")]
45         with mock.patch.object(self.view, "send") as send:
46             plugin_resp = mock.Mock()
47             plugin_resp.content = """{
48                 "result": true
49             }"""
50             plugin_resp.status_code = str(status.HTTP_200_OK)
51             send.return_value = plugin_resp
52
53             resp = self.view.post(req)
54             expect_body = {
55                 "VIMs": [{"cloud-owner": "openstack",
56                           "cloud-region-id": "RegionOne",
57                           "AZs": []}]
58             }
59             self.assertEqual(status.HTTP_200_OK, resp.status_code)
60             self.assertDictEqual(expect_body, resp.data)
61
62     def test_check_capacity_no_suitable_vim(self):
63         req = mock.Mock()
64         req.body = """
65         {
66             "vCPU": 1,
67             "Memory": 1,
68             "Storage": 500,
69             "VIMs": [{"cloud-owner": "openstack",
70              "cloud-region-id": "RegionOne"}]
71         }"""
72         req.get_full_path.return_value = ("http://msb.onap.org/api/multicloud"
73                                           "/v1/check_vim_capacity")
74         req.META.items.return_value = [("Project", "projectname1")]
75
76         with mock.patch.object(self.view, "send") as send:
77             plugin_resp = mock.Mock()
78             plugin_resp.content = """{
79                 "result": false
80             }"""
81             plugin_resp.status_code = str(status.HTTP_200_OK)
82             send.return_value = plugin_resp
83
84             resp = self.view.post(req)
85             expect_body = {
86                 "VIMs": []
87             }
88             self.assertEqual(status.HTTP_200_OK, resp.status_code)
89             self.assertDictEqual(expect_body, resp.data)
90
91     def test_check_capacity_invalid_input(self):
92         req = mock.Mock()
93         req.body = "hello world"
94         req.get_full_path.return_value = ("http://msb.onap.org/api/multicloud"
95                                           "/v1/check_vim_capacity")
96         req.META.items.return_value = [("Project", "projectname1")]
97         expect_body = {
98             "error": ("Invalidate request body "
99                       "No JSON object could be decoded.")
100         }
101         resp = self.view.post(req)
102         self.assertEqual(status.HTTP_400_BAD_REQUEST, resp.status_code)
103         self.assertDictEqual(expect_body, resp.data)