1#!/usr/bin/env python3 2# Copyright 2023 The Pigweed Authors 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); you may not 5# use this file except in compliance with the License. You may obtain a copy of 6# the License at 7# 8# https://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13# License for the specific language governing permissions and limitations under 14# the License. 15"""Tests for presubmit context classes.""" 16 17import unittest 18 19from pw_presubmit import presubmit_context 20 21 22class ContextTest(unittest.TestCase): 23 """Tests for presubmit context classes.""" 24 25 def test_presubmitcontext(self): # pylint: disable=no-self-use 26 _ = presubmit_context.PresubmitContext.create_for_testing() 27 28 def test_lucicontext(self): 29 ctx = presubmit_context.LuciContext.create_for_testing( 30 BUILDBUCKET_ID='88123', 31 BUILDBUCKET_NAME='project:bucket.dev.ci:builder-linux', 32 BUILD_NUMBER='12', 33 SWARMING_SERVER='https://chrome-swarming.appspot.com', 34 SWARMING_TASK_ID='123def', 35 ) 36 37 self.assertEqual(ctx.buildbucket_id, 88123) 38 self.assertEqual(ctx.build_number, 12) 39 self.assertEqual(ctx.project, 'project') 40 self.assertEqual(ctx.bucket, 'bucket.dev.ci') 41 self.assertEqual(ctx.builder, 'builder-linux') 42 self.assertEqual( 43 ctx.swarming_server, 44 'https://chrome-swarming.appspot.com', 45 ) 46 self.assertEqual(ctx.swarming_task_id, '123def') 47 self.assertEqual( 48 ctx.cas_instance, 49 'projects/chrome-swarming/instances/default_instance', 50 ) 51 52 self.assertFalse(ctx.is_try) 53 self.assertTrue(ctx.is_ci) 54 self.assertTrue(ctx.is_dev) 55 self.assertFalse(ctx.is_shadow) 56 self.assertFalse(ctx.is_prod) 57 58 def test_lucitrigger(self): 59 trigger = presubmit_context.LuciTrigger.create_for_testing( 60 number=1234, 61 patchset=5, 62 remote='https://pigweed-internal.googlesource.com/pigweed', 63 project='pigweed', 64 branch='main', 65 ref='refs/changes/34/1234/5', 66 gerrit_name='pigweed-internal', 67 submitted=False, 68 )[0] 69 70 self.assertEqual( 71 trigger.gerrit_host, 72 'https://pigweed-internal-review.googlesource.com', 73 ) 74 self.assertEqual( 75 trigger.gerrit_url, 76 'https://pigweed-internal-review.googlesource.com/c/1234', 77 ) 78 self.assertEqual( 79 trigger.gitiles_url, 80 'https://pigweed-internal.googlesource.com/pigweed/+/' 81 'refs/changes/34/1234/5', 82 ) 83 84 85if __name__ == '__main__': 86 unittest.main() 87