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