qvm_unpause.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # -*- encoding: utf8 -*-
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2017 Marek Marczykowski-Górecki
  6. # <marmarek@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation; either version 2.1 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License along
  19. # with this program; if not, see <http://www.gnu.org/licenses/>.
  20. import qubesadmin.tests
  21. import qubesadmin.tools.qvm_unpause
  22. class TC_00_qvm_unpause(qubesadmin.tests.QubesTestCase):
  23. def test_000_with_vm(self):
  24. self.app.expected_calls[
  25. ('dom0', 'admin.vm.List', None, None)] = \
  26. b'0\x00some-vm class=AppVM state=Running\n'
  27. self.app.expected_calls[
  28. ('some-vm', 'admin.vm.Unpause', None, None)] = b'0\x00'
  29. qubesadmin.tools.qvm_unpause.main(['some-vm'], app=self.app)
  30. self.assertAllCalled()
  31. def test_001_missing_vm(self):
  32. with self.assertRaises(SystemExit):
  33. with qubesadmin.tests.tools.StderrBuffer() as stderr:
  34. qubesadmin.tools.qvm_unpause.main([], app=self.app)
  35. self.assertIn('one of the arguments --all VMNAME is required',
  36. stderr.getvalue())
  37. self.assertAllCalled()
  38. def test_002_invalid_vm(self):
  39. self.app.expected_calls[
  40. ('dom0', 'admin.vm.List', None, None)] = \
  41. b'0\x00some-vm class=AppVM state=Running\n'
  42. with self.assertRaises(SystemExit):
  43. with qubesadmin.tests.tools.StderrBuffer() as stderr:
  44. qubesadmin.tools.qvm_unpause.main(['no-such-vm'], app=self.app)
  45. self.assertIn('no such domain', stderr.getvalue())
  46. self.assertAllCalled()
  47. def test_003_not_running(self):
  48. # TODO: some option to ignore this error?
  49. self.app.expected_calls[
  50. ('some-vm', 'admin.vm.Unpause', None, None)] = \
  51. b'2\x00QubesVMNotStartedError\x00\x00Domain is powered off: ' \
  52. b'some-vm\x00'
  53. self.app.expected_calls[
  54. ('dom0', 'admin.vm.List', None, None)] = \
  55. b'0\x00some-vm class=AppVM state=Halted\n'
  56. self.assertEqual(
  57. qubesadmin.tools.qvm_unpause.main(['some-vm'], app=self.app),
  58. 1)
  59. self.assertAllCalled()
  60. def test_004_multiple_vms(self):
  61. self.app.expected_calls[
  62. ('some-vm', 'admin.vm.Unpause', None, None)] = \
  63. b'0\x00'
  64. self.app.expected_calls[
  65. ('other-vm', 'admin.vm.Unpause', None, None)] = \
  66. b'0\x00'
  67. self.app.expected_calls[
  68. ('dom0', 'admin.vm.List', None, None)] = \
  69. b'0\x00some-vm class=AppVM state=Running\n' \
  70. b'other-vm class=AppVM state=Running\n'
  71. self.assertEqual(
  72. qubesadmin.tools.qvm_unpause.main(['some-vm', 'other-vm'],
  73. app=self.app),
  74. 0)
  75. self.assertAllCalled()