qvm_run.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #
  2. # The Qubes OS Project, https://www.qubes-os.org/
  3. #
  4. # Copyright (C) 2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  5. # Copyright (C) 2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License along
  18. # with this program; if not, write to the Free Software Foundation, Inc.,
  19. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. #
  21. import io
  22. import os
  23. import sys
  24. import tempfile
  25. import unittest
  26. import qubes
  27. import qubes.config
  28. import qubes.tools.qvm_run
  29. import qubes.vm
  30. import qubes.tests
  31. @qubes.tests.skipUnlessDom0
  32. class TC_00_qvm_run(qubes.tests.SystemTestsMixin, qubes.tests.QubesTestCase):
  33. def setUp(self):
  34. super(TC_00_qvm_run, self).setUp()
  35. self.init_default_template()
  36. self.vm1 = self.app.add_new_vm(qubes.vm.appvm.AppVM,
  37. name=self.make_vm_name('vm1'),
  38. template=self.app.default_template,
  39. label='red')
  40. self.vm1.create_on_disk()
  41. self.vm1.start()
  42. self.app.save()
  43. self.sharedopts = ['--qubesxml', qubes.tests.XMLPATH]
  44. def tearDown(self):
  45. # clean up after testing --colour-output
  46. sys.stdout = sys.__stdout__
  47. @staticmethod
  48. def get_qvm_run_output(args):
  49. assert '--localcmd' not in args, \
  50. 'get_qvm_run_output requires no --localcmd'
  51. outfile = tempfile.NamedTemporaryFile(prefix='qvm-run-output')
  52. args = list(args)
  53. args.insert(0, '--pass-io')
  54. args.insert(1, '--localcmd')
  55. args.insert(2, 'sh -c "dd of={}"'.format(outfile.name))
  56. qubes.tools.qvm_run.main(args)
  57. outfile.seek(0)
  58. output = outfile.read()
  59. outfile.close()
  60. return output
  61. def test_000_basic(self):
  62. self.assertEqual(0, qubes.tools.qvm_run.main(
  63. self.sharedopts + [self.vm1.name, 'true']))
  64. def test_001_passio_retcode(self):
  65. self.assertEqual(0, qubes.tools.qvm_run.main(
  66. self.sharedopts + ['--pass-io', self.vm1.name, 'true']))
  67. self.assertEqual(1, qubes.tools.qvm_run.main(
  68. self.sharedopts + ['--pass-io', self.vm1.name, 'false']))
  69. def test_002_passio_localcmd(self):
  70. self.assertEqual(b'aqq', self.get_qvm_run_output(
  71. self.sharedopts + [self.vm1.name, 'printf aqq']))
  72. def test_003_user(self):
  73. self.assertNotEqual(b'0\n', self.get_qvm_run_output(
  74. self.sharedopts + ['--user', 'user', self.vm1.name, 'id -u']))
  75. self.assertEqual(b'0\n', self.get_qvm_run_output(
  76. self.sharedopts + ['--user', 'root', self.vm1.name, 'id -u']))
  77. def test_004_autostart(self):
  78. vm2 = self.app.add_new_vm(qubes.vm.appvm.AppVM,
  79. name=self.make_vm_name('vm2'),
  80. template=qubes.tests.TEMPLATE,
  81. label='red')
  82. vm2.create_on_disk()
  83. self.app.save()
  84. # and do not start it
  85. self.assertEqual(-1, qubes.tools.qvm_run.main(
  86. self.sharedopts + [vm2.name, 'true']))
  87. self.assertEqual(0, qubes.tools.qvm_run.main(
  88. self.sharedopts + ['--autostart', vm2.name, 'true']))
  89. @unittest.skip('expected error')
  90. def test_005_colour_output(self):
  91. sys.stdout = io.StringIO()
  92. qubes.tools.qvm_run.main(
  93. self.sharedopts + ['--colour-output', '32', self.vm1.name, 'true'])
  94. self.assertEqual(b'\033[0;32m\033[0m', sys.stdout.getvalue())
  95. def test_006_filter_esc(self):
  96. self.assertEqual(b'\033', self.get_qvm_run_output(
  97. self.sharedopts + ['--no-filter-escape-chars', self.vm1.name,
  98. r'printf \\033']))
  99. self.assertEqual(b'_', self.get_qvm_run_output(
  100. self.sharedopts + ['--filter-escape-chars', self.vm1.name,
  101. r'printf \\033']))
  102. def test_007_gui(self): # pylint: disable=no-self-use
  103. raise unittest.SkipTest('test not implemented')
  104. #parser.add_argument('--gui',
  105. #parser.add_argument('--no-gui', '--nogui',