]>
git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/tests/test_host.py
6 from unittest
import mock
8 from orchestrator
import HostSpec
10 from . import ControllerTestCase
# pylint: disable=no-name-in-module
11 from ..controllers
.host
import get_hosts
, Host
, HostUi
15 class HostControllerTest(ControllerTestCase
):
16 URL_HOST
= '/api/host'
19 def setup_server(cls
):
20 # pylint: disable=protected-access
21 Host
._cp
_config
['tools.authenticate.on'] = False
22 cls
.setup_controllers([Host
])
24 @mock.patch('dashboard.controllers.host.get_hosts')
25 def test_host_list(self
, mock_get_hosts
):
46 def _get_hosts(from_ceph
=True, from_orchestrator
=True):
49 _hosts
.append(hosts
[0])
51 _hosts
.append(hosts
[1])
52 _hosts
.append(hosts
[2])
55 mock_get_hosts
.side_effect
= _get_hosts
57 self
._get
(self
.URL_HOST
)
58 self
.assertStatus(200)
59 self
.assertJsonBody(hosts
)
61 self
._get
('{}?sources=ceph'.format(self
.URL_HOST
))
62 self
.assertStatus(200)
63 self
.assertJsonBody([hosts
[0]])
65 self
._get
('{}?sources=orchestrator'.format(self
.URL_HOST
))
66 self
.assertStatus(200)
67 self
.assertJsonBody(hosts
[1:])
69 self
._get
('{}?sources=ceph,orchestrator'.format(self
.URL_HOST
))
70 self
.assertStatus(200)
71 self
.assertJsonBody(hosts
)
73 @mock.patch('dashboard.controllers.orchestrator.OrchClient.instance')
74 def test_get_1(self
, instance
):
75 mgr
.list_servers
.return_value
= []
77 fake_client
= mock
.Mock()
78 fake_client
.available
.return_value
= False
79 instance
.return_value
= fake_client
81 self
._get
('{}/node1'.format(self
.URL_HOST
))
82 self
.assertStatus(404)
84 @mock.patch('dashboard.controllers.orchestrator.OrchClient.instance')
85 def test_get_2(self
, instance
):
86 mgr
.list_servers
.return_value
= [{'hostname': 'node1'}]
88 fake_client
= mock
.Mock()
89 fake_client
.available
.return_value
= False
90 instance
.return_value
= fake_client
92 self
._get
('{}/node1'.format(self
.URL_HOST
))
93 self
.assertStatus(200)
94 self
.assertIn('labels', self
.json_body())
96 @mock.patch('dashboard.controllers.orchestrator.OrchClient.instance')
97 def test_get_3(self
, instance
):
98 mgr
.list_servers
.return_value
= []
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
105 self
._get
('{}/node1'.format(self
.URL_HOST
))
106 self
.assertStatus(200)
107 self
.assertIn('labels', self
.json_body())
109 @mock.patch('dashboard.controllers.orchestrator.OrchClient.instance')
110 def test_set_labels(self
, instance
):
111 mgr
.list_servers
.return_value
= []
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'])
118 fake_client
.hosts
.remove_label
= mock
.Mock()
119 fake_client
.hosts
.add_label
= mock
.Mock()
120 instance
.return_value
= fake_client
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')
128 class HostUiControllerTest(ControllerTestCase
):
129 URL_HOST
= '/ui-api/host'
132 def setup_server(cls
):
133 # pylint: disable=protected-access
134 HostUi
._cp
_config
['tools.authenticate.on'] = False
135 cls
.setup_controllers([HostUi
])
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'])
145 instance
.return_value
= fake_client
147 self
._get
('{}/labels'.format(self
.URL_HOST
))
148 self
.assertStatus(200)
149 labels
= self
.json_body()
151 self
.assertListEqual(labels
, ['bar', 'foo'])
154 class TestHosts(unittest
.TestCase
):
155 @mock.patch('dashboard.controllers.orchestrator.OrchClient.instance')
156 def test_get_hosts(self
, instance
):
157 mgr
.list_servers
.return_value
= [{
160 'hostname': 'localhost'
163 fake_client
= mock
.Mock()
164 fake_client
.available
.return_value
= True
165 fake_client
.hosts
.list.return_value
= [
166 HostSpec('node1', labels
=['foo', 'bar']),
167 HostSpec('node2', labels
=['bar'])
169 instance
.return_value
= fake_client
172 self
.assertEqual(len(hosts
), 3)
177 'orchestrator': False
186 'labels': ['bar', 'foo']
197 hostname
= host
['hostname']
198 self
.assertDictEqual(host
['sources'], checks
[hostname
]['sources'])
199 self
.assertListEqual(host
['labels'], checks
[hostname
]['labels'])