tests: catch and check stderr from invalid argument type tests

Verify if tool failed with expected message, but also make tests output
more readable.
This commit is contained in:
Marek Marczykowski-Górecki 2017-03-11 22:10:31 +01:00
parent c1ae5f8bf5
commit ede625d1dd
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724
8 changed files with 76 additions and 19 deletions

View File

@ -37,3 +37,19 @@ class StdoutBuffer(object):
def __exit__(self, exc_type, exc_val, exc_tb):
sys.stdout = sys.__stdout__
return False
class StderrBuffer(object):
def __init__(self):
if sys.version_info[0] >= 3:
self.stderr = io.StringIO()
else:
self.stderr = io.BytesIO()
def __enter__(self):
sys.stderr = self.stderr
return self.stderr
def __exit__(self, exc_type, exc_val, exc_tb):
sys.stderr = sys.__stderr__
return False

View File

@ -54,8 +54,11 @@ class TC_00_qubes_prefs(qubesmgmt.tests.QubesTestCase):
('dom0', 'mgmt.property.Get', 'no_such_property', None)] = \
b'2\x00AttributeError\x00\x00no_such_property\x00'
with self.assertRaises(SystemExit):
qubesmgmt.tools.qubes_prefs.main([
'no_such_property'], app=self.app)
with qubesmgmt.tests.tools.StderrBuffer() as stderr:
qubesmgmt.tools.qubes_prefs.main([
'no_such_property'], app=self.app)
self.assertIn('no such property: \'no_such_property\'',
stderr.getvalue())
self.assertAllCalled()
def test_004_set_invalid_property(self):
@ -63,6 +66,9 @@ class TC_00_qubes_prefs(qubesmgmt.tests.QubesTestCase):
('dom0', 'mgmt.property.Set', 'no_such_property', b'value')]\
= b'2\x00AttributeError\x00\x00no_such_property\x00'
with self.assertRaises(SystemExit):
qubesmgmt.tools.qubes_prefs.main([
'no_such_property', 'value'], app=self.app)
with qubesmgmt.tests.tools.StderrBuffer() as stderr:
qubesmgmt.tools.qubes_prefs.main([
'no_such_property', 'value'], app=self.app)
self.assertIn('no such property: \'no_such_property\'',
stderr.getvalue())
self.assertAllCalled()

View File

@ -19,6 +19,7 @@
# with this program; if not, see <http://www.gnu.org/licenses/>.
import qubesmgmt.tests
import qubesmgmt.tests.tools
import qubesmgmt.tools.qvm_kill
@ -34,7 +35,10 @@ class TC_00_qvm_kill(qubesmgmt.tests.QubesTestCase):
def test_001_missing_vm(self):
with self.assertRaises(SystemExit):
qubesmgmt.tools.qvm_kill.main([], app=self.app)
with qubesmgmt.tests.tools.StderrBuffer() as stderr:
qubesmgmt.tools.qvm_kill.main([], app=self.app)
self.assertIn('one of the arguments --all VMNAME is required',
stderr.getvalue())
self.assertAllCalled()
def test_002_invalid_vm(self):
@ -42,7 +46,9 @@ class TC_00_qvm_kill(qubesmgmt.tests.QubesTestCase):
('dom0', 'mgmt.vm.List', None, None)] = \
b'0\x00some-vm class=AppVM state=Running\n'
with self.assertRaises(SystemExit):
qubesmgmt.tools.qvm_kill.main(['no-such-vm'], app=self.app)
with qubesmgmt.tests.tools.StderrBuffer() as stderr:
qubesmgmt.tools.qvm_kill.main(['no-such-vm'], app=self.app)
self.assertIn('no such domain', stderr.getvalue())
self.assertAllCalled()
def test_003_not_running(self):

View File

@ -19,6 +19,7 @@
# with this program; if not, see <http://www.gnu.org/licenses/>.
import qubesmgmt.tests
import qubesmgmt.tests.tools
import qubesmgmt.tools.qvm_pause
@ -34,7 +35,10 @@ class TC_00_qvm_pause(qubesmgmt.tests.QubesTestCase):
def test_001_missing_vm(self):
with self.assertRaises(SystemExit):
qubesmgmt.tools.qvm_pause.main([], app=self.app)
with qubesmgmt.tests.tools.StderrBuffer() as stderr:
qubesmgmt.tools.qvm_pause.main([], app=self.app)
self.assertIn('one of the arguments --all VMNAME is required',
stderr.getvalue())
self.assertAllCalled()
def test_002_invalid_vm(self):
@ -42,7 +46,9 @@ class TC_00_qvm_pause(qubesmgmt.tests.QubesTestCase):
('dom0', 'mgmt.vm.List', None, None)] = \
b'0\x00some-vm class=AppVM state=Running\n'
with self.assertRaises(SystemExit):
qubesmgmt.tools.qvm_pause.main(['no-such-vm'], app=self.app)
with qubesmgmt.tests.tools.StderrBuffer() as stderr:
qubesmgmt.tools.qvm_pause.main(['no-such-vm'], app=self.app)
self.assertIn('no such domain', stderr.getvalue())
self.assertAllCalled()
def test_003_not_running(self):

View File

