]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/pybind/mgr/dashboard/tests/test_host.py
import 15.2.5
[ceph.git] / ceph / src / pybind / mgr / dashboard / tests / test_host.py
index c2ce606938083cd4ad859da32b67409638cabb7a..ab7286074b7386b53adb88a99756a2ffdc8e3c31 100644 (file)
@@ -8,7 +8,7 @@ except ImportError:
 from orchestrator import HostSpec
 
 from . import ControllerTestCase
-from ..controllers.host import get_hosts, Host
+from ..controllers.host import get_hosts, Host, HostUi
 from .. import mgr
 
 
@@ -23,26 +23,25 @@ class HostControllerTest(ControllerTestCase):
 
     @mock.patch('dashboard.controllers.host.get_hosts')
     def test_host_list(self, mock_get_hosts):
-        hosts = [
-            {
-                'hostname': 'host-0',
-                'sources': {
-                    'ceph': True, 'orchestrator': False
-                }
-            },
-            {
-                'hostname': 'host-1',
-                'sources': {
-                    'ceph': False, 'orchestrator': True
-                }
-            },
-            {
-                'hostname': 'host-2',
-                'sources': {
-                    'ceph': True, 'orchestrator': True
-                }
+        hosts = [{
+            'hostname': 'host-0',
+            'sources': {
+                'ceph': True,
+                'orchestrator': False
             }
-        ]
+        }, {
+            'hostname': 'host-1',
+            'sources': {
+                'ceph': False,
+                'orchestrator': True
+            }
+        }, {
+            'hostname': 'host-2',
+            'sources': {
+                'ceph': True,
+                'orchestrator': True
+            }
+        }]
 
         def _get_hosts(from_ceph=True, from_orchestrator=True):
             _hosts = []
@@ -52,6 +51,7 @@ class HostControllerTest(ControllerTestCase):
                 _hosts.append(hosts[1])
                 _hosts.append(hosts[2])
             return _hosts
+
         mock_get_hosts.side_effect = _get_hosts
 
         self._get(self.URL_HOST)
@@ -70,27 +70,130 @@ class HostControllerTest(ControllerTestCase):
         self.assertStatus(200)
         self.assertJsonBody(hosts)
 
+    @mock.patch('dashboard.controllers.orchestrator.OrchClient.instance')
+    def test_get_1(self, instance):
+        mgr.list_servers.return_value = []
 
-class TestHosts(unittest.TestCase):
+        fake_client = mock.Mock()
+        fake_client.available.return_value = False
+        instance.return_value = fake_client
+
+        self._get('{}/node1'.format(self.URL_HOST))
+        self.assertStatus(404)
+
+    @mock.patch('dashboard.controllers.orchestrator.OrchClient.instance')
+    def test_get_2(self, instance):
+        mgr.list_servers.return_value = [{'hostname': 'node1'}]
+
+        fake_client = mock.Mock()
+        fake_client.available.return_value = False
+        instance.return_value = fake_client
+
+        self._get('{}/node1'.format(self.URL_HOST))
+        self.assertStatus(200)
+        self.assertIn('labels', self.json_body())
+
+    @mock.patch('dashboard.controllers.orchestrator.OrchClient.instance')
+    def test_get_3(self, instance):
+        mgr.list_servers.return_value = []
+
+        fake_client = mock.Mock()
+        fake_client.available.return_value = True
+        fake_client.hosts.list.return_value = [HostSpec('node1')]
+        instance.return_value = fake_client
+
+        self._get('{}/node1'.format(self.URL_HOST))
+        self.assertStatus(200)
+        self.assertIn('labels', self.json_body())
 
+    @mock.patch('dashboard.controllers.orchestrator.OrchClient.instance')
+    def test_set_labels(self, instance):
+        mgr.list_servers.return_value = []
+
+        fake_client = mock.Mock()
+        fake_client.available.return_value = True
+        fake_client.hosts.list.return_value = [
+            HostSpec('node0', labels=['aaa', 'bbb'])
+        ]
+        fake_client.hosts.remove_label = mock.Mock()
+        fake_client.hosts.add_label = mock.Mock()
+        instance.return_value = fake_client
+
+        self._put('{}/node0'.format(self.URL_HOST), {'labels': ['bbb', 'ccc']})
+        self.assertStatus(200)
+        fake_client.hosts.remove_label.assert_called_once_with('node0', 'aaa')
+        fake_client.hosts.add_label.assert_called_once_with('node0', 'ccc')
+
+
+class HostUiControllerTest(ControllerTestCase):
+    URL_HOST = '/ui-api/host'
+
+    @classmethod
+    def setup_server(cls):
+        # pylint: disable=protected-access
+        HostUi._cp_config['tools.authenticate.on'] = False
+        cls.setup_controllers([HostUi])
+
+    @mock.patch('dashboard.controllers.orchestrator.OrchClient.instance')
+    def test_labels(self, instance):
+        fake_client = mock.Mock()
+        fake_client.available.return_value = True
+        fake_client.hosts.list.return_value = [
+            HostSpec('node1', labels=['foo']),
+            HostSpec('node2', labels=['foo', 'bar'])
+        ]
+        instance.return_value = fake_client
+
+        self._get('{}/labels'.format(self.URL_HOST))
+        self.assertStatus(200)
+        labels = self.json_body()
+        labels.sort()
+        self.assertListEqual(labels, ['bar', 'foo'])
+
+
+class TestHosts(unittest.TestCase):
     @mock.patch('dashboard.controllers.orchestrator.OrchClient.instance')
     def test_get_hosts(self, instance):
-        mgr.list_servers.return_value = [{'hostname': 'node1'}, {'hostname': 'localhost'}]
+        mgr.list_servers.return_value = [{
+            'hostname': 'node1'
+        }, {
+            'hostname': 'localhost'
+        }]
 
         fake_client = mock.Mock()
         fake_client.available.return_value = True
         fake_client.hosts.list.return_value = [
-            HostSpec('node1'), HostSpec('node2')]
+            HostSpec('node1', labels=['foo', 'bar']),
+            HostSpec('node2', labels=['bar'])
+        ]
         instance.return_value = fake_client
 
         hosts = get_hosts()
         self.assertEqual(len(hosts), 3)
-        check_sources = {
-            'localhost': {'ceph': True, 'orchestrator': False},
-            'node1': {'ceph': True, 'orchestrator': True},
-            'node2': {'ceph': False, 'orchestrator': True}
+        checks = {
+            'localhost': {
+                'sources': {
+                    'ceph': True,
+                    'orchestrator': False
+                },
+                'labels': []
+            },
+            'node1': {
+                'sources': {
+                    'ceph': True,
+                    'orchestrator': True
+                },
+                'labels': ['bar', 'foo']
+            },
+            'node2': {
+                'sources': {
+                    'ceph': False,
+                    'orchestrator': True
+                },
+                'labels': ['bar']
+            }
         }
         for host in hosts:
             hostname = host['hostname']
-            sources = host['sources']
-            self.assertDictEqual(sources, check_sources[hostname])
+            self.assertDictEqual(host['sources'], checks[hostname]['sources'])
+            self.assertListEqual(host['labels'], checks[hostname]['labels'])