]>
git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/tests/test_settings.py
da54a20655defa7aebbe86484e9cb5d2de174333
1 # -*- coding: utf-8 -*-
2 from __future__
import absolute_import
6 from . import KVStoreMockMixin
, ControllerTestCase
7 from .. import settings
8 from ..controllers
.settings
import Settings
as SettingsController
9 from ..settings
import Settings
, handle_option_command
12 class SettingsTest(unittest
.TestCase
, KVStoreMockMixin
):
15 # pylint: disable=protected-access
16 settings
.Options
.GRAFANA_API_HOST
= ('localhost', str)
17 settings
.Options
.GRAFANA_API_PORT
= (3000, int)
18 settings
.Options
.GRAFANA_ENABLED
= (False, bool)
19 settings
._OPTIONS
_COMMAND
_MAP
= settings
._options
_command
_map
()
23 if Settings
.GRAFANA_API_HOST
!= 'localhost':
24 Settings
.GRAFANA_API_HOST
= 'localhost'
25 if Settings
.GRAFANA_API_PORT
!= 3000:
26 Settings
.GRAFANA_API_PORT
= 3000
28 def test_get_setting(self
):
29 self
.assertEqual(Settings
.GRAFANA_API_HOST
, 'localhost')
30 self
.assertEqual(Settings
.GRAFANA_API_PORT
, 3000)
31 self
.assertEqual(Settings
.GRAFANA_ENABLED
, False)
33 def test_set_setting(self
):
34 Settings
.GRAFANA_API_HOST
= 'grafanahost'
35 self
.assertEqual(Settings
.GRAFANA_API_HOST
, 'grafanahost')
37 Settings
.GRAFANA_API_PORT
= 50
38 self
.assertEqual(Settings
.GRAFANA_API_PORT
, 50)
40 Settings
.GRAFANA_ENABLED
= True
41 self
.assertEqual(Settings
.GRAFANA_ENABLED
, True)
43 def test_get_cmd(self
):
44 r
, out
, err
= handle_option_command(
45 {'prefix': 'dashboard get-grafana-api-port'})
46 self
.assertEqual(r
, 0)
47 self
.assertEqual(out
, '3000')
48 self
.assertEqual(err
, '')
50 def test_set_cmd(self
):
51 r
, out
, err
= handle_option_command(
52 {'prefix': 'dashboard set-grafana-api-port',
54 self
.assertEqual(r
, 0)
55 self
.assertEqual(out
, 'Option GRAFANA_API_PORT updated')
56 self
.assertEqual(err
, '')
58 def test_reset_cmd(self
):
59 r
, out
, err
= handle_option_command(
60 {'prefix': 'dashboard reset-grafana-enabled'}
62 self
.assertEqual(r
, 0)
63 self
.assertEqual(out
, 'Option {} reset to default value "{}"'.format(
64 'GRAFANA_ENABLED', Settings
.GRAFANA_ENABLED
))
65 self
.assertEqual(err
, '')
67 def test_inv_cmd(self
):
68 r
, out
, err
= handle_option_command(
69 {'prefix': 'dashboard get-non-existent-option'})
70 self
.assertEqual(r
, -errno
.ENOSYS
)
71 self
.assertEqual(out
, '')
72 self
.assertEqual(err
, "Command not found "
73 "'dashboard get-non-existent-option'")
76 Settings
.GRAFANA_API_PORT
= 5000
77 r
, out
, err
= handle_option_command(
78 {'prefix': 'dashboard get-grafana-api-port'})
79 self
.assertEqual(r
, 0)
80 self
.assertEqual(out
, '5000')
81 self
.assertEqual(err
, '')
82 r
, out
, err
= handle_option_command(
83 {'prefix': 'dashboard set-grafana-api-host',
84 'value': 'new-local-host'})
85 self
.assertEqual(r
, 0)
86 self
.assertEqual(out
, 'Option GRAFANA_API_HOST updated')
87 self
.assertEqual(err
, '')
88 self
.assertEqual(Settings
.GRAFANA_API_HOST
, 'new-local-host')
90 def test_attribute_error(self
):
91 with self
.assertRaises(AttributeError) as ctx
:
92 _
= Settings
.NON_EXISTENT_OPTION
94 self
.assertEqual(str(ctx
.exception
),
95 "type object 'Options' has no attribute 'NON_EXISTENT_OPTION'")
98 class SettingsControllerTest(ControllerTestCase
, KVStoreMockMixin
):
100 def setup_server(cls
):
101 # pylint: disable=protected-access
103 SettingsController
._cp
_config
['tools.authenticate.on'] = False
104 cls
.setup_controllers([SettingsController
])
109 def test_settings_list(self
):
110 self
._get
('/api/settings')
111 data
= self
.json_body()
112 self
.assertTrue(len(data
) > 0)
113 self
.assertStatus(200)
114 self
.assertIn('default', data
[0].keys())
115 self
.assertIn('type', data
[0].keys())
116 self
.assertIn('name', data
[0].keys())
117 self
.assertIn('value', data
[0].keys())
119 def test_settings_list_filtered(self
):
120 self
._get
('/api/settings?names=GRAFANA_ENABLED,PWD_POLICY_ENABLED')
121 self
.assertStatus(200)
122 data
= self
.json_body()
123 self
.assertTrue(len(data
) == 2)
124 names
= [option
['name'] for option
in data
]
125 self
.assertIn('GRAFANA_ENABLED', names
)
126 self
.assertIn('PWD_POLICY_ENABLED', names
)
128 def test_rgw_daemon_get(self
):
129 self
._get
('/api/settings/grafana-api-username')
130 self
.assertStatus(200)
131 self
.assertJsonBody({
132 u
'default': u
'admin',
134 u
'name': u
'GRAFANA_API_USERNAME',
139 self
._put
('/api/settings/GRAFANA_API_USERNAME', {'value': 'foo'},)
140 self
.assertStatus(200)
142 self
._get
('/api/settings/GRAFANA_API_USERNAME')
143 self
.assertStatus(200)
144 self
.assertInJsonBody('default')
145 self
.assertInJsonBody('type')
146 self
.assertInJsonBody('name')
147 self
.assertInJsonBody('value')
148 self
.assertEqual(self
.json_body()['value'], 'foo')
150 def test_bulk_set(self
):
151 self
._put
('/api/settings', {
152 'GRAFANA_API_USERNAME': 'foo',
153 'GRAFANA_API_HOST': 'somehost',
155 self
.assertStatus(200)
157 self
._get
('/api/settings/grafana-api-username')
158 self
.assertStatus(200)
159 body
= self
.json_body()
160 self
.assertEqual(body
['value'], 'foo')
162 self
._get
('/api/settings/grafana-api-username')
163 self
.assertStatus(200)
164 self
.assertEqual(self
.json_body()['value'], 'foo')
166 self
._get
('/api/settings/grafana-api-host')
167 self
.assertStatus(200)
168 self
.assertEqual(self
.json_body()['value'], 'somehost')