@ -17,6 +17,7 @@
#
# You should have received a copy of the GNU Lesser General Public License along
# with this program; if not, see <http://www.gnu.org/licenses/>.
import sys
import qubesmgmt.tests
import qubesmgmt.tests.tools
@ -47,7 +48,13 @@ class TC_00_qvm_prefs(qubesmgmt.tests.QubesTestCase):
def test_001_no_vm(self):
with self.assertRaises(SystemExit):
qubesmgmt.tools.qvm_prefs.main([], app=self.app)
with qubesmgmt.tests.tools.StderrBuffer() as stderr:
qubesmgmt.tools.qvm_prefs.main([], app=self.app)
if sys.version_info[0] == 2:
self.assertIn('too few arguments', stderr.getvalue())
else:
self.assertIn('error: the following arguments are required: VMNAME',
stderr.getvalue())
self.assertAllCalled()
def test_002_set_property(self):
@ -69,8 +76,11 @@ class TC_00_qvm_prefs(qubesmgmt.tests.QubesTestCase):
('dom0', 'mgmt.vm.property.Get', 'no_such_property', None)] = \
b'2\x00AttributeError\x00\x00no_such_property\x00'
with self.assertRaises(SystemExit):
qubesmgmt.tools.qvm_prefs.main([
'dom0', 'no_such_property'], app=self.app)
with qubesmgmt.tests.tools.StderrBuffer() as stderr:
qubesmgmt.tools.qvm_prefs.main([
'dom0', 'no_such_property'], app=self.app)
self.assertIn('no such property: \'no_such_property\'',
stderr.getvalue())
self.assertAllCalled()
def test_004_set_invalid_property(self):
@ -81,6 +91,9 @@ class TC_00_qvm_prefs(qubesmgmt.tests.QubesTestCase):
('dom0', 'mgmt.vm.property.Set', 'no_such_property', b'value')] = \
b'2\x00AttributeError\x00\x00no_such_property\x00'
with self.assertRaises(SystemExit):
qubesmgmt.tools.qvm_prefs.main([
'dom0', 'no_such_property', 'value'], app=self.app)
with qubesmgmt.tests.tools.StderrBuffer() as stderr:
qubesmgmt.tools.qvm_prefs.main([
'dom0', 'no_such_property', 'value'], app=self.app)
self.assertIn('no such property: \'no_such_property\'',
stderr.getvalue())
self.assertAllCalled()

View File

@ -34,7 +34,10 @@ class TC_00_qvm_shutdown(qubesmgmt.tests.QubesTestCase):
def test_001_missing_vm(self):
with self.assertRaises(SystemExit):
qubesmgmt.tools.qvm_shutdown.main([], app=self.app)
with qubesmgmt.tests.tools.StderrBuffer() as stderr:
qubesmgmt.tools.qvm_shutdown.main([], app=self.app)
self.assertIn('one of the arguments --all VMNAME is required',
stderr.getvalue())
self.assertAllCalled()
def test_002_invalid_vm(self):
@ -42,7 +45,9 @@ class TC_00_qvm_shutdown(qubesmgmt.tests.QubesTestCase):
('dom0', 'mgmt.vm.List', None, None)] = \
b'0\x00some-vm class=AppVM state=Running\n'
with self.assertRaises(SystemExit):
qubesmgmt.tools.qvm_shutdown.main(['no-such-vm'], app=self.app)
with qubesmgmt.tests.tools.StderrBuffer() as stderr:
qubesmgmt.tools.qvm_shutdown.main(['no-such-vm'], app=self.app)
self.assertIn('no such domain', stderr.getvalue())
self.assertAllCalled()
def test_003_not_running(self):

View File

@ -34,7 +34,10 @@ class TC_00_qvm_unpause(qubesmgmt.tests.QubesTestCase):
def test_001_missing_vm(self):
with self.assertRaises(SystemExit):
qubesmgmt.tools.qvm_unpause.main([], app=self.app)
with qubesmgmt.tests.tools.StderrBuffer() as stderr:
qubesmgmt.tools.qvm_unpause.main([], app=self.app)
self.assertIn('one of the arguments --all VMNAME is required',
stderr.getvalue())
self.assertAllCalled()
def test_002_invalid_vm(self):
@ -42,7 +45,9 @@ class TC_00_qvm_unpause(qubesmgmt.tests.QubesTestCase):
('dom0', 'mgmt.vm.List', None, None)] = \
b'0\x00some-vm class=AppVM state=Running\n'
with self.assertRaises(SystemExit):
qubesmgmt.tools.qvm_unpause.main(['no-such-vm'], app=self.app)
with qubesmgmt.tests.tools.StderrBuffer() as stderr:
qubesmgmt.tools.qvm_unpause.main(['no-such-vm'], app=self.app)
self.assertIn('no such domain', stderr.getvalue())
self.assertAllCalled()
def test_003_not_running(self):

View File

@ -54,11 +54,11 @@ class TC_00_Properties(qubesmgmt.tests.vm.VMTestCase):
self.assertAllCalled()
def test_004_get_vm(self):
self.skipTest('not specified')
self.app.expected_calls[
('test-vm', 'mgmt.vm.property.Get', 'prop1', None)] = \
b'0\x00default=False type=vm test-vm'
self.assertEqual(self.vm.prop1, True)
self.assertIsInstance(self.vm.prop1, qubesmgmt.vm.QubesVM)
self.assertEqual(self.vm.prop1.name, 'test-vm')
self.assertAllCalled()
def test_005_get_none_vm(self):