helpers.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. from datetime import datetime
  2. import os
  3. import tempfile
  4. import unittest
  5. from attic.helpers import adjust_patterns, exclude_path, Location, format_timedelta, IncludePattern, ExcludePattern, make_path_safe, UpgradableLock
  6. from attic.testsuite import AtticTestCase
  7. class LocationTestCase(AtticTestCase):
  8. def test(self):
  9. self.assert_equal(
  10. repr(Location('ssh://user@host:1234/some/path::archive')),
  11. "Location(proto='ssh', user='user', host='host', port=1234, path='/some/path', archive='archive')"
  12. )
  13. self.assert_equal(
  14. repr(Location('file:///some/path::archive')),
  15. "Location(proto='file', user=None, host=None, port=None, path='/some/path', archive='archive')"
  16. )
  17. self.assert_equal(
  18. repr(Location('user@host:/some/path::archive')),
  19. "Location(proto='ssh', user='user', host='host', port=None, path='/some/path', archive='archive')"
  20. )
  21. self.assert_equal(
  22. repr(Location('mybackup.attic::archive')),
  23. "Location(proto='file', user=None, host=None, port=None, path='mybackup.attic', archive='archive')"
  24. )
  25. self.assert_equal(
  26. repr(Location('/some/absolute/path::archive')),
  27. "Location(proto='file', user=None, host=None, port=None, path='/some/absolute/path', archive='archive')"
  28. )
  29. self.assert_equal(
  30. repr(Location('some/relative/path::archive')),
  31. "Location(proto='file', user=None, host=None, port=None, path='some/relative/path', archive='archive')"
  32. )
  33. class FormatTimedeltaTestCase(AtticTestCase):
  34. def test(self):
  35. t0 = datetime(2001, 1, 1, 10, 20, 3, 0)
  36. t1 = datetime(2001, 1, 1, 12, 20, 4, 100000)
  37. self.assert_equal(
  38. format_timedelta(t1 - t0),
  39. '2 hours 1.10 seconds'
  40. )
  41. class PatternTestCase(AtticTestCase):
  42. files = [
  43. '/etc/passwd', '/etc/hosts',
  44. '/home/user/.profile', '/home/user/.bashrc',
  45. '/home/user2/.profile', '/home/user2/public_html/index.html',
  46. '/var/log/messages', '/var/log/dmesg',
  47. ]
  48. def evaluate(self, paths, excludes):
  49. patterns = adjust_patterns(paths, [ExcludePattern(p) for p in excludes])
  50. return [path for path in self.files if not exclude_path(path, patterns)]
  51. def test(self):
  52. self.assert_equal(self.evaluate(['/'], ['/home']),
  53. ['/etc/passwd', '/etc/hosts', '/var/log/messages', '/var/log/dmesg'])
  54. self.assert_equal(self.evaluate(['/'], ['*.profile', '/var/log']),
  55. ['/etc/passwd', '/etc/hosts', '/home/user/.bashrc', '/home/user2/public_html/index.html'])
  56. self.assert_equal(self.evaluate(['/'], ['/home/*/public_html', '*.profile', '*/log/*']),
  57. ['/etc/passwd', '/etc/hosts', '/home/user/.bashrc'])
  58. self.assert_equal(self.evaluate(['/etc', '/var'], ['dmesg']),
  59. ['/etc/passwd', '/etc/hosts', '/var/log/messages', '/var/log/dmesg'])
  60. class MakePathSafeTestCase(AtticTestCase):
  61. def test(self):
  62. self.assert_equal(make_path_safe('/foo/bar'), 'foo/bar')
  63. self.assert_equal(make_path_safe('/foo/bar'), 'foo/bar')
  64. self.assert_equal(make_path_safe('../foo/bar'), 'foo/bar')
  65. self.assert_equal(make_path_safe('../../foo/bar'), 'foo/bar')
  66. self.assert_equal(make_path_safe('/'), '.')
  67. self.assert_equal(make_path_safe('/'), '.')
  68. class UpgradableLockTestCase(AtticTestCase):
  69. def test(self):
  70. file = tempfile.NamedTemporaryFile()
  71. lock = UpgradableLock(file.name)
  72. lock.upgrade()
  73. lock.upgrade()
  74. lock.release()
  75. @unittest.skipIf(os.getuid() == 0, 'Root can always open files for writing')
  76. def test_read_only_lock_file(self):
  77. file = tempfile.NamedTemporaryFile()
  78. os.chmod(file.name, 0o444)
  79. lock = UpgradableLock(file.name)
  80. self.assert_raises(UpgradableLock.LockUpgradeFailed, lock.upgrade)
  81. lock.release()