]> git.proxmox.com Git - ceph.git/blame - ceph/src/pybind/mgr/dashboard/tests/test_host.py
import 15.2.5
[ceph.git] / ceph / src / pybind / mgr / dashboard / tests / test_host.py
CommitLineData
9f95a23c
TL
1import unittest
2
3try:
4 import mock
5except ImportError:
6 from unittest import mock
7
8from orchestrator import HostSpec
9
10from . import ControllerTestCase
f6b5b4d7 11from ..controllers.host import get_hosts, Host, HostUi
9f95a23c
TL
12from .. import mgr
13
14
15class HostControllerTest(ControllerTestCase):
16 URL_HOST = '/api/host'
17
18 @classmethod
19 def setup_server(cls):
20 # pylint: disable=protected-access
21 Host._cp_config['tools.authenticate.on'] = False
22 cls.setup_controllers([Host])
23
24 @mock.patch('dashboard.controllers.host.get_hosts')
25 def test_host_list(self, mock_get_hosts):
f6b5b4d7
TL
26 hosts = [{
27 'hostname': 'host-0',
28 'sources': {
29 'ceph': True,
30 'orchestrator': False
9f95a23c 31 }
f6b5b4d7
TL
32 }, {
33 'hostname': 'host-1',
34 'sources': {
35 'ceph': False,
36 'orchestrator': True
37 }
38 }, {
39 'hostname': 'host-2',
40 'sources': {
41 'ceph': True,
42 'orchestrator': True
43 }
44 }]
9f95a23c
TL
45
46 def _get_hosts(from_ceph=True, from_orchestrator=True):
47 _hosts = []
48 if from_ceph:
49 _hosts.append(hosts[0])
50 if from_orchestrator:
51 _hosts.append(hosts[1])
52 _hosts.append(hosts[2])
53 return _hosts
f6b5b4d7 54
9f95a23c
TL
55 mock_get_hosts.side_effect = _get_hosts
56
57 self._get(self.URL_HOST)
58 self.assertStatus(200)
59 self.assertJsonBody(hosts)
60
61 self._get('{}?sources=ceph'.format(self.URL_HOST))
62 self.assertStatus(200)
63 self.assertJsonBody([hosts[0]])
64
65 self._get('{}?sources=orchestrator'.format(self.URL_HOST))
66 self.assertStatus(200)
67 self.assertJsonBody(hosts[1:])
68
69 self._get('{}?sources=ceph,orchestrator'.format(self.URL_HOST))
70 self.assertStatus(200)
71 self.assertJsonBody(hosts)
72
f6b5b4d7
TL
73 @mock.patch('dashboard.controllers.orchestrator.OrchClient.instance')
74 def test_get_1(self, instance):
75 mgr.list_servers.return_value = []
9f95a23c 76
f6b5b4d7
TL
77 fake_client = mock.Mock()
78 fake_client.available.return_value = False
79 instance.return_value = fake_client
80
81 self._get('{}/node1'.format(self.URL_HOST))
82 self.assertStatus(404)
83
84 @mock.patch('dashboard.controllers.orchestrator.OrchClient.instance')
85 def test_get_2(self, instance):
86 mgr.list_servers.return_value = [{'hostname': 'node1'}]
87
88 fake_client = mock.Mock()
89 fake_client.available.return_value = False
90 instance.return_value = fake_client
91
92 self._get('{}/node1'.format(self.URL_HOST))
93 self.assertStatus(200)
94 self.assertIn('labels', self.json_body())
95
96 @mock.patch('dashboard.controllers.orchestrator.OrchClient.instance')
97 def test_get_3(self, instance):
98 mgr.list_servers.return_value = []
99
100 fake_client = mock.Mock()
101 fake_client.available.return_value = True
102 fake_client.hosts.list.return_value = [HostSpec('node1')]
103 instance.return_value = fake_client
104
105 self._get('{}/node1'.format(self.URL_HOST))
106 self.assertStatus(200)
107 self.assertIn('labels', self.json_body())
9f95a23c 108
f6b5b4d7
TL
109 @mock.patch('dashboard.controllers.orchestrator.OrchClient.instance')
110 def test_set_labels(self, instance):
111 mgr.list_servers.return_value = []
112
113 fake_client = mock.Mock()
114 fake_client.available.return_value = True
115 fake_client.hosts.list.return_value = [
116 HostSpec('node0', labels=['aaa', 'bbb'])
117 ]
118 fake_client.hosts.remove_label = mock.Mock()
119 fake_client.hosts.add_label = mock.Mock()
120 instance.return_value = fake_client
121
122 self._put('{}/node0'.format(self.URL_HOST), {'labels': ['bbb', 'ccc']})
123 self.assertStatus(200)
124 fake_client.hosts.remove_label.assert_called_once_with('node0', 'aaa')
125 fake_client.hosts.add_label.assert_called_once_with('node0', 'ccc')
126
127
128class HostUiControllerTest(ControllerTestCase):
129 URL_HOST = '/ui-api/host'
130
131 @classmethod
132 def setup_server(cls):
133 # pylint: disable=protected-access
134 HostUi._cp_config['tools.authenticate.on'] = False
135 cls.setup_controllers([HostUi])
136
137 @mock.patch('dashboard.controllers.orchestrator.OrchClient.instance')
138 def test_labels(self, instance):
139 fake_client = mock.Mock()
140 fake_client.available.return_value = True
141 fake_client.hosts.list.return_value = [
142 HostSpec('node1', labels=['foo']),
143 HostSpec('node2', labels=['foo', 'bar'])
144 ]
145 instance.return_value = fake_client
146
147 self._get('{}/labels'.format(self.URL_HOST))
148 self.assertStatus(200)
149 labels = self.json_body()
150 labels.sort()
151 self.assertListEqual(labels, ['bar', 'foo'])
152
153
154class TestHosts(unittest.TestCase):
9f95a23c
TL
155 @mock.patch('dashboard.controllers.orchestrator.OrchClient.instance')
156 def test_get_hosts(self, instance):
f6b5b4d7
TL
157 mgr.list_servers.return_value = [{
158 'hostname': 'node1'
159 }, {
160 'hostname': 'localhost'
161 }]
9f95a23c
TL
162
163 fake_client = mock.Mock()
164 fake_client.available.return_value = True
165 fake_client.hosts.list.return_value = [
f6b5b4d7
TL
166 HostSpec('node1', labels=['foo', 'bar']),
167 HostSpec('node2', labels=['bar'])
168 ]
9f95a23c
TL
169 instance.return_value = fake_client
170
171 hosts = get_hosts()
172 self.assertEqual(len(hosts), 3)
f6b5b4d7
TL
173 checks = {
174 'localhost': {
175 'sources': {
176 'ceph': True,
177 'orchestrator': False
178 },
179 'labels': []
180 },
181 'node1': {
182 'sources': {
183 'ceph': True,
184 'orchestrator': True
185 },
186 'labels': ['bar', 'foo']
187 },
188 'node2': {
189 'sources': {
190 'ceph': False,
191 'orchestrator': True
192 },
193 'labels': ['bar']
194 }
9f95a23c
TL
195 }
196 for host in hosts:
197 hostname = host['hostname']
f6b5b4d7
TL
198 self.assertDictEqual(host['sources'], checks[hostname]['sources'])
199 self.assertListEqual(host['labels'], checks[hostname]['labels'])