]> git.proxmox.com Git - ceph.git/blame - ceph/src/pybind/mgr/dashboard/tests/test_grafana.py
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / pybind / mgr / dashboard / tests / test_grafana.py
CommitLineData
92f5a8d4
TL
1import json
2import unittest
3
4try:
5 from mock import patch
6except ImportError:
7 from unittest.mock import patch
8
9f95a23c
TL
9from requests import RequestException
10
11fdf7f2 11from ..controllers.grafana import Grafana
92f5a8d4
TL
12from ..grafana import GrafanaRestClient
13from ..settings import Settings
f67539c2 14from . import ControllerTestCase, KVStoreMockMixin # pylint: disable=no-name-in-module
11fdf7f2
TL
15
16
9f95a23c 17class GrafanaTest(ControllerTestCase, KVStoreMockMixin):
11fdf7f2
TL
18 @classmethod
19 def setup_server(cls):
11fdf7f2
TL
20 # pylint: disable=protected-access
21 Grafana._cp_config['tools.authenticate.on'] = False
22 cls.setup_controllers([Grafana])
23
9f95a23c
TL
24 def setUp(self):
25 self.mock_kv_store()
26
27 @staticmethod
81eedcae 28 def server_settings(
81eedcae
TL
29 url='http://localhost:3000',
30 user='admin',
31 password='admin',
32 ):
81eedcae 33 if url is not None:
9f95a23c 34 Settings.GRAFANA_API_URL = url
81eedcae 35 if user is not None:
9f95a23c 36 Settings.GRAFANA_API_USERNAME = user
81eedcae 37 if password is not None:
9f95a23c 38 Settings.GRAFANA_API_PASSWORD = password
81eedcae 39
11fdf7f2 40 def test_url(self):
81eedcae 41 self.server_settings()
11fdf7f2
TL
42 self._get('/api/grafana/url')
43 self.assertStatus(200)
44 self.assertJsonBody({'instance': 'http://localhost:3000'})
45
9f95a23c
TL
46 @patch('dashboard.controllers.grafana.GrafanaRestClient.url_validation')
47 def test_validation_endpoint_returns(self, url_validation):
48 """
49 The point of this test is to see that `validation` is an active endpoint that returns a 200
50 status code.
51 """
52 url_validation.return_value = b'404'
81eedcae 53 self.server_settings()
11fdf7f2 54 self._get('/api/grafana/validation/foo')
9f95a23c
TL
55 self.assertStatus(200)
56 self.assertBody(b'"404"')
81eedcae 57
9f95a23c
TL
58 def test_dashboards_unavailable_no_url(self):
59 self.server_settings(url="")
81eedcae
TL
60 self._post('/api/grafana/dashboards')
61 self.assertStatus(500)
9f95a23c
TL
62
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="")
81eedcae
TL
67 self._post('/api/grafana/dashboards')
68 self.assertStatus(500)
9f95a23c
TL
69
70 def test_dashboards_unavailable_no_password(self):
71 self.server_settings(password="")
81eedcae
TL
72 self._post('/api/grafana/dashboards')
73 self.assertStatus(500)
92f5a8d4
TL
74
75
76class GrafanaRestClientTest(unittest.TestCase, KVStoreMockMixin):
77 headers = {
78 'Accept': 'application/json',
79 'Content-Type': 'application/json',
80 }
81 payload = json.dumps({
82 'dashboard': 'foo',
83 'overwrite': True
84 })
85
86 def setUp(self):
87 self.mock_kv_store()
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
92
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,
98 verify=True)
99
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,
106 verify=False)
107
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)
117
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)