qvm_run.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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.app = qubes.Qubes.create_empty_store(qubes.tests.XMLPATH,
  38. default_kernel=os.listdir(os.path.join(
  39. qubes.config.system_path['qubes_base_dir'],
  40. qubes.config.system_path['qubes_kernels_base_dir']))[0])
  41. self.app.add_new_vm(qubes.vm.templatevm.TemplateVM,
  42. name=qubes.tests.TEMPLATE,
  43. label='black')
  44. self.vm1 = self.app.add_new_vm(qubes.vm.appvm.AppVM,
  45. name=self.make_vm_name('vm1'),
  46. template=qubes.tests.TEMPLATE,
  47. label='red')
  48. self.vm1.create_on_disk()
  49. self.vm1.start()
  50. self.app.save()
  51. self.sharedopts = ['--qubesxml', qubes.tests.XMLPATH]
  52. def tearDown(self):
  53. # clean up after testing --colour-output
  54. sys.stdout = sys.__stdout__
  55. @staticmethod
  56. def get_qvm_run_output(args):
  57. assert '--localcmd' not in args, \
  58. 'get_qvm_run_output requires no --localcmd'
  59. outfile = tempfile.NamedTemporaryFile(prefix='qvm-run-output')
  60. args = list(args)
  61. args.insert(0, '--pass-io')
  62. args.insert(1, '--localcmd')
  63. args.insert(2, 'sh -c "dd of={}"'.format(outfile.name))
  64. qubes.tools.qvm_run.main(args)
  65. outfile.seek(0)
  66. output = outfile.read()
  67. outfile.close()
  68. return output
  69. def test_000_basic(self):
  70. self.assertEqual(0, qubes.tools.qvm_run.main(
  71. self.sharedopts + [self.vm1.name, 'true']))
  72. def test_001_passio_retcode(self):
  73. self.assertEqual(0, qubes.tools.qvm_run.main(
  74. self.sharedopts + ['--pass-io', self.vm1.name, 'true']))
  75. self.assertEqual(1, qubes.tools.qvm_run.main(
  76. self.sharedopts + ['--pass-io', self.vm1.name, 'false']))
  77. def test_002_passio_localcmd(self):
  78. self.assertEqual('aqq', self.get_qvm_run_output(
  79. self.sharedopts + [self.vm1.name, 'printf aqq']))
  80. def test_003_user(self):
  81. self.assertNotEqual('0\n', self.get_qvm_run_output(
  82. self.sharedopts + ['--user', 'user', self.vm1.name, 'id -u']))
  83. self.assertEqual('0\n', self.get_qvm_run_output(
  84. self.sharedopts + ['--user', 'root', self.vm1.name, 'id -u']))
  85. def test_004_autostart(self):
  86. vm2 = self.app.add_new_vm(qubes.vm.appvm.AppVM,
  87. name=self.make_vm_name('vm2'),
  88. template=qubes.tests.TEMPLATE,
  89. label='red')
  90. vm2.create_on_disk()
  91. self.app.save()
  92. # and do not start it
  93. self.assertEqual(-1, qubes.tools.qvm_run.main(
  94. self.sharedopts + [vm2.name, 'true']))
  95. self.assertEqual(0, qubes.tools.qvm_run.main(
  96. self.sharedopts + ['--autostart', vm2.name, 'true']))
  97. def test_005_colour_output(self):
  98. sys.stdout = StringIO.StringIO()
  99. qubes.tools.qvm_run.main(
  100. self.sharedopts + ['--colour-output', '32', self.vm1.name, 'true'])
  101. self.assertEqual('\033[0;32m\033[0m', sys.stdout.getvalue())
  102. def test_006_filter_esc(self):
  103. self.assertEqual('\033', self.get_qvm_run_output(
  104. self.sharedopts + ['--no-filter-escape-chars', self.vm1.name,
  105. r'printf \\033']))
  106. self.assertEqual('_', self.get_qvm_run_output(
  107. self.sharedopts + ['--filter-escape-chars', self.vm1.name,
  108. r'printf \\033']))
  109. def test_007_gui(self): # pylint: disable=no-self-use
  110. raise unittest.SkipTest('test not implemented')
  111. #parser.add_argument('--gui',
  112. #parser.add_argument('--no-gui', '--nogui',