summaryrefslogtreecommitdiff
path: root/unit_tests/test_actions_zap_disk.py
blob: 61266e3ab94ae4f67c21fe88c1c235a81942b8c5 (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
# Copyright 2018 Canonical Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#  http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import mock

from actions import zap_disk

from test_utils import CharmTestCase


class ZapDiskActionTests(CharmTestCase):
    def setUp(self):
        super(ZapDiskActionTests, self).setUp(
            zap_disk, ['hookenv',
                       'is_block_device',
                       'is_device_mounted',
                       'is_active_bluestore_device',
                       'is_mapped_luks_device',
                       'kv'])
        self.is_device_mounted.return_value = False
        self.is_block_device.return_value = True
        self.is_active_bluestore_device.return_value = False
        self.is_mapped_luks_device.return_value = False
        self.kv.return_value = self.kv
        self.hookenv.local_unit.return_value = "ceph-osd-test/0"

    @mock.patch.object(zap_disk, 'zap_disk')
    def test_authorized_zap_single_disk(self,
                                        _zap_disk):
        """Will zap disk with extra config set"""
        def side_effect(arg):
            return {
                'devices': '/dev/vdb',
                'i-really-mean-it': True,
            }.get(arg)
        self.hookenv.action_get.side_effect = side_effect
        self.kv.get.return_value = ['/dev/vdb', '/dev/vdz']
        zap_disk.zap()
        _zap_disk.assert_called_with('/dev/vdb')
        self.kv.get.assert_called_with('osd-devices', [])
        self.kv.set.assert_called_with('osd-devices', ['/dev/vdz'])
        self.hookenv.action_set.assert_called_with({
            'message': "1 disk(s) have been zapped, to use "
                       "them as OSDs, run: \njuju "
                       "run-action ceph-osd-test/0 add-disk "
                       "osd-devices=\"/dev/vdb\""
        })

    @mock.patch.object(zap_disk, 'zap_disk')
    def test_authorized_zap_multiple_disks(self,
                                           _zap_disk):
        """Will zap disk with extra config set"""
        def side_effect(arg):
            return {
                'devices': '/dev/vdb /dev/vdc',
                'i-really-mean-it': True,
            }.get(arg)
        self.hookenv.action_get.side_effect = side_effect
        self.kv.get.return_value = ['/dev/vdb', '/dev/vdz']
        zap_disk.zap()
        _zap_disk.assert_has_calls([
            mock.call('/dev/vdb'),
            mock.call('/dev/vdc'),
        ])
        self.kv.get.assert_called_with('osd-devices', [])
        self.kv.set.assert_called_with('osd-devices', ['/dev/vdz'])
        self.hookenv.action_set.assert_called_with({
            'message': "2 disk(s) have been zapped, to use "
                       "them as OSDs, run: \njuju "
                       "run-action ceph-osd-test/0 add-disk "
                       "osd-devices=\"/dev/vdb /dev/vdc\""
        })

    @mock.patch.object(zap_disk, 'zap_disk')
    def test_wont_zap_non_block_device(self,
                                       _zap_disk,):
        """Will not zap a disk that isn't a block device"""
        def side_effect(arg):
            return {
                'devices': '/dev/vdb',
                'i-really-mean-it': True,
            }.get(arg)
        self.hookenv.action_get.side_effect = side_effect
        self.is_block_device.return_value = False
        zap_disk.zap()
        _zap_disk.assert_not_called()
        self.hookenv.action_fail.assert_called_with(
            "1 devices are not block devices: /dev/vdb")

    @mock.patch.object(zap_disk, 'zap_disk')
    def test_wont_zap_mounted_block_device(self,
                                           _zap_disk):
        """Will not zap a disk that is mounted"""
        def side_effect(arg):
            return {
                'devices': '/dev/vdb',
                'i-really-mean-it': True,
            }.get(arg)
        self.hookenv.action_get.side_effect = side_effect
        self.is_device_mounted.return_value = True
        zap_disk.zap()
        _zap_disk.assert_not_called()
        self.hookenv.action_fail.assert_called_with(
            "1 devices are mounted: /dev/vdb")

    @mock.patch.object(zap_disk, 'zap_disk')
    def test_wont_zap__mounted_bluestore_device(self,
                                                _zap_disk):
        """Will not zap a disk that is mounted"""
        def side_effect(arg):
            return {
                'devices': '/dev/vdb',
                'i-really-mean-it': True,
            }.get(arg)
        self.hookenv.action_get.side_effect = side_effect
        self.is_active_bluestore_device.return_value = True
        zap_disk.zap()
        _zap_disk.assert_not_called()
        self.hookenv.action_fail.assert_called_with(
            "1 devices are mounted: /dev/vdb")

    @mock.patch.object(zap_disk, 'zap_disk')
    def test_wont_zap__mapped_luks_device(self, _zap_disk):
        """Will not zap a disk that has a LUKS header"""
        def side_effect(arg):
            return {
                'devices': '/dev/vdb',
                'i-really-mean-it': True,
            }.get(arg)
        self.hookenv.action_get.side_effect = side_effect
        self.is_active_bluestore_device.return_value = False
        self.is_mapped_luks_device.return_value = True
        zap_disk.zap()
        _zap_disk.assert_not_called()
        self.hookenv.action_fail.assert_called_with(
            "1 devices are mounted: /dev/vdb")

    @mock.patch.object(zap_disk, 'zap_disk')
    def test_zap_luks_not_mapped(self, _zap_disk):
        """Will zap disk with extra config set"""
        def side_effect(arg):
            return {
                'devices': '/dev/vdb',
                'i-really-mean-it': True,
            }.get(arg)

        self.is_active_bluestore_device.return_value = False
        self.is_mapped_luks_device.return_value = False

        self.hookenv.action_get.side_effect = side_effect
        self.kv.get.return_value = ['/dev/vdb', '/dev/vdz']
        zap_disk.zap()
        _zap_disk.assert_called_with('/dev/vdb')
        self.kv.get.assert_called_with('osd-devices', [])
        self.kv.set.assert_called_with('osd-devices', ['/dev/vdz'])
        self.hookenv.action_set.assert_called_with({
            'message': "1 disk(s) have been zapped, to use "
                       "them as OSDs, run: \njuju "
                       "run-action ceph-osd-test/0 add-disk "
                       "osd-devices=\"/dev/vdb\""
        })