OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import collections | 5 import collections |
| 6 import glob |
6 import json | 7 import json |
7 import logging | 8 import logging |
8 import os | 9 import os |
| 10 import shutil |
9 import sys | 11 import sys |
10 import tempfile | 12 import tempfile |
11 | 13 |
12 from cStringIO import StringIO | 14 from cStringIO import StringIO |
13 | 15 |
14 from infra.services.gnumbd import inner_loop as gnumbd | 16 from infra.services.gnumbd import gnumbd |
15 | 17 |
16 from infra.services.gnumbd.support import config_ref, data, git | 18 from infra.libs import git2 |
| 19 from infra.libs.git2 import data |
17 | 20 |
18 from infra.services.gnumbd.test import gnumbd_smoketests | 21 from infra.services.gnumbd.test import gnumbd_test_definitions |
19 | 22 |
20 from infra.ext import testing_support # pylint: disable=W0611 | 23 from infra.ext import testing_support # pylint: disable=W0611 |
21 from testing_support import expect_tests # pylint: disable=F0401 | 24 from testing_support import expect_tests # pylint: disable=F0401 |
22 | 25 |
23 BASE_PATH = os.path.dirname(os.path.abspath(__file__)) | 26 BASE_PATH = os.path.dirname(os.path.abspath(__file__)) |
24 | 27 |
25 | 28 |
26 # TODO(iannucci): Make these first class data library objects | 29 # TODO(iannucci): Make these first class data library objects |
27 class GitEntry(object): | 30 class GitEntry(object): |
28 typ = None | 31 typ = None |
(...skipping 29 matching lines...) Expand all Loading... |
58 self.entries = entries | 61 self.entries = entries |
59 | 62 |
60 def intern(self, repo): | 63 def intern(self, repo): |
61 with tempfile.TemporaryFile() as tf: | 64 with tempfile.TemporaryFile() as tf: |
62 for path, entry in self.entries.iteritems(): | 65 for path, entry in self.entries.iteritems(): |
63 tf.write('%s %s %s\t%s' % | 66 tf.write('%s %s %s\t%s' % |
64 (entry.mode, entry.typ, entry.intern(repo), path)) | 67 (entry.mode, entry.typ, entry.intern(repo), path)) |
65 tf.seek(0) | 68 tf.seek(0) |
66 return repo.run('mktree', '-z', stdin=tf).strip() | 69 return repo.run('mktree', '-z', stdin=tf).strip() |
67 | 70 |
68 class TestRef(git.Ref): | 71 class TestRef(git2.Ref): |
69 def synthesize_commit(self, message, number=None, tree=None, svn=False, | 72 def synthesize_commit(self, message, number=None, tree=None, svn=False, |
70 footers=None): | 73 footers=None): |
71 footers = footers or collections.OrderedDict() | 74 footers = footers or collections.OrderedDict() |
72 if number is not None: | 75 if number is not None: |
73 if svn: | 76 if svn: |
74 footers[gnumbd.GIT_SVN_ID] = [ | 77 footers[gnumbd.GIT_SVN_ID] = [ |
75 'svn://repo/path@%s 0039d316-1c4b-4281-b951-d872f2087c98' % number] | 78 'svn://repo/path@%s 0039d316-1c4b-4281-b951-d872f2087c98' % number] |
76 else: | 79 else: |
77 footers[gnumbd.COMMIT_POSITION] = [ | 80 footers[gnumbd.COMMIT_POSITION] = [ |
78 gnumbd.FMT_COMMIT_POSITION(self, number)] | 81 gnumbd.FMT_COMMIT_POSITION(self, number)] |
79 | 82 |
80 commit = self.repo.synthesize_commit(self.commit, message, footers=footers, | 83 commit = self.repo.synthesize_commit(self.commit, message, footers=footers, |
81 tree=tree) | 84 tree=tree) |
82 self.update_to(commit) | 85 self.update_to(commit) |
83 return commit | 86 return commit |
84 | 87 |
85 | 88 |
86 class TestClock(object): | 89 class TestClock(object): |
87 def __init__(self): | 90 def __init__(self): |
88 self._time = 1402589336 | 91 self._time = 1402589336 |
89 | 92 |
90 def time(self): | 93 def time(self): |
91 self._time += 10 | 94 self._time += 10 |
92 return self._time | 95 return self._time |
93 | 96 |
94 | 97 |
95 class TestConfigRef(config_ref.ConfigRef): | 98 class TestConfigRef(gnumbd.GnumbdConfigRef): |
96 def update(self, **values): | 99 def update(self, **values): |
97 new_config = self.current | 100 new_config = self.current |
98 new_config.update(values) | 101 new_config.update(values) |
99 self.ref.synthesize_commit( | 102 self.ref.synthesize_commit( |
100 'update(%r)' % values.keys(), | 103 'update(%r)' % values.keys(), |
101 tree=GitTree({'config.json': GitFile(json.dumps(new_config))})) | 104 tree=GitTree({'config.json': GitFile(json.dumps(new_config))})) |
102 | 105 |
103 | 106 |
104 class TestRepo(git.Repo): | 107 class TestRepo(git2.Repo): |
105 def __init__(self, short_name, tmpdir, clock, mirror_of=None): | 108 def __init__(self, short_name, tmpdir, clock, mirror_of=None): |
106 super(TestRepo, self).__init__(mirror_of or 'local test repo') | 109 super(TestRepo, self).__init__(mirror_of or 'local test repo') |
107 self._short_name = short_name | 110 self._short_name = short_name |
108 self.repos_dir = tmpdir | 111 self.repos_dir = tmpdir |
109 | 112 |
110 if mirror_of is None: | 113 if mirror_of is None: |
111 self._repo_path = tempfile.mkdtemp(dir=self.repos_dir, suffix='.git') | 114 self._repo_path = tempfile.mkdtemp(dir=self.repos_dir, suffix='.git') |
112 self.run('init', '--bare') | 115 self.run('init', '--bare') |
113 | 116 |
114 self._clock = clock | 117 self._clock = clock |
115 | 118 |
116 # pylint: disable=W0212 | 119 # pylint: disable=W0212 |
117 repo_path = property(lambda self: self._repo_path) | 120 repo_path = property(lambda self: self._repo_path) |
118 | 121 |
119 def __getitem__(self, refstr): | 122 def __getitem__(self, refstr): |
120 return TestRef(self, refstr) | 123 return TestRef(self, refstr) |
121 | 124 |
122 def synthesize_commit(self, parent, message, tree=None, footers=None): | 125 def synthesize_commit(self, parent, message, tree=None, footers=None): |
123 tree = tree or GitTree({'file': GitFile('contents')}) | 126 tree = tree or GitTree({'file': GitFile('contents')}) |
124 tree = tree.intern(self) if isinstance(tree, GitTree) else tree | 127 tree = tree.intern(self) if isinstance(tree, GitTree) else tree |
125 assert isinstance(tree, str) | 128 assert isinstance(tree, str) |
126 | 129 |
127 parents = [parent.hsh] if parent is not git.INVALID else [] | 130 parents = [parent.hsh] if parent is not git2.INVALID else [] |
128 | 131 |
129 timestamp = data.CommitTimestamp(self._clock.time(), '+', 8, 0) | 132 timestamp = data.CommitTimestamp(self._clock.time(), '+', 8, 0) |
130 user = data.CommitUser('Test User', 'test_user@example.com', timestamp) | 133 user = data.CommitUser('Test User', 'test_user@example.com', timestamp) |
131 | 134 |
132 return self.get_commit(self.intern(data.CommitData( | 135 return self.get_commit(self.intern(data.CommitData( |
133 tree, parents, user, user, (), message.splitlines(), | 136 tree, parents, user, user, (), message.splitlines(), |
134 data.CommitData.merge_lines([], footers or {}) | 137 data.CommitData.merge_lines([], footers or {}) |
135 ), 'commit')) | 138 ), 'commit')) |
136 | 139 |
137 def snap(self, include_committer=False, include_config=False): | 140 def snap(self, include_committer=False, include_config=False): |
138 ret = {} | 141 ret = {} |
139 if include_committer: | 142 if include_committer: |
140 fmt = '%H%x00committer %cn <%ce> %ci%n%n%B%x00%x00' | 143 fmt = '%H%x00committer %cn <%ce> %ci%n%n%B%x00%x00' |
141 else: | 144 else: |
142 fmt = '%H%x00%B%x00%x00' | 145 fmt = '%H%x00%B%x00%x00' |
143 for ref in (r.ref for r in self.refglob('*')): | 146 for ref in (r.ref for r in self.refglob('*')): |
144 if ref == gnumbd.DEFAULT_CONFIG_REF and not include_config: | 147 if ref == gnumbd.GnumbdConfigRef.REF and not include_config: |
145 continue | 148 continue |
146 log = self.run('log', ref, '--format=%s' % fmt) | 149 log = self.run('log', ref, '--format=%s' % fmt) |
147 ret[ref] = collections.OrderedDict( | 150 ret[ref] = collections.OrderedDict( |
148 (commit, message.splitlines()) | 151 (commit, message.splitlines()) |
149 for commit, message in ( | 152 for commit, message in ( |
150 r.split('\0') for r in log.split('\0\0\n') if r) | 153 r.split('\0') for r in log.split('\0\0\n') if r) |
151 ) | 154 ) |
152 return ret | 155 return ret |
153 | 156 |
154 def __repr__(self): | 157 def __repr__(self): |
155 return 'TestRepo(%r)' % self._short_name | 158 return 'TestRepo(%r)' % self._short_name |
156 | 159 |
157 | 160 |
158 def RunTest(tmpdir, test_name): | 161 def RunTest(tmpdir, test_name): |
159 ret = [] | 162 ret = [] |
160 clock = TestClock() | 163 clock = TestClock() |
161 origin = TestRepo('origin', tmpdir, clock) | 164 origin = TestRepo('origin', tmpdir, clock) |
162 local = TestRepo('local', tmpdir, clock, origin.repo_path) | 165 local = TestRepo('local', tmpdir, clock, origin.repo_path) |
163 | 166 |
164 cref = TestConfigRef(origin[gnumbd.DEFAULT_CONFIG_REF]) | 167 cref = TestConfigRef(origin) |
165 cref.update(enabled_refglobs=['refs/heads/*'], interval=0) | 168 cref.update(enabled_refglobs=['refs/heads/*'], interval=0) |
166 | 169 |
167 def checkpoint(message, include_committer=False, include_config=False): | 170 def checkpoint(message, include_committer=False, include_config=False): |
168 ret.append([message, {'origin': origin.snap(include_committer, | 171 ret.append([message, {'origin': origin.snap(include_committer, |
169 include_config)}]) | 172 include_config)}]) |
170 | 173 |
171 def run(include_log=True): | 174 def run(include_log=True): |
172 stdout = sys.stdout | 175 stdout = sys.stdout |
173 stderr = sys.stderr | 176 stderr = sys.stderr |
174 | 177 |
(...skipping 16 matching lines...) Expand all Loading... |
191 ret.append(traceback.format_exc().splitlines()) | 194 ret.append(traceback.format_exc().splitlines()) |
192 finally: | 195 finally: |
193 sys.stdout = stdout | 196 sys.stdout = stdout |
194 sys.stderr = stderr | 197 sys.stderr = stderr |
195 | 198 |
196 if include_log: | 199 if include_log: |
197 root_logger.removeHandler(shandler) | 200 root_logger.removeHandler(shandler) |
198 root_logger.setLevel(log_level) | 201 root_logger.setLevel(log_level) |
199 ret.append({'log output': logout.getvalue().splitlines()}) | 202 ret.append({'log output': logout.getvalue().splitlines()}) |
200 | 203 |
201 gnumbd_smoketests.GNUMBD_TESTS[test_name]( | 204 gnumbd_test_definitions.GNUMBD_TESTS[test_name]( |
202 origin, local, cref, run, checkpoint) | 205 origin, local, cref, run, checkpoint) |
203 | 206 |
204 return expect_tests.Result(ret) | 207 return expect_tests.Result(ret) |
205 | 208 |
206 | 209 |
207 def GenTests(tmpdir): | 210 @expect_tests.test_generator |
208 for test_name, test in gnumbd_smoketests.GNUMBD_TESTS.iteritems(): | 211 def GenTests(): |
| 212 suffix = '.gnumbd_inner_loop' |
| 213 tmpdir = tempfile.mkdtemp(suffix) |
| 214 for p in glob.glob(os.path.join(os.path.dirname(tmpdir), '*'+suffix)): |
| 215 if p != tmpdir: # pragma: no cover |
| 216 shutil.rmtree(p) |
| 217 |
| 218 yield expect_tests.Cleanup(expect_tests.FuncCall(shutil.rmtree, tmpdir)) |
| 219 |
| 220 for test_name, test in gnumbd_test_definitions.GNUMBD_TESTS.iteritems(): |
209 yield expect_tests.Test( | 221 yield expect_tests.Test( |
210 __package__ + '.' + test_name, | 222 __package__ + '.' + test_name, |
211 expect_tests.FuncCall(RunTest, tmpdir, test_name), | 223 expect_tests.FuncCall(RunTest, tmpdir, test_name), |
212 os.path.join(BASE_PATH, 'gnumbd_smoketests.expected'), | 224 expect_base=test_name, ext='yaml', break_funcs=[test], |
213 test_name, 'yaml', break_funcs=[test]) | 225 covers=( |
| 226 expect_tests.Test.covers_obj(RunTest) + |
| 227 expect_tests.Test.covers_obj(gnumbd_test_definitions) |
| 228 )) |
OLD | NEW |