Add vsphere type
[multicloud/framework.git] / multivimbroker / multivimbroker / tests / test_syscomm.py
1 # Copyright (c) 2017-2018 VMware, Inc.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #       http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS,
9 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
11 import mock
12 import unittest
13
14 from multivimbroker.pub import exceptions
15 from multivimbroker.pub.msapi import extsys
16 from multivimbroker.pub.utils import syscomm
17
18
19 class TestSyscomm(unittest.TestCase):
20
21     def test_getVIMTypes(self):
22         expected_body = {
23             "openstack": ["titanium_cloud", "ocata", "pike", "starlingx"],
24             "vmware": ["4.0"],
25             "vsphere": ["6.5"],
26             "azure": ["1.0"],
27         }
28         ret = syscomm.getVIMTypes()
29         for item in ret:
30             for v in item['versions']:
31                 self.assertIn(v, expected_body[item['vim_type']])
32
33     @mock.patch.object(extsys, "get_vim_by_id")
34     def test_getMultivimDriver(self, mock_get_vim):
35         mock_get_vim.return_value = {
36             "type": "openstack",
37             "version": "ocata"
38         }
39         full_path = "multicloud/v0/openstack_regionone/identity"
40         expect_path = "multicloud-ocata/v0/openstack_regionone/identity"
41         ret_path = syscomm.getMultivimDriver("openstack_regionone", full_path)
42         self.assertEqual(expect_path, ret_path)
43
44     def test_findMultivimDriver_no_type(self):
45         vim = {"type": "wrong type"}
46         self.assertRaises(exceptions.NotFound, syscomm.findMultivimDriver, vim)
47
48     def test_findMultivimDriver_no_version(self):
49         vim = {"type": "openstack"}
50         ret = syscomm.findMultivimDriver(vim)
51         self.assertEqual("multicloud-ocata", ret)
52
53     def test_findMultivimDriver_with_version(self):
54         vim = {"type": "openstack", "version": "titanium_cloud"}
55         ret = syscomm.findMultivimDriver(vim)
56         self.assertEqual("multicloud-titaniumcloud", ret)
57
58     def test_originHeaders(self):
59         req = mock.Mock()
60         req.META = {
61             "HTTP_X_AUTH_TOKEN": "token_1",
62             "NOT_STARTSWITH_HTTP": "value_1",
63             "CONTENT_TYPE": "application/json"
64         }
65         expect_headers = {
66             "X-AUTH-TOKEN": "token_1",
67             "CONTENT-TYPE": "application/json"
68         }
69         ret_headers = syscomm.originHeaders(req)
70         self.assertDictEqual(expect_headers, ret_headers)
71
72     def test_getHeadersKeys_no_connection(self):
73         resp = {}
74         self.assertEqual([], syscomm.getHeadersKeys(resp))
75
76     def test_getHeadersKeys_with_connection(self):
77         resp = {
78             "connection": "aa,bb",
79             "keep-alive": "yes",
80             "aa": "vaa",
81             "bb": "vbb",
82             "cc": "vcc"
83         }
84         self.assertEqual(["cc"], syscomm.getHeadersKeys(resp))