OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2016 The LUCI Authors. All rights reserved. | 2 # Copyright 2016 The LUCI Authors. All rights reserved. |
3 # Use of this source code is governed under the Apache License, Version 2.0 | 3 # Use of this source code is governed under the Apache License, Version 2.0 |
4 # that can be found in the LICENSE file. | 4 # that can be found in the LICENSE file. |
5 | 5 |
6 import logging | 6 import logging |
7 import os | 7 import os |
8 import sys | 8 import sys |
9 import tempfile | 9 import tempfile |
10 import unittest | 10 import unittest |
11 | 11 |
12 ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath( | 12 ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath( |
13 __file__.decode(sys.getfilesystemencoding())))) | 13 __file__.decode(sys.getfilesystemencoding())))) |
14 sys.path.insert(0, ROOT_DIR) | 14 sys.path.insert(0, ROOT_DIR) |
15 sys.path.insert(0, os.path.join(ROOT_DIR, 'third_party')) | 15 sys.path.insert(0, os.path.join(ROOT_DIR, 'third_party')) |
16 | 16 |
17 from depot_tools import fix_encoding | 17 from depot_tools import fix_encoding |
18 from utils import file_path | 18 from utils import file_path |
19 from utils import fs | 19 from utils import fs |
20 import named_cache | 20 import named_cache |
21 | 21 |
22 | 22 |
| 23 def write_file(path, contents): |
| 24 with open(path, 'wb') as f: |
| 25 f.write(contents) |
| 26 |
| 27 |
| 28 def read_file(path): |
| 29 with open(path, 'rb') as f: |
| 30 return f.read() |
| 31 |
| 32 |
23 class CacheManagerTest(unittest.TestCase): | 33 class CacheManagerTest(unittest.TestCase): |
24 def setUp(self): | 34 def setUp(self): |
25 self.tempdir = tempfile.mkdtemp(prefix=u'named_cache_test') | 35 self.tempdir = tempfile.mkdtemp(prefix=u'named_cache_test') |
26 self.manager = named_cache.CacheManager(self.tempdir) | 36 self.manager = named_cache.CacheManager(self.tempdir) |
27 | 37 |
28 def tearDown(self): | 38 def tearDown(self): |
29 try: | 39 try: |
30 file_path.rmtree(self.tempdir) | 40 file_path.rmtree(self.tempdir) |
31 finally: | 41 finally: |
32 super(CacheManagerTest, self).tearDown() | 42 super(CacheManagerTest, self).tearDown() |
33 | 43 |
34 def test_request(self): | 44 def make_caches(self, names): |
35 with self.assertRaises(AssertionError): | 45 dest_dir = tempfile.mkdtemp(prefix=u'named_cache_test') |
36 self.manager.request('foo') # manager is not open | 46 try: |
37 with self.manager.open(): | 47 names = map(unicode, names) |
38 foo_path = self.manager.request('foo') | 48 for n in names: |
39 self.assertEqual(foo_path, self.manager.request('foo')) | 49 self.manager.install(os.path.join(dest_dir, n), n) |
40 bar_path = self.manager.request('bar') | 50 self.assertEqual(set(names), set(os.listdir(dest_dir))) |
41 self.assertEqual( | 51 for n in names: |
42 foo_path, | 52 self.manager.uninstall(os.path.join(dest_dir, n), n) |
43 os.path.abspath(os.readlink( | 53 self.assertEqual([], os.listdir(dest_dir)) |
44 os.path.join(self.tempdir, 'named', 'foo')))) | 54 self.assertTrue(self.manager.available.issuperset(names)) |
45 self.assertEqual( | 55 finally: |
46 bar_path, | 56 file_path.rmtree(dest_dir) |
47 os.path.abspath(os.readlink( | |
48 os.path.join(self.tempdir, 'named', 'bar')))) | |
49 self.assertEqual(os.path.dirname(bar_path), self.tempdir) | |
50 self.assertEqual([], os.listdir(foo_path)) | |
51 self.assertEqual([], os.listdir(bar_path)) | |
52 | 57 |
53 def test_get_oldest(self): | 58 def test_get_oldest(self): |
54 with self.manager.open(): | 59 with self.manager.open(): |
55 self.assertIsNone(self.manager.get_oldest()) | 60 self.assertIsNone(self.manager.get_oldest()) |
56 for i in xrange(10): | 61 self.make_caches(range(10)) |
57 self.manager.request(str(i)) | 62 self.assertEqual(self.manager.get_oldest(), u'0') |
58 self.assertEqual(self.manager.get_oldest(), '0') | |
59 | 63 |
60 def test_get_timestamp(self): | 64 def test_get_timestamp(self): |
61 now = 0 | 65 now = 0 |
62 time_fn = lambda: now | 66 time_fn = lambda: now |
63 with self.manager.open(time_fn=time_fn): | 67 with self.manager.open(time_fn=time_fn): |
64 for i in xrange(10): | 68 for i in xrange(10): |
65 self.manager.request(str(i)) | 69 self.make_caches([i]) |
66 now += 1 | 70 now += 1 |
67 for i in xrange(10): | 71 for i in xrange(10): |
68 self.assertEqual(i, self.manager.get_timestamp(str(i))) | 72 self.assertEqual(i, self.manager.get_timestamp(str(i))) |
69 | 73 |
70 def test_create_symlinks(self): | 74 def test_clean_cache(self): |
71 dest_dir = tempfile.mkdtemp(prefix=u'named_cache_test') | 75 dest_dir = tempfile.mkdtemp(prefix=u'named_cache_test') |
72 with self.manager.open(): | 76 with self.manager.open(): |
73 for i in xrange(10): | 77 self.assertEqual([], os.listdir(self.manager.root_dir)) |
74 self.manager.request(str(i)) | 78 |
75 self.manager.create_symlinks(dest_dir, [('1', 'a'), ('3', 'c')]) | 79 a_path = os.path.join(dest_dir, u'a') |
76 self.assertEqual({'a', 'c'}, set(os.listdir(dest_dir))) | 80 b_path = os.path.join(dest_dir, u'b') |
| 81 |
| 82 self.manager.install(a_path, u'1') |
| 83 self.manager.install(b_path, u'2') |
| 84 |
| 85 self.assertEqual({u'a', u'b'}, set(os.listdir(dest_dir))) |
| 86 self.assertFalse(self.manager.available) |
| 87 self.assertEqual([], os.listdir(self.manager.root_dir)) |
| 88 |
| 89 write_file(os.path.join(a_path, u'x'), u'x') |
| 90 write_file(os.path.join(b_path, u'y'), u'y') |
| 91 |
| 92 self.manager.uninstall(a_path, u'1') |
| 93 self.manager.uninstall(b_path, u'2') |
| 94 |
| 95 self.assertEqual(3, len(os.listdir(self.manager.root_dir))) |
| 96 path1 = os.path.join(self.manager.root_dir, self.manager._lru['1']) |
| 97 path2 = os.path.join(self.manager.root_dir, self.manager._lru['2']) |
| 98 |
| 99 self.assertEqual('x', read_file(os.path.join(path1, u'x'))) |
| 100 self.assertEqual('y', read_file(os.path.join(path2, u'y'))) |
| 101 self.assertEqual(os.readlink(self.manager._get_named_path('1')), path1) |
| 102 self.assertEqual(os.readlink(self.manager._get_named_path('2')), path2) |
| 103 |
| 104 def test_existing_cache(self): |
| 105 dest_dir = tempfile.mkdtemp(prefix=u'named_cache_test') |
| 106 with self.manager.open(): |
| 107 # Assume test_clean passes. |
| 108 a_path = os.path.join(dest_dir, u'a') |
| 109 b_path = os.path.join(dest_dir, u'b') |
| 110 |
| 111 self.manager.install(a_path, u'1') |
| 112 write_file(os.path.join(dest_dir, u'a', u'x'), u'x') |
| 113 self.manager.uninstall(a_path, u'1') |
| 114 |
| 115 # Test starts here. |
| 116 self.manager.install(a_path, u'1') |
| 117 self.manager.install(b_path, u'2') |
| 118 self.assertEqual({'a', 'b'}, set(os.listdir(dest_dir))) |
| 119 self.assertFalse(self.manager.available) |
| 120 self.assertEqual(['named'], os.listdir(self.manager.root_dir)) |
| 121 |
77 self.assertEqual( | 122 self.assertEqual( |
78 os.readlink(os.path.join(dest_dir, 'a')), self.manager.request('1')) | 123 'x', read_file(os.path.join(os.path.join(dest_dir, u'a', u'x')))) |
79 self.assertEqual( | 124 write_file(os.path.join(a_path, 'x'), 'x2') |
80 os.readlink(os.path.join(dest_dir, 'c')), self.manager.request('3')) | 125 write_file(os.path.join(b_path, 'y'), 'y') |
81 self.assertEqual([], os.listdir(os.path.join(dest_dir, 'c'))) | 126 |
| 127 self.manager.uninstall(a_path, '1') |
| 128 self.manager.uninstall(b_path, '2') |
| 129 |
| 130 self.assertEqual(3, len(os.listdir(self.manager.root_dir))) |
| 131 path1 = os.path.join(self.manager.root_dir, self.manager._lru['1']) |
| 132 path2 = os.path.join(self.manager.root_dir, self.manager._lru['2']) |
| 133 |
| 134 self.assertEqual('x2', read_file(os.path.join(path1, 'x'))) |
| 135 self.assertEqual('y', read_file(os.path.join(path2, 'y'))) |
| 136 self.assertEqual(os.readlink(self.manager._get_named_path('1')), path1) |
| 137 self.assertEqual(os.readlink(self.manager._get_named_path('2')), path2) |
82 | 138 |
83 def test_trim(self): | 139 def test_trim(self): |
84 with self.manager.open(): | 140 with self.manager.open(): |
85 item_count = named_cache.MAX_CACHE_SIZE + 10 | 141 item_count = named_cache.MAX_CACHE_SIZE + 10 |
86 for i in xrange(item_count): | 142 self.make_caches(range(item_count)) |
87 self.manager.request(str(i)) | |
88 self.assertEqual(len(self.manager), item_count) | 143 self.assertEqual(len(self.manager), item_count) |
89 self.manager.trim(None) | 144 self.manager.trim(None) |
90 self.assertEqual(len(self.manager), named_cache.MAX_CACHE_SIZE) | 145 self.assertEqual(len(self.manager), named_cache.MAX_CACHE_SIZE) |
91 self.assertEqual( | 146 self.assertEqual( |
92 set(map(str, xrange(10, 10 + named_cache.MAX_CACHE_SIZE))), | 147 set(map(str, xrange(10, 10 + named_cache.MAX_CACHE_SIZE))), |
93 set(os.listdir(os.path.join(self.tempdir, 'named'))), | 148 set(os.listdir(os.path.join(self.tempdir, 'named')))) |
94 ) | |
95 | 149 |
96 def test_corrupted(self): | 150 def test_corrupted(self): |
97 with open(os.path.join(self.tempdir, u'state.json'), 'w') as f: | 151 with open(os.path.join(self.tempdir, u'state.json'), 'w') as f: |
98 f.write('}}}}') | 152 f.write('}}}}') |
99 fs.makedirs(os.path.join(self.tempdir, 'a'), 0777) | 153 fs.makedirs(os.path.join(self.tempdir, 'a'), 0777) |
100 with self.manager.open(): | 154 with self.manager.open(): |
101 self.assertFalse(os.path.isdir(self.tempdir)) | 155 self.assertFalse(os.path.isdir(self.tempdir)) |
102 self.manager.request('a') | 156 self.make_caches(['a']) |
103 self.assertTrue(fs.islink(os.path.join(self.tempdir, 'named', 'a'))) | 157 self.assertTrue(fs.islink(os.path.join(self.tempdir, 'named', 'a'))) |
104 | 158 |
105 | |
106 if __name__ == '__main__': | 159 if __name__ == '__main__': |
107 fix_encoding.fix_encoding() | 160 fix_encoding.fix_encoding() |
108 VERBOSE = '-v' in sys.argv | 161 VERBOSE = '-v' in sys.argv |
109 logging.basicConfig(level=logging.DEBUG if VERBOSE else logging.ERROR) | 162 logging.basicConfig(level=logging.DEBUG if VERBOSE else logging.ERROR) |
110 unittest.main() | 163 unittest.main() |
OLD | NEW |