Skip to contentSkip to conversation

qemu-iotests: add test for fd passing via SCM rights

PATCH Superseded Wenchao Xia 2 participants 7 messages Sep 2, 2013 – Sep 3, 2013

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover qemu-iotests: add test for fd passing via SCM rights

This series add test case for fd passing with unix socket at runtime. Since getfd and closefd interface will interact with monitor's data, so it will help to do regression test for monitor patches. Since python2 do not support sendmsg(), so a C helper program is added to do the job.

v2:
1: add missing $ in the makefile rule.

v3:
Address Eric's comments:
1: typo fix, remove "." in the end of error message, strick
check argc as "!=", use EXIT_SUCCESS and EXIT_FAILURE as exit values, strict error check for strtol() call.
Address Luiz's comments:
1: change the helper program parameter as "bin < socket-fd > < file-path >",
the program open the file itself now, data parameter is removed and blank is always used as iov data, better usage tip message, folder the string parsing code into a function.
2: related change for helper program parameter change.
3: related change for helper program parameter change.
Other:
1: remove "LINK" rule in makefile, remove fd checking code inside send_fd()
since it is already checked before calling, add '' around %s for path and number string in error message.
2: renamed fd_bin to bin in send_fd_scm() to tip better, add '' around %s
for path in error message.

Wenchao Xia (3):
1 qemu-iotests: add unix socket help program
2 qemu-iotests: add infrastructure of fd passing via SCM 3 qemu-iotests: add tests for runtime fd passing via SCM rights

 QMP/qmp.py                             |    6 ++
 configure                              |    2 +-
 tests/Makefile                         |    3 +-
 tests/qemu-iotests/045                 |   36 ++++++++-
 tests/qemu-iotests/045.out             |    4 +-
 tests/qemu-iotests/check               |    1 +
 tests/qemu-iotests/iotests.py          |   23 ++++++
 tests/qemu-iotests/socket_scm_helper.c |  135 ++++++++++++++++++++++++++++++++
 8 files changed, 205 insertions(+), 5 deletions(-)
 create mode 100644 tests/qemu-iotests/socket_scm_helper.c
#1 lore raw

PATCH v3 1/3 qemu-iotests: add unix socket help program

This program can do a sendmsg call to transfer fd with unix socket, which is not supported in python2.

The built binary will not be deleted in clean, but it is a existing issue in ./tests, which should be solved in another patch.

---
 configure                              |    2 +-
 tests/Makefile                         |    3 +-
 tests/qemu-iotests/socket_scm_helper.c |  135 ++++++++++++++++++++++++++++++++
 3 files changed, 138 insertions(+), 2 deletions(-)
 create mode 100644 tests/qemu-iotests/socket_scm_helper.c
configure +1 -1
index 0a55c20..5080c38 100755
@@ -4544,7 +4544,7 @@ if [ "$dtc_internal" = "yes" ]; then
 fi
 
 # build tree in object directory in case the source is not in the current directory
-DIRS="tests tests/tcg tests/tcg/cris tests/tcg/lm32 tests/libqos tests/qapi-schema tests/tcg/xtensa"
+DIRS="tests tests/tcg tests/tcg/cris tests/tcg/lm32 tests/libqos tests/qapi-schema tests/tcg/xtensa tests/qemu-iotests"
 DIRS="$DIRS pc-bios/optionrom pc-bios/spapr-rtas pc-bios/s390-ccw"
 DIRS="$DIRS roms/seabios roms/vgabios"
 DIRS="$DIRS qapi-generated"
tests/Makefile +2 -1
index baba9e9..c285805 100644
@@ -172,6 +172,7 @@ tests/boot-order-test$(EXESUF): tests/boot-order-test.o $(libqos-obj-y)
 tests/tmp105-test$(EXESUF): tests/tmp105-test.o $(libqos-omap-obj-y)
 tests/i440fx-test$(EXESUF): tests/i440fx-test.o $(libqos-pc-obj-y)
 tests/fw_cfg-test$(EXESUF): tests/fw_cfg-test.o $(libqos-pc-obj-y)
+tests/qemu-iotests/socket_scm_helper$(EXESUF): tests/qemu-iotests/socket_scm_helper.o
 
 # QTest rules
 
@@ -250,7 +251,7 @@ check-report.html: check-report.xml
 # Other tests
 
 .PHONY: check-tests/qemu-iotests-quick.sh
-check-tests/qemu-iotests-quick.sh: tests/qemu-iotests-quick.sh qemu-img$(EXESUF) qemu-io$(EXESUF)
+check-tests/qemu-iotests-quick.sh: tests/qemu-iotests-quick.sh qemu-img$(EXESUF) qemu-io$(EXESUF) tests/qemu-iotests/socket_scm_helper$(EXESUF)
 	$<
 
 .PHONY: check-tests/test-qapi.py
