]>
git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/tests/test_grafana.py
7 from unittest
.mock
import patch
9 from requests
import RequestException
11 from . import ControllerTestCase
, KVStoreMockMixin
12 from ..controllers
.grafana
import Grafana
13 from ..grafana
import GrafanaRestClient
14 from ..settings
import Settings
17 class GrafanaTest(ControllerTestCase
, KVStoreMockMixin
):
19 def setup_server(cls
):
20 # pylint: disable=protected-access
21 Grafana
._cp
_config
['tools.authenticate.on'] = False
22 cls
.setup_controllers([Grafana
])
29 url
='http://localhost:3000',
34 Settings
.GRAFANA_API_URL
= url
36 Settings
.GRAFANA_API_USERNAME
= user
37 if password
is not None:
38 Settings
.GRAFANA_API_PASSWORD
= password
41 self
.server_settings()
42 self
._get
('/api/grafana/url')
43 self
.assertStatus(200)
44 self
.assertJsonBody({'instance': 'http://localhost:3000'})
46 @patch('dashboard.controllers.grafana.GrafanaRestClient.url_validation')
47 def test_validation_endpoint_returns(self
, url_validation
):
49 The point of this test is to see that `validation` is an active endpoint that returns a 200
52 url_validation
.return_value
= b
'404'
53 self
.server_settings()
54 self
._get
('/api/grafana/validation/foo')
55 self
.assertStatus(200)
56 self
.assertBody(b
'"404"')
58 def test_dashboards_unavailable_no_url(self
):
59 self
.server_settings(url
="")
60 self
._post
('/api/grafana/dashboards')
61 self
.assertStatus(500)
63 @patch('dashboard.controllers.grafana.GrafanaRestClient.push_dashboard')
64 def test_dashboards_unavailable_no_user(self
, pd
):
65 pd
.side_effect
= RequestException
66 self
.server_settings(user
="")
67 self
._post
('/api/grafana/dashboards')
68 self
.assertStatus(500)
70 def test_dashboards_unavailable_no_password(self
):
71 self
.server_settings(password
="")
72 self
._post
('/api/grafana/dashboards')
73 self
.assertStatus(500)
76 class GrafanaRestClientTest(unittest
.TestCase
, KVStoreMockMixin
):
78 'Accept': 'application/json',
79 'Content-Type': 'application/json',
81 payload
= json
.dumps({
88 Settings
.GRAFANA_API_URL
= 'https://foo/bar'
89 Settings
.GRAFANA_API_USERNAME
= 'xyz'
90 Settings
.GRAFANA_API_PASSWORD
= 'abc'
91 Settings
.GRAFANA_API_SSL_VERIFY
= True
93 def test_ssl_verify_url_validation(self
):
94 with
patch('requests.request') as mock_request
:
95 rest_client
= GrafanaRestClient()
96 rest_client
.url_validation('FOO', Settings
.GRAFANA_API_URL
)
97 mock_request
.assert_called_with('FOO', Settings
.GRAFANA_API_URL
,
100 def test_no_ssl_verify_url_validation(self
):
101 Settings
.GRAFANA_API_SSL_VERIFY
= False
102 with
patch('requests.request') as mock_request
:
103 rest_client
= GrafanaRestClient()
104 rest_client
.url_validation('BAR', Settings
.GRAFANA_API_URL
)
105 mock_request
.assert_called_with('BAR', Settings
.GRAFANA_API_URL
,
108 def test_ssl_verify_push_dashboard(self
):
109 with
patch('requests.post') as mock_request
:
110 rest_client
= GrafanaRestClient()
111 rest_client
.push_dashboard('foo')
112 mock_request
.assert_called_with(
113 Settings
.GRAFANA_API_URL
+ '/api/dashboards/db',
114 auth
=(Settings
.GRAFANA_API_USERNAME
,
115 Settings
.GRAFANA_API_PASSWORD
),
116 data
=self
.payload
, headers
=self
.headers
, verify
=True)
118 def test_no_ssl_verify_push_dashboard(self
):
119 Settings
.GRAFANA_API_SSL_VERIFY
= False
120 with
patch('requests.post') as mock_request
:
121 rest_client
= GrafanaRestClient()
122 rest_client
.push_dashboard('foo')
123 mock_request
.assert_called_with(
124 Settings
.GRAFANA_API_URL
+ '/api/dashboards/db',
125 auth
=(Settings
.GRAFANA_API_USERNAME
,
126 Settings
.GRAFANA_API_PASSWORD
),
127 data
=self
.payload
, headers
=self
.headers
, verify
=False)