helpers.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import hashlib
  2. from time import mktime, strptime
  3. from datetime import datetime, timezone, timedelta
  4. import pytest
  5. import msgpack
  6. from ..helpers import adjust_patterns, exclude_path, Location, format_timedelta, ExcludePattern, make_path_safe, \
  7. prune_within, prune_split, \
  8. StableDict, int_to_bigint, bigint_to_int, parse_timestamp, CompressionSpec
  9. from . import BaseTestCase
  10. class BigIntTestCase(BaseTestCase):
  11. def test_bigint(self):
  12. self.assert_equal(int_to_bigint(0), 0)
  13. self.assert_equal(int_to_bigint(2**63-1), 2**63-1)
  14. self.assert_equal(int_to_bigint(-2**63+1), -2**63+1)
  15. self.assert_equal(int_to_bigint(2**63), b'\x00\x00\x00\x00\x00\x00\x00\x80\x00')
  16. self.assert_equal(int_to_bigint(-2**63), b'\x00\x00\x00\x00\x00\x00\x00\x80\xff')
  17. self.assert_equal(bigint_to_int(int_to_bigint(-2**70)), -2**70)
  18. self.assert_equal(bigint_to_int(int_to_bigint(2**70)), 2**70)
  19. class LocationTestCase(BaseTestCase):
  20. def test(self):
  21. self.assert_equal(
  22. repr(Location('ssh://user@host:1234/some/path::archive')),
  23. "Location(proto='ssh', user='user', host='host', port=1234, path='/some/path', archive='archive')"
  24. )
  25. self.assert_equal(
  26. repr(Location('file:///some/path::archive')),
  27. "Location(proto='file', user=None, host=None, port=None, path='/some/path', archive='archive')"
  28. )
  29. self.assert_equal(
  30. repr(Location('user@host:/some/path::archive')),
  31. "Location(proto='ssh', user='user', host='host', port=None, path='/some/path', archive='archive')"
  32. )
  33. self.assert_equal(
  34. repr(Location('path::archive')),
  35. "Location(proto='file', user=None, host=None, port=None, path='path', archive='archive')"
  36. )
  37. self.assert_equal(
  38. repr(Location('/some/absolute/path::archive')),
  39. "Location(proto='file', user=None, host=None, port=None, path='/some/absolute/path', archive='archive')"
  40. )
  41. self.assert_equal(
  42. repr(Location('some/relative/path::archive')),
  43. "Location(proto='file', user=None, host=None, port=None, path='some/relative/path', archive='archive')"
  44. )
  45. self.assert_raises(ValueError, lambda: Location('ssh://localhost:22/path:archive'))
  46. def test_canonical_path(self):
  47. locations = ['some/path::archive', 'file://some/path::archive', 'host:some/path::archive',
  48. 'host:~user/some/path::archive', 'ssh://host/some/path::archive',
  49. 'ssh://user@host:1234/some/path::archive']
  50. for location in locations:
  51. self.assert_equal(Location(location).canonical_path(),
  52. Location(Location(location).canonical_path()).canonical_path())
  53. class FormatTimedeltaTestCase(BaseTestCase):
  54. def test(self):
  55. t0 = datetime(2001, 1, 1, 10, 20, 3, 0)
  56. t1 = datetime(2001, 1, 1, 12, 20, 4, 100000)
  57. self.assert_equal(
  58. format_timedelta(t1 - t0),
  59. '2 hours 1.10 seconds'
  60. )
  61. class PatternTestCase(BaseTestCase):
  62. files = [
  63. '/etc/passwd', '/etc/hosts', '/home',
  64. '/home/user/.profile', '/home/user/.bashrc',
  65. '/home/user2/.profile', '/home/user2/public_html/index.html',
  66. '/var/log/messages', '/var/log/dmesg',
  67. ]
  68. def evaluate(self, paths, excludes):
  69. patterns = adjust_patterns(paths, [ExcludePattern(p) for p in excludes])
  70. return [path for path in self.files if not exclude_path(path, patterns)]
  71. def test(self):
  72. self.assert_equal(self.evaluate(['/'], []), self.files)
  73. self.assert_equal(self.evaluate([], []), self.files)
  74. self.assert_equal(self.evaluate(['/'], ['/h']), self.files)
  75. self.assert_equal(self.evaluate(['/'], ['/home']),
  76. ['/etc/passwd', '/etc/hosts', '/var/log/messages', '/var/log/dmesg'])
  77. self.assert_equal(self.evaluate(['/'], ['/home/']),
  78. ['/etc/passwd', '/etc/hosts', '/home', '/var/log/messages', '/var/log/dmesg'])
  79. self.assert_equal(self.evaluate(['/home/u'], []), [])
  80. self.assert_equal(self.evaluate(['/', '/home', '/etc/hosts'], ['/']), [])
  81. self.assert_equal(self.evaluate(['/home/'], ['/home/user2']),
  82. ['/home', '/home/user/.profile', '/home/user/.bashrc'])
  83. self.assert_equal(self.evaluate(['/'], ['*.profile', '/var/log']),
  84. ['/etc/passwd', '/etc/hosts', '/home', '/home/user/.bashrc', '/home/user2/public_html/index.html'])
  85. self.assert_equal(self.evaluate(['/'], ['/home/*/public_html', '*.profile', '*/log/*']),
  86. ['/etc/passwd', '/etc/hosts', '/home', '/home/user/.bashrc'])
  87. self.assert_equal(self.evaluate(['/etc/', '/var'], ['dmesg']),
  88. ['/etc/passwd', '/etc/hosts', '/var/log/messages', '/var/log/dmesg'])
  89. def test_compression_specs():
  90. with pytest.raises(ValueError):
  91. CompressionSpec('')
  92. assert CompressionSpec('0') == dict(name='zlib', level=0)
  93. assert CompressionSpec('1') == dict(name='zlib', level=1)
  94. assert CompressionSpec('9') == dict(name='zlib', level=9)
  95. with pytest.raises(ValueError):
  96. CompressionSpec('10')
  97. assert CompressionSpec('none') == dict(name='none')
  98. assert CompressionSpec('lz4') == dict(name='lz4')
  99. assert CompressionSpec('zlib') == dict(name='zlib', level=6)
  100. assert CompressionSpec('zlib,0') == dict(name='zlib', level=0)
  101. assert CompressionSpec('zlib,9') == dict(name='zlib', level=9)
  102. with pytest.raises(ValueError):
  103. CompressionSpec('zlib,9,invalid')
  104. assert CompressionSpec('lzma') == dict(name='lzma', level=6)
  105. assert CompressionSpec('lzma,0') == dict(name='lzma', level=0)
  106. assert CompressionSpec('lzma,9') == dict(name='lzma', level=9)
  107. with pytest.raises(ValueError):
  108. CompressionSpec('lzma,9,invalid')
  109. with pytest.raises(ValueError):
  110. CompressionSpec('invalid')
  111. class MakePathSafeTestCase(BaseTestCase):
  112. def test(self):
  113. self.assert_equal(make_path_safe('/foo/bar'), 'foo/bar')
  114. self.assert_equal(make_path_safe('/foo/bar'), 'foo/bar')
  115. self.assert_equal(make_path_safe('/f/bar'), 'f/bar')
  116. self.assert_equal(make_path_safe('fo/bar'), 'fo/bar')
  117. self.assert_equal(make_path_safe('../foo/bar'), 'foo/bar')
  118. self.assert_equal(make_path_safe('../../foo/bar'), 'foo/bar')
  119. self.assert_equal(make_path_safe('/'), '.')
  120. self.assert_equal(make_path_safe('/'), '.')
  121. class MockArchive:
  122. def __init__(self, ts):
  123. self.ts = ts
  124. def __repr__(self):
  125. return repr(self.ts)
  126. class PruneSplitTestCase(BaseTestCase):
  127. def test(self):
  128. def local_to_UTC(month, day):
  129. """Convert noon on the month and day in 2013 to UTC."""
  130. seconds = mktime(strptime('2013-%02d-%02d 12:00' % (month, day), '%Y-%m-%d %H:%M'))
  131. return datetime.fromtimestamp(seconds, tz=timezone.utc)
  132. def subset(lst, indices):
  133. return {lst[i] for i in indices}
  134. def dotest(test_archives, n, skip, indices):
  135. for ta in test_archives, reversed(test_archives):
  136. self.assert_equal(set(prune_split(ta, '%Y-%m', n, skip)),
  137. subset(test_archives, indices))
  138. test_pairs = [(1, 1), (2, 1), (2, 28), (3, 1), (3, 2), (3, 31), (5, 1)]
  139. test_dates = [local_to_UTC(month, day) for month, day in test_pairs]
  140. test_archives = [MockArchive(date) for date in test_dates]
  141. dotest(test_archives, 3, [], [6, 5, 2])
  142. dotest(test_archives, -1, [], [6, 5, 2, 0])
  143. dotest(test_archives, 3, [test_archives[6]], [5, 2, 0])
  144. dotest(test_archives, 3, [test_archives[5]], [6, 2, 0])
  145. dotest(test_archives, 3, [test_archives[4]], [6, 5, 2])
  146. dotest(test_archives, 0, [], [])
  147. class PruneWithinTestCase(BaseTestCase):
  148. def test(self):
  149. def subset(lst, indices):
  150. return {lst[i] for i in indices}
  151. def dotest(test_archives, within, indices):
  152. for ta in test_archives, reversed(test_archives):
  153. self.assert_equal(set(prune_within(ta, within)),
  154. subset(test_archives, indices))
  155. # 1 minute, 1.5 hours, 2.5 hours, 3.5 hours, 25 hours, 49 hours
  156. test_offsets = [60, 90*60, 150*60, 210*60, 25*60*60, 49*60*60]
  157. now = datetime.now(timezone.utc)
  158. test_dates = [now - timedelta(seconds=s) for s in test_offsets]
  159. test_archives = [MockArchive(date) for date in test_dates]
  160. dotest(test_archives, '1H', [0])
  161. dotest(test_archives, '2H', [0, 1])
  162. dotest(test_archives, '3H', [0, 1, 2])
  163. dotest(test_archives, '24H', [0, 1, 2, 3])
  164. dotest(test_archives, '26H', [0, 1, 2, 3, 4])
  165. dotest(test_archives, '2d', [0, 1, 2, 3, 4])
  166. dotest(test_archives, '50H', [0, 1, 2, 3, 4, 5])
  167. dotest(test_archives, '3d', [0, 1, 2, 3, 4, 5])
  168. dotest(test_archives, '1w', [0, 1, 2, 3, 4, 5])
  169. dotest(test_archives, '1m', [0, 1, 2, 3, 4, 5])
  170. dotest(test_archives, '1y', [0, 1, 2, 3, 4, 5])
  171. class StableDictTestCase(BaseTestCase):
  172. def test(self):
  173. d = StableDict(foo=1, bar=2, boo=3, baz=4)
  174. self.assert_equal(list(d.items()), [('bar', 2), ('baz', 4), ('boo', 3), ('foo', 1)])
  175. self.assert_equal(hashlib.md5(msgpack.packb(d)).hexdigest(), 'fc78df42cd60691b3ac3dd2a2b39903f')
  176. class TestParseTimestamp(BaseTestCase):
  177. def test(self):
  178. self.assert_equal(parse_timestamp('2015-04-19T20:25:00.226410'), datetime(2015, 4, 19, 20, 25, 0, 226410, timezone.utc))
  179. self.assert_equal(parse_timestamp('2015-04-19T20:25:00'), datetime(2015, 4, 19, 20, 25, 0, 0, timezone.utc))