blob: 1185d9061d5f05cf4dcb685944354c6311b05364 [file] [log] [blame]
Don Garrettbe3608b2018-06-05 17:10:09 -07001# -*- coding: utf-8 -*-
2# Copyright 2018 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Test gen_luci_scheduler."""
7from __future__ import print_function
8
Don Garrett8dace272018-07-19 14:07:42 -07009from chromite.config import chromeos_config
Don Garrettbe3608b2018-06-05 17:10:09 -070010from chromite.lib import cros_test_lib
11from chromite.lib import config_lib
12from chromite.lib import config_lib_unittest
13from chromite.scripts import gen_luci_scheduler
14
15# It's reasonable for unittests to look at internals.
16# pylint: disable=protected-access
17
18
19class GenLuciSchedulerTest(cros_test_lib.MockTestCase):
20 """Tests for cbuildbot_launch script."""
21 def testSanityAgainstProd(self):
22 """Test we can generate a luci scheduler config with live data."""
23 # If it runs without crashing, we pass.
Don Garrett8dace272018-07-19 14:07:42 -070024 gen_luci_scheduler.genLuciSchedulerConfig(
25 config_lib.GetConfig(), chromeos_config.BranchScheduleConfig())
Don Garrettbe3608b2018-06-05 17:10:09 -070026
27 def testGenSchedulerJob(self):
28 """Test the job creation helper."""
29 build_config = config_lib_unittest.MockBuildConfig().apply(
30 schedule='funky schedule'
31 )
32
33 expected = '''
34job {
35 id: "amd64-generic-paladin"
36 acl_sets: "default"
37 schedule: "funky schedule"
38 buildbucket: {
39 server: "cr-buildbucket.appspot.com"
40 bucket: "luci.chromeos.general"
41 builder: "Prod"
42 tags: "cbb_branch:master"
Don Garrettbe3608b2018-06-05 17:10:09 -070043 tags: "cbb_config:amd64-generic-paladin"
Don Garrett73148e52018-08-17 16:54:46 -070044 tags: "cbb_display_label:MockLabel"
Don Garrettbe3608b2018-06-05 17:10:09 -070045 properties: "cbb_branch:master"
Don Garrettbe3608b2018-06-05 17:10:09 -070046 properties: "cbb_config:amd64-generic-paladin"
Don Garrett73148e52018-08-17 16:54:46 -070047 properties: "cbb_display_label:MockLabel"
Don Garrettbe3608b2018-06-05 17:10:09 -070048 properties: "cbb_extra_args:[\\"--buildbot\\"]"
49 }
50}
51'''
52
53 result = gen_luci_scheduler.genSchedulerJob(build_config)
54 self.assertEqual(result, expected)
55
56 def testGenSchedulerTriggerSimple(self):
57 """Test the trigger creation helper."""
58 trigger_name = 'simple'
59 repo = 'url://repo'
60 refs = ['refs/path']
61 builds = ['test_build']
62
63 expected = '''
64trigger {
65 id: "simple"
66 acl_sets: "default"
67 schedule: "with 5m interval"
68 gitiles: {
69 repo: "url://repo"
70 refs: "refs/path"
71 }
72 triggers: "test_build"
73}
74'''
75
76 result = gen_luci_scheduler.genSchedulerTrigger(
77 trigger_name, repo, refs, builds)
78
79 self.assertEqual(result, expected)
80
81 def testGenSchedulerTriggerComplex(self):
82 """Test the trigger creation helper."""
83 trigger_name = 'complex'
84 repo = 'url://repo'
85 refs = ['refs/path', 'refs/other_path']
86 builds = ['test_build_a', 'test_build_b']
87
88 expected = '''
89trigger {
90 id: "complex"
91 acl_sets: "default"
92 schedule: "with 5m interval"
93 gitiles: {
94 repo: "url://repo"
95 refs: "refs/path"
96 refs: "refs/other_path"
97 }
98 triggers: "test_build_a"
99 triggers: "test_build_b"
100}
101'''
102
103 result = gen_luci_scheduler.genSchedulerTrigger(
104 trigger_name, repo, refs, builds)
105
106 self.assertEqual(result, expected)
107
Don Garrett8dace272018-07-19 14:07:42 -0700108
109 def testGenSchedulerBranched(self):
110 """Test the job creation helper."""
111 build_config = config_lib_unittest.MockBuildConfig().apply(
112 schedule_branch='mock_branch',
113 schedule='funky schedule',
114 )
115
116 expected = '''
117job {
118 id: "mock_branch-amd64-generic-paladin"
119 acl_sets: "default"
120 schedule: "funky schedule"
121 buildbucket: {
122 server: "cr-buildbucket.appspot.com"
123 bucket: "luci.chromeos.general"
124 builder: "Prod"
125 tags: "cbb_branch:mock_branch"
Don Garrett8dace272018-07-19 14:07:42 -0700126 tags: "cbb_config:amd64-generic-paladin"
Don Garrett73148e52018-08-17 16:54:46 -0700127 tags: "cbb_display_label:MockLabel"
Don Garrett8dace272018-07-19 14:07:42 -0700128 properties: "cbb_branch:mock_branch"
Don Garrett8dace272018-07-19 14:07:42 -0700129 properties: "cbb_config:amd64-generic-paladin"
Don Garrett73148e52018-08-17 16:54:46 -0700130 properties: "cbb_display_label:MockLabel"
131 properties: "cbb_extra_args:[\\"--buildbot\\"]"
132 }
133}
134'''
135
136 result = gen_luci_scheduler.genSchedulerJob(build_config)
137 self.assertEqual(result, expected)
138
139 def testGenSchedulerWorkspaceBranch(self):
140 """Test the job creation helper."""
141 build_config = config_lib_unittest.MockBuildConfig().apply(
142 workspace_branch='work_branch',
143 schedule='funky schedule',
144 )
145
146 expected = '''
147job {
148 id: "amd64-generic-paladin"
149 acl_sets: "default"
150 schedule: "funky schedule"
151 buildbucket: {
152 server: "cr-buildbucket.appspot.com"
153 bucket: "luci.chromeos.general"
154 builder: "Prod"
155 tags: "cbb_branch:master"
156 tags: "cbb_config:amd64-generic-paladin"
157 tags: "cbb_display_label:MockLabel"
158 tags: "cbb_workspace_branch:work_branch"
159 properties: "cbb_branch:master"
160 properties: "cbb_config:amd64-generic-paladin"
161 properties: "cbb_display_label:MockLabel"
162 properties: "cbb_workspace_branch:work_branch"
Don Garrett8dace272018-07-19 14:07:42 -0700163 properties: "cbb_extra_args:[\\"--buildbot\\"]"
164 }
165}
166'''
167
168 result = gen_luci_scheduler.genSchedulerJob(build_config)
169 self.assertEqual(result, expected)
170
Yoshisato Yanagisawa16285b42018-09-19 14:00:58 +0900171 def testGenSchedulerGomaClientType(self):
172 """Test the job creation helper."""
173 build_config = config_lib_unittest.MockBuildConfig().apply(
174 goma_client_type='client_type',
175 schedule='funky schedule',
176 )
177
178 expected = '''
179job {
180 id: "amd64-generic-paladin"
181 acl_sets: "default"
182 schedule: "funky schedule"
183 buildbucket: {
184 server: "cr-buildbucket.appspot.com"
185 bucket: "luci.chromeos.general"
186 builder: "Prod"
187 tags: "cbb_branch:master"
188 tags: "cbb_config:amd64-generic-paladin"
189 tags: "cbb_display_label:MockLabel"
190 tags: "cbb_goma_client_type:client_type"
191 properties: "cbb_branch:master"
192 properties: "cbb_config:amd64-generic-paladin"
193 properties: "cbb_display_label:MockLabel"
194 properties: "cbb_goma_client_type:client_type"
195 properties: "cbb_extra_args:[\\"--buildbot\\"]"
196 }
197}
198'''
199
200 result = gen_luci_scheduler.genSchedulerJob(build_config)
201 self.assertEqual(result, expected)
202
Don Garrettbe3608b2018-06-05 17:10:09 -0700203 def testGenLuciSchedulerConfig(self):
204 """Test a full LUCI Scheduler config file."""
205 site_config = config_lib.SiteConfig()
206
207 site_config.Add(
208 'not_scheduled',
209 luci_builder='ProdBuilder',
210 display_label='MockLabel',
211 )
212
213 site_config.Add(
214 'build_prod',
215 luci_builder='ProdBuilder',
216 display_label='MockLabel',
217 schedule='run once in a while',
218 )
219
220 site_config.Add(
221 'build_tester',
222 luci_builder='TestBuilder',
223 display_label='TestLabel',
224 schedule='run daily',
225 )
226
227 site_config.Add(
228 'build_triggered_a',
229 luci_builder='ProdBuilder',
230 display_label='MockLabel',
231 schedule='triggered',
232 triggered_gitiles=[[
233 'gitiles_url_a',
234 ['ref_a', 'ref_b'],
235 ], [
236 'gitiles_url_b',
237 ['ref_c'],
238 ]],
239 )
240
241 site_config.Add(
242 'build_triggered_b',
243 luci_builder='ProdBuilder',
244 display_label='MockLabel',
245 schedule='triggered',
246 triggered_gitiles=[[
247 'gitiles_url_b',
248 ['ref_c'],
249 ]],
250 )
251
Don Garrett8dace272018-07-19 14:07:42 -0700252 default_config = config_lib.GetConfig().GetDefault()
253
254 branch_configs = [
255 default_config.derive(
256 name='branch_tester',
257 luci_builder='TestBuilder',
258 display_label='TestLabel',
259 schedule='run daily',
260 schedule_branch='test-branch',
261 ),
262 ]
263
264
Don Garrettbe3608b2018-06-05 17:10:09 -0700265 expected = '''# Defines buckets on luci-scheduler.appspot.com.
266#
267# For schema of this file and documentation see ProjectConfig message in
268# https://github.com/luci/luci-go/blob/master/scheduler/appengine/messages/config.proto
269
270# Generated with chromite/scripts/gen_luci_scheduler
271
Don Garrettff45f4b2018-10-04 09:08:01 -0700272# Autodeployed with:
273# http://cros-goldeneye/chromeos/legoland/builderHistory?buildConfig=luci-scheduler-updater
274
Don Garrettbe3608b2018-06-05 17:10:09 -0700275acl_sets {
276 name: "default"
277 acls {
278 role: READER
279 granted_to: "group:googlers"
280 }
281 acls {
282 role: OWNER
283 granted_to: "group:project-chromeos-admins"
284 }
285}
286
287trigger {
288 id: "trigger_0"
289 acl_sets: "default"
290 schedule: "with 5m interval"
291 gitiles: {
292 repo: "gitiles_url_a"
293 refs: "ref_a"
294 refs: "ref_b"
295 }
296 triggers: "build_triggered_a"
297}
298
299trigger {
300 id: "trigger_1"
301 acl_sets: "default"
302 schedule: "with 5m interval"
303 gitiles: {
304 repo: "gitiles_url_b"
305 refs: "ref_c"
306 }
307 triggers: "build_triggered_a"
308 triggers: "build_triggered_b"
309}
310
311job {
312 id: "build_prod"
313 acl_sets: "default"
314 schedule: "run once in a while"
315 buildbucket: {
316 server: "cr-buildbucket.appspot.com"
317 bucket: "luci.chromeos.general"
318 builder: "ProdBuilder"
319 tags: "cbb_branch:master"
Don Garrettbe3608b2018-06-05 17:10:09 -0700320 tags: "cbb_config:build_prod"
Don Garrett73148e52018-08-17 16:54:46 -0700321 tags: "cbb_display_label:MockLabel"
Don Garrettbe3608b2018-06-05 17:10:09 -0700322 properties: "cbb_branch:master"
Don Garrettbe3608b2018-06-05 17:10:09 -0700323 properties: "cbb_config:build_prod"
Don Garrett73148e52018-08-17 16:54:46 -0700324 properties: "cbb_display_label:MockLabel"
Don Garrettbe3608b2018-06-05 17:10:09 -0700325 properties: "cbb_extra_args:[\\"--buildbot\\"]"
326 }
327}
328
329job {
330 id: "build_tester"
331 acl_sets: "default"
332 schedule: "run daily"
333 buildbucket: {
334 server: "cr-buildbucket.appspot.com"
335 bucket: "luci.chromeos.general"
336 builder: "TestBuilder"
337 tags: "cbb_branch:master"
Don Garrettbe3608b2018-06-05 17:10:09 -0700338 tags: "cbb_config:build_tester"
Don Garrett73148e52018-08-17 16:54:46 -0700339 tags: "cbb_display_label:TestLabel"
Don Garrettbe3608b2018-06-05 17:10:09 -0700340 properties: "cbb_branch:master"
Don Garrettbe3608b2018-06-05 17:10:09 -0700341 properties: "cbb_config:build_tester"
Don Garrett73148e52018-08-17 16:54:46 -0700342 properties: "cbb_display_label:TestLabel"
Don Garrettbe3608b2018-06-05 17:10:09 -0700343 properties: "cbb_extra_args:[\\"--buildbot\\"]"
344 }
345}
346
347job {
348 id: "build_triggered_a"
349 acl_sets: "default"
350 schedule: "triggered"
351 buildbucket: {
352 server: "cr-buildbucket.appspot.com"
353 bucket: "luci.chromeos.general"
354 builder: "ProdBuilder"
355 tags: "cbb_branch:master"
Don Garrettbe3608b2018-06-05 17:10:09 -0700356 tags: "cbb_config:build_triggered_a"
Don Garrett73148e52018-08-17 16:54:46 -0700357 tags: "cbb_display_label:MockLabel"
Don Garrettbe3608b2018-06-05 17:10:09 -0700358 properties: "cbb_branch:master"
Don Garrettbe3608b2018-06-05 17:10:09 -0700359 properties: "cbb_config:build_triggered_a"
Don Garrett73148e52018-08-17 16:54:46 -0700360 properties: "cbb_display_label:MockLabel"
Don Garrettbe3608b2018-06-05 17:10:09 -0700361 properties: "cbb_extra_args:[\\"--buildbot\\"]"
362 }
363}
364
365job {
366 id: "build_triggered_b"
367 acl_sets: "default"
368 schedule: "triggered"
369 buildbucket: {
370 server: "cr-buildbucket.appspot.com"
371 bucket: "luci.chromeos.general"
372 builder: "ProdBuilder"
373 tags: "cbb_branch:master"
Don Garrettbe3608b2018-06-05 17:10:09 -0700374 tags: "cbb_config:build_triggered_b"
Don Garrett73148e52018-08-17 16:54:46 -0700375 tags: "cbb_display_label:MockLabel"
Don Garrettbe3608b2018-06-05 17:10:09 -0700376 properties: "cbb_branch:master"
Don Garrettbe3608b2018-06-05 17:10:09 -0700377 properties: "cbb_config:build_triggered_b"
Don Garrett73148e52018-08-17 16:54:46 -0700378 properties: "cbb_display_label:MockLabel"
Don Garrettbe3608b2018-06-05 17:10:09 -0700379 properties: "cbb_extra_args:[\\"--buildbot\\"]"
380 }
381}
Don Garrett8dace272018-07-19 14:07:42 -0700382
383job {
384 id: "test-branch-branch_tester"
385 acl_sets: "default"
386 schedule: "run daily"
387 buildbucket: {
388 server: "cr-buildbucket.appspot.com"
389 bucket: "luci.chromeos.general"
390 builder: "TestBuilder"
391 tags: "cbb_branch:test-branch"
Don Garrett8dace272018-07-19 14:07:42 -0700392 tags: "cbb_config:branch_tester"
Don Garrett73148e52018-08-17 16:54:46 -0700393 tags: "cbb_display_label:TestLabel"
Don Garrett8dace272018-07-19 14:07:42 -0700394 properties: "cbb_branch:test-branch"
Don Garrett8dace272018-07-19 14:07:42 -0700395 properties: "cbb_config:branch_tester"
Don Garrett73148e52018-08-17 16:54:46 -0700396 properties: "cbb_display_label:TestLabel"
Don Garrett8dace272018-07-19 14:07:42 -0700397 properties: "cbb_extra_args:[\\"--buildbot\\"]"
398 }
399}
Don Garrettbe3608b2018-06-05 17:10:09 -0700400'''
Don Garrett8dace272018-07-19 14:07:42 -0700401 result = gen_luci_scheduler.genLuciSchedulerConfig(
402 site_config, branch_configs)
Don Garrettbe3608b2018-06-05 17:10:09 -0700403
404 self.assertEqual(result, expected)