Skip to content

Commit

Permalink
tests: catch and check stderr from invalid argument type tests
Browse files Browse the repository at this point in the history
Verify if tool failed with expected message, but also make tests output
more readable.
  • Loading branch information
marmarek committed Mar 13, 2017
1 parent c1ae5f8 commit ede625d
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 19 deletions.
16 changes: 16 additions & 0 deletions qubesmgmt/tests/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,19 @@ def __enter__(self):
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
14 changes: 10 additions & 4 deletions qubesmgmt/tests/tools/qubes_prefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,21 @@ def test_003_invalid_property(self):
('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):
self.app.expected_calls[
('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()
10 changes: 8 additions & 2 deletions qubesmgmt/tests/tools/qvm_kill.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -34,15 +35,20 @@ def test_000_with_vm(self):

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):
self.app.expected_calls[
('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):
Expand Down
10 changes: 8 additions & 2 deletions qubesmgmt/tests/tools/qvm_pause.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -34,15 +35,20 @@ def test_000_with_vm(self):

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):
self.app.expected_calls[
('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):
Expand Down
23 changes: 18 additions & 5 deletions qubesmgmt/tests/tools/qvm_prefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -47,7 +48,13 @@ def test_000_list(self):

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):
Expand All @@ -69,8 +76,11 @@ def test_003_invalid_property(self):
('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):
Expand All @@ -81,6 +91,9 @@ def test_004_set_invalid_property(self):
('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()
9 changes: 7 additions & 2 deletions qubesmgmt/tests/tools/qvm_shutdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,20 @@ def test_000_with_vm(self):

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):
self.app.expected_calls[
('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):
Expand Down
9 changes: 7 additions & 2 deletions qubesmgmt/tests/tools/qvm_unpause.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,20 @@ def test_000_with_vm(self):

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):
self.app.expected_calls[
('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):
Expand Down
4 changes: 2 additions & 2 deletions qubesmgmt/tests/vm/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ def test_003_get_bool(self):
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):
Expand Down

0 comments on commit ede625d

Please sign in to comment.