k8s.io/test-infra@v0.0.0-20240520184403-27c6b4c223d8/gubernator/pull_request_test.py (about) 1 #!/usr/bin/env python 2 3 # Copyright 2016 The Kubernetes Authors. 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 17 import unittest 18 19 import pull_request 20 21 22 def make(number, version, result, start_time=1000): 23 started = None if version is None else { 24 'timestamp': start_time, 'version': version} 25 finished = result and {'result': result} 26 return (number, started, finished) 27 28 def makePodutil(number, revision, result, start_time=1000): 29 started = {'timestamp': start_time} 30 finished = result and {'result': result, 'revision': revision} 31 return (number, started, finished) 32 33 class TableTest(unittest.TestCase): 34 35 def test_builds_to_table(self): 36 jobs = {'J1': [make(4, 'v2', 'A', 9), make(3, 'v2', 'B', 10)], 37 'J2': [make(5, 'v1', 'C', 7), make(4, 'v1', 'D', 6)]} 38 max_builds, headings, rows = pull_request.builds_to_table(jobs) 39 40 self.assertEqual(max_builds, 4) 41 self.assertEqual(headings, [('v2', 2, 9), ('v1', 2, 6)]) 42 self.assertEqual(rows, [('J1', [(4, 'A'), (3, 'B')]), 43 ('J2', [None, None, (5, 'C'), (4, 'D')])]) 44 45 def test_builds_to_table_no_header(self): 46 jobs = {'J': [make(5, None, 'A', 3), make(4, '', 'B', 2)]} 47 self.assertEqual(pull_request.builds_to_table(jobs), 48 (0, [], [('J', [(5, 'A'), (4, 'B')])])) 49 50 def test_pull_ref_commit(self): 51 jobs = {'J1': [make(4, 'v2', 'A', 9)]} 52 jobs['J1'][0][1]['pull'] = 'master:1234,35:abcd' 53 _, headings, _ = pull_request.builds_to_table(jobs) 54 self.assertEqual(headings, [('abcd', 1, 9)]) 55 56 def test_builds_to_table_podutils(self): 57 jobs = {'J1': [makePodutil(4, 'v2', 'A', 9), makePodutil(3, 'v2', 'B', 10)], 58 'J2': [makePodutil(5, 'v1', 'C', 7), makePodutil(4, 'v1', 'D', 6)]} 59 max_builds, headings, rows = pull_request.builds_to_table(jobs) 60 61 self.assertEqual(max_builds, 4) 62 self.assertEqual(headings, [('v2', 2, 9), ('v1', 2, 6)]) 63 self.assertEqual(rows, [('J1', [(4, 'A'), (3, 'B')]), 64 ('J2', [None, None, (5, 'C'), (4, 'D')])])