Federico Simoncelli has uploaded a new change for review.
Change subject: lib: add support for utillinux commands
......................................................................
lib: add support for utillinux commands
Change-Id: I2ea7dd19fadc600b8fe78fb436ae430d35f52165
Signed-off-by: Federico Simoncelli <fsimonce(a)redhat.com>
---
M lib/vdsm/Makefile.am
A lib/vdsm/utillinux.py
M tests/Makefile.am
A tests/utillinuxTests.py
M vdsm.spec.in
5 files changed, 132 insertions(+), 0 deletions(-)
git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/29/35629/1
diff --git a/lib/vdsm/Makefile.am b/lib/vdsm/Makefile.am
index b862e71..4e0868a 100644
--- a/lib/vdsm/Makefile.am
+++ b/lib/vdsm/Makefile.am
@@ -33,6 +33,7 @@
profile.py \
qemuimg.py \
sslutils.py \
+ utillinux.py \
utils.py \
vdscli.py \
virtsparsify.py \
diff --git a/lib/vdsm/utillinux.py b/lib/vdsm/utillinux.py
new file mode 100644
index 0000000..7fb42cf
--- /dev/null
+++ b/lib/vdsm/utillinux.py
@@ -0,0 +1,47 @@
+#
+# 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 os
+import re
+import signal
+
+from . import utils
+
+_blkdiscard = utils.CommandPath("blkdiscard",
+ "/sbin/blkdiscard",) # Fedora, EL6
+
+
+def blkdiscard(device, offset=None, length=None, secure=False):
+ cmd = [_blkdiscard.cmd]
+
+ if offset:
+ cmd.extend(("-o", str(offset)))
+
+ if length:
+ cmd.extend(("-l", str(length)))
+
+ if secure:
+ cmd.append("-s")
+
+ cmd.append(device)
+ rc, out, err = utils.execCmd(cmd, deathSignal=signal.SIGKILL)
+
+ if rc != 0:
+ raise QImgError(rc, out, err)
diff --git a/tests/Makefile.am b/tests/Makefile.am
index b365921..5aa9fc3 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -79,6 +79,7 @@
testlibTests.py \
toolTests.py \
transportWrapperTests.py \
+ utillinuxTests.py \
utilsTests.py \
vdsClientTests.py \
vmApiTests.py \
diff --git a/tests/utillinuxTests.py b/tests/utillinuxTests.py
new file mode 100644
index 0000000..dbbb50e
--- /dev/null
+++ b/tests/utillinuxTests.py
@@ -0,0 +1,82 @@
+#
+# 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
+#
+
+from testlib import VdsmTestCase as TestCaseBase
+import monkeypatch
+from vdsm import utillinux
+from vdsm import utils
+
+BLKDISCARD = utillinux._blkdiscard.cmd
+
+
+class FakeCmd(object):
+
+ def __init__(self, module, name, *calls):
+ self.patch = monkeypatch.Patch([(module, name, self)])
+ self.calls = list(calls)
+
+ def __call__(self, cmd, **kw):
+ call = self.calls.pop(0)
+ return call(cmd, **kw)
+
+ def __enter__(self):
+ self.patch.apply()
+
+ def __exit__(self, t=None, v=None, tb=None):
+ self.patch.revert()
+
+
+class BlkDiscardTests(TestCaseBase):
+
+ def test_no_options(self):
+ def discard(cmd, **kw):
+ expected = [BLKDISCARD, 'device']
+ self.assertEqual(cmd, expected)
+ return 0, '', ''
+
+ with FakeCmd(utils, 'execCmd', discard):
+ utillinux.blkdiscard('device')
+
+ def test_offset(self):
+ def discard(cmd, **kw):
+ expected = [BLKDISCARD, '-o', '1024', 'device']
+ self.assertEqual(cmd, expected)
+ return 0, '', ''
+
+ with FakeCmd(utils, 'execCmd', discard):
+ utillinux.blkdiscard('device', offset=1024)
+
+ def test_length(self):
+ def discard(cmd, **kw):
+ expected = [BLKDISCARD, '-l', '2048', 'device']
+ self.assertEqual(cmd, expected)
+ return 0, '', ''
+
+ with FakeCmd(utils, 'execCmd', discard):
+ utillinux.blkdiscard('device', length=2048)
+
+ def test_offset_and_length(self):
+ def discard(cmd, **kw):
+ expected = [BLKDISCARD, '-o', '1024', '-l',
'2048', 'device']
+ self.assertEqual(cmd, expected)
+ return 0, '', ''
+
+ with FakeCmd(utils, 'execCmd', discard):
+ utillinux.blkdiscard('device', offset=1024, length=2048)
diff --git a/vdsm.spec.in b/vdsm.spec.in
index d15f5c2..befe0e0 100644
--- a/vdsm.spec.in
+++ b/vdsm.spec.in
@@ -1252,6 +1252,7 @@
%{python_sitelib}/%{vdsm_name}/qemuimg.py*
%{python_sitelib}/%{vdsm_name}/netconfpersistence.py*
%{python_sitelib}/%{vdsm_name}/sslutils.py*
+%{python_sitelib}/%{vdsm_name}/utillinux.py*
%{python_sitelib}/%{vdsm_name}/utils.py*
%{python_sitelib}/%{vdsm_name}/vdscli.py*
%{python_sitelib}/%{vdsm_name}/virtsparsify.py*
--
To view, visit
http://gerrit.ovirt.org/35629
To unsubscribe, visit
http://gerrit.ovirt.org/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I2ea7dd19fadc600b8fe78fb436ae430d35f52165
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Federico Simoncelli <fsimonce(a)redhat.com>