dispvm.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 General Public License as published by
  10. # the Free Software Foundation; either version 2 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 General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, see <http://www.gnu.org/licenses/>.
  20. import unittest.mock as mock
  21. import asyncio
  22. import qubes.vm.dispvm
  23. import qubes.vm.appvm
  24. import qubes.vm.templatevm
  25. import qubes.tests
  26. import qubes.tests.vm
  27. import qubes.tests.vm.appvm
  28. class TestApp(qubes.tests.vm.TestApp):
  29. def __init__(self):
  30. super(TestApp, self).__init__()
  31. self.qid_counter = 1
  32. def add_new_vm(self, cls, **kwargs):
  33. qid = self.qid_counter
  34. self.qid_counter += 1
  35. vm = cls(self, None, qid=qid, **kwargs)
  36. self.domains[vm.name] = vm
  37. self.domains[vm] = vm
  38. return vm
  39. class TC_00_DispVM(qubes.tests.QubesTestCase):
  40. def setUp(self):
  41. super(TC_00_DispVM, self).setUp()
  42. self.app = TestApp()
  43. self.app.save = mock.Mock()
  44. self.app.pools['default'] = qubes.tests.vm.appvm.TestPool('default')
  45. self.app.pools['linux-kernel'] = mock.Mock(**{
  46. 'init_volume.return_value.pool': 'linux-kernel'})
  47. self.app.vmm.offline_mode = True
  48. self.template = self.app.add_new_vm(qubes.vm.templatevm.TemplateVM,
  49. name='test-template', label='red')
  50. self.appvm = self.app.add_new_vm(qubes.vm.appvm.AppVM,
  51. name='test-vm', template=self.template, label='red')
  52. self.app.domains[self.appvm.name] = self.appvm
  53. self.app.domains[self.appvm] = self.appvm
  54. @asyncio.coroutine
  55. def mock_coro(self, *args, **kwargs):
  56. pass
  57. @mock.patch('os.symlink')
  58. @mock.patch('os.makedirs')
  59. @mock.patch('qubes.storage.Storage')
  60. def test_000_from_appvm(self, mock_storage, mock_makedirs, mock_symlink):
  61. mock_storage.return_value.create.side_effect = self.mock_coro
  62. self.appvm.dispvm_allowed = True
  63. orig_getitem = self.app.domains.__getitem__
  64. with mock.patch.object(self.app, 'domains', wraps=self.app.domains) \
  65. as mock_domains:
  66. mock_domains.configure_mock(**{
  67. 'get_new_unused_dispid': mock.Mock(return_value=42),
  68. '__getitem__.side_effect': orig_getitem
  69. })
  70. dispvm = self.loop.run_until_complete(
  71. qubes.vm.dispvm.DispVM.from_appvm(self.appvm))
  72. mock_domains.get_new_unused_dispid.assert_called_once_with()
  73. self.assertTrue(dispvm.name.startswith('disp'))
  74. self.assertEqual(dispvm.template, self.appvm)
  75. self.assertEqual(dispvm.label, self.appvm.label)
  76. self.assertEqual(dispvm.label, self.appvm.label)
  77. self.assertEqual(dispvm.auto_cleanup, True)
  78. mock_makedirs.assert_called_once_with(
  79. '/var/lib/qubes/appvms/' + dispvm.name, mode=0o775)
  80. mock_symlink.assert_called_once_with(
  81. '/usr/share/icons/hicolor/128x128/devices/appvm-red.png',
  82. '/var/lib/qubes/appvms/{}/icon.png'.format(dispvm.name))
  83. def test_001_from_appvm_reject_not_allowed(self):
  84. with self.assertRaises(qubes.exc.QubesException):
  85. dispvm = self.loop.run_until_complete(
  86. qubes.vm.dispvm.DispVM.from_appvm(self.appvm))