blob: e0bae414f876183b4cc2c0f3ccde434b2f69ed5b [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001# Copyright 2018 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style
3# license that can be found in the LICENSE file or at
4# https://developers.google.com/open-source/licenses/bsd
5
6"""Unittests for the flt launch issues conversion task."""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10import copy
11import unittest
12import settings
13import mock
14
15from businesslogic import work_env
16from framework import exceptions
17from framework import permissions
18from services import service_manager
19from services import template_svc
20from tracker import fltconversion
21from tracker import tracker_bizobj
22from testing import fake
23from testing import testing_helpers
24from proto import tracker_pb2
25
26class FLTConvertTask(unittest.TestCase):
27
28 def setUp(self):
29 self.services = service_manager.Services(
30 issue=fake.IssueService(),
31 user=fake.UserService(),
32 project=fake.ProjectService(),
33 config=fake.ConfigService(),
34 template=mock.Mock(spec=template_svc.TemplateService),)
35 self.mr = testing_helpers.MakeMonorailRequest()
36 self.task = fltconversion.FLTConvertTask(
37 'req', 'res', services=self.services)
38 self.task.mr = self.mr
39 self.issue = fake.MakeTestIssue(
40 789, 1, 'summary', 'New', 111, issue_id=78901)
41 self.config = tracker_bizobj.MakeDefaultProjectIssueConfig(789)
42 self.work_env = work_env.WorkEnv(
43 self.mr, self.services, 'Testing')
44 self.issue1 = fake.MakeTestIssue(
45 789, 1, 'sum', 'New', 111, issue_id=78901,
46 labels=[
47 'Launch-M-Approved-71-Stable', 'Launch-M-Target-70-Beta',
48 'Launch-UI-Yes', 'Launch-Privacy-NeedInfo',
49 'pm-jojwang', 'tl-annajo', 'ux-shiba', 'Type-Launch'])
50 self.issue2 = fake.MakeTestIssue(
51 789, 2, 'sum', 'New', 111, issue_id=78902,
52 labels=[
53 'Launch-M-Target-71-Stable', 'Launch-M-Approved-70-Beta',
54 'pm-jojwang', 'tl-annajo', 'OS-Chrome', 'OS-Android',
55 'Type-Launch'])
56 self.issue3 = fake.MakeTestIssue(
57 789, 3, 'sum', 'New', 111, issue_id=78903,
58 labels=['Launch-M-Approved-71-Stable',
59 'Launch-M-Approved-79-Stable-Exp', 'Launch-M-Target-70-Beta',
60 'Launch-M-Target-70-Stable', 'Launch-UI-Yes',
61 'Launch-Exp-Leadership-Yes', 'pm-annajo', 'tl-jojwang',
62 'OS-Chrome', 'Type-Launch'])
63 self.issue4 = fake.MakeTestIssue(
64 789, 4, 'sum', 'New', 111, issue_id=78904,
65 labels=['Launch-UI-Yes', 'OS-Chrome', 'Type-Launch'])
66 self.issue5 = fake.MakeTestIssue(
67 789, 5, 'sum', 'New', 111, issue_id=78905,
68 labels=['Launch-M-Approved-71-Stable',
69 'Launch-M-Approved-79-Stable-Exp', 'Launch-M-Target-70-Beta',
70 'Launch-M-Target-70-Stable', 'Launch-UI-Yes',
71 'Launch-Privacy-NeedInfo', 'Launch-Exp-Leadership-Yes',
72 'pm-annajo', 'tl-jojwang', 'OS-Chrome', 'Type-Launch'])
73
74 self.approval_values = [
75 tracker_pb2.ApprovalValue(
76 approval_id=7, status=tracker_pb2.ApprovalStatus.NOT_SET),
77 tracker_pb2.ApprovalValue(
78 approval_id=8, status=tracker_pb2.ApprovalStatus.NEEDS_REVIEW)]
79 self.phases = [tracker_pb2.Phase(name='Stable', phase_id=88),
80 tracker_pb2.Phase(name='Beta', phase_id=89)]
81
82 def testAssertBasePermission(self):
83 self.mr.auth.user_pb.is_site_admin = True
84 settings.app_id = 'monorail-staging'
85 self.task.AssertBasePermission(self.mr)
86
87 settings.app_id = 'monorail-prod'
88 self.task.AssertBasePermission(self.mr)
89
90 self.mr.auth.user_pb.is_site_admin = False
91 self.assertRaises(permissions.PermissionException,
92 self.task.AssertBasePermission, self.mr)
93
94 def testHandleRequest(self):
95 # Set up Objects
96 project_info = fltconversion.ProjectInfo(
97 self.config, 'q=query', self.approval_values, self.phases,
98 11, 12, 13, 16, 14, 15, fltconversion.BROWSER_PHASE_MAP,
99 fltconversion.BROWSER_APPROVALS_TO_LABELS,
100 fltconversion.BROWSER_M_LABELS_RE)
101
102 self.config.field_defs = [
103 tracker_pb2.FieldDef(field_id=7, field_name='Chrome-UX',
104 field_type=tracker_pb2.FieldTypes.APPROVAL_TYPE),
105 tracker_pb2.FieldDef(field_id=8, field_name='Chrome-Privacy',
106 field_type=tracker_pb2.FieldTypes.APPROVAL_TYPE)
107 ]
108
109 # Set up mocks
110 patcher = mock.patch(
111 'search.frontendsearchpipeline.FrontendSearchPipeline',
112 spec=True, visible_results=[self.issue1, self.issue2])
113 mockPipeline = patcher.start()
114
115 self.task.services.issue.GetIssue = mock.Mock(
116 side_effect=[self.issue1, self.issue2])
117
118 self.task.FetchAndAssertProjectInfo = mock.Mock(return_value=project_info)
119
120 with self.work_env as we:
121 we.ListIssues = mock.Mock(return_value=mockPipeline)
122
123 def side_effect(_cnxn, email):
124 if email == 'jojwang@chromium.org':
125 return 111
126 if email == 'annajo@google.com':
127 return 222
128 if email == 'shiba@google.com':
129 return 333
130 raise exceptions.NoSuchUserException
131 self.task.services.user.LookupUserID = mock.Mock(side_effect=side_effect)
132
133 self.task.ExecuteIssueChanges = mock.Mock(return_value=[])
134
135 # Call
136 json = self.task.HandleRequest(self.mr)
137
138 # assert
139 self.assertEqual(json['converted_issues'], [1, 2])
140
141 new_approvals1 = [
142 tracker_pb2.ApprovalValue(
143 approval_id=7, status=tracker_pb2.ApprovalStatus.APPROVED),
144 tracker_pb2.ApprovalValue(
145 approval_id=8, status=tracker_pb2.ApprovalStatus.NEED_INFO)]
146 new_fvs1 = [
147 # M-Approved Stable
148 tracker_bizobj.MakeFieldValue(
149 15, 71, None, None, None, None, False, phase_id=88),
150 # M-Target Beta
151 tracker_bizobj.MakeFieldValue(
152 14, 70, None, None, None, None, False, phase_id=89),
153 # PM field
154 tracker_bizobj.MakeFieldValue(
155 11, None, None, 111, None, None, False),
156 # TL field
157 tracker_bizobj.MakeFieldValue(
158 12, None, None, 222, None, None, False),
159 # UX field
160 tracker_bizobj.MakeFieldValue(
161 16, None, None, 333, None, None, False)
162 ]
163
164
165 new_approvals2 = [
166 tracker_pb2.ApprovalValue(
167 approval_id=7, status=tracker_pb2.ApprovalStatus.NOT_SET),
168 tracker_pb2.ApprovalValue(
169 approval_id=8, status=tracker_pb2.ApprovalStatus.NEEDS_REVIEW)
170 ]
171 new_fvs2 = [
172 tracker_bizobj.MakeFieldValue(
173 14, 71, None, None, None, None, False, phase_id=88),
174 tracker_bizobj.MakeFieldValue(
175 15, 70, None, None, None, None, False, phase_id=89),
176 # PM field
177 tracker_bizobj.MakeFieldValue(
178 11, None, None, 111, None, None, False),
179 # TL field
180 tracker_bizobj.MakeFieldValue(
181 12, None, None, 222, None, None, False)]
182
183 execute_calls = [
184 mock.call(
185 self.config, self.issue1, new_approvals1, self.phases, new_fvs1),
186 mock.call(
187 self.config, self.issue2, new_approvals2, self.phases, new_fvs2)]
188 self.task.ExecuteIssueChanges.assert_has_calls(execute_calls)
189
190 patcher.stop()
191
192 def testHandleRequest_UndoConversion(self):
193 # test Delete() is actually called
194 mr = testing_helpers.MakeMonorailRequest(path='url/url?launch=delete')
195 self.task.UndoConversion = mock.Mock(return_value={'deleteing': [1, 2]})
196 actualReturn = self.task.HandleRequest(mr)
197 self.assertEqual({'deleteing': [1, 2]}, actualReturn)
198
199 def testUndoConversion(self):
200 # Set up objects
201 self.issue1.field_values = [
202 # Test non phase and TL/PM/TE field_values are not deleted
203 tracker_bizobj.MakeFieldValue(
204 17, None, 'strvalue', None, None, None, False)]
205 issue1 = copy.deepcopy(self.issue1)
206 issue2 = copy.deepcopy(self.issue2)
207 fvs = [
208 tracker_bizobj.MakeFieldValue(
209 11, None, None, 222, None, None, False),
210 tracker_bizobj.MakeFieldValue(
211 12, None, None, 111, None, None, False),
212 tracker_bizobj.MakeFieldValue(
213 16, None, None, 111, None, None, False)]
214 self.config.field_defs = [
215 tracker_pb2.FieldDef(field_id=11, field_name='PM'),
216 tracker_pb2.FieldDef(field_id=12, field_name='TL'),
217 tracker_pb2.FieldDef(field_id=13, field_name='TE'),
218 tracker_pb2.FieldDef(field_id=16, field_name='UX')]
219 # Make element edits made during conversion that should be undone.
220 issue1.labels.extend(['Type-FLT-Launch', 'FLT-Conversion'])
221 issue1.labels.remove('Type-Launch')
222 issue2.labels.extend(['Type-FLT-Launch', 'FLT-Conversion'])
223 issue2.labels.remove('Type-Launch')
224 issue1.approval_values = self.approval_values
225 issue2.approval_values = self.approval_values
226 issue1.phases = self.phases
227 issue2.phases = self.phases
228 issue1.field_values.extend(fvs)
229
230 # Set up mocks
231 patcher = mock.patch(
232 'search.frontendsearchpipeline.FrontendSearchPipeline',
233 spec=True, visible_results=[issue1, issue2]) # converted issues
234 mockPipeline = patcher.start()
235 self.task.services.project.GetProjectByName = mock.Mock()
236 self.task.services.config.GetProjectConfig = mock.Mock(
237 return_value=self.config)
238 self.task.services.issue.GetIssue = mock.Mock(
239 side_effect=[issue1, issue2])
240 self.task.services.issue._UpdateIssuesApprovals = mock.Mock()
241 self.task.services.issue.UpdateIssue = mock.Mock()
242
243 with self.work_env as we:
244 we.ListIssues = mock.Mock(return_value=mockPipeline)
245
246 json = self.task.UndoConversion(self.mr)
247 self.assertEqual(json['deleting'], [1, 2])
248 # assert convert issue1 is back to the pre-conversion state, self.issue1.
249 self.assertEqual(issue1, self.issue1)
250 self.assertEqual(issue2, self.issue2)
251
252 # assert UpdateIssue calls were made with pre-conversion state issues.
253 update_calls = [
254 mock.call(self.mr.cnxn, self.issue1),
255 mock.call(self.mr.cnxn, self.issue2)]
256 self.task.services.issue._UpdateIssuesApprovals.assert_has_calls(
257 update_calls)
258 self.task.services.issue.UpdateIssue.assert_has_calls(update_calls)
259 patcher.stop()
260
261 def testVerifyConversion(self):
262 # Set up objects
263 self.issue1.labels.extend(
264 # Launch-M-Target-70-Stable-Exp should be ignored
265 ['Rollout-Type-Default', 'Launch-M-Target-70-Stable-Exp'])
266 self.issue1.phases = [tracker_pb2.Phase(name='Beta', phase_id=1),
267 tracker_pb2.Phase(name='Stable', phase_id=2)]
268 self.issue1.approval_values = [
269 tracker_pb2.ApprovalValue(
270 approval_id=1, status=tracker_pb2.ApprovalStatus.NOT_SET),
271 tracker_pb2.ApprovalValue(
272 approval_id=2, status=tracker_pb2.ApprovalStatus.APPROVED),
273 tracker_pb2.ApprovalValue(
274 approval_id=3, status=tracker_pb2.ApprovalStatus.NEED_INFO),
275 ]
276 self.issue1.field_values = [
277 # problem = expected field for TL
278 tracker_bizobj.MakeFieldValue(4, None, None, 111, None, None, False),
279 tracker_pb2.FieldValue(field_id=7, int_value=70, phase_id=1),
280 tracker_pb2.FieldValue(field_id=8, int_value=71, phase_id=2),
281 ]
282
283 self.issue2.labels.extend(['Rollout-Type-Finch'])
284 self.issue2.phases = [tracker_pb2.Phase(name='Beta', phase_id=1),
285 tracker_pb2.Phase(name='Stable-Full', phase_id=2),
286 tracker_pb2.Phase(name='Stable-Exp', phase_id=3)]
287 self.issue2.approval_values = [
288 tracker_pb2.ApprovalValue(
289 approval_id=1, status=tracker_pb2.ApprovalStatus.NOT_SET),
290 tracker_pb2.ApprovalValue(
291 approval_id=2, status=tracker_pb2.ApprovalStatus.NOT_SET),
292 tracker_pb2.ApprovalValue(
293 # problem = approval Chrome-Privacy has status approved for None
294 approval_id=3, status=tracker_pb2.ApprovalStatus.APPROVED),
295 ]
296 self.issue2.field_values = [
297 # problem = no phase field for label 'Launch-M-Approved-70-Beta'
298 tracker_pb2.FieldValue(field_id=7, int_value=71, phase_id=2),
299 tracker_bizobj.MakeFieldValue(4, None, None, 111, None, None, False),
300 tracker_bizobj.MakeFieldValue(5, None, None, 111, None, None, False),
301 ]
302
303 self.issue3.labels.extend(['Rollout-Type-Default'])
304 self.issue3.phases = [tracker_pb2.Phase(name='Feature Freeze', phase_id=4),
305 tracker_pb2.Phase(name='Branch', phase_id=5),
306 tracker_pb2.Phase(name='Stable', phase_id=6)]
307 self.issue3.approval_values = [
308 tracker_pb2.ApprovalValue(
309 approval_id=9, status=tracker_pb2.ApprovalStatus.APPROVED),
310 tracker_pb2.ApprovalValue(
311 approval_id=10, status=tracker_pb2.ApprovalStatus.NEEDS_REVIEW)]
312 # problem = no phase field label Launch-M-Target-70-Stable
313 # problem = missing a field for TL
314 self.issue3.field_values = [
315 tracker_pb2.FieldValue(field_id=8, int_value=71, phase_id=6),
316 tracker_bizobj.MakeFieldValue(4, None, None, 111, None, None, False)
317 ]
318
319 self.issue4.labels.extend(['Rollout-Type-Default'])
320 # problem = incorrect phases for OS default launch
321 self.issue4.phases = [tracker_pb2.Phase(name='Branch', phase_id=5),
322 tracker_pb2.Phase(name='Stable-Exp', phase_id=7)]
323 # problem = approval ChromeOS-UX has status 'NEEDS_REVIEW'
324 # for label value Yes
325 self.issue4.approval_values = [
326 tracker_pb2.ApprovalValue(
327 approval_id=9, status=tracker_pb2.ApprovalStatus.NEEDS_REVIEW)]
328
329 self.issue5.labels.extend(['Rollout-Type-Finch'])
330 self.issue5.phases = [tracker_pb2.Phase(name='Branch', phase_id=5),
331 tracker_pb2.Phase(name='Feature Freeze', phase_id=4),
332 tracker_pb2.Phase(name='Stable-Exp', phase_id=7),
333 tracker_pb2.Phase(name='Stable-Full', phase_id=8)]
334 self.issue5.approval_values = [
335 tracker_pb2.ApprovalValue(
336 approval_id=9, status=tracker_pb2.ApprovalStatus.APPROVED),
337 # problem = approval ChromeOS-Privacy has status 'REVIEW_REQUESTED'
338 # for label value NeedInfo
339 tracker_pb2.ApprovalValue(
340 approval_id=11, status=tracker_pb2.ApprovalStatus.REVIEW_REQUESTED),
341 # problem = approval ChromeOS-Leadership-Exp has status 'NA' for label
342 # value Yes.
343 tracker_pb2.ApprovalValue(
344 approval_id=13, status=tracker_pb2.ApprovalStatus.NA)
345 ]
346
347 # problem = no phase field for label Launch-M-Approved-79-Stable-Exp
348 # problem = no phase field for label Launch-M-Target-70-Stable
349 self.issue5.field_values = [
350 tracker_pb2.FieldValue(field_id=8, int_value=71, phase_id=8),
351 tracker_bizobj.MakeFieldValue(4, None, None, 111, None, None, False),
352 tracker_bizobj.MakeFieldValue(5, None, None, 111, None, None, False)]
353
354 self.config.field_defs = [
355 tracker_pb2.FieldDef(field_id=1, field_name='Chrome-Test'),
356 tracker_pb2.FieldDef(field_id=2, field_name='Chrome-UX'),
357 tracker_pb2.FieldDef(field_id=3, field_name='Chrome-Privacy'),
358 tracker_pb2.FieldDef(field_id=4, field_name='PM'),
359 tracker_pb2.FieldDef(field_id=5, field_name='TL'),
360 tracker_pb2.FieldDef(field_id=6, field_name='TE'),
361 tracker_pb2.FieldDef(field_id=12, field_name='UX'),
362 tracker_pb2.FieldDef(field_id=7, field_name='M-Target'),
363 tracker_pb2.FieldDef(field_id=8, field_name='M-Approved'),
364 tracker_pb2.FieldDef(field_id=9, field_name='ChromeOS-UX'),
365 tracker_pb2.FieldDef(field_id=10, field_name='ChromeOS-Enterprise'),
366 tracker_pb2.FieldDef(field_id=11, field_name='ChromeOS-Privacy'),
367 tracker_pb2.FieldDef(field_id=13, field_name='ChromeOS-Leadership-Exp')
368 ]
369
370 # Set up mocks
371 patcher = mock.patch(
372 'search.frontendsearchpipeline.FrontendSearchPipeline',
373 spec=True, allowed_results=[
374 self.issue1, self.issue2, self.issue3, self.issue4, self.issue5])
375 mockPipeline = patcher.start()
376 self.task.services.project.GetProjectByName = mock.Mock()
377 self.task.services.config.GetProjectConfig = mock.Mock(
378 return_value=self.config)
379 self.task.services.issue.GetIssue = mock.Mock(
380 side_effect=[
381 self.issue1, self.issue2, self.issue3, self.issue4, self.issue5])
382 self.task.services.user.LookupUserID = mock.Mock(return_value=111)
383 with self.work_env as we:
384 we.ListIssues = mock.Mock(return_value=mockPipeline)
385
386 # Assert
387 json = self.task.VerifyConversion(self.mr)
388 self.assertEqual(json['issues verified'],
389 ['issue 1', 'issue 2', 'issue 3', 'issue 4', 'issue 5'])
390 problems = json['problems found']
391 expected_problems = [
392 'issue 1: missing a field for TL',
393 'issue 1: missing a field for UX',
394 'issue 2: approval Chrome-Privacy has status \'APPROVED\' for '
395 'label value None',
396 'issue 2: no phase field for label Launch-M-Approved-70-Beta',
397 'issue 3: missing a field for TL',
398 'issue 3: no phase field for label Launch-M-Target-70-Stable',
399 'issue 4: incorrect phases for OS default launch.',
400 'issue 4: approval ChromeOS-UX has status \'NEEDS_REVIEW\' for '
401 'label value Yes',
402 'issue 5: approval ChromeOS-Privacy has status \'REVIEW_REQUESTED\' '
403 'for label value NeedInfo',
404 'issue 5: approval ChromeOS-Leadership-Exp has status \'NA\' for label '
405 'value Yes',
406 'issue 5: no phase field for label Launch-M-Approved-79-Stable-Exp',
407 'issue 5: no phase field for label Launch-M-Target-70-Stable',
408 ]
409 self.assertEqual(problems, expected_problems)
410 patcher.stop()
411
412 def testFetchAndAssertProjectInfo(self):
413
414 # test no 'launch' in request
415 self.assertRaisesRegexp(
416 AssertionError, r'bad launch type:',
417 self.task.FetchAndAssertProjectInfo, self.mr)
418
419 # test bad 'launch' in request
420 mr = testing_helpers.MakeMonorailRequest(path='url/url?launch=bad')
421 self.assertRaisesRegexp(
422 AssertionError, r'bad launch type: bad',
423 self.task.FetchAndAssertProjectInfo, mr)
424
425 self.task.services.project.GetProjectByName = mock.Mock()
426 self.task.services.config.GetProjectConfig = mock.Mock(
427 return_value=self.config)
428
429 mr = testing_helpers.MakeMonorailRequest(path='url/url?launch=default')
430 # test no template
431 self.task.services.template.GetTemplateByName = mock.Mock(
432 return_value=None)
433 self.assertRaisesRegexp(
434 AssertionError, r'not found in chromium project',
435 self.task.FetchAndAssertProjectInfo, mr)
436
437 # test template has no phases/approvals
438 template = tracker_bizobj.MakeIssueTemplate(
439 'template', 'sum', 'New', 111, 'content', [], [], [], [])
440 self.task.services.template.GetTemplateByName = mock.Mock(
441 return_value=template)
442 self.assertRaisesRegexp(
443 AssertionError, 'no approvals or phases in',
444 self.task.FetchAndAssertProjectInfo, mr)
445
446 # test phases not recognized
447 template.phases = [tracker_pb2.Phase(name='WeirdPhase')]
448 template.approval_values = [tracker_pb2.ApprovalValue()]
449 self.assertRaisesRegexp(
450 AssertionError, 'one or more phases not recognized',
451 self.task.FetchAndAssertProjectInfo, mr)
452
453 template.phases = [tracker_pb2.Phase(name='Stable'),
454 tracker_pb2.Phase(name='Stable-Exp')]
455 template.approval_values = [
456 tracker_pb2.ApprovalValue(approval_id=1),
457 tracker_pb2.ApprovalValue(approval_id=2),
458 tracker_pb2.ApprovalValue(approval_id=3)]
459
460 # test approvals not recognized
461 self.assertRaisesRegexp(
462 AssertionError, 'one or more approvals not recognized',
463 self.task.FetchAndAssertProjectInfo, mr)
464
465 self.config.field_defs = [
466 tracker_pb2.FieldDef(field_id=1, field_name='ChromeOS-Enterprise',
467 field_type=tracker_pb2.FieldTypes.APPROVAL_TYPE),
468 tracker_pb2.FieldDef(field_id=2, field_name='Chrome-UX',
469 field_type=tracker_pb2.FieldTypes.APPROVAL_TYPE),
470 tracker_pb2.FieldDef(field_id=3, field_name='Chrome-Privacy',
471 field_type=tracker_pb2.FieldTypes.APPROVAL_TYPE)
472 ]
473
474 # test approvals not in config's approval_defs
475 self.assertRaisesRegexp(
476 AssertionError, 'one or more approvals not in config.approval_defs',
477 self.task.FetchAndAssertProjectInfo, mr)
478
479 self.config.approval_defs = [
480 tracker_pb2.ApprovalDef(approval_id=1),
481 tracker_pb2.ApprovalDef(approval_id=2),
482 tracker_pb2.ApprovalDef(approval_id=3)]
483
484 # test no pm field exists in project
485 self.assertRaisesRegexp(
486 AssertionError, 'project has no FieldDef %s' % fltconversion.PM_FIELD,
487 self.task.FetchAndAssertProjectInfo, mr)
488
489 self.config.field_defs.extend([
490 tracker_pb2.FieldDef(field_id=4, field_name='PM',
491 field_type=tracker_pb2.FieldTypes.USER_TYPE),
492 tracker_pb2.FieldDef(field_id=5, field_name='TL',
493 field_type=tracker_pb2.FieldTypes.USER_TYPE),
494 tracker_pb2.FieldDef(field_id=9, field_name='UX',
495 field_type=tracker_pb2.FieldTypes.USER_TYPE),
496 tracker_pb2.FieldDef(field_id=6, field_name='TE')
497 ])
498
499 # test no USER_TYPE te field exists in project
500 self.assertRaisesRegexp(
501 AssertionError, 'project has no FieldDef %s' % fltconversion.TE_FIELD,
502 self.task.FetchAndAssertProjectInfo, mr)
503
504 self.config.field_defs[-1].field_type = tracker_pb2.FieldTypes.USER_TYPE
505 self.config.field_defs.extend([
506 tracker_pb2.FieldDef(
507 field_id=7, field_name='M-Target', is_phase_field=True),
508 tracker_pb2.FieldDef(
509 field_id=8, field_name='M-Approved', is_multivalued=True,
510 field_type=tracker_pb2.FieldTypes.INT_TYPE)
511 ])
512
513 # test no M-Target INT_TYPE multivalued Phase FieldDefs
514 self.assertRaisesRegexp(
515 AssertionError,
516 'project has no FieldDef %s' % fltconversion.MTARGET_FIELD,
517 self.task.FetchAndAssertProjectInfo, mr)
518
519 self.config.field_defs[-2].field_type = tracker_pb2.FieldTypes.INT_TYPE
520 self.config.field_defs[-2].is_multivalued = True
521
522 # test no M-Approved INT_TYPE multivalued Phase FieldDefs
523 self.assertRaisesRegexp(
524 AssertionError,
525 'project has no FieldDef %s' % fltconversion.MAPPROVED_FIELD,
526 self.task.FetchAndAssertProjectInfo, mr)
527
528 self.config.field_defs[-1].is_phase_field = True
529
530 self.assertEqual(
531 self.task.FetchAndAssertProjectInfo(mr),
532 fltconversion.ProjectInfo(
533 self.config, fltconversion.QUERY_MAP['default'],
534 template.approval_values, template.phases, 4, 5, 6, 9, 7, 8,
535 fltconversion.BROWSER_PHASE_MAP,
536 fltconversion.BROWSER_APPROVALS_TO_LABELS,
537 fltconversion.BROWSER_M_LABELS_RE))
538
539 # FINCH special case
540 # test approvals for Finch not required
541 mr = testing_helpers.MakeMonorailRequest(path='url/url?launch=finch')
542 self.assertRaisesRegexp(
543 AssertionError, 'finch template not set up correctly',
544 self.task.FetchAndAssertProjectInfo, mr)
545
546 for av in template.approval_values:
547 av.status = tracker_pb2.ApprovalStatus.NEEDS_REVIEW
548
549 self.assertEqual(
550 self.task.FetchAndAssertProjectInfo(mr),
551 fltconversion.ProjectInfo(
552 self.config, fltconversion.QUERY_MAP['finch'],
553 template.approval_values, template.phases, 4, 5, 6, 9, 7, 8,
554 fltconversion.BROWSER_PHASE_MAP,
555 fltconversion.BROWSER_APPROVALS_TO_LABELS,
556 fltconversion.BROWSER_M_LABELS_RE))
557
558 def testFetchAndAssertProjectInfo_OS(self):
559 self.task.services.project.GetProjectByName = mock.Mock()
560 self.task.services.config.GetProjectConfig = mock.Mock(
561 return_value=self.config)
562
563 mr = testing_helpers.MakeMonorailRequest(path='url/url?launch=os')
564 template = tracker_bizobj.MakeIssueTemplate(
565 'template', 'sum', 'New', 111, 'content', [], [], [], [])
566 self.task.services.template.GetTemplateByName = mock.Mock(
567 return_value=template)
568
569 # test phases not recognized
570 template.phases = [tracker_pb2.Phase(name='Chrome-Test')]
571 template.approval_values = [tracker_pb2.ApprovalValue()]
572 self.assertRaisesRegexp(
573 AssertionError, 'one or more phases not recognized',
574 self.task.FetchAndAssertProjectInfo, mr)
575
576 template.phases = [tracker_pb2.Phase(name='feature freeze'),
577 tracker_pb2.Phase(name='branch')]
578
579 # test template not set up correctly
580 template.approval_values = [
581 tracker_pb2.ApprovalValue(approval_id=1),
582 tracker_pb2.ApprovalValue(approval_id=2),
583 tracker_pb2.ApprovalValue(approval_id=3)]
584 self.assertRaisesRegexp(
585 AssertionError, 'os template not set up correctly',
586 self.task.FetchAndAssertProjectInfo, mr)
587
588 for av in template.approval_values:
589 av.status = tracker_pb2.ApprovalStatus.NEEDS_REVIEW
590
591 # test approvals not recognized
592 self.assertRaisesRegexp(
593 AssertionError, 'one or more approvals not recognized',
594 self.task.FetchAndAssertProjectInfo, mr)
595
596 self.config.field_defs = [
597 tracker_pb2.FieldDef(field_id=1, field_name='ChromeOS-Enterprise',
598 field_type=tracker_pb2.FieldTypes.APPROVAL_TYPE),
599 tracker_pb2.FieldDef(field_id=2, field_name='ChromeOS-UX',
600 field_type=tracker_pb2.FieldTypes.APPROVAL_TYPE),
601 tracker_pb2.FieldDef(field_id=3, field_name='ChromeOS-Privacy',
602 field_type=tracker_pb2.FieldTypes.APPROVAL_TYPE)
603 ]
604
605 # Skip remaining checks. No different from Browser process.
606 self.config.approval_defs = [
607 tracker_pb2.ApprovalDef(approval_id=1),
608 tracker_pb2.ApprovalDef(approval_id=2),
609 tracker_pb2.ApprovalDef(approval_id=3)]
610
611 self.config.field_defs.extend([
612 tracker_pb2.FieldDef(field_id=4, field_name='PM',
613 field_type=tracker_pb2.FieldTypes.USER_TYPE),
614 tracker_pb2.FieldDef(field_id=5, field_name='TL',
615 field_type=tracker_pb2.FieldTypes.USER_TYPE),
616 tracker_pb2.FieldDef(field_id=6, field_name='TE',
617 field_type=tracker_pb2.FieldTypes.USER_TYPE),
618 tracker_pb2.FieldDef(field_id=9, field_name='UX',
619 field_type=tracker_pb2.FieldTypes.USER_TYPE)
620 ])
621 self.config.field_defs.extend([
622 tracker_pb2.FieldDef(
623 field_id=7, field_name='M-Target', is_phase_field=True,
624 is_multivalued=True, field_type=tracker_pb2.FieldTypes.INT_TYPE),
625 tracker_pb2.FieldDef(
626 field_id=8, field_name='M-Approved', is_phase_field=True,
627 is_multivalued=True, field_type=tracker_pb2.FieldTypes.INT_TYPE)
628 ])
629
630 self.assertEqual(
631 self.task.FetchAndAssertProjectInfo(mr),
632 fltconversion.ProjectInfo(
633 self.config, fltconversion.QUERY_MAP['os'],
634 template.approval_values, template.phases, 4, 5, 6, 9, 7, 8,
635 fltconversion.OS_PHASE_MAP, fltconversion.OS_APPROVALS_TO_LABELS,
636 fltconversion.OS_M_LABELS_RE))
637
638 @mock.patch('time.time')
639 def testExecuteIssueChanges(self, mockTime):
640 mockTime.return_value = 123
641 self.task.services.issue._UpdateIssuesApprovals = mock.Mock()
642 self.task.services.issue.DeltaUpdateIssue = mock.Mock(
643 return_value=([], None))
644 self.task.services.issue.InsertComment = mock.Mock()
645 self.config.approval_defs = [
646 tracker_pb2.ApprovalDef(
647 # test empty survey
648 approval_id=1, survey='', approver_ids=[111, 222]),
649 tracker_pb2.ApprovalDef(approval_id=2), # test missing survey
650 tracker_pb2.ApprovalDef(survey='Missing approval_id should not error.'),
651 tracker_pb2.ApprovalDef(approval_id=3, survey='Q1\nQ2\n\nQ3'),
652 tracker_pb2.ApprovalDef(approval_id=4, survey='Q1\nQ2\n\nQ3 two'),
653 tracker_pb2.ApprovalDef()]
654
655 new_avs = [tracker_pb2.ApprovalValue(
656 approval_id=1, status=tracker_pb2.ApprovalStatus.APPROVED),
657 tracker_pb2.ApprovalValue(approval_id=4),
658 tracker_pb2.ApprovalValue(approval_id=2),
659 tracker_pb2.ApprovalValue(approval_id=3)]
660
661 phases = [tracker_pb2.Phase(phase_id=1, name='Phase1', rank=1)]
662 new_fvs = [tracker_bizobj.MakeFieldValue(
663 11, 70, None, None, None, None, False, phase_id=1),
664 tracker_bizobj.MakeFieldValue(
665 12, None, 'strfield', None, None, None, False)]
666 _amendments = self.task.ExecuteIssueChanges(
667 self.config, self.issue, new_avs, phases, new_fvs)
668
669 # approver_ids set in ExecuteIssueChanges()
670 new_avs[0].approver_ids = [111, 222]
671 self.issue.approval_values = new_avs
672 self.issue.phases = phases
673 delta = tracker_pb2.IssueDelta(
674 labels_add=['Type-FLT-Launch', 'FLT-Conversion'],
675 labels_remove=['Type-Launch'], field_vals_add=new_fvs)
676 cmt_1 = tracker_pb2.IssueComment(
677 issue_id=78901, project_id=789, user_id=self.mr.auth.user_id,
678 content='', is_description=True, approval_id=1, timestamp=123)
679 cmt_2 = tracker_pb2.IssueComment(
680 issue_id=78901, project_id=789, user_id=self.mr.auth.user_id,
681 content='', is_description=True, approval_id=2, timestamp=123)
682 cmt_3 = tracker_pb2.IssueComment(
683 issue_id=78901, project_id=789, user_id=self.mr.auth.user_id,
684 content='<b>Q1</b>\n<b>Q2</b>\n<b></b>\n<b>Q3</b>',
685 is_description=True, approval_id=3, timestamp=123)
686 cmt_4 = tracker_pb2.IssueComment(
687 issue_id=78901, project_id=789, user_id=self.mr.auth.user_id,
688 content='<b>Q1</b>\n<b>Q2</b>\n<b></b>\n<b>Q3 two</b>',
689 is_description=True, approval_id=4, timestamp=123)
690
691
692 comment_calls = [mock.call(self.mr.cnxn, cmt_1),
693 mock.call(self.mr.cnxn, cmt_4),
694 mock.call(self.mr.cnxn, cmt_2),
695 mock.call(self.mr.cnxn, cmt_3)]
696 self.task.services.issue.InsertComment.assert_has_calls(comment_calls)
697
698 self.task.services.issue._UpdateIssuesApprovals.assert_called_once_with(
699 self.mr.cnxn, self.issue)
700 self.task.services.issue.DeltaUpdateIssue.assert_called_once_with(
701 self.mr.cnxn, self.task.services, self.mr.auth.user_id, 789,
702 self.config, self.issue, delta,
703 comment=fltconversion.CONVERSION_COMMENT)
704
705 def testConvertPeopleLabels(self):
706 self.task.services.user.LookupUserID = mock.Mock(
707 side_effect=[1, 2, 3, 4, 5, 6])
708 labels = [
709 'pm-u1', 'pm-u2', 'tl-u2', 'test-3', 'test-4', 'ux-u5', 'ux-6']
710 fvs = self.task.ConvertPeopleLabels(self.mr, labels, 11, 12, 13, 14)
711 expected = [
712 tracker_bizobj.MakeFieldValue(11, None, None, 1, None, None, False),
713 tracker_bizobj.MakeFieldValue(12, None, None, 2, None, None, False),
714 tracker_bizobj.MakeFieldValue(13, None, None, 3, None, None, False),
715 tracker_bizobj.MakeFieldValue(13, None, None, 4, None, None, False),
716 tracker_bizobj.MakeFieldValue(14, None, None, 5, None, None, False),
717 tracker_bizobj.MakeFieldValue(14, None, None, 6, None, None, False),
718 ]
719 self.assertEqual(fvs, expected)
720
721 def testConvertPeopleLabels_NoUsers(self):
722 def side_effect(_cnxn, _email):
723 raise exceptions.NoSuchUserException()
724 labels = []
725 self.task.services.user.LookupUserID = mock.Mock(side_effect=side_effect)
726 self.assertFalse(
727 len(self.task.ConvertPeopleLabels(self.mr, labels, 11, 12, 13, 14)))
728
729 def testCreateUserFieldValue_Chromium(self):
730 self.task.services.user.LookupUserID = mock.Mock(return_value=1)
731 actual = self.task.CreateUserFieldValue(self.mr, 'ldap', 11)
732 expected = tracker_bizobj.MakeFieldValue(
733 11, None, None, 1, None, None, False)
734 self.assertEqual(actual, expected)
735 self.task.services.user.LookupUserID.assert_called_once_with(
736 self.mr.cnxn, 'ldap@chromium.org')
737
738 def testCreateUserFieldValue_Goog(self):
739 def side_effect(_cnxn, email):
740 if email.endswith('chromium.org'):
741 raise exceptions.NoSuchUserException()
742 else:
743 return 2
744 self.task.services.user.LookupUserID = mock.Mock(side_effect=side_effect)
745 actual = self.task.CreateUserFieldValue(self.mr, 'ldap', 11)
746 expected = tracker_bizobj.MakeFieldValue(
747 11, None, None, 2, None, None, False)
748 self.assertEqual(actual, expected)
749 self.task.services.user.LookupUserID.assert_any_call(
750 self.mr.cnxn, 'ldap@chromium.org')
751 self.task.services.user.LookupUserID.assert_any_call(
752 self.mr.cnxn, 'ldap@google.com')
753
754 def testCreateUserFieldValue_NoUserFound(self):
755 def side_effect(_cnxn, _email):
756 raise exceptions.NoSuchUserException()
757 self.task.services.user.LookupUserID = mock.Mock(side_effect=side_effect)
758 self.assertIsNone(self.task.CreateUserFieldValue(self.mr, 'ldap', 11))
759
760
761class ConvertMLabels(unittest.TestCase):
762
763 def setUp(self):
764 self.target_id = 24
765 self.approved_id = 27
766 self.beta_phase = tracker_pb2.Phase(phase_id=1, name='bEtA')
767 self.stable_phase = tracker_pb2.Phase(phase_id=2, name='StAbLe')
768 self.stable_full_phase = tracker_pb2.Phase(phase_id=3, name='stable-FULL')
769 self.stable_exp_phase = tracker_pb2.Phase(phase_id=4, name='STABLE-exp')
770 self.feature_freeze_phase = tracker_pb2.Phase(
771 phase_id=5, name='FEATURE Freeze')
772 self.branch_phase = tracker_pb2.Phase(phase_id=6, name='bRANCH')
773
774 def testConvertMLabels_NormalFinch(self):
775
776 phases = [self.stable_exp_phase, self.beta_phase, self.stable_full_phase]
777 labels = [
778 'launch-m-approved-81-beta', # beta:M-Approved=81
779 'launch-m-target-80-stable-car', # ignore
780 'a-Launch-M-Target-80-Stable-car', # ignore
781 'launch-m-target-70-Stable', # stable-full:M-Target=70
782 'LAUNCH-M-TARGET-71-STABLE', # stable-full:M-Target=71
783 'launch-m-target-70-stable-exp', # stable-exp:M-Target=70
784 'launch-m-target-69-stable-exp', # stable-exp:M-Target=69
785 'launch-M-APPROVED-70-Stable-Exp', # stable-exp:M-Approved-70
786 'launch-m-approved-73-stable', # stable-full:M-Approved-73
787 'launch-m-error-73-stable', # ignore
788 'launch-m-approved-8-stable', #ignore
789 'irrelevant label-weird', # ignore
790 ]
791 actual_fvs = fltconversion.ConvertMLabels(
792 labels, phases, self.target_id, self.approved_id,
793 fltconversion.BROWSER_M_LABELS_RE, fltconversion.BROWSER_PHASE_MAP)
794
795 expected_fvs = [
796 tracker_pb2.FieldValue(
797 field_id=self.approved_id, int_value=81,
798 phase_id=self.beta_phase.phase_id, derived=False,),
799 tracker_pb2.FieldValue(
800 field_id=self.target_id, int_value=70,
801 phase_id=self.stable_full_phase.phase_id, derived=False),
802 tracker_pb2.FieldValue(
803 field_id=self.target_id, int_value=71,
804 phase_id=self.stable_full_phase.phase_id, derived=False),
805 tracker_pb2.FieldValue(
806 field_id=self.target_id, int_value=70,
807 phase_id=self.stable_exp_phase.phase_id, derived=False),
808 tracker_pb2.FieldValue(
809 field_id=self.target_id, int_value=69,
810 phase_id=self.stable_exp_phase.phase_id, derived=False),
811 tracker_pb2.FieldValue(
812 field_id=self.approved_id, int_value=70,
813 phase_id=self.stable_exp_phase.phase_id, derived=False),
814 tracker_pb2.FieldValue(
815 field_id=self.approved_id, int_value=73,
816 phase_id=self.stable_full_phase.phase_id, derived=False)
817 ]
818
819 self.assertEqual(actual_fvs, expected_fvs)
820
821 def testConvertMLabels_OS(self):
822 phases = [self.feature_freeze_phase, self.branch_phase, self.stable_phase]
823 labels = [
824 'launch-m-approved-81-beta', # ignore
825 'launch-m-target-80-stable-car', # ignore
826 'a-Launch-M-Target-80-Stable-car', # ignore
827 'launch-m-target-70-Stable', # stable:M-Target=70
828 'LAUNCH-M-TARGET-71-STABLE', # stable:M-Target=71
829 'launch-m-target-70-stable-exp', # ignore
830 'launch-M-APPROVED-70-Stable-Exp', # ignore
831 'launch-m-approved-73-stable', # stable:M-Approved-73
832 'launch-m-error-73-stable', # ignore
833 'launch-m-approved-8-stable', #ignore
834 'irrelevant label-weird', # ignore
835 ]
836 actual_fvs = fltconversion.ConvertMLabels(
837 labels, phases, self.target_id, self.approved_id,
838 fltconversion.OS_M_LABELS_RE, fltconversion.OS_PHASE_MAP)
839
840 expected_fvs = [
841 tracker_pb2.FieldValue(
842 field_id=self.target_id, int_value=70,
843 phase_id=self.stable_phase.phase_id, derived=False,),
844 tracker_pb2.FieldValue(
845 field_id=self.target_id, int_value=71,
846 phase_id=self.stable_phase.phase_id, derived=False),
847 tracker_pb2.FieldValue(
848 field_id=self.approved_id, int_value=73,
849 phase_id=self.stable_phase.phase_id, derived=False)
850 ]
851
852 self.assertEqual(actual_fvs, expected_fvs)
853
854
855class ConvertLaunchLabels(unittest.TestCase):
856
857 def setUp(self):
858 self.project_fds = [
859 tracker_pb2.FieldDef(
860 field_id=1, project_id=789, field_name='String',
861 field_type=tracker_pb2.FieldTypes.STR_TYPE),
862 tracker_pb2.FieldDef(
863 field_id=2, project_id=789, field_name='Chrome-UX',
864 field_type=tracker_pb2.FieldTypes.APPROVAL_TYPE),
865 tracker_pb2.FieldDef(
866 field_id=3, project_id=789, field_name='Chrome-Privacy',
867 field_type=tracker_pb2.FieldTypes.APPROVAL_TYPE)
868 ]
869 approvalUX = tracker_pb2.ApprovalValue(
870 approval_id=2, status=tracker_pb2.ApprovalStatus.NEEDS_REVIEW)
871 approvalPrivacy = tracker_pb2.ApprovalValue(approval_id=3)
872 self.approvals = [approvalUX, approvalPrivacy]
873
874 def testConvertLaunchLabels_Normal(self):
875 labels = [
876 'Launch-UX-NotReviewed', 'Launch-Privacy-Yes', 'Launch-NotRelevant']
877 actual = fltconversion.ConvertLaunchLabels(
878 labels, self.approvals, self.project_fds,
879 fltconversion.BROWSER_APPROVALS_TO_LABELS)
880 expected = [
881 tracker_pb2.ApprovalValue(
882 approval_id=2, status=tracker_pb2.ApprovalStatus.NEEDS_REVIEW),
883 tracker_pb2.ApprovalValue(
884 approval_id=3, status=tracker_pb2.ApprovalStatus.APPROVED)
885 ]
886 self.assertEqual(actual, expected)
887
888 def testConvertLaunchLabels_ExtraAndMissingLabels(self):
889 labels = [
890 'Blah-Launch-Privacy-Yes', # Missing, this is not a valid Label
891 'Launch-Security-Yes', # Extra, no matching approval in given approvals
892 'Launch-UI-Yes'] # Missing Launch-Privacy
893 actual = fltconversion.ConvertLaunchLabels(
894 labels, self.approvals, self.project_fds,
895 fltconversion.BROWSER_APPROVALS_TO_LABELS)
896 expected = [
897 tracker_pb2.ApprovalValue(
898 approval_id=2, status=tracker_pb2.ApprovalStatus.APPROVED),
899 tracker_pb2.ApprovalValue(
900 approval_id=3, status=tracker_pb2.ApprovalStatus.NOT_SET)
901 ]
902 self.assertEqual(actual, expected)
903
904class ExtractLabelLDAPs(unittest.TestCase):
905
906 def testExtractLabelLDAPs_Normal(self):
907 labels = [
908 'tl-USER1',
909 'pm-',
910 'tL-User2',
911 'test-user4',
912 'PM-USER3',
913 'pm',
914 'test-user5',
915 'test-',
916 'ux-user9']
917 (actual_pm, actual_tl, actual_tests,
918 actual_ux) = fltconversion.ExtractLabelLDAPs(labels)
919 self.assertEqual(actual_pm, 'user3')
920 self.assertEqual(actual_tl, 'user2')
921 self.assertEqual(actual_tests, ['user4', 'user5'])
922 self.assertEqual(actual_ux, ['user9'])
923
924 def testExtractLabelLDAPs_NoLabels(self):
925 (actual_pm, actual_tl, actual_tests,
926 actual_ux) = fltconversion.ExtractLabelLDAPs([])
927 self.assertIsNone(actual_pm)
928 self.assertIsNone(actual_tl)
929 self.assertFalse(len(actual_tests))
930 self.assertFalse(len(actual_ux))