qvm_pause.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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.tests.tools
  22. import qubesadmin.tools.qvm_pause
  23. class TC_00_qvm_pause(qubesadmin.tests.QubesTestCase):
  24. def test_000_with_vm(self):
  25. self.app.expected_calls[
  26. ('dom0', 'mgmt.vm.List', None, None)] = \
  27. b'0\x00some-vm class=AppVM state=Running\n'
  28. self.app.expected_calls[
  29. ('some-vm', 'mgmt.vm.Pause', None, None)] = b'0\x00'
  30. qubesadmin.tools.qvm_pause.main(['some-vm'], app=self.app)
  31. self.assertAllCalled()
  32. def test_001_missing_vm(self):
  33. with self.assertRaises(SystemExit):
  34. with qubesadmin.tests.tools.StderrBuffer() as stderr:
  35. qubesadmin.tools.qvm_pause.main([], app=self.app)
  36. self.assertIn('one of the arguments --all VMNAME is required',
  37. stderr.getvalue())
  38. self.assertAllCalled()
  39. def test_002_invalid_vm(self):
  40. self.app.expected_calls[
  41. ('dom0', 'mgmt.vm.List', None, None)] = \
  42. b'0\x00some-vm class=AppVM state=Running\n'
  43. with self.assertRaises(SystemExit):
  44. with qubesadmin.tests.tools.StderrBuffer() as stderr:
  45. qubesadmin.tools.qvm_pause.main(['no-such-vm'], app=self.app)
  46. self.assertIn('no such domain', stderr.getvalue())
  47. self.assertAllCalled()
  48. def test_003_not_running(self):
  49. # TODO: some option to ignore this error?
  50. self.app.expected_calls[
  51. ('some-vm', 'mgmt.vm.Pause', None, None)] = \
  52. b'2\x00QubesVMNotStartedError\x00\x00Domain is powered off: ' \
  53. b'some-vm\x00'
  54. self.app.expected_calls[
  55. ('dom0', 'mgmt.vm.List', None, None)] = \
  56. b'0\x00some-vm class=AppVM state=Halted\n'
  57. self.assertEqual(
  58. qubesadmin.tools.qvm_pause.main(['some-vm'], app=self.app),
  59. 1)
  60. self.assertAllCalled()
  61. def test_004_multiple_vms(self):
  62. self.app.expected_calls[
  63. ('some-vm', 'mgmt.vm.Pause', None, None)] = \
  64. b'0\x00'
  65. self.app.expected_calls[
  66. ('other-vm', 'mgmt.vm.Pause', None, None)] = \
  67. b'0\x00'
  68. self.app.expected_calls[
  69. ('dom0', 'mgmt.vm.List', None, None)] = \
  70. b'0\x00some-vm class=AppVM state=Running\n' \
  71. b'other-vm class=AppVM state=Running\n'
  72. self.assertEqual(
  73. qubesadmin.tools.qvm_pause.main(['some-vm', 'other-vm'],
  74. app=self.app),
  75. 0)
  76. self.assertAllCalled()