summaryrefslogtreecommitdiffstats
path: root/qa/tasks/mgr/dashboard/test_rbd_mirroring.py
blob: b6a86e40563fb6ae44297fa69a153f77da7f1e40 (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
187
188
189
190
191
192
193
194
195
# -*- coding: utf-8 -*-
# pylint: disable=too-many-public-methods

from __future__ import absolute_import

from .helper import DashboardTestCase


class RbdMirroringTest(DashboardTestCase):
    AUTH_ROLES = ['pool-manager', 'block-manager']

    @classmethod
    def get_pool(cls, pool):
        data = cls._get('/api/block/mirroring/pool/{}'.format(pool))
        if isinstance(data, dict):
            return data
        return {}

    @classmethod
    def update_pool(cls, pool, mirror_mode):
        data = {'mirror_mode': mirror_mode}
        return cls._task_put('/api/block/mirroring/pool/{}'.format(pool),
                             data)

    @classmethod
    def list_peers(cls, pool):
        data = cls._get('/api/block/mirroring/pool/{}/peer'.format(pool))
        if isinstance(data, list):
            return data
        return []

    @classmethod
    def get_peer(cls, pool, peer_uuid):
        data = cls._get('/api/block/mirroring/pool/{}/peer/{}'.format(pool, peer_uuid))
        if isinstance(data, dict):
            return data
        return {}

    @classmethod
    def create_peer(cls, pool, cluster_name, client_id, **kwargs):
        data = {'cluster_name': cluster_name, 'client_id': client_id}
        data.update(kwargs)
        return cls._task_post('/api/block/mirroring/pool/{}/peer'.format(pool),
                              data)

    @classmethod
    def update_peer(cls, pool, peer_uuid, **kwargs):
        return cls._task_put('/api/block/mirroring/pool/{}/peer/{}'.format(pool, peer_uuid),
                             kwargs)

    @classmethod
    def delete_peer(cls, pool, peer_uuid):
        return cls._task_delete('/api/block/mirroring/pool/{}/peer/{}'.format(pool, peer_uuid))

    @classmethod
    def setUpClass(cls):
        super(RbdMirroringTest, cls).setUpClass()
        cls.create_pool('rbd', 2**3, 'replicated')

    @classmethod
    def tearDownClass(cls):
        super(RbdMirroringTest, cls).tearDownClass()
        cls._ceph_cmd(['osd', 'pool', 'delete', 'rbd', 'rbd', '--yes-i-really-really-mean-it'])

    @DashboardTestCase.RunAs('test', 'test', [{'rbd-mirroring': ['create', 'update', 'delete']}])
    def test_read_access_permissions(self):
        self.get_pool('rbd')
        self.assertStatus(403)
        self.list_peers('rbd')
        self.assertStatus(403)
        self.get_peer('rbd', '123')
        self.assertStatus(403)

    @DashboardTestCase.RunAs('test', 'test', [{'rbd-mirroring': ['read', 'update', 'delete']}])
    def test_create_access_permissions(self):
        self.create_peer('rbd', 'remote', 'id')
        self.assertStatus(403)

    @DashboardTestCase.RunAs('test', 'test', [{'rbd-mirroring': ['read', 'create', 'delete']}])
    def test_update_access_permissions(self):
        self.update_peer('rbd', '123')
        self.assertStatus(403)

    @DashboardTestCase.RunAs('test', 'test', [{'rbd-mirroring': ['read', 'create', 'update']}])
    def test_delete_access_permissions(self):
        self.delete_peer('rbd', '123')
        self.assertStatus(403)

    def test_mirror_mode(self):
        self.update_pool('rbd', 'disabled')
        mode = self.get_pool('rbd').get('mirror_mode')
        self.assertEqual(mode, 'disabled')

        self.update_pool('rbd', 'image')
        mode = self.get_pool('rbd').get('mirror_mode')
        self.assertEqual(mode, 'image')

        self.update_pool('rbd', 'pool')
        mode = self.get_pool('rbd').get('mirror_mode')
        self.assertEqual(mode, 'pool')

        self.update_pool('rbd', 'disabled')
        mode = self.get_pool('rbd').get('mirror_mode')
        self.assertEqual(mode, 'disabled')

    def test_set_invalid_mirror_mode(self):
        self.update_pool('rbd', 'invalid')
        self.assertStatus(400)

    def test_set_same_mirror_mode(self):
        self.update_pool('rbd', 'disabled')
        self.update_pool('rbd', 'disabled')
        self.assertStatus(200)

    def test_peer(self):
        self.update_pool('rbd', 'image')
        self.assertStatus(200)

        peers = self.list_peers('rbd')
        self.assertStatus(200)
        self.assertEqual([], peers)

        uuid = self.create_peer('rbd', 'remote', 'admin')['uuid']
        self.assertStatus(201)

        peers = self.list_peers('rbd')
        self.assertStatus(200)
        self.assertEqual([uuid], peers)

        expected_peer = {
            'uuid': uuid,
            'cluster_name': 'remote',
            'site_name': 'remote',
            'client_id': 'admin',
            'mon_host': '',
            'key': '',
            'direction': 'rx-tx',
            'mirror_uuid': ''
        }
        peer = self.get_peer('rbd', uuid)
        self.assertEqual(expected_peer, peer)

        self.update_peer('rbd', uuid, mon_host='1.2.3.4')
        self.assertStatus(200)

        expected_peer['mon_host'] = '1.2.3.4'
        peer = self.get_peer('rbd', uuid)
        self.assertEqual(expected_peer, peer)

        self.delete_peer('rbd', uuid)
        self.assertStatus(204)

        self.update_pool('rbd', 'disabled')
        self.assertStatus(200)

    def test_disable_mirror_with_peers(self):
        self.update_pool('rbd', 'image')
        self.assertStatus(200)

        uuid = self.create_peer('rbd', 'remote', 'admin')['uuid']
        self.assertStatus(201)

        self.update_pool('rbd', 'disabled')
        self.assertStatus(400)

        self.delete_peer('rbd', uuid)
        self.assertStatus(204)

        self.update_pool('rbd', 'disabled')
        self.assertStatus(200)

    def test_site_name(self):
        expected_site_name = {'site_name': 'site-a'}
        self._task_put('/api/block/mirroring/site_name', expected_site_name)
        self.assertStatus(200)

        site_name = self._get('/api/block/mirroring/site_name')
        self.assertStatus(200)
        self.assertEqual(expected_site_name, site_name)

    def test_bootstrap(self):
        self.update_pool('rbd', 'image')
        token_data = self._task_post('/api/block/mirroring/pool/rbd/bootstrap/token', {})
        self.assertStatus(200)

        import_data = {
            'token': token_data['token'],
            'direction': 'invalid'}
        self._task_post('/api/block/mirroring/pool/rbd/bootstrap/peer', import_data)
        self.assertStatus(400)

        # cannot import "youself" as peer
        import_data['direction'] = 'rx'
        self._task_post('/api/block/mirroring/pool/rbd/bootstrap/peer', import_data)
        self.assertStatus(400)