summaryrefslogtreecommitdiffstats
path: root/tests/test_mounts.py
blob: 49e8104a6ff73a34d7633744af05ad46b92fa4fc (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#
# Copyright 2014 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
#
# Refer to the README and COPYING files for full details of the license
#

import mock
import os

from testrunner import PluginsTestCase as TestCaseBase
from plugins import check_mounts


class diskTests(TestCaseBase):
    def mock_getMountPoint(self, mount):
        return mount

    def mock_open(self, filename, mode):
        return open('mounts.txt', 'r')

    def test_parseProcMounts(self):
        check_mounts.open = self.mock_open
        expected = {
            '/bricks/brick2': {
                'device': '/dev/mapper/mygroup-thinv2',
                'mountOptions': 'rw,relatime,attr2,delaylog,'
                'logbsize=64k,sunit=128,swidth=128,noquota',
                'fsType': 'xfs'},
            '/bricks/brick1': {
                'device': '/dev/mapper/mygroup-thinv1',
                'mountOptions': 'rw,relatime,attr2,delaylog,'
                'logbsize=64k,sunit=128,swidth=128,noquota',
                'fsType': 'xfs'},
            '/home': {
                'device': '/dev/mapper/vg_dhcp42149-lv_home',
                'mountOptions': 'rw,relatime,barrier=1,'
                'data=ordered',
                'fsType': 'ext4'},
            '/boot': {
                'device': '/dev/vda1',
                'mountOptions': 'rw,relatime,barrier=1,'
                'data=ordered',
                'fsType': 'ext4'},
            '/': {
                'device': '/dev/mapper/vg_dhcp42149-lv_root',
                'mountOptions': 'rw,relatime,barrier=1,data=ordered',
                'fsType': 'ext4'},
            '/proc/bus/usb': {
                'device': '/proc/bus/usb',
                'mountOptions': 'rw,relatime',
                'fsType': 'usbfs'}
            }
        actual = check_mounts._parseProcMounts()
        self.assertEquals(expected, actual)

    def mock_os_statvfs(self):
        f_bsize = 4096
        f_frsize = 4096
        f_blocks = 12868767
        f_bfree = 11503139
        f_bavail = 10847779
        f_files = 3276800
        f_ffree = 3220784
        f_favail = 3220784
        f_flag = 4096
        f_namemax = 255
        return os.statvfs_result((f_bsize, f_frsize,
                                 f_blocks, f_bfree,
                                 f_bavail, f_files,
                                 f_ffree, f_favail,
                                 f_flag, f_namemax))

    @mock.patch('plugins.check_mounts.os.statvfs')
    def test_getStats(self, mock_os_statvfs):
        mock_os_statvfs.return_value = self.mock_os_statvfs()
        expected = {'used_percent': 10.611956840931228,
                    'used': 5.2094573974609375,
                    'free_inode': 3220784,
                    'used_inode': 56016,
                    'total_inode': 3276800,
                    'used_percent_inode': 1.70947265625,
                    'total': 49.090450286865234,
                    'free': 43.8809928894043}
        actual = check_mounts._getStats("mount_point")
        self.assertEquals(expected, actual)

    def mock_utils_execCommand(self):
        rc = 0
        out = ['  LVM2_LV_UUID=jTHoCy-qFBS-K1FS-WSUp-SG8J-4R7W-2hI01n$LVM2_LV'
               '_NAME=mythinpool$LVM2_DATA_PERCENT=1.09$LVM2_POOL_LV=$LVM2_LV'
               '_ATTR=twi-a-tz--$LVM2_LV_SIZE=1024.00$LVM2_LV_PATH=$LVM2_LV_'
               'METADATA_SIZE=4.00$LVM2_METADATA_PERCENT=1.07$LVM2_VG_NAME='
               'mygroup']
        err = []
        return rc, out, err

    @mock.patch('plugins.check_mounts.utils.execCmd')
    def test_getLvs(self, mock_utils_execCmd):
        mock_utils_execCmd.return_value = self.mock_utils_execCommand()
        expected = {
            'mygroup/mythinpool': {
                'LVM2_LV_PATH': '',
                'LVM2_DATA_PERCENT': '1.09',
                'LVM2_LV_ATTR': 'twi-a-tz--',
                'LVM2_POOL_LV': '',
                'LVM2_LV_NAME': 'mythinpool',
                'LVM2_METADATA_PERCENT': '1.07',
                'LVM2_VG_NAME': 'mygroup',
                'LVM2_LV_SIZE': '1024.00',
                'LVM2_LV_UUID': 'jTHoCy-qFBS-K1FS-WSUp-SG8J-4R7W-2hI01n',
                'LVM2_LV_METADATA_SIZE': '4.00'}
            }
        actual = check_mounts.getLvs()
        self.assertEquals(expected, actual)

    def mock_getStats(self):
        return {'used_percent': 3.194726947731752,
                'used': 0.031635284423828125,
                'free_inode': 1048562,
                'used_inode': 14,
                'total_inode': 1048576,
                'used_percent_inode': 1,
                'total': 0.990234375,
                'free': 0.9585990905761719}

    def mock_parseProcMounts(self):
        return {
            '/brick/brk1': {
                'device': '/dev/mapper/mygroup-thinv1',
                'mountOptions': 'rw,seclabel,relatime,'
                'attr2,inode64,noquota',
                'fsType': 'xfs'},
            '/': {
                'device': '/dev/mapper/fedora_dhcp43--63-root',
                'mountOptions': 'rw,seclabel,relatime,data=ordered',
                'fsType': 'ext4'}
        }

    def mock_getLvs(self):
        return {
            '/dev/mygroup/lvol0': {
                'LVM2_LV_PATH': '/dev/mygroup/lvol0',
                'LVM2_DATA_PERCENT': '',
                'LVM2_LV_ATTR': '-wi-------',
                'LVM2_POOL_LV': '',
                'LVM2_LV_NAME': 'lvol0',
                'LVM2_METADATA_PERCENT': '',
                'LVM2_VG_NAME': 'mygroup',
                'LVM2_LV_SIZE': '4.00',
                'LVM2_LV_UUID': 'NDYC4q-Z57a-DKY3-FQt1-FT63-BuHd-ZScwJZ',
                'LVM2_LV_METADATA_SIZE': ''},
            'mygroup/mythinpool': {
                'LVM2_LV_PATH': '/dev/mygroup/mythinpool',
                'LVM2_DATA_PERCENT': '1.21',
                'LVM2_LV_ATTR': 'twi-a-tz--',
                'LVM2_POOL_LV': '',
                'LVM2_LV_NAME': 'mythinpool',
                'LVM2_METADATA_PERCENT': '1.37',
                'LVM2_VG_NAME': 'mygroup',
                'LVM2_LV_SIZE': '1536.00',
                'LVM2_LV_UUID': 'viQeR0-JHrs-5g27-vVTp-WoOw-bWFy-goaJfO',
                'LVM2_LV_METADATA_SIZE': '4.00'},
            '/dev/dm-7': {
                'LVM2_LV_PATH': '/dev/mygroup/thinv1',
                'LVM2_DATA_PERCENT': '1.04',
                'LVM2_LV_ATTR': 'Vwi-aotz--',
                'LVM2_POOL_LV': 'mythinpool',
                'LVM2_LV_NAME': 'thinv1',
                'LVM2_METADATA_PERCENT': '',
                'LVM2_VG_NAME': 'mygroup',
                'LVM2_LV_SIZE': '1024.00',
                'LVM2_LV_UUID': 'am1XA3-AgYh-IKL4-LfeB-7XR7-dm8U-vbX2Fr',
                'LVM2_LV_METADATA_SIZE': ''}
        }

    def mock_getThinpoolStat(self):
        return {'metadata_used': 5.351562500000018e-05,
                'thinpool_used_percent': 1.21,
                'thinpool_used': 0.01814999999999989,
                'metadata_used_percent': 1.37,
                'metadata_size': 0.00390625,
                'metadata_free': 0.003852734375,
                'thinpool_size': 1.5,
                'thinpool_free': 1.4818500000000001}

    @mock.patch('plugins.check_mounts.getLvs')
    @mock.patch('plugins.check_mounts._getStats')
    @mock.patch('plugins.check_mounts.os.path.realpath')
    @mock.patch('plugins.check_mounts._parseProcMounts')
    @mock.patch('plugins.check_mounts._getMountPoint')
    def test_getMountStats(self, mock_getMountPoint, mock_parseProcMounts,
                           mock_os_path_realpath,
                           mock_getStats, mock_getLvs):
        mock_getMountPoint.return_value = '/brick/brk1'
        mock_os_path_realpath.return_value = '/dev/dm-7'
        mock_parseProcMounts.return_value = self.mock_parseProcMounts()
        mock_getLvs.return_value = self.mock_getLvs()
        mock_getStats.return_value = self.mock_getStats()
        expected = {'/brick/brk1': {
            'used_percent': 3.194726947731752,
            'used': 0.031635284423828125,
            'free_inode': 1048562,
            'used_inode': 14,
            'free': 0.9585990905761719,
            'total_inode': 1048576,
            'mount_point': '/brick/brk1',
            'metadata_used_percent': 1.37,
            'total': 0.990234375,
            'thinpool_free': 1.4818500000000001,
            'metadata_used': 5.351562500000018e-05,
            'thinpool_used_percent': 1.21,
            'used_percent_inode': 1,
            'thinpool_used': 0.01814999999999989,
            'metadata_size': 0.00390625,
            'metadata_free': 0.003852734375,
            'thinpool_size': 1.5}
        }
        include = ['/brick/brk1']
        exclude = []
        actual = check_mounts.getMountStats(exclude, include)
        self.assertEquals(expected, actual)

    def _getDetail(self):
        return {'used_percent': 3.194726947731752,
                'used': 0.031635284423828125,
                'free_inode': 1048562,
                'used_inode': 14,
                'free': 0.9585990905761719,
                'total_inode': 1048576,
                'mount_point': '/run/gluster/snaps/680bdbbeed47'
                '44a9bcba941580a5feed/brick1',
                'metadata_used_percent': 1.37,
                'total': 0.990234375,
                'thinpool_free': 1.4818500000000001,
                'metadata_used': 5.351562500000018e-05,
                'thinpool_used_percent': 1.21,
                'used_percent_inode': 1,
                'thinpool_used': 0.01814999999999989,
                'metadata_size': 0.00390625,
                'metadata_free': 0.003852734375,
                'thinpool_size': 1.5}

    def test_getOutputTest(self):
        detail = self._getDetail()
        actual = check_mounts._getOutputText(detail)
        expected = "/run/gluster/snaps/680bdbbeed4744a9bcba941580a5feed/" \
                   "brick1 - {space - free: 0.959 GiB, used: 3.195%}, " \
                   "{inode - free: 1048562 , used: 1.000%}, {thinpool-data " \
                   "- free: 1.482 GiB, used: 1.210%}, {thinpool-metadata " \
                   "- free: 0.004 GiB, used: 1.370%}"
        self.assertEquals(actual, expected)

    def test_getPerfdata(self):
        detail = self._getDetail()
        warn = 80
        crit = 90
        actual = check_mounts._getPerfdata(detail, warn, crit)
        expected = "/run/gluster/snaps/680bdbbeed4744a9bcba941580a5feed/" \
                   "brick1=3.195%;80;90;0;0.990 /run/gluster/snaps/" \
                   "680bdbbeed4744a9bcba941580a5feed/brick1.inode=1.000%;80;" \
                   "90;0;1048576 /run/gluster/snaps/680bdbbeed4744a9bcba941" \
                   "580a5feed/brick1.thinpool=1.210%;80;90;0;1.500 /run/" \
                   "gluster/snaps/680bdbbeed4744a9bcba941580a5feed/brick1." \
                   "thinpool-metadata=1.370%;80;90;0;0.004"
        self.assertEquals(actual, expected)

    def test_getStatusInfo(self):
        detail = self._getDetail()
        warn = 2
        crit = 4
        actual = check_mounts._getStatusInfo(detail, warn, crit)
        expected = ('WARNING',
                    "mount point /run/gluster/snaps/"
                    "680bdbbeed4744a9bcba941580a5feed/brick1 "
                    "{space used 0.032 / 0.990 GiB}")
        self.assertEquals(actual, expected)

    def test_getPrintableStatus(self):
        mountDetail = {
            '/home': {
                'used_percent': 0.24067115853902976,
                'used': 0.0429229736328125,
                'free_inode': 1196017,
                'used_inode': 15,
                'free': 17.79177474975586,
                'total_inode': 1196032,
                'mount_point': '/home',
                'metadata_used_percent': None,
                'total': 17.834697723388672,
                'thinpool_free': None,
                'metadata_used': None,
                'thinpool_used_percent': None,
                'used_percent_inode': 1,
                'thinpool_used': None,
                'metadata_size': None,
                'metadata_free': None,
                'thinpool_size': None},
            '/boot': {
                'used_percent': 13.709776644000229,
                'used': 0.06375885009765625,
                'free_inode': 127662,
                'used_inode': 354,
                'free': 0.4013023376464844,
                'total_inode': 128016,
                'mount_point': '/boot',
                'metadata_used_percent': None,
                'total': 0.4650611877441406,
                'thinpool_free': None,
                'metadata_used': None,
                'thinpool_used_percent': None,
                'used_percent_inode': 1,
                'thinpool_used': None,
                'metadata_size': None,
                'metadata_free': None,
                'thinpool_size': None},
            '/brick/brk1': {
                'used_percent': 3.194726947731752,
                'used': 0.031635284423828125,
                'free_inode': 1048562,
                'used_inode': 14,
                'free': 0.9585990905761719,
                'total_inode': 1048576,
                'mount_point': '/brick/brk1',
                'metadata_used_percent': 1.37,
                'total': 0.990234375,
                'thinpool_free': 1.4818500000000001,
                'metadata_used': 5.351562500000018e-05,
                'thinpool_used_percent': 1.21,
                'used_percent_inode': 1,
                'thinpool_used': 0.01814999999999989,
                'metadata_size': 0.00390625,
                'metadata_free': 0.003852734375,
                'thinpool_size': 1.5
            }
        }
        expRc = 'CRITICAL'
        expMsg = ['mount point /boot {space used 0.064 / 0.465 GiB}',
                  'mount point /brick/brk1 '
                  '{space used 0.032 / 0.990 GiB}']
        expOut = ['/boot - {space - free: 0.401 GiB, used: 13.710%},'
                  ' {inode - free: 127662 , used: 1.000%}',
                  '/brick/brk1 - {space - free: 0.959 GiB, used: 3.195%},'
                  ' {inode - free: 1048562 , used: 1.000%}, '
                  '{thinpool-data - free: 1.482 GiB, used: 1.210%},'
                  ' {thinpool-metadata - free: 0.004 GiB, used: 1.370%}',
                  '/home - {space - free: 17.792 GiB, used: 0.241%},'
                  ' {inode - free: 1196017 , used: 1.000%}']
        expPerfData = ['/boot=13.710%;2;4;0;0.465 /boot.inode='
                       '1.000%;2;4;0;128016',
                       '/brick/brk1=3.195%;2;4;0;0.990 '
                       '/brick/brk1.inode=1.000%;2;4;0;1048576 '
                       '/brick/brk1.thinpool=1.210%;2;4;0;1.500 '
                       '/brick/brk1.thinpool-metadata=1.370%;2;4;0;0.004',
                       '/home=0.241%;2;4;0;17.835 '
                       '/home.inode=1.000%;2;4;0;1196032']
        warning = 2
        critical = 4
        actRc, actMsg, actOut, actPerfData = check_mounts.getPrintableStatus(
            mountDetail,
            warning,
            critical
        )
        self.assertEquals(actRc, expRc)
        self.assertEquals(actMsg, expMsg)
        self.assertEquals(actOut, expOut)
        self.assertEquals(actPerfData, expPerfData)