]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/tests/test_rgw_client.py
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / pybind / mgr / dashboard / tests / test_rgw_client.py
1 # -*- coding: utf-8 -*-
2 # pylint: disable=too-many-public-methods
3 from unittest import TestCase
4 from unittest.mock import Mock, patch
5
6 from ..exceptions import DashboardException
7 from ..services.rgw_client import NoCredentialsException, \
8 NoRgwDaemonsException, RgwClient, _parse_frontend_config
9 from ..settings import Settings
10 from . import KVStoreMockMixin, RgwStub # pylint: disable=no-name-in-module
11
12
13 @patch('dashboard.services.rgw_client.RgwClient._get_user_id', Mock(
14 return_value='dummy_admin'))
15 class RgwClientTest(TestCase, KVStoreMockMixin):
16 def setUp(self):
17 RgwStub.get_daemons()
18 self.mock_kv_store()
19 self.CONFIG_KEY_DICT.update({
20 'RGW_API_ACCESS_KEY': 'klausmustermann',
21 'RGW_API_SECRET_KEY': 'supergeheim',
22 })
23
24 def test_ssl_verify(self):
25 Settings.RGW_API_SSL_VERIFY = True
26 instance = RgwClient.admin_instance()
27 self.assertTrue(instance.session.verify)
28
29 def test_no_ssl_verify(self):
30 Settings.RGW_API_SSL_VERIFY = False
31 instance = RgwClient.admin_instance()
32 self.assertFalse(instance.session.verify)
33
34 def test_no_daemons(self):
35 RgwStub.get_mgr_no_services()
36 with self.assertRaises(NoRgwDaemonsException) as cm:
37 RgwClient.admin_instance()
38 self.assertIn('No RGW service is running.', str(cm.exception))
39
40 def test_no_credentials(self):
41 self.CONFIG_KEY_DICT.update({
42 'RGW_API_ACCESS_KEY': '',
43 'RGW_API_SECRET_KEY': '',
44 })
45 with self.assertRaises(NoCredentialsException) as cm:
46 RgwClient.admin_instance()
47 self.assertIn('No RGW credentials found', str(cm.exception))
48
49 def test_default_daemon_wrong_settings(self):
50 self.CONFIG_KEY_DICT.update({
51 'RGW_API_HOST': '172.20.0.2',
52 'RGW_API_PORT': '7990',
53 })
54 with self.assertRaises(DashboardException) as cm:
55 RgwClient.admin_instance()
56 self.assertIn('No RGW daemon found with user-defined host:', str(cm.exception))
57
58 @patch.object(RgwClient, '_get_daemon_zone_info')
59 def test_get_placement_targets_from_zone(self, zone_info):
60 zone_info.return_value = {
61 'id': 'a0df30ea-4b5b-4830-b143-2bedf684663d',
62 'placement_pools': [
63 {
64 'key': 'default-placement',
65 'val': {
66 'index_pool': 'default.rgw.buckets.index',
67 'storage_classes': {
68 'STANDARD': {
69 'data_pool': 'default.rgw.buckets.data'
70 }
71 }
72 }
73 }
74 ]
75 }
76
77 instance = RgwClient.admin_instance()
78 expected_result = {
79 'zonegroup': 'zonegroup1',
80 'placement_targets': [
81 {
82 'name': 'default-placement',
83 'data_pool': 'default.rgw.buckets.data'
84 }
85 ]
86 }
87 self.assertEqual(expected_result, instance.get_placement_targets())
88
89 @patch.object(RgwClient, '_get_realms_info')
90 def test_get_realms(self, realms_info):
91 realms_info.side_effect = [
92 {
93 'default_info': '51de8373-bc24-4f74-a9b7-8e9ef4cb71f7',
94 'realms': [
95 'realm1',
96 'realm2'
97 ]
98 },
99 {}
100 ]
101 instance = RgwClient.admin_instance()
102
103 self.assertEqual(['realm1', 'realm2'], instance.get_realms())
104 self.assertEqual([], instance.get_realms())
105
106
107 class RgwClientHelperTest(TestCase):
108 def test_parse_frontend_config_1(self):
109 self.assertEqual(_parse_frontend_config('beast port=8000'), (8000, False))
110
111 def test_parse_frontend_config_2(self):
112 self.assertEqual(_parse_frontend_config('beast port=80 port=8000'), (80, False))
113
114 def test_parse_frontend_config_3(self):
115 self.assertEqual(_parse_frontend_config('beast ssl_port=443 port=8000'), (443, True))
116
117 def test_parse_frontend_config_4(self):
118 self.assertEqual(_parse_frontend_config('beast endpoint=192.168.0.100:8000'), (8000, False))
119
120 def test_parse_frontend_config_5(self):
121 self.assertEqual(_parse_frontend_config('beast endpoint=[::1]'), (80, False))
122
123 def test_parse_frontend_config_6(self):
124 self.assertEqual(_parse_frontend_config(
125 'beast ssl_endpoint=192.168.0.100:8443'), (8443, True))
126
127 def test_parse_frontend_config_7(self):
128 self.assertEqual(_parse_frontend_config('beast ssl_endpoint=192.168.0.100'), (443, True))
129
130 def test_parse_frontend_config_8(self):
131 self.assertEqual(_parse_frontend_config(
132 'beast ssl_endpoint=[::1]:8443 endpoint=192.0.2.3:80'), (8443, True))
133
134 def test_parse_frontend_config_9(self):
135 self.assertEqual(_parse_frontend_config(
136 'beast port=8080 endpoint=192.0.2.3:80'), (8080, False))
137
138 def test_parse_frontend_config_10(self):
139 self.assertEqual(_parse_frontend_config(
140 'beast ssl_endpoint=192.0.2.3:8443 port=8080'), (8443, True))
141
142 def test_parse_frontend_config_11(self):
143 self.assertEqual(_parse_frontend_config('civetweb port=8000s'), (8000, True))
144
145 def test_parse_frontend_config_12(self):
146 self.assertEqual(_parse_frontend_config('civetweb port=443s port=8000'), (443, True))
147
148 def test_parse_frontend_config_13(self):
149 self.assertEqual(_parse_frontend_config('civetweb port=192.0.2.3:80'), (80, False))
150
151 def test_parse_frontend_config_14(self):
152 self.assertEqual(_parse_frontend_config('civetweb port=172.5.2.51:8080s'), (8080, True))
153
154 def test_parse_frontend_config_15(self):
155 self.assertEqual(_parse_frontend_config('civetweb port=[::]:8080'), (8080, False))
156
157 def test_parse_frontend_config_16(self):
158 self.assertEqual(_parse_frontend_config('civetweb port=ip6-localhost:80s'), (80, True))
159
160 def test_parse_frontend_config_17(self):
161 self.assertEqual(_parse_frontend_config('civetweb port=[2001:0db8::1234]:80'), (80, False))
162
163 def test_parse_frontend_config_18(self):
164 self.assertEqual(_parse_frontend_config('civetweb port=[::1]:8443s'), (8443, True))
165
166 def test_parse_frontend_config_19(self):
167 self.assertEqual(_parse_frontend_config('civetweb port=127.0.0.1:8443s+8000'), (8443, True))
168
169 def test_parse_frontend_config_20(self):
170 self.assertEqual(_parse_frontend_config('civetweb port=127.0.0.1:8080+443s'), (8080, False))
171
172 def test_parse_frontend_config_21(self):
173 with self.assertRaises(LookupError) as ctx:
174 _parse_frontend_config('civetweb port=xyz')
175 self.assertEqual(str(ctx.exception),
176 'Failed to determine RGW port from "civetweb port=xyz"')
177
178 def test_parse_frontend_config_22(self):
179 with self.assertRaises(LookupError) as ctx:
180 _parse_frontend_config('civetweb')
181 self.assertEqual(str(ctx.exception), 'Failed to determine RGW port from "civetweb"')
182
183 def test_parse_frontend_config_23(self):
184 with self.assertRaises(LookupError) as ctx:
185 _parse_frontend_config('mongoose port=8080')
186 self.assertEqual(str(ctx.exception),
187 'Failed to determine RGW port from "mongoose port=8080"')