__init__.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. class BackupTestsMixin(SystemTestsMixin):
  2. def setUp(self):
  3. super(BackupTestsMixin, self).setUp()
  4. self.error_detected = multiprocessing.Queue()
  5. self.verbose = False
  6. if self.verbose:
  7. print >>sys.stderr, "-> Creating backupvm"
  8. # TODO: allow non-default template
  9. self.backupvm = self.qc.add_new_vm("QubesAppVm",
  10. name=self.make_vm_name('backupvm'),
  11. template=self.qc.get_default_template())
  12. self.backupvm.create_on_disk(verbose=self.verbose)
  13. self.backupdir = os.path.join(os.environ["HOME"], "test-backup")
  14. if os.path.exists(self.backupdir):
  15. shutil.rmtree(self.backupdir)
  16. os.mkdir(self.backupdir)
  17. def tearDown(self):
  18. super(BackupTestsMixin, self).tearDown()
  19. shutil.rmtree(self.backupdir)
  20. def print_progress(self, progress):
  21. if self.verbose:
  22. print >> sys.stderr, "\r-> Backing up files: {0}%...".format(progress)
  23. def error_callback(self, message):
  24. self.error_detected.put(message)
  25. if self.verbose:
  26. print >> sys.stderr, "ERROR: {0}".format(message)
  27. def print_callback(self, msg):
  28. if self.verbose:
  29. print msg
  30. def fill_image(self, path, size=None, sparse=False):
  31. block_size = 4096
  32. if self.verbose:
  33. print >>sys.stderr, "-> Filling %s" % path
  34. f = open(path, 'w+')
  35. if size is None:
  36. f.seek(0, 2)
  37. size = f.tell()
  38. f.seek(0)
  39. for block_num in xrange(size/block_size):
  40. f.write('a' * block_size)
  41. if sparse:
  42. f.seek(block_size, 1)
  43. f.close()
  44. # NOTE: this was create_basic_vms
  45. def create_backup_vms(self):
  46. template=self.qc.get_default_template()
  47. vms = []
  48. vmname = self.make_vm_name('test1')
  49. if self.verbose:
  50. print >>sys.stderr, "-> Creating %s" % vmname
  51. testvm1 = self.qc.add_new_vm('QubesAppVm',
  52. name=vmname, template=template)
  53. testvm1.create_on_disk(verbose=self.verbose)
  54. vms.append(testvm1)
  55. self.fill_image(testvm1.private_img, 100*1024*1024)
  56. vmname = self.make_vm_name('testhvm1')
  57. if self.verbose:
  58. print >>sys.stderr, "-> Creating %s" % vmname
  59. testvm2 = self.qc.add_new_vm('QubesHVm', name=vmname)
  60. testvm2.create_on_disk(verbose=self.verbose)
  61. self.fill_image(testvm2.root_img, 1024*1024*1024, True)
  62. vms.append(testvm2)
  63. self.qc.save()
  64. return vms
  65. def make_backup(self, vms, prepare_kwargs=dict(), do_kwargs=dict(),
  66. target=None):
  67. # XXX: bakup_prepare and backup_do don't support host_collection
  68. self.qc.unlock_db()
  69. if target is None:
  70. target = self.backupdir
  71. try:
  72. files_to_backup = \
  73. qubes.backup.backup_prepare(vms,
  74. print_callback=self.print_callback,
  75. **prepare_kwargs)
  76. except qubes.qubes.QubesException as e:
  77. self.fail("QubesException during backup_prepare: %s" % str(e))
  78. try:
  79. qubes.backup.backup_do(target, files_to_backup, "qubes",
  80. progress_callback=self.print_progress,
  81. **do_kwargs)
  82. except qubes.qubes.QubesException as e:
  83. self.fail("QubesException during backup_do: %s" % str(e))
  84. self.qc.lock_db_for_writing()
  85. self.qc.load()
  86. def restore_backup(self, source=None, appvm=None, options=None):
  87. if source is None:
  88. backupfile = os.path.join(self.backupdir,
  89. sorted(os.listdir(self.backupdir))[-1])
  90. else:
  91. backupfile = source
  92. with self.assertNotRaises(qubes.qubes.QubesException):
  93. backup_info = qubes.backup.backup_restore_prepare(
  94. backupfile, "qubes",
  95. host_collection=self.qc,
  96. print_callback=self.print_callback,
  97. appvm=appvm,
  98. options=options or {})
  99. if self.verbose:
  100. qubes.backup.backup_restore_print_summary(backup_info)
  101. with self.assertNotRaises(qubes.qubes.QubesException):
  102. qubes.backup.backup_restore_do(
  103. backup_info,
  104. host_collection=self.qc,
  105. print_callback=self.print_callback if self.verbose else None,
  106. error_callback=self.error_callback)
  107. # maybe someone forgot to call .save()
  108. self.qc.load()
  109. errors = []
  110. while not self.error_detected.empty():
  111. errors.append(self.error_detected.get())
  112. self.assertTrue(len(errors) == 0,
  113. "Error(s) detected during backup_restore_do: %s" %
  114. '\n'.join(errors))
  115. if not appvm and not os.path.isdir(backupfile):
  116. os.unlink(backupfile)
  117. def create_sparse(self, path, size):
  118. f = open(path, "w")
  119. f.truncate(size)
  120. f.close()