tests/qemu-iotests/socket_scm_helper.c new file +135 -0

135 lines not on this page · raw message

Signature · 1 line
1.7.1
#2 lore raw

PATCH v3 2/3 qemu-iotests: add infrastructure of fd passing via SCM

In reply to Wenchao Xia

This patch make use of the compiled scm helper program to transfer fd via unix socket at runtime.

---
 QMP/qmp.py                    |    6 ++++++
 tests/qemu-iotests/check      |    1 +
 tests/qemu-iotests/iotests.py |   23 +++++++++++++++++++++++
 3 files changed, 30 insertions(+), 0 deletions(-)
QMP/qmp.py +6 -0
index c551df1..074f09a 100644
@@ -188,3 +188,9 @@ class QEMUMonitorProtocol:
 
     def settimeout(self, timeout):
         self.__sock.settimeout(timeout)
+
+    def get_sock_fd(self):
+        return self.__sock.fileno()
+
+    def is_scm_available(self):
+        return self.__sock.family == socket.AF_UNIX
tests/qemu-iotests/check +1 -0
index 74628ae..2f7f78e 100755
@@ -164,6 +164,7 @@ QEMU_IO       -- $QEMU_IO
 IMGFMT        -- $FULL_IMGFMT_DETAILS
 IMGPROTO      -- $FULL_IMGPROTO_DETAILS
 PLATFORM      -- $FULL_HOST_DETAILS
+SOCKET_SCM_HELPER -- $SOCKET_SCM_HELPER
 
 EOF
 #MKFS_OPTIONS  -- $FULL_MKFS_OPTIONS
tests/qemu-iotests/iotests.py +23 -0
index 33ad0ec..d79cc56 100644
@@ -38,6 +38,8 @@ imgfmt = os.environ.get('IMGFMT', 'raw')
 imgproto = os.environ.get('IMGPROTO', 'file')
 test_dir = os.environ.get('TEST_DIR', '/var/tmp')
 
+socket_scm_helper = os.environ.get('SOCKET_SCM_HELPER', 'socket_scm_helper')
+
 def qemu_img(*args):
     '''Run qemu-img and return the exit code'''
     devnull = open('/dev/null', 'r+')
@@ -80,6 +82,12 @@ class VM(object):
                      '-display', 'none', '-vga', 'none']
         self._num_drives = 0
 
+    #This can be used to add unused monitor
+    def add_monitor_telnet(self, ip, port):
+        args = 'tcp:%s:%d,server,nowait,telnet' % (ip, port)
+        self._args.append('-monitor')
+        self._args.append(args)
+
     def add_drive(self, path, opts=''):
         '''Add a virtio-blk drive to the VM'''
         options = ['if=virtio',
@@ -112,6 +120,21 @@ class VM(object):
         self._args.append(','.join(options))
         return self
 
+    def send_fd_scm(self, fd_file_path):
+        #In iotest.py, the qmp should always use unix socket.
+        assert self._qmp.is_scm_available()
+        bin = socket_scm_helper
+        if os.path.exists(bin) == False:
+            print "Scm help program does not present, path '%s'." % bin
+            return -1
+        fd_param = ["%s" % bin,
+                    "%d" % self._qmp.get_sock_fd(),
+                    "%s" % fd_file_path]
+        devnull = open('/dev/null', 'rb')
+        p = subprocess.Popen(fd_param, stdin=devnull, stdout=sys.stdout,
+                             stderr=sys.stderr)
+        return p.wait()
+
     def launch(self):
         '''Launch the VM and establish a QMP connection'''
         devnull = open('/dev/null', 'rb')
Signature · 1 line
1.7.1
#3 lore raw

PATCH v3 3/3 qemu-iotests: add tests for runtime fd passing via SCM rights

In reply to Wenchao Xia

This case will test whether the monitor can receive fd at runtime. To verify better, additional monitor is created to see if qemu can handler two monitor instances correctly.

---
 tests/qemu-iotests/045     |   36 +++++++++++++++++++++++++++++++++++-
 tests/qemu-iotests/045.out |    4 ++--
 2 files changed, 37 insertions(+), 3 deletions(-)
tests/qemu-iotests/045 +35 -1
index 2b6f1af..4381d8a 100755
@@ -1,6 +1,6 @@
 #!/usr/bin/env python
 #
-# Tests for fdsets.
+# Tests for fdsets and getfd.
 #
 # Copyright (C) 2012 IBM Corp.
 #
@@ -125,5 +125,39 @@ class TestFdSets(iotests.QMPTestCase):
                 'No file descriptor supplied via SCM_RIGHTS')
         self.vm.shutdown()
 
