1#!/usr/bin/env python3 2# Copyright 2020 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"""Tests for monitor_pgo_profiles.""" 7 8import datetime 9import subprocess 10import unittest 11import unittest.mock 12 13import monitor_pgo_profiles 14 15 16class Test(unittest.TestCase): 17 """Tests for monitor_pgo_profiles.""" 18 def test_compose_complaint_with_zero_out_of_date(self): 19 self.assertIsNone(monitor_pgo_profiles.compose_complaint([])) 20 21 def test_compose_complaint_with_one_out_of_date(self): 22 profdata_info = monitor_pgo_profiles.ProfdataInfo( 23 date=datetime.datetime(2020, 1, 2, 3, 4, 5), 24 location='gs://somewhere', 25 ) 26 result = monitor_pgo_profiles.compose_complaint([ 27 ('some_arch', profdata_info), 28 ]) 29 self.assertEqual( 30 result, 31 '\n'.join(( 32 '1 profile is out of date:', 33 f'- some_arch (most recent profile was from {profdata_info.date} ' 34 f'at {profdata_info.location!r})', 35 '', 36 '', 37 'PTAL to see if the llvm-pgo-generate bots are functioning ' 38 'normally. Their status can be found at ' 39 f'{monitor_pgo_profiles.PGO_BUILDBOT_LINK}.', 40 )), 41 ) 42 43 def test_compose_complaint_with_two_out_of_date(self): 44 profdata_info_1 = monitor_pgo_profiles.ProfdataInfo( 45 date=datetime.datetime(2020, 1, 2, 3, 4, 5), 46 location='gs://somewhere', 47 ) 48 profdata_info_2 = monitor_pgo_profiles.ProfdataInfo( 49 date=datetime.datetime(2020, 3, 2, 1, 4, 5), 50 location='gs://somewhere-else', 51 ) 52 result = monitor_pgo_profiles.compose_complaint([ 53 ('some_arch', profdata_info_1), 54 ('some_other_arch', profdata_info_2), 55 ]) 56 self.assertEqual( 57 result, 58 '\n'.join(( 59 '2 profiles are out of date:', 60 f'- some_arch (most recent profile was from {profdata_info_1.date} ' 61 f'at {profdata_info_1.location!r})', 62 f'- some_other_arch (most recent profile was from ' 63 f'{profdata_info_2.date} at {profdata_info_2.location!r})', 64 '', 65 '', 66 'PTAL to see if the llvm-pgo-generate bots are functioning ' 67 'normally. Their status can be found at ' 68 f'{monitor_pgo_profiles.PGO_BUILDBOT_LINK}.', 69 )), 70 ) 71 72 @unittest.mock.patch.object(subprocess, 'run') 73 def test_fetching_profdata_functions(self, subprocess_run_mock): 74 ls_return_value = unittest.mock.MagicMock() 75 ls_return_value.stdout = '\n'.join(( 76 ' 1234 2020-06-26T05:26:40Z gs://bar', 77 ' 44 2020-06-23T05:26:40Z gs://foo', 78 ' 1234 2020-06-25T05:26:40Z gs://zzz', 79 )) 80 subprocess_run_mock.return_value = ls_return_value 81 82 most_recent = monitor_pgo_profiles.fetch_most_recent_profdata('arm') 83 self.assertEqual( 84 most_recent, 85 monitor_pgo_profiles.ProfdataInfo( 86 date=datetime.datetime(2020, 6, 26, 5, 26, 40), 87 location='gs://bar', 88 )) 89 90 91if __name__ == '__main__': 92 unittest.main() 93