actions.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # -*- encoding: utf-8 -*-
  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 unittest
  21. import qubesadmin.tests.vm
  22. class TC_00_Actions(qubesadmin.tests.vm.VMTestCase):
  23. def test_000_start(self):
  24. self.app.expected_calls[
  25. ('test-vm', 'admin.vm.Start', None, None)] = \
  26. b'0\x00'
  27. self.vm.start()
  28. self.assertAllCalled()
  29. def test_001_shutdown(self):
  30. self.app.expected_calls[
  31. ('test-vm', 'admin.vm.Shutdown', None, None)] = \
  32. b'0\x00'
  33. self.vm.shutdown()
  34. self.assertAllCalled()
  35. def test_002_kill(self):
  36. self.app.expected_calls[
  37. ('test-vm', 'admin.vm.Kill', None, None)] = \
  38. b'0\x00'
  39. self.vm.kill()
  40. self.assertAllCalled()
  41. def test_003_pause(self):
  42. self.app.expected_calls[
  43. ('test-vm', 'admin.vm.Pause', None, None)] = \
  44. b'0\x00'
  45. self.vm.pause()
  46. self.assertAllCalled()
  47. def test_004_unpause(self):
  48. self.app.expected_calls[
  49. ('test-vm', 'admin.vm.Unpause', None, None)] = \
  50. b'0\x00'
  51. self.vm.unpause()
  52. self.assertAllCalled()
  53. @unittest.skip('Not part of the mgmt API yet')
  54. def test_005_suspend(self):
  55. self.app.expected_calls[
  56. ('test-vm', 'admin.vm.Suspend', None, None)] = \
  57. b'0\x00'
  58. self.vm.suspend()
  59. self.assertAllCalled()
  60. @unittest.skip('Not part of the mgmt API yet')
  61. def test_006_resume(self):
  62. self.app.expected_calls[
  63. ('test-vm', 'admin.vm.Resume', None, None)] = \
  64. b'0\x00'
  65. self.vm.resume()
  66. self.assertAllCalled()
  67. def test_010_run_linux(self):
  68. self.app.expected_calls[
  69. ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'os', None)] = \
  70. b'2\x00QubesFeatureNotFoundError\x00\x00Feature \'os\' not set\x00'
  71. self.vm.run('some command')
  72. self.assertEqual(self.app.service_calls, [
  73. ('test-vm', 'qubes.VMShell', {}),
  74. ('test-vm', 'qubes.VMShell', b'some command; exit\n'),
  75. ])
  76. def test_011_run_windows(self):
  77. self.app.expected_calls[
  78. ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'os', None)] = \
  79. b'0\x00Windows'
  80. self.vm.run('some command')
  81. self.assertEqual(self.app.service_calls, [
  82. ('test-vm', 'qubes.VMShell', {}),
  83. ('test-vm', 'qubes.VMShell', b'some command& exit\n'),
  84. ])
  85. def test_015_run_with_args_shell(self):
  86. self.app.expected_calls[
  87. ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'vmexec', None)] = \
  88. b'2\x00QubesFeatureNotFoundError\x00\x00Feature \'vmexec\' not set\x00'
  89. self.app.expected_calls[
  90. ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'os', None)] = \
  91. b'2\x00QubesFeatureNotFoundError\x00\x00Feature \'os\' not set\x00'
  92. self.vm.run_with_args('some', 'argument with spaces',
  93. 'and $pecial; chars')
  94. self.assertEqual(self.app.service_calls, [
  95. ('test-vm', 'qubes.VMShell', {}),
  96. ('test-vm', 'qubes.VMShell',
  97. b'some \'argument with spaces\' \'and $pecial; chars\'; '
  98. b'exit\n'),
  99. ])
  100. def test_016_run_with_args_exec(self):
  101. self.app.expected_calls[
  102. ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'vmexec', None)] = \
  103. b'0\x001'
  104. self.vm.run_with_args('some', 'argument with spaces',
  105. 'and $pecial; chars')
  106. self.assertEqual(self.app.service_calls, [
  107. ('test-vm',
  108. 'qubes.VMExec+some+argument-20with-20spaces+and-20-24pecial-3B-20chars',
  109. {}),
  110. ('test-vm',
  111. 'qubes.VMExec+some+argument-20with-20spaces+and-20-24pecial-3B-20chars',
  112. b''),
  113. ])