| OLD | NEW |
| (Empty) |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 import unittest | |
| 6 | |
| 7 import mock | |
| 8 | |
| 9 from dashboard.common import namespaced_stored_object | |
| 10 from dashboard.common import testing_common | |
| 11 from dashboard.pinpoint.models import change | |
| 12 | |
| 13 | |
| 14 _CATAPULT_URL = ('https://chromium.googlesource.com/' | |
| 15 'external/github.com/catapult-project/catapult') | |
| 16 _CHROMIUM_URL = 'https://chromium.googlesource.com/chromium/src' | |
| 17 | |
| 18 | |
| 19 class ChangeTest(testing_common.TestCase): | |
| 20 | |
| 21 def setUp(self): | |
| 22 super(ChangeTest, self).setUp() | |
| 23 | |
| 24 self.SetCurrentUser('internal@chromium.org', is_admin=True) | |
| 25 | |
| 26 namespaced_stored_object.Set('repositories', { | |
| 27 'catapult': {'repository_url': _CATAPULT_URL}, | |
| 28 'chromium': {'repository_url': _CHROMIUM_URL}, | |
| 29 }) | |
| 30 | |
| 31 def testChange(self): | |
| 32 base_commit = change.Dep('chromium', 'aaa7336c821888839f759c6c0a36b56c6678') | |
| 33 dep = change.Dep('catapult', 'e0a2efbb3d1a81aac3c90041eefec24f066d26ba') | |
| 34 patch = change.Patch('https://codereview.chromium.org', 2565263002, 20001) | |
| 35 | |
| 36 # Also test the deps conversion to frozenset. | |
| 37 c = change.Change(base_commit, [dep], patch) | |
| 38 | |
| 39 self.assertEqual(c, change.Change(base_commit, (dep,), patch)) | |
| 40 string = ('chromium@aaa7336 catapult@e0a2efb + ' | |
| 41 'https://codereview.chromium.org/2565263002/20001') | |
| 42 id_string = ('chromium@aaa7336c821888839f759c6c0a36b56c6678 ' | |
| 43 'catapult@e0a2efbb3d1a81aac3c90041eefec24f066d26ba + ' | |
| 44 'https://codereview.chromium.org/2565263002/20001') | |
| 45 self.assertEqual(str(c), string) | |
| 46 self.assertEqual(c.id_string, id_string) | |
| 47 self.assertEqual(c.base_commit, base_commit) | |
| 48 self.assertEqual(c.deps, frozenset((dep,))) | |
| 49 self.assertEqual(c.all_deps, (base_commit, dep)) | |
| 50 self.assertEqual(c.patch, patch) | |
| 51 | |
| 52 def testAsDict(self): | |
| 53 base_commit = change.Dep('chromium', 'aaa7336c82') | |
| 54 dep = change.Dep('catapult', 'e0a2efbb3d') | |
| 55 patch = change.Patch('https://codereview.chromium.org', 2565263002, 20001) | |
| 56 c = change.Change(base_commit, [dep], patch) | |
| 57 | |
| 58 expected = { | |
| 59 'base_commit': { | |
| 60 'repository': 'chromium', | |
| 61 'git_hash': 'aaa7336c82', | |
| 62 'url': _CHROMIUM_URL + '/+/aaa7336c82', | |
| 63 }, | |
| 64 'deps': [{ | |
| 65 'repository': 'catapult', | |
| 66 'git_hash': 'e0a2efbb3d', | |
| 67 'url': _CATAPULT_URL + '/+/e0a2efbb3d', | |
| 68 }], | |
| 69 'patch': { | |
| 70 'server': 'https://codereview.chromium.org', | |
| 71 'issue': 2565263002, | |
| 72 'patchset': 20001, | |
| 73 }, | |
| 74 } | |
| 75 self.assertEqual(c.AsDict(), expected) | |
| 76 | |
| 77 @mock.patch('dashboard.services.gitiles_service.CommitInfo') | |
| 78 def testFromDictWithJustBaseCommit(self, _): | |
| 79 c = change.Change.FromDict({ | |
| 80 'base_commit': {'repository': 'chromium', 'git_hash': 'aaa7336'}, | |
| 81 }) | |
| 82 | |
| 83 expected = change.Change(change.Dep('chromium', 'aaa7336')) | |
| 84 self.assertEqual(c, expected) | |
| 85 | |
| 86 @mock.patch('dashboard.services.gitiles_service.CommitInfo') | |
| 87 def testFromDictWithAllFields(self, _): | |
| 88 c = change.Change.FromDict({ | |
| 89 'base_commit': {'repository': 'chromium', 'git_hash': 'aaa7336'}, | |
| 90 'deps': ({'repository': 'catapult', 'git_hash': 'e0a2efb'},), | |
| 91 'patch': { | |
| 92 'server': 'https://codereview.chromium.org', | |
| 93 'issue': 2565263002, | |
| 94 'patchset': 20001, | |
| 95 }, | |
| 96 }) | |
| 97 | |
| 98 base_commit = change.Dep('chromium', 'aaa7336') | |
| 99 deps = (change.Dep('catapult', 'e0a2efb'),) | |
| 100 patch = change.Patch('https://codereview.chromium.org', 2565263002, 20001) | |
| 101 expected = change.Change(base_commit, deps, patch) | |
| 102 self.assertEqual(c, expected) | |
| 103 | |
| 104 @mock.patch('dashboard.services.gitiles_service.CommitRange') | |
| 105 def testMidpointSuccess(self, commit_range): | |
| 106 commit_range.return_value = [ | |
| 107 {'commit': 'babe852'}, | |
| 108 {'commit': 'b57345e'}, | |
| 109 {'commit': '949b36d'}, | |
| 110 {'commit': '1ef4789'}, | |
| 111 ] | |
| 112 | |
| 113 change_a = change.Change(change.Dep('chromium', '0e57e2b'), | |
| 114 (change.Dep('catapult', 'e0a2efb'),)) | |
| 115 change_b = change.Change(change.Dep('chromium', 'babe852'), | |
| 116 (change.Dep('catapult', 'e0a2efb'),)) | |
| 117 self.assertEqual(change.Change.Midpoint(change_a, change_b), | |
| 118 change.Change(change.Dep('chromium', '949b36d'), | |
| 119 (change.Dep('catapult', 'e0a2efb'),))) | |
| 120 | |
| 121 @mock.patch('dashboard.services.gitiles_service.FileContents') | |
| 122 @mock.patch('dashboard.services.gitiles_service.CommitRange') | |
| 123 def testMidpointWithDepsRoll(self, commit_range, file_contents): | |
| 124 def _CommitRange(repository_url, first_git_hash, last_git_hash): | |
| 125 del repository_url | |
| 126 if first_git_hash == '0e57e2b' and last_git_hash == 'babe852': | |
| 127 return [{'commit': 'babe852'}] | |
| 128 if first_git_hash == '0000000' and last_git_hash == '2222222': | |
| 129 return [{'commit': '2222222'}, {'commit': '1111111'}] | |
| 130 raise NotImplementedError() | |
| 131 commit_range.side_effect = _CommitRange | |
| 132 | |
| 133 def _FileContents(repository_url, git_hash, path): | |
| 134 del repository_url | |
| 135 del path | |
| 136 if git_hash == '0e57e2b': | |
| 137 # Also test the stripping of '.git' from repository URLs. | |
| 138 return 'deps = {"chromium/catapult": "%s@0000000"}' % ( | |
| 139 _CATAPULT_URL + '.git') | |
| 140 if git_hash == 'babe852': | |
| 141 return 'deps = {"chromium/catapult": "%s@2222222"}' % _CATAPULT_URL | |
| 142 raise NotImplementedError() | |
| 143 file_contents.side_effect = _FileContents | |
| 144 | |
| 145 change_a = change.Change(change.Dep('chromium', '0e57e2b')) | |
| 146 change_b = change.Change(change.Dep('chromium', 'babe852')) | |
| 147 expected = change.Change(change.Dep('chromium', '0e57e2b'), | |
| 148 (change.Dep('catapult', '1111111'),)) | |
| 149 | |
| 150 self.assertEqual(change.Change.Midpoint(change_a, change_b), expected) | |
| 151 | |
| 152 @mock.patch('dashboard.services.gitiles_service.FileContents') | |
| 153 @mock.patch('dashboard.services.gitiles_service.CommitRange') | |
| 154 def testMidpointAcrossDepsRoll(self, commit_range, file_contents): | |
| 155 def _CommitRange(repository_url, first_git_hash, last_git_hash): | |
| 156 del repository_url | |
| 157 if first_git_hash == '0e57e2b' and last_git_hash == 'babe852': | |
| 158 return [{'commit': 'babe852'}] | |
| 159 if first_git_hash == '1111111' and last_git_hash == '3333333': | |
| 160 return [{'commit': '3333333'}, {'commit': '2222222'}] | |
| 161 raise NotImplementedError() | |
| 162 commit_range.side_effect = _CommitRange | |
| 163 | |
| 164 def _FileContents(repository_url, git_hash, path): | |
| 165 del repository_url | |
| 166 del path | |
| 167 if git_hash == '0e57e2b': | |
| 168 return 'deps = {"src/catapult": "%s@0000000"}' % _CATAPULT_URL | |
| 169 if git_hash == 'babe852': | |
| 170 return 'deps = {"src/catapult": "%s@3333333"}' % _CATAPULT_URL | |
| 171 raise NotImplementedError() | |
| 172 file_contents.side_effect = _FileContents | |
| 173 | |
| 174 change_a = change.Change(change.Dep('chromium', '0e57e2b'), | |
| 175 (change.Dep('catapult', '1111111'),)) | |
| 176 change_b = change.Change(change.Dep('chromium', 'babe852')) | |
| 177 expected = change.Change(change.Dep('chromium', '0e57e2b'), | |
| 178 (change.Dep('catapult', '2222222'),)) | |
| 179 | |
| 180 self.assertEqual(change.Change.Midpoint(change_a, change_b), expected) | |
| 181 | |
| 182 def testMidpointRaisesWithDifferingPatch(self): | |
| 183 change_a = change.Change(change.Dep('chromium', '0e57e2b')) | |
| 184 change_b = change.Change( | |
| 185 change.Dep('chromium', 'babe852'), | |
| 186 patch=change.Patch('https://codereview.chromium.org', 2565263002, 20001)
) | |
| 187 with self.assertRaises(change.NonLinearError): | |
| 188 change.Change.Midpoint(change_a, change_b) | |
| 189 | |
| 190 def testMidpointRaisesWithDifferingRepository(self): | |
| 191 change_a = change.Change(change.Dep('chromium', '0e57e2b')) | |
| 192 change_b = change.Change(change.Dep('not_chromium', 'babe852')) | |
| 193 with self.assertRaises(change.NonLinearError): | |
| 194 change.Change.Midpoint(change_a, change_b) | |
| 195 | |
| 196 @mock.patch('dashboard.services.gitiles_service.FileContents') | |
| 197 @mock.patch('dashboard.services.gitiles_service.CommitRange') | |
| 198 def testMidpointRaisesAcrossDepsRollWhenDepAlreadyOverridden( | |
| 199 self, commit_range, file_contents): | |
| 200 def _CommitRange(repository_url, first_git_hash, last_git_hash): | |
| 201 del repository_url | |
| 202 if first_git_hash == '0e57e2b' and last_git_hash == 'babe852': | |
| 203 return [{'commit': 'babe852'}] | |
| 204 if first_git_hash == '1111111' and last_git_hash == '3333333': | |
| 205 return [{'commit': '3333333'}, {'commit': '2222222'}] | |
| 206 raise NotImplementedError() | |
| 207 commit_range.side_effect = _CommitRange | |
| 208 | |
| 209 def _FileContents(repository_url, git_hash, path): | |
| 210 del repository_url | |
| 211 del path | |
| 212 if git_hash == '0e57e2b': | |
| 213 return 'deps = {"src/catapult": "%s@0000000"}' % _CATAPULT_URL | |
| 214 if git_hash == 'babe852': | |
| 215 return 'deps = {"src/catapult": "%s@4444444"}' % _CATAPULT_URL | |
| 216 raise NotImplementedError() | |
| 217 file_contents.side_effect = _FileContents | |
| 218 | |
| 219 change_a = change.Change(change.Dep('chromium', '0e57e2b'), | |
| 220 (change.Dep('catapult', '1111111'),)) | |
| 221 change_b = change.Change(change.Dep('chromium', 'babe852'), | |
| 222 (change.Dep('catapult', '3333333'),)) | |
| 223 | |
| 224 with self.assertRaises(change.NonLinearError): | |
| 225 change.Change.Midpoint(change_a, change_b) | |
| 226 | |
| 227 def testMidpointReturnsNoneWithTheSameChange(self): | |
| 228 c = change.Change(change.Dep('chromium', '0e57e2b')) | |
| 229 self.assertIsNone(change.Change.Midpoint(c, c)) | |
| 230 | |
| 231 @mock.patch('dashboard.services.gitiles_service.FileContents') | |
| 232 @mock.patch('dashboard.services.gitiles_service.CommitRange') | |
| 233 def testMidpointReturnsNoneWithAdjacentCommitsAndNoDepsRoll( | |
| 234 self, commit_range, file_contents): | |
| 235 commit_range.return_value = [{'commit': 'b57345e'}] | |
| 236 file_contents.return_value = 'deps = {}' | |
| 237 | |
| 238 change_a = change.Change(change.Dep('chromium', '949b36d')) | |
| 239 change_b = change.Change(change.Dep('chromium', 'b57345e')) | |
| 240 self.assertIsNone(change.Change.Midpoint(change_a, change_b)) | |
| 241 | |
| 242 | |
| 243 class DepTest(testing_common.TestCase): | |
| 244 | |
| 245 def setUp(self): | |
| 246 super(DepTest, self).setUp() | |
| 247 | |
| 248 self.SetCurrentUser('internal@chromium.org', is_admin=True) | |
| 249 | |
| 250 namespaced_stored_object.Set('repositories', { | |
| 251 'chromium': {'repository_url': _CHROMIUM_URL}, | |
| 252 }) | |
| 253 | |
| 254 def testDep(self): | |
| 255 dep = change.Dep('chromium', 'aaa7336c821888839f759c6c0a36b56c6678') | |
| 256 | |
| 257 other_dep = change.Dep(u'chromium', u'aaa7336c821888839f759c6c0a36b56c6678') | |
| 258 self.assertEqual(dep, other_dep) | |
| 259 self.assertEqual(str(dep), 'chromium@aaa7336') | |
| 260 self.assertEqual(dep.id_string, | |
| 261 'chromium@aaa7336c821888839f759c6c0a36b56c6678') | |
| 262 self.assertEqual(dep.repository, 'chromium') | |
| 263 self.assertEqual(dep.git_hash, 'aaa7336c821888839f759c6c0a36b56c6678') | |
| 264 self.assertEqual(dep.repository_url, | |
| 265 'https://chromium.googlesource.com/chromium/src') | |
| 266 | |
| 267 @mock.patch('dashboard.services.gitiles_service.FileContents') | |
| 268 def testDeps(self, file_contents): | |
| 269 file_contents.return_value = """ | |
| 270 vars = { | |
| 271 'chromium_git': 'https://chromium.googlesource.com', | |
| 272 } | |
| 273 deps = { | |
| 274 'src/v8': Var('chromium_git') + '/v8/v8.git' + '@' + 'c092edb', | |
| 275 } | |
| 276 deps_os = { | |
| 277 'win': { | |
| 278 'src/third_party/cygwin': | |
| 279 Var('chromium_git') + '/chromium/deps/cygwin.git' + '@' + 'c89e446', | |
| 280 } | |
| 281 } | |
| 282 """ | |
| 283 | |
| 284 dep = change.Dep('chromium', 'aaa7336') | |
| 285 expected = frozenset(( | |
| 286 change.Dep('cygwin', 'c89e446'), | |
| 287 change.Dep('v8', 'c092edb'), | |
| 288 )) | |
| 289 self.assertEqual(dep.Deps(), expected) | |
| 290 | |
| 291 def testAsDict(self): | |
| 292 dep = change.Dep('chromium', 'aaa7336') | |
| 293 expected = { | |
| 294 'repository': 'chromium', | |
| 295 'git_hash': 'aaa7336', | |
| 296 'url': _CHROMIUM_URL + '/+/aaa7336', | |
| 297 } | |
| 298 self.assertEqual(dep.AsDict(), expected) | |
| 299 | |
| 300 @mock.patch('dashboard.services.gitiles_service.CommitInfo') | |
| 301 def testFromDict(self, _): | |
| 302 dep = change.Dep.FromDict({ | |
| 303 'repository': 'chromium', | |
| 304 'git_hash': 'aaa7336', | |
| 305 }) | |
| 306 | |
| 307 expected = change.Dep('chromium', 'aaa7336') | |
| 308 self.assertEqual(dep, expected) | |
| 309 | |
| 310 @mock.patch('dashboard.services.gitiles_service.CommitInfo') | |
| 311 def testFromDictWithRepositoryUrl(self, _): | |
| 312 dep = change.Dep.FromDict({ | |
| 313 'repository': 'https://chromium.googlesource.com/chromium/src', | |
| 314 'git_hash': 'aaa7336', | |
| 315 }) | |
| 316 | |
| 317 expected = change.Dep('chromium', 'aaa7336') | |
| 318 self.assertEqual(dep, expected) | |
| 319 | |
| 320 def testFromDictFailureFromUnknownRepo(self): | |
| 321 with self.assertRaises(KeyError): | |
| 322 change.Dep.FromDict({ | |
| 323 'repository': 'unknown repo', | |
| 324 'git_hash': 'git hash', | |
| 325 }) | |
| 326 | |
| 327 @mock.patch('dashboard.services.gitiles_service.CommitInfo') | |
| 328 def testFromDictFailureFromUnknownCommit(self, commit_info): | |
| 329 commit_info.side_effect = KeyError() | |
| 330 | |
| 331 with self.assertRaises(KeyError): | |
| 332 change.Dep.FromDict({ | |
| 333 'repository': 'chromium', | |
| 334 'git_hash': 'unknown git hash', | |
| 335 }) | |
| 336 | |
| 337 @mock.patch('dashboard.services.gitiles_service.CommitRange') | |
| 338 def testMidpointSuccess(self, commit_range): | |
| 339 commit_range.return_value = [ | |
| 340 {'commit': 'babe852'}, | |
| 341 {'commit': 'b57345e'}, | |
| 342 {'commit': '949b36d'}, | |
| 343 {'commit': '1ef4789'}, | |
| 344 ] | |
| 345 | |
| 346 dep_a = change.Dep('chromium', '0e57e2b') | |
| 347 dep_b = change.Dep('chromium', 'babe852') | |
| 348 self.assertEqual(change.Dep.Midpoint(dep_a, dep_b), | |
| 349 change.Dep('chromium', '949b36d')) | |
| 350 | |
| 351 def testMidpointRaisesWithDifferingRepositories(self): | |
| 352 dep_a = change.Dep('chromium', '0e57e2b') | |
| 353 dep_b = change.Dep('not_chromium', 'babe852') | |
| 354 | |
| 355 with self.assertRaises(ValueError): | |
| 356 change.Dep.Midpoint(dep_a, dep_b) | |
| 357 | |
| 358 @mock.patch('dashboard.services.gitiles_service.CommitRange') | |
| 359 def testMidpointReturnsNoneWithAdjacentCommits(self, commit_range): | |
| 360 commit_range.return_value = [{'commit': 'b57345e'}] | |
| 361 | |
| 362 dep_a = change.Dep('chromium', '949b36d') | |
| 363 dep_b = change.Dep('chromium', 'b57345e') | |
| 364 self.assertIsNone(change.Dep.Midpoint(dep_a, dep_b)) | |
| 365 | |
| 366 @mock.patch('dashboard.services.gitiles_service.CommitRange') | |
| 367 def testMidpointReturnsNoneWithEmptyRange(self, commit_range): | |
| 368 commit_range.return_value = [] | |
| 369 | |
| 370 dep_b = change.Dep('chromium', 'b57345e') | |
| 371 dep_a = change.Dep('chromium', '949b36d') | |
| 372 self.assertIsNone(change.Dep.Midpoint(dep_a, dep_b)) | |
| 373 | |
| 374 | |
| 375 class PatchTest(unittest.TestCase): | |
| 376 | |
| 377 def testPatch(self): | |
| 378 patch = change.Patch('https://codereview.chromium.org', 2851943002, 40001) | |
| 379 | |
| 380 other_patch = change.Patch(u'https://codereview.chromium.org', | |
| 381 2851943002, 40001) | |
| 382 self.assertEqual(patch, other_patch) | |
| 383 string = 'https://codereview.chromium.org/2851943002/40001' | |
| 384 self.assertEqual(str(patch), string) | |
| 385 self.assertEqual(patch.id_string, string) | |
| 386 | |
| 387 def testAsDict(self): | |
| 388 patch = change.Patch('https://codereview.chromium.org', 2851943002, 40001) | |
| 389 expected = { | |
| 390 'server': 'https://codereview.chromium.org', | |
| 391 'issue': 2851943002, | |
| 392 'patchset': 40001, | |
| 393 } | |
| 394 self.assertEqual(patch.AsDict(), expected) | |
| 395 | |
| 396 def testFromDict(self): | |
| 397 patch = change.Patch.FromDict({ | |
| 398 'server': 'https://codereview.chromium.org', | |
| 399 'issue': 2851943002, | |
| 400 'patchset': 40001, | |
| 401 }) | |
| 402 | |
| 403 expected = change.Patch('https://codereview.chromium.org', | |
| 404 2851943002, 40001) | |
| 405 self.assertEqual(patch, expected) | |
| OLD | NEW |