Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 1 | # -*- 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.""" |
| 7 | from __future__ import print_function |
| 8 | |
Don Garrett | 8dace27 | 2018-07-19 14:07:42 -0700 | [diff] [blame^] | 9 | from chromite.config import chromeos_config |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 10 | from chromite.lib import cros_test_lib |
| 11 | from chromite.lib import config_lib |
| 12 | from chromite.lib import config_lib_unittest |
| 13 | from chromite.scripts import gen_luci_scheduler |
| 14 | |
| 15 | # It's reasonable for unittests to look at internals. |
| 16 | # pylint: disable=protected-access |
| 17 | |
| 18 | |
| 19 | class 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 Garrett | 8dace27 | 2018-07-19 14:07:42 -0700 | [diff] [blame^] | 24 | gen_luci_scheduler.genLuciSchedulerConfig( |
| 25 | config_lib.GetConfig(), chromeos_config.BranchScheduleConfig()) |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 26 | |
| 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 = ''' |
| 34 | job { |
| 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" |
| 43 | tags: "cbb_display_label:MockLabel" |
| 44 | tags: "cbb_config:amd64-generic-paladin" |
| 45 | properties: "cbb_branch:master" |
| 46 | properties: "cbb_display_label:MockLabel" |
| 47 | properties: "cbb_config:amd64-generic-paladin" |
| 48 | 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 = ''' |
| 64 | trigger { |
| 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 = ''' |
| 89 | trigger { |
| 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 Garrett | 8dace27 | 2018-07-19 14:07:42 -0700 | [diff] [blame^] | 108 | |
| 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 = ''' |
| 117 | job { |
| 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" |
| 126 | tags: "cbb_display_label:MockLabel" |
| 127 | tags: "cbb_config:amd64-generic-paladin" |
| 128 | properties: "cbb_branch:mock_branch" |
| 129 | properties: "cbb_display_label:MockLabel" |
| 130 | properties: "cbb_config:amd64-generic-paladin" |
| 131 | properties: "cbb_extra_args:[\\"--buildbot\\"]" |
| 132 | } |
| 133 | } |
| 134 | ''' |
| 135 | |
| 136 | result = gen_luci_scheduler.genSchedulerJob(build_config) |
| 137 | self.assertEqual(result, expected) |
| 138 | |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 139 | def testGenLuciSchedulerConfig(self): |
| 140 | """Test a full LUCI Scheduler config file.""" |
| 141 | site_config = config_lib.SiteConfig() |
| 142 | |
| 143 | site_config.Add( |
| 144 | 'not_scheduled', |
| 145 | luci_builder='ProdBuilder', |
| 146 | display_label='MockLabel', |
| 147 | ) |
| 148 | |
| 149 | site_config.Add( |
| 150 | 'build_prod', |
| 151 | luci_builder='ProdBuilder', |
| 152 | display_label='MockLabel', |
| 153 | schedule='run once in a while', |
| 154 | ) |
| 155 | |
| 156 | site_config.Add( |
| 157 | 'build_tester', |
| 158 | luci_builder='TestBuilder', |
| 159 | display_label='TestLabel', |
| 160 | schedule='run daily', |
| 161 | ) |
| 162 | |
| 163 | site_config.Add( |
| 164 | 'build_triggered_a', |
| 165 | luci_builder='ProdBuilder', |
| 166 | display_label='MockLabel', |
| 167 | schedule='triggered', |
| 168 | triggered_gitiles=[[ |
| 169 | 'gitiles_url_a', |
| 170 | ['ref_a', 'ref_b'], |
| 171 | ], [ |
| 172 | 'gitiles_url_b', |
| 173 | ['ref_c'], |
| 174 | ]], |
| 175 | ) |
| 176 | |
| 177 | site_config.Add( |
| 178 | 'build_triggered_b', |
| 179 | luci_builder='ProdBuilder', |
| 180 | display_label='MockLabel', |
| 181 | schedule='triggered', |
| 182 | triggered_gitiles=[[ |
| 183 | 'gitiles_url_b', |
| 184 | ['ref_c'], |
| 185 | ]], |
| 186 | ) |
| 187 | |
Don Garrett | 8dace27 | 2018-07-19 14:07:42 -0700 | [diff] [blame^] | 188 | default_config = config_lib.GetConfig().GetDefault() |
| 189 | |
| 190 | branch_configs = [ |
| 191 | default_config.derive( |
| 192 | name='branch_tester', |
| 193 | luci_builder='TestBuilder', |
| 194 | display_label='TestLabel', |
| 195 | schedule='run daily', |
| 196 | schedule_branch='test-branch', |
| 197 | ), |
| 198 | ] |
| 199 | |
| 200 | |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 201 | expected = '''# Defines buckets on luci-scheduler.appspot.com. |
| 202 | # |
| 203 | # For schema of this file and documentation see ProjectConfig message in |
| 204 | # https://github.com/luci/luci-go/blob/master/scheduler/appengine/messages/config.proto |
| 205 | |
| 206 | # Generated with chromite/scripts/gen_luci_scheduler |
| 207 | |
| 208 | acl_sets { |
| 209 | name: "default" |
| 210 | acls { |
| 211 | role: READER |
| 212 | granted_to: "group:googlers" |
| 213 | } |
| 214 | acls { |
| 215 | role: OWNER |
| 216 | granted_to: "group:project-chromeos-admins" |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | trigger { |
| 221 | id: "trigger_0" |
| 222 | acl_sets: "default" |
| 223 | schedule: "with 5m interval" |
| 224 | gitiles: { |
| 225 | repo: "gitiles_url_a" |
| 226 | refs: "ref_a" |
| 227 | refs: "ref_b" |
| 228 | } |
| 229 | triggers: "build_triggered_a" |
| 230 | } |
| 231 | |
| 232 | trigger { |
| 233 | id: "trigger_1" |
| 234 | acl_sets: "default" |
| 235 | schedule: "with 5m interval" |
| 236 | gitiles: { |
| 237 | repo: "gitiles_url_b" |
| 238 | refs: "ref_c" |
| 239 | } |
| 240 | triggers: "build_triggered_a" |
| 241 | triggers: "build_triggered_b" |
| 242 | } |
| 243 | |
| 244 | job { |
| 245 | id: "build_prod" |
| 246 | acl_sets: "default" |
| 247 | schedule: "run once in a while" |
| 248 | buildbucket: { |
| 249 | server: "cr-buildbucket.appspot.com" |
| 250 | bucket: "luci.chromeos.general" |
| 251 | builder: "ProdBuilder" |
| 252 | tags: "cbb_branch:master" |
| 253 | tags: "cbb_display_label:MockLabel" |
| 254 | tags: "cbb_config:build_prod" |
| 255 | properties: "cbb_branch:master" |
| 256 | properties: "cbb_display_label:MockLabel" |
| 257 | properties: "cbb_config:build_prod" |
| 258 | properties: "cbb_extra_args:[\\"--buildbot\\"]" |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | job { |
| 263 | id: "build_tester" |
| 264 | acl_sets: "default" |
| 265 | schedule: "run daily" |
| 266 | buildbucket: { |
| 267 | server: "cr-buildbucket.appspot.com" |
| 268 | bucket: "luci.chromeos.general" |
| 269 | builder: "TestBuilder" |
| 270 | tags: "cbb_branch:master" |
| 271 | tags: "cbb_display_label:TestLabel" |
| 272 | tags: "cbb_config:build_tester" |
| 273 | properties: "cbb_branch:master" |
| 274 | properties: "cbb_display_label:TestLabel" |
| 275 | properties: "cbb_config:build_tester" |
| 276 | properties: "cbb_extra_args:[\\"--buildbot\\"]" |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | job { |
| 281 | id: "build_triggered_a" |
| 282 | acl_sets: "default" |
| 283 | schedule: "triggered" |
| 284 | buildbucket: { |
| 285 | server: "cr-buildbucket.appspot.com" |
| 286 | bucket: "luci.chromeos.general" |
| 287 | builder: "ProdBuilder" |
| 288 | tags: "cbb_branch:master" |
| 289 | tags: "cbb_display_label:MockLabel" |
| 290 | tags: "cbb_config:build_triggered_a" |
| 291 | properties: "cbb_branch:master" |
| 292 | properties: "cbb_display_label:MockLabel" |
| 293 | properties: "cbb_config:build_triggered_a" |
| 294 | properties: "cbb_extra_args:[\\"--buildbot\\"]" |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | job { |
| 299 | id: "build_triggered_b" |
| 300 | acl_sets: "default" |
| 301 | schedule: "triggered" |
| 302 | buildbucket: { |
| 303 | server: "cr-buildbucket.appspot.com" |
| 304 | bucket: "luci.chromeos.general" |
| 305 | builder: "ProdBuilder" |
| 306 | tags: "cbb_branch:master" |
| 307 | tags: "cbb_display_label:MockLabel" |
| 308 | tags: "cbb_config:build_triggered_b" |
| 309 | properties: "cbb_branch:master" |
| 310 | properties: "cbb_display_label:MockLabel" |
| 311 | properties: "cbb_config:build_triggered_b" |
| 312 | properties: "cbb_extra_args:[\\"--buildbot\\"]" |
| 313 | } |
| 314 | } |
Don Garrett | 8dace27 | 2018-07-19 14:07:42 -0700 | [diff] [blame^] | 315 | |
| 316 | job { |
| 317 | id: "test-branch-branch_tester" |
| 318 | acl_sets: "default" |
| 319 | schedule: "run daily" |
| 320 | buildbucket: { |
| 321 | server: "cr-buildbucket.appspot.com" |
| 322 | bucket: "luci.chromeos.general" |
| 323 | builder: "TestBuilder" |
| 324 | tags: "cbb_branch:test-branch" |
| 325 | tags: "cbb_display_label:TestLabel" |
| 326 | tags: "cbb_config:branch_tester" |
| 327 | properties: "cbb_branch:test-branch" |
| 328 | properties: "cbb_display_label:TestLabel" |
| 329 | properties: "cbb_config:branch_tester" |
| 330 | properties: "cbb_extra_args:[\\"--buildbot\\"]" |
| 331 | } |
| 332 | } |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 333 | ''' |
Don Garrett | 8dace27 | 2018-07-19 14:07:42 -0700 | [diff] [blame^] | 334 | result = gen_luci_scheduler.genLuciSchedulerConfig( |
| 335 | site_config, branch_configs) |
Don Garrett | be3608b | 2018-06-05 17:10:09 -0700 | [diff] [blame] | 336 | |
| 337 | self.assertEqual(result, expected) |