basic.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # The Qubes OS Project, http://www.qubes-os.org
  5. #
  6. # Copyright (C) 2014 Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU General Public License
  10. # as published by the Free Software Foundation; either version 2
  11. # of the License, or (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
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. #
  22. import multiprocessing
  23. import os
  24. import shutil
  25. import unittest
  26. import time
  27. from qubes.qubes import QubesVmCollection, QubesException, system_path
  28. VM_PREFIX = "test-"
  29. class BasicTests(unittest.TestCase):
  30. def setUp(self):
  31. self.qc = QubesVmCollection()
  32. self.qc.lock_db_for_reading()
  33. self.qc.load()
  34. self.qc.unlock_db()
  35. def remove_vms(self, vms):
  36. self.qc.lock_db_for_writing()
  37. self.qc.load()
  38. for vm in vms:
  39. if isinstance(vm, str):
  40. vm = self.qc.get_vm_by_name(vm)
  41. else:
  42. vm = self.qc[vm.qid]
  43. try:
  44. vm.remove_from_disk()
  45. except OSError:
  46. pass
  47. self.qc.pop(vm.qid)
  48. self.qc.save()
  49. self.qc.unlock_db()
  50. def tearDown(self):
  51. vmlist = [vm for vm in self.qc.values() if vm.name.startswith(
  52. VM_PREFIX)]
  53. self.remove_vms(vmlist)
  54. def test_create(self):
  55. self.qc.lock_db_for_writing()
  56. self.qc.load()
  57. vmname = VM_PREFIX + "appvm"
  58. vm = self.qc.add_new_vm("QubesAppVm", name=vmname,
  59. template=self.qc.get_default_template())
  60. self.qc.save()
  61. self.qc.unlock_db()
  62. self.assertIsNotNone(vm)
  63. self.assertEqual(vm.name, vmname)
  64. self.assertEqual(vm.template, self.qc.get_default_template())
  65. vm.create_on_disk(verbose=False)
  66. try:
  67. vm.verify_files()
  68. except QubesException:
  69. self.fail("verify_files() failed")
  70. # Bug: #906
  71. def test_db_locking(self):
  72. def create_vm(name):
  73. qc = QubesVmCollection()
  74. qc.lock_db_for_writing()
  75. qc.load()
  76. time.sleep(1)
  77. vmname = VM_PREFIX + name
  78. qc.add_new_vm("QubesAppVm", name=vmname, template=qc.get_default_template())
  79. qc.save()
  80. qc.unlock_db()
  81. t = multiprocessing.Process(target=create_vm, args=("test1",))
  82. t.start()
  83. create_vm("test2")
  84. t.join()
  85. self.qc.lock_db_for_reading()
  86. self.qc.load()
  87. self.qc.unlock_db()
  88. self.assertIsNotNone(self.qc.get_vm_by_name(VM_PREFIX + "test1"))
  89. self.assertIsNotNone(self.qc.get_vm_by_name(VM_PREFIX + "test2"))
  90. class VmPropTests(unittest.TestCase):
  91. def setUp(self):
  92. self.qc = QubesVmCollection()
  93. self.qc.lock_db_for_writing()
  94. self.qc.load()
  95. self.vmname = VM_PREFIX + "appvm"
  96. self.vm = self.qc.add_new_vm("QubesAppVm", name=self.vmname,
  97. template=self.qc.get_default_template())
  98. self.qc.save()
  99. self.vm.create_on_disk(verbose=False)
  100. # WARNING: lock remains taken
  101. def remove_vms(self, vms):
  102. for vm in vms:
  103. if isinstance(vm, str):
  104. vm = self.qc.get_vm_by_name(vm)
  105. else:
  106. vm = self.qc[vm.qid]
  107. vm.remove_from_disk()
  108. self.qc.pop(vm.qid)
  109. def tearDown(self):
  110. # WARNING: lock still taken in setUp()
  111. vmlist = [vm for vm in self.qc.values() if vm.name.startswith(
  112. VM_PREFIX)]
  113. self.remove_vms(vmlist)
  114. self.qc.save()
  115. self.qc.unlock_db()
  116. def test_rename(self):
  117. self.assertEqual(self.vm.name, self.vmname)
  118. newname = VM_PREFIX + "newname"
  119. #TODO: change to setting property when implemented
  120. self.vm.set_name(newname)
  121. self.assertEqual(self.vm.name, newname)
  122. self.assertEqual(self.vm.dir_path,
  123. os.path.join(system_path['qubes_appvms_dir'], newname))
  124. self.assertEqual(self.vm.conf_file,
  125. os.path.join(self.vm.dir_path, newname + '.conf'))
  126. self.assertTrue(os.path.exists(
  127. os.path.join(self.vm.dir_path, "apps", newname + "-vm.directory")))
  128. # FIXME: set whitelisted-appmenus.list first
  129. self.assertTrue(os.path.exists(
  130. os.path.join(self.vm.dir_path, "apps", newname + "-firefox.desktop")))
  131. self.assertTrue(os.path.exists(
  132. os.path.join(os.getenv("HOME"), ".local/share/desktop-directories",
  133. newname + "-vm.directory")))
  134. self.assertTrue(os.path.exists(
  135. os.path.join(os.getenv("HOME"), ".local/share/applications",
  136. newname + "-firefox.desktop")))
  137. self.assertFalse(os.path.exists(
  138. os.path.join(os.getenv("HOME"), ".local/share/desktop-directories",
  139. self.vmname + "-vm.directory")))
  140. self.assertFalse(os.path.exists(
  141. os.path.join(os.getenv("HOME"), ".local/share/applications",
  142. self.vmname + "-firefox.desktop")))