summaryrefslogtreecommitdiffstats
path: root/qa/tasks/mgr/dashboard/test_orchestrator.py
blob: 99c004711bd49942183281b0c1aa145d63f1df7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import json

from .helper import DashboardTestCase


test_data = {
    'inventory': [
        {
            'name': 'test-host0',
            'devices': [
                {
                    'path': '/dev/sda',
                }
            ]
        },
        {
            'name': 'test-host1',
            'devices': [
                {
                    'path': '/dev/sdb',
                }
            ]
        }
    ],
    'services': [
        {
            'nodename': 'test-host0',
            'service_type': 'mon',
            'service_instance': 'a'
        },
        {
            'nodename': 'test-host0',
            'service_type': 'mgr',
            'service_instance': 'x'
        },
        {
            'nodename': 'test-host0',
            'service_type': 'osd',
            'service_instance': '0'
        },
        {
            'nodename': 'test-host1',
            'service_type': 'osd',
            'service_instance': '1'
        }
    ]
}


class OrchestratorControllerTest(DashboardTestCase):

    AUTH_ROLES = ['cluster-manager']

    URL_STATUS = '/api/orchestrator/status'
    URL_INVENTORY = '/api/orchestrator/inventory'
    URL_SERVICE = '/api/orchestrator/service'
    URL_OSD = '/api/orchestrator/osd'


    @property
    def test_data_inventory(self):
        return test_data['inventory']

    @property
    def test_data_services(self):
        return test_data['services']

    @classmethod
    def setUpClass(cls):
        super(OrchestratorControllerTest, cls).setUpClass()
        cls._load_module('test_orchestrator')
        cmd = ['orchestrator', 'set', 'backend', 'test_orchestrator']
        cls.mgr_cluster.mon_manager.raw_cluster_cmd(*cmd)

        cmd = ['test_orchestrator', 'load_data', '-i', '-']
        cls.mgr_cluster.mon_manager.raw_cluster_cmd_result(*cmd, stdin=json.dumps(test_data))

    @classmethod
    def tearDownClass(cls):
        cmd = ['test_orchestrator', 'load_data', '-i', '-']
        cls.mgr_cluster.mon_manager.raw_cluster_cmd_result(*cmd, stdin='{}')

    def _validate_inventory(self, data, resp_data):
        self.assertEqual(data['name'], resp_data['name'])
        self.assertEqual(len(data['devices']), len(resp_data['devices']))

        if not data['devices']:
            return
        test_devices = sorted(data['devices'], key=lambda d: d['path'])
        resp_devices = sorted(resp_data['devices'], key=lambda d: d['path'])

        for test, resp in zip(test_devices, resp_devices):
            self._validate_device(test, resp)

    def _validate_device(self, data, resp_data):
        for key, value in data.items():
            self.assertEqual(value, resp_data[key])

    def _validate_service(self, data, resp_data):
        for key, value in data.items():
            self.assertEqual(value, resp_data[key])

    @DashboardTestCase.RunAs('test', 'test', ['block-manager'])
    def test_access_permissions(self):
        self._get(self.URL_STATUS)
        self.assertStatus(200)
        self._get(self.URL_INVENTORY)
        self.assertStatus(403)
        self._get(self.URL_SERVICE)
        self.assertStatus(403)

    def test_status_get(self):
        data = self._get(self.URL_STATUS)
        self.assertTrue(data['available'])

    def test_inventory_list(self):
        # get all inventory
        data = self._get(self.URL_INVENTORY)
        self.assertStatus(200)

        sorting_key = lambda node: node['name']
        test_inventory = sorted(self.test_data_inventory, key=sorting_key)
        resp_inventory = sorted(data, key=sorting_key)
        self.assertEqual(len(test_inventory), len(resp_inventory))
        for test, resp in zip(test_inventory, resp_inventory):
            self._validate_inventory(test, resp)

        # get inventory by hostname
        node = self.test_data_inventory[-1]
        data = self._get('{}?hostname={}'.format(self.URL_INVENTORY, node['name']))
        self.assertStatus(200)
        self.assertEqual(len(data), 1)
        self._validate_inventory(node, data[0])

    def test_service_list(self):
        # get all services
        data = self._get(self.URL_SERVICE)
        self.assertStatus(200)

        sorting_key = lambda svc: '%(nodename)s.%(service_type)s.%(service_instance)s' % svc
        test_services = sorted(self.test_data_services, key=sorting_key)
        resp_services = sorted(data, key=sorting_key)
        self.assertEqual(len(test_services), len(resp_services))
        for test, resp in zip(test_services, resp_services):
            self._validate_service(test, resp)

        # get service by hostname
        nodename = self.test_data_services[-1]['nodename']
        test_services = sorted(filter(lambda svc: svc['nodename'] == nodename, test_services),
                          key=sorting_key)
        data = self._get('{}?hostname={}'.format(self.URL_SERVICE, nodename))
        resp_services = sorted(data, key=sorting_key)
        for test, resp in zip(test_services, resp_services):
            self._validate_service(test, resp)

    def test_create_osds(self):
        data = {
            'drive_group': {
                'host_pattern': '*',
                'data_devices': {
                    'vendor': 'abc',
                    'model': 'cba',
                    'rotational': True,
                    'size': '4 TB'
                },
                'wal_devices': {
                    'vendor': 'def',
                    'model': 'fed',
                    'rotational': False,
                    'size': '1 TB'
                },
                'db_devices': {
                    'vendor': 'ghi',
                    'model': 'ihg',
                    'rotational': False,
                    'size': '512 GB'
                },
                'wal_slots': 5,
                'db_slots': 5,
                'encrypted': True
            }
        }
        self._post(self.URL_OSD, data)
        self.assertStatus(201)