convert.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import os
  2. import shutil
  3. import tempfile
  4. import pytest
  5. try:
  6. import attic.repository
  7. import attic.key
  8. import attic.helpers
  9. except ImportError:
  10. attic = None
  11. pytestmark = pytest.mark.skipif(attic is None,
  12. reason = 'cannot find an attic install')
  13. from ..converter import AtticRepositoryConverter, AtticKeyfileKey
  14. from ..helpers import get_keys_dir
  15. from ..key import KeyfileKey
  16. from ..repository import Repository, MAGIC
  17. from . import BaseTestCase
  18. class ConversionTestCase(BaseTestCase):
  19. def open(self, path, repo_type = Repository, create=False):
  20. return repo_type(os.path.join(path, 'repository'), create = create)
  21. def setUp(self):
  22. self.tmppath = tempfile.mkdtemp()
  23. self.attic_repo = self.open(self.tmppath,
  24. repo_type = attic.repository.Repository,
  25. create = True)
  26. # throw some stuff in that repo, copied from `RepositoryTestCase.test1`_
  27. for x in range(100):
  28. self.attic_repo.put(('%-32d' % x).encode('ascii'), b'SOMEDATA')
  29. self.attic_repo.close()
  30. def tearDown(self):
  31. shutil.rmtree(self.tmppath)
  32. def repo_valid(self,):
  33. repository = self.open(self.tmppath)
  34. state = repository.check() # can't check raises() because check() handles the error
  35. repository.close()
  36. return state
  37. def test_convert_segments(self):
  38. # check should fail because of magic number
  39. assert not self.repo_valid()
  40. print("opening attic repository with borg and converting")
  41. repo = self.open(self.tmppath, repo_type = AtticRepositoryConverter)
  42. segments = [ filename for i, filename in repo.io.segment_iterator() ]
  43. repo.close()
  44. repo.convert_segments(segments, dryrun=False)
  45. assert self.repo_valid()
  46. class EncryptedConversionTestCase(ConversionTestCase):
  47. class MockArgs:
  48. def __init__(self, path):
  49. self.repository = attic.helpers.Location(path)
  50. def setUp(self):
  51. super().setUp()
  52. # we use the repo dir for the created keyfile, because we do
  53. # not want to clutter existing keyfiles
  54. os.environ['ATTIC_KEYS_DIR'] = self.tmppath
  55. # we use the same directory for the converted files, which
  56. # will clutter the previously created one, which we don't care
  57. # about anyways. in real runs, the original key will be retained.
  58. os.environ['BORG_KEYS_DIR'] = self.tmppath
  59. os.environ['ATTIC_PASSPHRASE'] = 'test'
  60. self.key = attic.key.KeyfileKey.create(self.attic_repo, self.MockArgs(self.tmppath))
  61. def test_keys(self):
  62. repository = self.open(self.tmppath, repo_type = AtticRepositoryConverter)
  63. keyfile = AtticKeyfileKey.find_key_file(repository)
  64. AtticRepositoryConverter.convert_keyfiles(keyfile, dryrun=False)
  65. # check that the new keyfile is alright
  66. keyfile = os.path.join(get_keys_dir(),
  67. os.path.basename(self.key.path))
  68. with open(keyfile, 'r') as f:
  69. assert f.read().startswith(KeyfileKey.FILE_ID)
  70. def test_convert_all(self):
  71. # check should fail because of magic number
  72. assert not self.repo_valid()
  73. print("opening attic repository with borg and converting")
  74. with pytest.raises(NotImplementedError):
  75. self.open(self.tmppath, repo_type = AtticRepositoryConverter).convert(dryrun=False)
  76. # check that the new keyfile is alright
  77. keyfile = os.path.join(get_keys_dir(),
  78. os.path.basename(self.key.path))
  79. with open(keyfile, 'r') as f:
  80. assert f.read().startswith(KeyfileKey.FILE_ID)
  81. assert self.repo_valid()