From fd4843f8e72227318b1e2ee5fbd2d43cc3732a1d Mon Sep 17 00:00:00 2001 From: Kun Huang Date: Wed, 13 Nov 2013 19:20:16 +0800 Subject: [PATCH] catch OSError to prevent breaking request /recon/diskusage swift.common.utils.ismount maybe raise some OSError in some special cases; and the request against /recon/diskusage doesn't handle it before. This patch let output of mounted keyword is the error's message. Change-Id: I5d9018f580181e618a3fa072b7a760d41795d8eb Closes-Bug: #1249181 --- swift/common/middleware/recon.py | 9 ++++++++- test/unit/common/middleware/test_recon.py | 12 ++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/swift/common/middleware/recon.py b/swift/common/middleware/recon.py index 3c37e00ce2..ffd289f3d1 100644 --- a/swift/common/middleware/recon.py +++ b/swift/common/middleware/recon.py @@ -201,7 +201,14 @@ class ReconMiddleware(object): """get disk utilization statistics""" devices = [] for entry in os.listdir(self.devices): - if check_mount(self.devices, entry): + try: + mounted = check_mount(self.devices, entry) + except OSError as err: + devices.append({'device': entry, 'mounted': str(err), + 'size': '', 'used': '', 'avail': ''}) + continue + + if mounted: path = os.path.join(self.devices, entry) disk = os.statvfs(path) capacity = disk.f_bsize * disk.f_blocks diff --git a/test/unit/common/middleware/test_recon.py b/test/unit/common/middleware/test_recon.py index ba72f67b98..64afe4e085 100644 --- a/test/unit/common/middleware/test_recon.py +++ b/test/unit/common/middleware/test_recon.py @@ -27,6 +27,10 @@ from swift.common.middleware import recon from swift.common.utils import json +def fake_check_mount(a, b): + raise OSError('Input/Output Error') + + class FakeApp(object): def __call__(self, env, start_response): return "FAKE APP" @@ -606,6 +610,14 @@ class TestReconSuccess(TestCase): [(('/srv/node/canhazdrive1',), {})]) self.assertEquals(rv, du_resp) + @mock.patch("swift.common.middleware.recon.check_mount", fake_check_mount) + def test_get_diskusage_oserror(self): + du_resp = [{'device': 'canhazdrive1', 'avail': '', + 'mounted': 'Input/Output Error', 'used': '', 'size': ''}] + self.mockos.ls_output = ['canhazdrive1'] + rv = self.app.get_diskusage() + self.assertEquals(rv, du_resp) + def test_get_quarantine_count(self): self.mockos.ls_output = ['sda'] self.mockos.ismount_output = True