1 # -*- coding: utf-8 -*-
2 from __future__
import absolute_import
7 from mgr_module
import ERROR_MSG_EMPTY_INPUT_FILE
9 from .. import settings
10 from ..controllers
.settings
import Settings
as SettingsController
11 from ..settings
import Settings
, handle_option_command
12 from . import ControllerTestCase
, KVStoreMockMixin
# pylint: disable=no-name-in-module
15 class SettingsTest(unittest
.TestCase
, KVStoreMockMixin
):
18 setattr(settings
.Options
, 'GRAFANA_API_HOST', settings
.Setting('localhost', [str]))
19 setattr(settings
.Options
, 'GRAFANA_API_PORT', settings
.Setting(3000, [int]))
20 setattr(settings
.Options
, 'GRAFANA_ENABLED', settings
.Setting(False, [bool]))
21 # pylint: disable=protected-access
22 settings
._OPTIONS
_COMMAND
_MAP
= settings
._options
_command
_map
()
26 if Settings
.GRAFANA_API_HOST
!= 'localhost':
27 Settings
.GRAFANA_API_HOST
= 'localhost'
28 if Settings
.GRAFANA_API_PORT
!= 3000:
29 Settings
.GRAFANA_API_PORT
= 3000
31 def test_get_setting(self
):
32 self
.assertEqual(Settings
.GRAFANA_API_HOST
, 'localhost')
33 self
.assertEqual(Settings
.GRAFANA_API_PORT
, 3000)
34 self
.assertEqual(Settings
.GRAFANA_ENABLED
, False)
36 def test_set_setting(self
):
37 Settings
.GRAFANA_API_HOST
= 'grafanahost'
38 self
.assertEqual(Settings
.GRAFANA_API_HOST
, 'grafanahost')
40 Settings
.GRAFANA_API_PORT
= 50
41 self
.assertEqual(Settings
.GRAFANA_API_PORT
, 50)
43 Settings
.GRAFANA_ENABLED
= True
44 self
.assertEqual(Settings
.GRAFANA_ENABLED
, True)
46 def test_get_cmd(self
):
47 r
, out
, err
= handle_option_command(
48 {'prefix': 'dashboard get-grafana-api-port'},
51 self
.assertEqual(r
, 0)
52 self
.assertEqual(out
, '3000')
53 self
.assertEqual(err
, '')
55 def test_set_cmd(self
):
56 r
, out
, err
= handle_option_command(
57 {'prefix': 'dashboard set-grafana-api-port',
61 self
.assertEqual(r
, 0)
62 self
.assertEqual(out
, 'Option GRAFANA_API_PORT updated')
63 self
.assertEqual(err
, '')
65 def test_set_secret_empty(self
):
66 r
, out
, err
= handle_option_command(
67 {'prefix': 'dashboard set-grafana-api-password'},
70 self
.assertEqual(r
, -errno
.EINVAL
)
71 self
.assertEqual(out
, '')
72 self
.assertIn(ERROR_MSG_EMPTY_INPUT_FILE
, err
)
74 def test_set_secret(self
):
75 r
, out
, err
= handle_option_command(
76 {'prefix': 'dashboard set-grafana-api-password'},
79 self
.assertEqual(r
, 0)
80 self
.assertEqual(out
, 'Option GRAFANA_API_PASSWORD updated')
81 self
.assertEqual(err
, '')
83 def test_reset_cmd(self
):
84 r
, out
, err
= handle_option_command(
85 {'prefix': 'dashboard reset-grafana-enabled'},
88 self
.assertEqual(r
, 0)
89 self
.assertEqual(out
, 'Option {} reset to default value "{}"'.format(
90 'GRAFANA_ENABLED', Settings
.GRAFANA_ENABLED
))
91 self
.assertEqual(err
, '')
93 def test_inv_cmd(self
):
94 r
, out
, err
= handle_option_command(
95 {'prefix': 'dashboard get-non-existent-option'},
98 self
.assertEqual(r
, -errno
.ENOSYS
)
99 self
.assertEqual(out
, '')
100 self
.assertEqual(err
, "Command not found "
101 "'dashboard get-non-existent-option'")
104 Settings
.GRAFANA_API_PORT
= 5000
105 r
, out
, err
= handle_option_command(
106 {'prefix': 'dashboard get-grafana-api-port'},
109 self
.assertEqual(r
, 0)
110 self
.assertEqual(out
, '5000')
111 self
.assertEqual(err
, '')
112 r
, out
, err
= handle_option_command(
113 {'prefix': 'dashboard set-grafana-api-host',
114 'value': 'new-local-host'},
117 self
.assertEqual(r
, 0)
118 self
.assertEqual(out
, 'Option GRAFANA_API_HOST updated')
119 self
.assertEqual(err
, '')
120 self
.assertEqual(Settings
.GRAFANA_API_HOST
, 'new-local-host')
122 def test_attribute_error(self
):
123 with self
.assertRaises(AttributeError) as ctx
:
124 _
= Settings
.NON_EXISTENT_OPTION
126 self
.assertEqual(str(ctx
.exception
),
127 "type object 'Options' has no attribute 'NON_EXISTENT_OPTION'")
130 class SettingsControllerTest(ControllerTestCase
, KVStoreMockMixin
):
132 def setup_server(cls
):
133 # pylint: disable=protected-access
135 SettingsController
._cp
_config
['tools.authenticate.on'] = False
136 cls
.setup_controllers([SettingsController
])
141 setattr(settings
.Options
, 'GRAFANA_API_HOST', settings
.Setting('localhost', [str]))
142 setattr(settings
.Options
, 'GRAFANA_ENABLED', settings
.Setting(False, [bool]))
145 def tearDownClass(cls
):
146 super().tearDownClass()
152 def test_settings_list(self
):
153 self
._get
('/api/settings')
154 data
= self
.json_body()
155 self
.assertTrue(len(data
) > 0)
156 self
.assertStatus(200)
157 self
.assertIn('default', data
[0].keys())
158 self
.assertIn('type', data
[0].keys())
159 self
.assertIn('name', data
[0].keys())
160 self
.assertIn('value', data
[0].keys())
162 def test_settings_list_filtered(self
):
163 self
._get
('/api/settings?names=GRAFANA_ENABLED,PWD_POLICY_ENABLED')
164 self
.assertStatus(200)
165 data
= self
.json_body()
166 self
.assertTrue(len(data
) == 2)
167 names
= [option
['name'] for option
in data
]
168 self
.assertIn('GRAFANA_ENABLED', names
)
169 self
.assertIn('PWD_POLICY_ENABLED', names
)
171 def test_rgw_daemon_get(self
):
172 self
._get
('/api/settings/grafana-api-username')
173 self
.assertStatus(200)
174 self
.assertJsonBody({
175 u
'default': u
'admin',
177 u
'name': u
'GRAFANA_API_USERNAME',
182 self
._put
('/api/settings/GRAFANA_API_USERNAME', {'value': 'foo'})
183 self
.assertStatus(200)
185 self
._get
('/api/settings/GRAFANA_API_USERNAME')
186 self
.assertStatus(200)
187 self
.assertInJsonBody('default')
188 self
.assertInJsonBody('type')
189 self
.assertInJsonBody('name')
190 self
.assertInJsonBody('value')
191 self
.assertEqual(self
.json_body()['value'], 'foo')
193 def test_bulk_set(self
):
194 self
._put
('/api/settings', {
195 'GRAFANA_API_USERNAME': 'foo',
196 'GRAFANA_API_HOST': 'somehost',
198 self
.assertStatus(200)
200 self
._get
('/api/settings/grafana-api-username')
201 self
.assertStatus(200)
202 body
= self
.json_body()
203 self
.assertEqual(body
['value'], 'foo')
205 self
._get
('/api/settings/grafana-api-username')
206 self
.assertStatus(200)
207 self
.assertEqual(self
.json_body()['value'], 'foo')
209 self
._get
('/api/settings/grafana-api-host')
210 self
.assertStatus(200)
211 self
.assertEqual(self
.json_body()['value'], 'somehost')