blob: cda63ae5e47d329f61e05504cf5c1ed15fdc7af6 [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
Don Garrettbe3608b2018-06-05 17:10:09 -0700171 def testGenLuciSchedulerConfig(self):
172 """Test a full LUCI Scheduler config file."""
173 site_config = config_lib.SiteConfig()
174
175 site_config.Add(
176 'not_scheduled',
177 luci_builder='ProdBuilder',
178 display_label='MockLabel',
179 )
180
181 site_config.Add(
182 'build_prod',
183 luci_builder='ProdBuilder',
184 display_label='MockLabel',
185 schedule='run once in a while',
186 )
187
188 site_config.Add(
189 'build_tester',
190 luci_builder='TestBuilder',
191 display_label='TestLabel',
192 schedule='run daily',
193 )
194
195 site_config.Add(
196 'build_triggered_a',
197 luci_builder='ProdBuilder',
198 display_label='MockLabel',
199 schedule='triggered',
200 triggered_gitiles=[[
201 'gitiles_url_a',
202 ['ref_a', 'ref_b'],
203 ], [
204 'gitiles_url_b',
205 ['ref_c'],
206 ]],
207 )
208
209 site_config.Add(
210 'build_triggered_b',
211 luci_builder='ProdBuilder',
212 display_label='MockLabel',
213 schedule='triggered',
214 triggered_gitiles=[[
215 'gitiles_url_b',
216 ['ref_c'],
217 ]],
218 )
219
Don Garrett8dace272018-07-19 14:07:42 -0700220 default_config = config_lib.GetConfig().GetDefault()
221
222 branch_configs = [
223 default_config.derive(
224 name='branch_tester',
225 luci_builder='TestBuilder',
226 display_label='TestLabel',
227 schedule='run daily',
228 schedule_branch='test-branch',
229 ),
230 ]
231
232
Don Garrettbe3608b2018-06-05 17:10:09 -0700233 expected = '''# Defines buckets on luci-scheduler.appspot.com.
234#
235# For schema of this file and documentation see ProjectConfig message in
236# https://github.com/luci/luci-go/blob/master/scheduler/appengine/messages/config.proto
237
238# Generated with chromite/scripts/gen_luci_scheduler
239
240acl_sets {
241 name: "default"
242 acls {
243 role: READER
244 granted_to: "group:googlers"
245 }
246 acls {
247 role: OWNER
248 granted_to: "group:project-chromeos-admins"
249 }
250}
251
252trigger {
253 id: "trigger_0"
254 acl_sets: "default"
255 schedule: "with 5m interval"
256 gitiles: {
257 repo: "gitiles_url_a"
258 refs: "ref_a"
259 refs: "ref_b"
260 }
261 triggers: "build_triggered_a"
262}
263
264trigger {
265 id: "trigger_1"
266 acl_sets: "default"
267 schedule: "with 5m interval"
268 gitiles: {
269 repo: "gitiles_url_b"
270 refs: "ref_c"
271 }
272 triggers: "build_triggered_a"
273 triggers: "build_triggered_b"
274}
275
276job {
277 id: "build_prod"
278 acl_sets: "default"
279 schedule: "run once in a while"
280 buildbucket: {
281 server: "cr-buildbucket.appspot.com"
282 bucket: "luci.chromeos.general"
283 builder: "ProdBuilder"
284 tags: "cbb_branch:master"
Don Garrettbe3608b2018-06-05 17:10:09 -0700285 tags: "cbb_config:build_prod"
Don Garrett73148e52018-08-17 16:54:46 -0700286 tags: "cbb_display_label:MockLabel"
Don Garrettbe3608b2018-06-05 17:10:09 -0700287 properties: "cbb_branch:master"
Don Garrettbe3608b2018-06-05 17:10:09 -0700288 properties: "cbb_config:build_prod"
Don Garrett73148e52018-08-17 16:54:46 -0700289 properties: "cbb_display_label:MockLabel"
Don Garrettbe3608b2018-06-05 17:10:09 -0700290 properties: "cbb_extra_args:[\\"--buildbot\\"]"
291 }
292}
293
294job {
295 id: "build_tester"
296 acl_sets: "default"
297 schedule: "run daily"
298 buildbucket: {
299 server: "cr-buildbucket.appspot.com"
300 bucket: "luci.chromeos.general"
301 builder: "TestBuilder"
302 tags: "cbb_branch:master"
Don Garrettbe3608b2018-06-05 17:10:09 -0700303 tags: "cbb_config:build_tester"
Don Garrett73148e52018-08-17 16:54:46 -0700304 tags: "cbb_display_label:TestLabel"
Don Garrettbe3608b2018-06-05 17:10:09 -0700305 properties: "cbb_branch:master"
Don Garrettbe3608b2018-06-05 17:10:09 -0700306 properties: "cbb_config:build_tester"
Don Garrett73148e52018-08-17 16:54:46 -0700307 properties: "cbb_display_label:TestLabel"
Don Garrettbe3608b2018-06-05 17:10:09 -0700308 properties: "cbb_extra_args:[\\"--buildbot\\"]"
309 }
310}
311
312job {
313 id: "build_triggered_a"
314 acl_sets: "default"
315 schedule: "triggered"
316 buildbucket: {
317 server: "cr-buildbucket.appspot.com"
318 bucket: "luci.chromeos.general"
319 builder: "ProdBuilder"
320 tags: "cbb_branch:master"
Don Garrettbe3608b2018-06-05 17:10:09 -0700321 tags: "cbb_config:build_triggered_a"
Don Garrett73148e52018-08-17 16:54:46 -0700322 tags: "cbb_display_label:MockLabel"
Don Garrettbe3608b2018-06-05 17:10:09 -0700323 properties: "cbb_branch:master"
Don Garrettbe3608b2018-06-05 17:10:09 -0700324 properties: "cbb_config:build_triggered_a"
Don Garrett73148e52018-08-17 16:54:46 -0700325 properties: "cbb_display_label:MockLabel"
Don Garrettbe3608b2018-06-05 17:10:09 -0700326 properties: "cbb_extra_args:[\\"--buildbot\\"]"
327 }
328}
329
330job {
331 id: "build_triggered_b"
332 acl_sets: "default"
333 schedule: "triggered"
334 buildbucket: {
335 server: "cr-buildbucket.appspot.com"
336 bucket: "luci.chromeos.general"
337 builder: "ProdBuilder"
338 tags: "cbb_branch:master"
Don Garrettbe3608b2018-06-05 17:10:09 -0700339 tags: "cbb_config:build_triggered_b"
Don Garrett73148e52018-08-17 16:54:46 -0700340 tags: "cbb_display_label:MockLabel"
Don Garrettbe3608b2018-06-05 17:10:09 -0700341 properties: "cbb_branch:master"
Don Garrettbe3608b2018-06-05 17:10:09 -0700342 properties: "cbb_config:build_triggered_b"
Don Garrett73148e52018-08-17 16:54:46 -0700343 properties: "cbb_display_label:MockLabel"
Don Garrettbe3608b2018-06-05 17:10:09 -0700344 properties: "cbb_extra_args:[\\"--buildbot\\"]"
345 }
346}
Don Garrett8dace272018-07-19 14:07:42 -0700347
348job {
349 id: "test-branch-branch_tester"
350 acl_sets: "default"
351 schedule: "run daily"
352 buildbucket: {
353 server: "cr-buildbucket.appspot.com"
354 bucket: "luci.chromeos.general"
355 builder: "TestBuilder"
356 tags: "cbb_branch:test-branch"
Don Garrett8dace272018-07-19 14:07:42 -0700357 tags: "cbb_config:branch_tester"
Don Garrett73148e52018-08-17 16:54:46 -0700358 tags: "cbb_display_label:TestLabel"
Don Garrett8dace272018-07-19 14:07:42 -0700359 properties: "cbb_branch:test-branch"
Don Garrett8dace272018-07-19 14:07:42 -0700360 properties: "cbb_config:branch_tester"
Don Garrett73148e52018-08-17 16:54:46 -0700361 properties: "cbb_display_label:TestLabel"
Don Garrett8dace272018-07-19 14:07:42 -0700362 properties: "cbb_extra_args:[\\"--buildbot\\"]"
363 }
364}
Don Garrettbe3608b2018-06-05 17:10:09 -0700365'''
Don Garrett8dace272018-07-19 14:07:42 -0700366 result = gen_luci_scheduler.genLuciSchedulerConfig(
367 site_config, branch_configs)
Don Garrettbe3608b2018-06-05 17:10:09 -0700368
369 self.assertEqual(result, expected)