qvm_run.py 4.4 KB

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