1*23e956bfSCorey Bryant#!/usr/bin/env python 2*23e956bfSCorey Bryant# 3*23e956bfSCorey Bryant# Tests for fdsets. 4*23e956bfSCorey Bryant# 5*23e956bfSCorey Bryant# Copyright (C) 2012 IBM Corp. 6*23e956bfSCorey Bryant# 7*23e956bfSCorey Bryant# This program is free software; you can redistribute it and/or modify 8*23e956bfSCorey Bryant# it under the terms of the GNU General Public License as published by 9*23e956bfSCorey Bryant# the Free Software Foundation; either version 2 of the License, or 10*23e956bfSCorey Bryant# (at your option) any later version. 11*23e956bfSCorey Bryant# 12*23e956bfSCorey Bryant# This program is distributed in the hope that it will be useful, 13*23e956bfSCorey Bryant# but WITHOUT ANY WARRANTY; without even the implied warranty of 14*23e956bfSCorey Bryant# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15*23e956bfSCorey Bryant# GNU General Public License for more details. 16*23e956bfSCorey Bryant# 17*23e956bfSCorey Bryant# You should have received a copy of the GNU General Public License 18*23e956bfSCorey Bryant# along with this program. If not, see <http://www.gnu.org/licenses/>. 19*23e956bfSCorey Bryant# 20*23e956bfSCorey Bryant 21*23e956bfSCorey Bryantimport os 22*23e956bfSCorey Bryantimport iotests 23*23e956bfSCorey Bryantfrom iotests import qemu_img 24*23e956bfSCorey Bryant 25*23e956bfSCorey Bryantimage0 = os.path.join(iotests.test_dir, 'image0') 26*23e956bfSCorey Bryantimage1 = os.path.join(iotests.test_dir, 'image1') 27*23e956bfSCorey Bryantimage2 = os.path.join(iotests.test_dir, 'image2') 28*23e956bfSCorey Bryantimage3 = os.path.join(iotests.test_dir, 'image3') 29*23e956bfSCorey Bryantimage4 = os.path.join(iotests.test_dir, 'image4') 30*23e956bfSCorey Bryant 31*23e956bfSCorey Bryantclass TestFdSets(iotests.QMPTestCase): 32*23e956bfSCorey Bryant 33*23e956bfSCorey Bryant def setUp(self): 34*23e956bfSCorey Bryant self.vm = iotests.VM() 35*23e956bfSCorey Bryant qemu_img('create', '-f', iotests.imgfmt, image0, '128K') 36*23e956bfSCorey Bryant qemu_img('create', '-f', iotests.imgfmt, image1, '128K') 37*23e956bfSCorey Bryant qemu_img('create', '-f', iotests.imgfmt, image2, '128K') 38*23e956bfSCorey Bryant qemu_img('create', '-f', iotests.imgfmt, image3, '128K') 39*23e956bfSCorey Bryant qemu_img('create', '-f', iotests.imgfmt, image4, '128K') 40*23e956bfSCorey Bryant self.file0 = open(image0, 'r') 41*23e956bfSCorey Bryant self.file1 = open(image1, 'w+') 42*23e956bfSCorey Bryant self.file2 = open(image2, 'r') 43*23e956bfSCorey Bryant self.file3 = open(image3, 'r') 44*23e956bfSCorey Bryant self.file4 = open(image4, 'r') 45*23e956bfSCorey Bryant self.vm.add_fd(self.file0.fileno(), 1, 'image0:r') 46*23e956bfSCorey Bryant self.vm.add_fd(self.file1.fileno(), 1, 'image1:w+') 47*23e956bfSCorey Bryant self.vm.add_fd(self.file2.fileno(), 0, 'image2:r') 48*23e956bfSCorey Bryant self.vm.add_fd(self.file3.fileno(), 2, 'image3:r') 49*23e956bfSCorey Bryant self.vm.add_fd(self.file4.fileno(), 2, 'image4:r') 50*23e956bfSCorey Bryant self.vm.add_drive("/dev/fdset/1") 51*23e956bfSCorey Bryant self.vm.launch() 52*23e956bfSCorey Bryant 53*23e956bfSCorey Bryant def tearDown(self): 54*23e956bfSCorey Bryant self.vm.shutdown() 55*23e956bfSCorey Bryant self.file0.close() 56*23e956bfSCorey Bryant self.file1.close() 57*23e956bfSCorey Bryant self.file2.close() 58*23e956bfSCorey Bryant self.file3.close() 59*23e956bfSCorey Bryant self.file4.close() 60*23e956bfSCorey Bryant os.remove(image0) 61*23e956bfSCorey Bryant os.remove(image1) 62*23e956bfSCorey Bryant os.remove(image2) 63*23e956bfSCorey Bryant os.remove(image3) 64*23e956bfSCorey Bryant os.remove(image4) 65*23e956bfSCorey Bryant 66*23e956bfSCorey Bryant def test_query_fdset(self): 67*23e956bfSCorey Bryant result = self.vm.qmp('query-fdsets') 68*23e956bfSCorey Bryant self.assert_qmp(result, 'return[0]/fdset-id', 2) 69*23e956bfSCorey Bryant self.assert_qmp(result, 'return[1]/fdset-id', 1) 70*23e956bfSCorey Bryant self.assert_qmp(result, 'return[2]/fdset-id', 0) 71*23e956bfSCorey Bryant self.assert_qmp(result, 'return[0]/fds[0]/opaque', 'image3:r') 72*23e956bfSCorey Bryant self.assert_qmp(result, 'return[0]/fds[1]/opaque', 'image4:r') 73*23e956bfSCorey Bryant self.assert_qmp(result, 'return[1]/fds[0]/opaque', 'image0:r') 74*23e956bfSCorey Bryant self.assert_qmp(result, 'return[1]/fds[1]/opaque', 'image1:w+') 75*23e956bfSCorey Bryant self.assert_qmp(result, 'return[2]/fds[0]/opaque', 'image2:r') 76*23e956bfSCorey Bryant self.vm.shutdown() 77*23e956bfSCorey Bryant 78*23e956bfSCorey Bryant def test_remove_fdset(self): 79*23e956bfSCorey Bryant result = self.vm.qmp('remove-fd', fdset_id=2) 80*23e956bfSCorey Bryant self.assert_qmp(result, 'return', {}) 81*23e956bfSCorey Bryant result = self.vm.qmp('query-fdsets') 82*23e956bfSCorey Bryant self.assert_qmp(result, 'return[0]/fdset-id', 1) 83*23e956bfSCorey Bryant self.assert_qmp(result, 'return[1]/fdset-id', 0) 84*23e956bfSCorey Bryant self.assert_qmp(result, 'return[0]/fds[0]/opaque', 'image0:r') 85*23e956bfSCorey Bryant self.assert_qmp(result, 'return[0]/fds[1]/opaque', 'image1:w+') 86*23e956bfSCorey Bryant self.assert_qmp(result, 'return[1]/fds[0]/opaque', 'image2:r') 87*23e956bfSCorey Bryant self.vm.shutdown() 88*23e956bfSCorey Bryant 89*23e956bfSCorey Bryant def test_remove_fd(self): 90*23e956bfSCorey Bryant result = self.vm.qmp('query-fdsets') 91*23e956bfSCorey Bryant fd_image3 = result['return'][0]['fds'][0]['fd'] 92*23e956bfSCorey Bryant result = self.vm.qmp('remove-fd', fdset_id=2, fd=fd_image3) 93*23e956bfSCorey Bryant self.assert_qmp(result, 'return', {}) 94*23e956bfSCorey Bryant result = self.vm.qmp('query-fdsets') 95*23e956bfSCorey Bryant self.assert_qmp(result, 'return[0]/fdset-id', 2) 96*23e956bfSCorey Bryant self.assert_qmp(result, 'return[1]/fdset-id', 1) 97*23e956bfSCorey Bryant self.assert_qmp(result, 'return[2]/fdset-id', 0) 98*23e956bfSCorey Bryant self.assert_qmp(result, 'return[0]/fds[0]/opaque', 'image4:r') 99*23e956bfSCorey Bryant self.assert_qmp(result, 'return[1]/fds[0]/opaque', 'image0:r') 100*23e956bfSCorey Bryant self.assert_qmp(result, 'return[1]/fds[1]/opaque', 'image1:w+') 101*23e956bfSCorey Bryant self.assert_qmp(result, 'return[2]/fds[0]/opaque', 'image2:r') 102*23e956bfSCorey Bryant self.vm.shutdown() 103*23e956bfSCorey Bryant 104*23e956bfSCorey Bryant def test_remove_fd_invalid_fdset(self): 105*23e956bfSCorey Bryant result = self.vm.qmp('query-fdsets') 106*23e956bfSCorey Bryant fd_image3 = result['return'][0]['fds'][0]['fd'] 107*23e956bfSCorey Bryant result = self.vm.qmp('remove-fd', fdset_id=3, fd=fd_image3) 108*23e956bfSCorey Bryant self.assert_qmp(result, 'error/class', 'GenericError') 109*23e956bfSCorey Bryant self.assert_qmp(result, 'error/desc', 110*23e956bfSCorey Bryant 'File descriptor named \'fdset-id:3, fd:%d\' not found' % fd_image3) 111*23e956bfSCorey Bryant self.vm.shutdown() 112*23e956bfSCorey Bryant 113*23e956bfSCorey Bryant def test_remove_fd_invalid_fd(self): 114*23e956bfSCorey Bryant result = self.vm.qmp('query-fdsets') 115*23e956bfSCorey Bryant result = self.vm.qmp('remove-fd', fdset_id=2, fd=999) 116*23e956bfSCorey Bryant self.assert_qmp(result, 'error/class', 'GenericError') 117*23e956bfSCorey Bryant self.assert_qmp(result, 'error/desc', 118*23e956bfSCorey Bryant 'File descriptor named \'fdset-id:2, fd:999\' not found') 119*23e956bfSCorey Bryant self.vm.shutdown() 120*23e956bfSCorey Bryant 121*23e956bfSCorey Bryant def test_add_fd_invalid_fd(self): 122*23e956bfSCorey Bryant result = self.vm.qmp('add-fd', fdset_id=2) 123*23e956bfSCorey Bryant self.assert_qmp(result, 'error/class', 'GenericError') 124*23e956bfSCorey Bryant self.assert_qmp(result, 'error/desc', 125*23e956bfSCorey Bryant 'No file descriptor supplied via SCM_RIGHTS') 126*23e956bfSCorey Bryant self.vm.shutdown() 127*23e956bfSCorey Bryant 128*23e956bfSCorey Bryantif __name__ == '__main__': 129*23e956bfSCorey Bryant iotests.main(supported_fmts=['raw']) 130