xref: /aosp_15_r20/tools/asuite/atest/metrics/metrics_utils_unittest.py (revision c2e18aaa1096c836b086f94603d04f4eb9cf37f5)
1*c2e18aaaSAndroid Build Coastguard Worker#!/usr/bin/env python3
2*c2e18aaaSAndroid Build Coastguard Worker#
3*c2e18aaaSAndroid Build Coastguard Worker# Copyright 2022, The Android Open Source Project
4*c2e18aaaSAndroid Build Coastguard Worker#
5*c2e18aaaSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
6*c2e18aaaSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
7*c2e18aaaSAndroid Build Coastguard Worker# You may obtain a copy of the License at
8*c2e18aaaSAndroid Build Coastguard Worker#
9*c2e18aaaSAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
10*c2e18aaaSAndroid Build Coastguard Worker#
11*c2e18aaaSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
12*c2e18aaaSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
13*c2e18aaaSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*c2e18aaaSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
15*c2e18aaaSAndroid Build Coastguard Worker# limitations under the License.
16*c2e18aaaSAndroid Build Coastguard Worker
17*c2e18aaaSAndroid Build Coastguard Worker"""Unittests for metrics_utils."""
18*c2e18aaaSAndroid Build Coastguard Worker
19*c2e18aaaSAndroid Build Coastguard Worker# pylint: disable=invalid-name
20*c2e18aaaSAndroid Build Coastguard Worker
21*c2e18aaaSAndroid Build Coastguard Workerfrom io import StringIO
22*c2e18aaaSAndroid Build Coastguard Workerimport sys
23*c2e18aaaSAndroid Build Coastguard Workerimport unittest
24*c2e18aaaSAndroid Build Coastguard Workerfrom unittest import mock
25*c2e18aaaSAndroid Build Coastguard Worker
26*c2e18aaaSAndroid Build Coastguard Workerfrom atest.metrics import metrics_base
27*c2e18aaaSAndroid Build Coastguard Workerfrom atest.metrics import metrics_utils
28*c2e18aaaSAndroid Build Coastguard Workerfrom atest.proto import internal_user_log_pb2
29*c2e18aaaSAndroid Build Coastguard Worker
30*c2e18aaaSAndroid Build Coastguard Worker
31*c2e18aaaSAndroid Build Coastguard Workerclass MetricsUtilsUnittests(unittest.TestCase):
32*c2e18aaaSAndroid Build Coastguard Worker  """Unit tests for metrics_utils.py"""
33*c2e18aaaSAndroid Build Coastguard Worker
34*c2e18aaaSAndroid Build Coastguard Worker  def setUp(self) -> None:
35*c2e18aaaSAndroid Build Coastguard Worker    self.maxDiff = None
36*c2e18aaaSAndroid Build Coastguard Worker
37*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch('atest.metrics.metrics_base.get_user_type')
38*c2e18aaaSAndroid Build Coastguard Worker  def test_print_data_collection_notice(self, mock_get_user_type):
39*c2e18aaaSAndroid Build Coastguard Worker    """Test method print_data_collection_notice."""
40*c2e18aaaSAndroid Build Coastguard Worker
41*c2e18aaaSAndroid Build Coastguard Worker    # get_user_type return 1(external).
42*c2e18aaaSAndroid Build Coastguard Worker    mock_get_user_type.return_value = 1
43*c2e18aaaSAndroid Build Coastguard Worker    capture_output = StringIO()
44*c2e18aaaSAndroid Build Coastguard Worker    sys.stdout = capture_output
45*c2e18aaaSAndroid Build Coastguard Worker    metrics_utils.print_data_collection_notice(colorful=False)
46*c2e18aaaSAndroid Build Coastguard Worker    sys.stdout = sys.__stdout__
47*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(capture_output.getvalue(), "")
48*c2e18aaaSAndroid Build Coastguard Worker
49*c2e18aaaSAndroid Build Coastguard Worker    # get_user_type return 0(internal).
50*c2e18aaaSAndroid Build Coastguard Worker    red = '31m'
51*c2e18aaaSAndroid Build Coastguard Worker    green = '32m'
52*c2e18aaaSAndroid Build Coastguard Worker    start = '\033[1;'
53*c2e18aaaSAndroid Build Coastguard Worker    end = '\033[0m'
54*c2e18aaaSAndroid Build Coastguard Worker    mock_get_user_type.return_value = 0
55*c2e18aaaSAndroid Build Coastguard Worker    notice_str = (
56*c2e18aaaSAndroid Build Coastguard Worker        f'\n==================\n{start}{red}Notice:{end}\n'
57*c2e18aaaSAndroid Build Coastguard Worker        f'{start}{green} We collect usage statistics (including usernames) '
58*c2e18aaaSAndroid Build Coastguard Worker        'in accordance with our '
59*c2e18aaaSAndroid Build Coastguard Worker        'Content Licenses (https://source.android.com/setup/start/licenses), '
60*c2e18aaaSAndroid Build Coastguard Worker        'Contributor License Agreement (https://cla.developers.google.com/), '
61*c2e18aaaSAndroid Build Coastguard Worker        'Privacy Policy (https://policies.google.com/privacy) and '
62*c2e18aaaSAndroid Build Coastguard Worker        f'Terms of Service (https://policies.google.com/terms).{end}'
63*c2e18aaaSAndroid Build Coastguard Worker        '\n==================\n\n'
64*c2e18aaaSAndroid Build Coastguard Worker    )
65*c2e18aaaSAndroid Build Coastguard Worker    capture_output = StringIO()
66*c2e18aaaSAndroid Build Coastguard Worker    sys.stdout = capture_output
67*c2e18aaaSAndroid Build Coastguard Worker    metrics_utils.print_data_collection_notice()
68*c2e18aaaSAndroid Build Coastguard Worker    sys.stdout = sys.__stdout__
69*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(capture_output.getvalue(), notice_str)
70*c2e18aaaSAndroid Build Coastguard Worker
71*c2e18aaaSAndroid Build Coastguard Worker  def test_send_start_event(self):
72*c2e18aaaSAndroid Build Coastguard Worker    metrics_base.MetricsBase.tool_name = 'test_tool'
73*c2e18aaaSAndroid Build Coastguard Worker    metrics_base.MetricsBase.user_type = metrics_base.INTERNAL_USER
74*c2e18aaaSAndroid Build Coastguard Worker    fake_cc = FakeClearcutClient()
75*c2e18aaaSAndroid Build Coastguard Worker    metrics_base.MetricsBase.cc = fake_cc
76*c2e18aaaSAndroid Build Coastguard Worker
77*c2e18aaaSAndroid Build Coastguard Worker    metrics_utils.send_start_event(
78*c2e18aaaSAndroid Build Coastguard Worker        command_line='test_command',
79*c2e18aaaSAndroid Build Coastguard Worker        test_references=['test'],
80*c2e18aaaSAndroid Build Coastguard Worker        cwd='cwd',
81*c2e18aaaSAndroid Build Coastguard Worker        operating_system='test system',
82*c2e18aaaSAndroid Build Coastguard Worker        source_root='test_source',
83*c2e18aaaSAndroid Build Coastguard Worker        hostname='test_host',
84*c2e18aaaSAndroid Build Coastguard Worker    )
85*c2e18aaaSAndroid Build Coastguard Worker
86*c2e18aaaSAndroid Build Coastguard Worker    logged_events = fake_cc.get_logged_events()
87*c2e18aaaSAndroid Build Coastguard Worker    expected_start_event = (
88*c2e18aaaSAndroid Build Coastguard Worker        internal_user_log_pb2.AtestLogEventInternal.AtestStartEvent(
89*c2e18aaaSAndroid Build Coastguard Worker            command_line='test_command',
90*c2e18aaaSAndroid Build Coastguard Worker            test_references=['test'],
91*c2e18aaaSAndroid Build Coastguard Worker            cwd='cwd',
92*c2e18aaaSAndroid Build Coastguard Worker            os='test system',
93*c2e18aaaSAndroid Build Coastguard Worker            source_root='test_source',
94*c2e18aaaSAndroid Build Coastguard Worker            hostname='test_host',
95*c2e18aaaSAndroid Build Coastguard Worker        )
96*c2e18aaaSAndroid Build Coastguard Worker    )
97*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(len(logged_events), 1)
98*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(
99*c2e18aaaSAndroid Build Coastguard Worker        expected_start_event,
100*c2e18aaaSAndroid Build Coastguard Worker        internal_user_log_pb2.AtestLogEventInternal.FromString(
101*c2e18aaaSAndroid Build Coastguard Worker            logged_events[0].source_extension
102*c2e18aaaSAndroid Build Coastguard Worker        ).atest_start_event,
103*c2e18aaaSAndroid Build Coastguard Worker    )
104*c2e18aaaSAndroid Build Coastguard Worker
105*c2e18aaaSAndroid Build Coastguard Worker
106*c2e18aaaSAndroid Build Coastguard Workerclass FakeClearcutClient:
107*c2e18aaaSAndroid Build Coastguard Worker
108*c2e18aaaSAndroid Build Coastguard Worker  def __init__(self):
109*c2e18aaaSAndroid Build Coastguard Worker    self.logged_event = []
110*c2e18aaaSAndroid Build Coastguard Worker
111*c2e18aaaSAndroid Build Coastguard Worker  def log(self, event):
112*c2e18aaaSAndroid Build Coastguard Worker    self.logged_event.extend([event])
113*c2e18aaaSAndroid Build Coastguard Worker
114*c2e18aaaSAndroid Build Coastguard Worker  def get_logged_events(self):
115*c2e18aaaSAndroid Build Coastguard Worker    return self.logged_event
116