helpers.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. from datetime import datetime
  2. import os
  3. import tempfile
  4. import unittest
  5. from attic.helpers import 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. def test(self):
  43. self.assert_equal(IncludePattern('/usr').match('/usr'), True)
  44. self.assert_equal(IncludePattern('/usr').match('/usr/bin'), True)
  45. self.assert_equal(IncludePattern('/usr').match('/usrbin'), False)
  46. self.assert_equal(ExcludePattern('*.py').match('foo.py'), True)
  47. self.assert_equal(ExcludePattern('*.py').match('foo.pl'), False)
  48. self.assert_equal(ExcludePattern('/tmp').match('/tmp'), True)
  49. self.assert_equal(ExcludePattern('/tmp').match('/tmp/foo'), True)
  50. self.assert_equal(ExcludePattern('/tmp').match('/tmofoo'), False)
  51. class MakePathSafeTestCase(AtticTestCase):
  52. def test(self):
  53. self.assert_equal(make_path_safe('/foo/bar'), 'foo/bar')
  54. self.assert_equal(make_path_safe('/foo/bar'), 'foo/bar')
  55. self.assert_equal(make_path_safe('../foo/bar'), 'foo/bar')
  56. self.assert_equal(make_path_safe('../../foo/bar'), 'foo/bar')
  57. self.assert_equal(make_path_safe('/'), '.')
  58. self.assert_equal(make_path_safe('/'), '.')
  59. class UpgradableLockTestCase(AtticTestCase):
  60. def test(self):
  61. file = tempfile.NamedTemporaryFile()
  62. lock = UpgradableLock(file.name)
  63. lock.upgrade()
  64. lock.upgrade()
  65. lock.release()
  66. @unittest.skipIf(os.getuid() == 0, 'Root can always open files for writing')
  67. def test_read_only_lock_file(self):
  68. file = tempfile.NamedTemporaryFile()
  69. os.chmod(file.name, 0o444)
  70. lock = UpgradableLock(file.name)
  71. self.assert_raises(UpgradableLock.LockUpgradeFailed, lock.upgrade)
  72. lock.release()