1#!/usr/bin/env python3 2# 3# Copyright 2022, The Android Open Source Project 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"""Unittests for metrics_utils.""" 18 19# pylint: disable=invalid-name 20 21from io import StringIO 22import sys 23import unittest 24from unittest import mock 25 26from atest.metrics import metrics_base 27from atest.metrics import metrics_utils 28from atest.proto import internal_user_log_pb2 29 30 31class MetricsUtilsUnittests(unittest.TestCase): 32 """Unit tests for metrics_utils.py""" 33 34 def setUp(self) -> None: 35 self.maxDiff = None 36 37 @mock.patch('atest.metrics.metrics_base.get_user_type') 38 def test_print_data_collection_notice(self, mock_get_user_type): 39 """Test method print_data_collection_notice.""" 40 41 # get_user_type return 1(external). 42 mock_get_user_type.return_value = 1 43 capture_output = StringIO() 44 sys.stdout = capture_output 45 metrics_utils.print_data_collection_notice(colorful=False) 46 sys.stdout = sys.__stdout__ 47 self.assertEqual(capture_output.getvalue(), "") 48 49 # get_user_type return 0(internal). 50 red = '31m' 51 green = '32m' 52 start = '\033[1;' 53 end = '\033[0m' 54 mock_get_user_type.return_value = 0 55 notice_str = ( 56 f'\n==================\n{start}{red}Notice:{end}\n' 57 f'{start}{green} We collect usage statistics (including usernames) ' 58 'in accordance with our ' 59 'Content Licenses (https://source.android.com/setup/start/licenses), ' 60 'Contributor License Agreement (https://cla.developers.google.com/), ' 61 'Privacy Policy (https://policies.google.com/privacy) and ' 62 f'Terms of Service (https://policies.google.com/terms).{end}' 63 '\n==================\n\n' 64 ) 65 capture_output = StringIO() 66 sys.stdout = capture_output 67 metrics_utils.print_data_collection_notice() 68 sys.stdout = sys.__stdout__ 69 self.assertEqual(capture_output.getvalue(), notice_str) 70 71 def test_send_start_event(self): 72 metrics_base.MetricsBase.tool_name = 'test_tool' 73 metrics_base.MetricsBase.user_type = metrics_base.INTERNAL_USER 74 fake_cc = FakeClearcutClient() 75 metrics_base.MetricsBase.cc = fake_cc 76 77 metrics_utils.send_start_event( 78 command_line='test_command', 79 test_references=['test'], 80 cwd='cwd', 81 operating_system='test system', 82 source_root='test_source', 83 hostname='test_host', 84 ) 85 86 logged_events = fake_cc.get_logged_events() 87 expected_start_event = ( 88 internal_user_log_pb2.AtestLogEventInternal.AtestStartEvent( 89 command_line='test_command', 90 test_references=['test'], 91 cwd='cwd', 92 os='test system', 93 source_root='test_source', 94 hostname='test_host', 95 ) 96 ) 97 self.assertEqual(len(logged_events), 1) 98 self.assertEqual( 99 expected_start_event, 100 internal_user_log_pb2.AtestLogEventInternal.FromString( 101 logged_events[0].source_extension 102 ).atest_start_event, 103 ) 104 105 106class FakeClearcutClient: 107 108 def __init__(self): 109 self.logged_event = [] 110 111 def log(self, event): 112 self.logged_event.extend([event]) 113 114 def get_logged_events(self): 115 return self.logged_event 116