+#Add fd at runtime, there are two ways: monitor related or fdset related
+class TestSCMFd(iotests.QMPTestCase):
+    def setUp(self):
+        self.vm = iotests.VM()
+        qemu_img('create', '-f', iotests.imgfmt, image0, '128K')
+        #Add a unused monitor, to verify it works fine when two minitor present
+        self.vm.add_monitor_telnet("0",4445)
+        self.vm.launch()
+
+    def tearDown(self):
+        self.vm.shutdown()
+        os.remove(image0)
+
+    def _send_fd_by_SCM(self):
+        ret = self.vm.send_fd_scm(image0)
+        self.assertEqual(ret, 0, 'Failed to send fd with UNIX SCM')
+
+    def test_add_fd(self):
+        self._send_fd_by_SCM()
+        result = self.vm.qmp('add-fd', fdset_id=2, opaque='image0:r')
+        self.assert_qmp(result, 'return/fdset-id', 2)
+
+    def test_getfd(self):
+        self._send_fd_by_SCM()
+        result = self.vm.qmp('getfd', fdname='image0:r')
+        self.assert_qmp(result, 'return', {})
+
+    def test_closefd(self):
+        self._send_fd_by_SCM()
+        result = self.vm.qmp('getfd', fdname='image0:r')
+        self.assert_qmp(result, 'return', {})
+        result = self.vm.qmp('closefd', fdname='image0:r')
+        self.assert_qmp(result, 'return', {})
+
 if __name__ == '__main__':
     iotests.main(supported_fmts=['raw'])
tests/qemu-iotests/045.out +2 -2
index 3f8a935..dae404e 100644
@@ -1,5 +1,5 @@
-......
+.........
 ----------------------------------------------------------------------
-Ran 6 tests
+Ran 9 tests
 
 OK
Signature · 1 line
1.7.1
#4 lore raw

Re: [Qemu-devel] [PATCH V3 3/3] qemu-iotests: add tests for runtime fd passing via SCM rights

tests/qemu-iotests/045 class TestFdSets(iotests.QMPTestCase): · line 134

Quoted 11 lines · Wenchao Xia
tests/qemu-iotests/045 +7 -0
@@ -125,5 +125,39 @@ class TestFdSets(iotests.QMPTestCase):
                 'No file descriptor supplied via SCM_RIGHTS')
         self.vm.shutdown()
 
+#Add fd at runtime, there are two ways: monitor related or fdset related
+class TestSCMFd(iotests.QMPTestCase):
+    def setUp(self):
+        self.vm = iotests.VM()
+        qemu_img('create', '-f', iotests.imgfmt, image0, '128K')
+        #Add a unused monitor, to verify it works fine when two minitor present
+        self.vm.add_monitor_telnet("0",4445)

Sorry to be picky: please use the same whitespace style as the existing code in these patches.

# Comments have a space after the hash
function(args, have, space)

Quoted 16 lines
tests/qemu-iotests/045 +16 -0
+        self.vm.launch()
+
+    def tearDown(self):
+        self.vm.shutdown()
+        os.remove(image0)
+
+    def _send_fd_by_SCM(self):
+        ret = self.vm.send_fd_scm(image0)
+        self.assertEqual(ret, 0, 'Failed to send fd with UNIX SCM')
+
+    def test_add_fd(self):
+        self._send_fd_by_SCM()
+        result = self.vm.qmp('add-fd', fdset_id=2, opaque='image0:r')
+        self.assert_qmp(result, 'return/fdset-id', 2)
+
+    def test_getfd(self):

tests/qemu-iotests/045 class TestFdSets(iotests.QMPTestCase): · line 160

Quoted 10 lines
tests/qemu-iotests/045 +10 -0
+        self._send_fd_by_SCM()
+        result = self.vm.qmp('getfd', fdname='image0:r')
+        self.assert_qmp(result, 'return', {})
+
+    def test_closefd(self):
+        self._send_fd_by_SCM()
+        result = self.vm.qmp('getfd', fdname='image0:r')
+        self.assert_qmp(result, 'return', {})
+        result = self.vm.qmp('closefd', fdname='image0:r')
+        self.assert_qmp(result, 'return', {})

It would be good to also check the error cases like the existing tests do (e.g. getfd fdname=asdf -> error).

#5 lore raw

Re: [Qemu-devel] [PATCH V3 3/3] qemu-iotests: add tests for runtime fd passing via SCM rights

