utils.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # -*- encoding: utf8 -*-
  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
  22. import qubesadmin.utils
  23. class TestVMUsage(qubesadmin.tests.QubesTestCase):
  24. def setUp(self):
  25. super(TestVMUsage, self).setUp()
  26. self.app.expected_calls[
  27. ('dom0', 'admin.vm.List', None, None)] = \
  28. b'0\x00vm1 class=AppVM state=Running\n' \
  29. b'template1 class=TemplateVM state=Halted\n' \
  30. b'template2 class=TemplateVM state=Running\n' \
  31. b'vm2 class=AppVM state=Running\n' \
  32. b'sys-net class=AppVM state=Running\n' \
  33. b'sys-firewall class=AppVM state=Running\n'
  34. self.global_properties = ['default_dispvm', 'default_netvm',
  35. 'default_template', 'clockvm', 'updatevm']
  36. for prop in self.global_properties:
  37. self.app.expected_calls[
  38. ('dom0', 'admin.property.Get', prop, None)] = \
  39. b'0\x00default=True type=vm vm2'
  40. self.vms = ['vm1', 'vm2', 'sys-net', 'sys-firewall',
  41. 'template1', 'template2']
  42. self.vm_properties = ['template', 'netvm', 'default_dispvm']
  43. for vm in self.vms:
  44. for prop in self.vm_properties:
  45. if not prop.startswith('template') or \
  46. not vm.startswith('template'):
  47. self.app.expected_calls[
  48. (vm, 'admin.vm.property.Get', prop, None)] = \
  49. b'0\x00default=False type=vm template1'
  50. else:
  51. self.app.expected_calls[
  52. (vm, 'admin.vm.property.Get', prop, None)] = \
  53. b'2\0QubesNoSuchPropertyError\0\0invalid property\0'
  54. def test_00_only_global(self):
  55. result = qubesadmin.utils.vm_dependencies(self.app, self.app.domains['vm2'])
  56. self.assertListEqual(result,
  57. [(None, prop) for prop in self.global_properties],
  58. "Incorrect global properties listed.")
  59. def test_01_only_vm(self):
  60. result = qubesadmin.utils.vm_dependencies(
  61. self.app, self.app.domains['template1'])
  62. self.assertSetEqual(
  63. set(result),
  64. set([(vm, prop) for vm in self.vms for prop in self.vm_properties
  65. if (not vm.startswith('template')
  66. or not prop.startswith('template'))]),
  67. "Incorrect VM properties listed.")
  68. def test_02_empty(self):
  69. result = qubesadmin.utils.vm_dependencies(self.app, self.app.domains['vm1'])
  70. self.assertListEqual(result, [], "Incorrect use found.")
  71. def test_03_access_error(self):
  72. self.app.expected_calls[
  73. ('dom0', 'admin.property.Get', 'default_dispvm', None)] = b''
  74. result = qubesadmin.utils.vm_dependencies(self.app, self.app.domains['vm1'])
  75. self.assertListEqual(result, [], "Incorrect use found.")
  76. def test_04_defaults(self):
  77. self.app.expected_calls[
  78. ('vm2', 'admin.vm.property.Get', 'netvm', None)] = \
  79. b'0\x00default=True type=vm sys-net'
  80. self.app.expected_calls[
  81. ('vm1', 'admin.vm.property.Get', 'netvm', None)] = \
  82. b'0\x00default=False type=vm sys-net'
  83. result = qubesadmin.utils.vm_dependencies(self.app,
  84. self.app.domains['sys-net'])
  85. self.assertListEqual(result, [(self.app.domains['vm1'], 'netvm')])