Quoted 16 lines · Stefan Hajnoczi

tests/qemu-iotests/045 class TestFdSets(iotests.QMPTestCase): · line 134

Quoted 11 lines · Wenchao Xia
tests/qemu-iotests/045 +7 -0
@@ -125,5 +125,39 @@ class TestFdSets(iotests.QMPTestCase):
                  'No file descriptor supplied via SCM_RIGHTS')
          self.vm.shutdown()
 
+#Add fd at runtime, there are two ways: monitor related or fdset related
+class TestSCMFd(iotests.QMPTestCase):
+    def setUp(self):
+        self.vm = iotests.VM()
+        qemu_img('create', '-f', iotests.imgfmt, image0, '128K')
+        #Add a unused monitor, to verify it works fine when two minitor present
+        self.vm.add_monitor_telnet("0",4445)

Sorry to be picky: please use the same whitespace style as the existing code in these patches.

Quoted 3 lines

# Comments have a space after the hash
function(args, have, space)

OK, will check all the comments and fix.

Quoted 21 lines
Quoted 21 lines
tests/qemu-iotests/045 +21 -0
+        self.vm.launch()
+
+    def tearDown(self):
+        self.vm.shutdown()
+        os.remove(image0)
+
+    def _send_fd_by_SCM(self):
+        ret = self.vm.send_fd_scm(image0)
+        self.assertEqual(ret, 0, 'Failed to send fd with UNIX SCM')
+
+    def test_add_fd(self):
+        self._send_fd_by_SCM()
+        result = self.vm.qmp('add-fd', fdset_id=2, opaque='image0:r')
+        self.assert_qmp(result, 'return/fdset-id', 2)
+
+    def test_getfd(self):
+        self._send_fd_by_SCM()
+        result = self.vm.qmp('getfd', fdname='image0:r')
+        self.assert_qmp(result, 'return', {})
+
+    def test_closefd(self):
Quoted 9 lines

tests/qemu-iotests/045 class TestFdSets(iotests.QMPTestCase): · line 160

Quoted 5 lines
tests/qemu-iotests/045 +5 -0
+        self._send_fd_by_SCM()
+        result = self.vm.qmp('getfd', fdname='image0:r')
+        self.assert_qmp(result, 'return', {})
+        result = self.vm.qmp('closefd', fdname='image0:r')
+        self.assert_qmp(result, 'return', {})

It would be good to also check the error cases like the existing tests do (e.g. getfd fdname=asdf -> error).

OK. I'll wait a few days and respin with fix.

Signature · 3 lines
Best Regards

Wenchao Xia
#6 lore raw
In reply to Wenchao Xia
Quoted 32 lines · Wenchao Xia

This series add test case for fd passing with unix socket at runtime. Since getfd and closefd interface will interact with monitor's data, so it will help to do regression test for monitor patches. Since python2 do not support sendmsg(), so a C helper program is added to do the job.

v2:
1: add missing $ in the makefile rule.

v3:
Address Eric's comments:
1: typo fix, remove "." in the end of error message, strick
check argc as "!=", use EXIT_SUCCESS and EXIT_FAILURE as exit values, strict error check for strtol() call.
Address Luiz's comments:
1: change the helper program parameter as "bin < socket-fd > < file-path >",
the program open the file itself now, data parameter is removed and blank is always used as iov data, better usage tip message, folder the string parsing code into a function.
2: related change for helper program parameter change.
3: related change for helper program parameter change.
Other:
1: remove "LINK" rule in makefile, remove fd checking code inside send_fd()
since it is already checked before calling, add '' around %s for path and number string in error message.
2: renamed fd_bin to bin in send_fd_scm() to tip better, add '' around %s
for path in error message.

Wenchao Xia (3):
1 qemu-iotests: add unix socket help program
2 qemu-iotests: add infrastructure of fd passing via SCM 3 qemu-iotests: add tests for runtime fd passing via SCM rights

Quoted 10 lines
 QMP/qmp.py                             |    6 ++
 configure                              |    2 +-
 tests/Makefile                         |    3 +-
 tests/qemu-iotests/045                 |   36 ++++++++-
 tests/qemu-iotests/045.out             |    4 +-
 tests/qemu-iotests/check               |    1 +
 tests/qemu-iotests/iotests.py          |   23 ++++++
 tests/qemu-iotests/socket_scm_helper.c |  135 ++++++++++++++++++++++++++++++++
 8 files changed, 205 insertions(+), 5 deletions(-)
 create mode 100644 tests/qemu-iotests/socket_scm_helper.c

Looks useful. I have left comments about whitespace cleanups and requested a few additional tests for addfd/getfd/closefd errors.