xref: /aosp_15_r20/tools/asuite/aidegen/lib/aidegen_metrics_unittest.py (revision c2e18aaa1096c836b086f94603d04f4eb9cf37f5)
1*c2e18aaaSAndroid Build Coastguard Worker#!/usr/bin/env python3
2*c2e18aaaSAndroid Build Coastguard Worker#
3*c2e18aaaSAndroid Build Coastguard Worker# Copyright 2019, 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 aidegen_metrics."""
18*c2e18aaaSAndroid Build Coastguard Worker
19*c2e18aaaSAndroid Build Coastguard Workerimport unittest
20*c2e18aaaSAndroid Build Coastguard Workerfrom unittest import mock
21*c2e18aaaSAndroid Build Coastguard Worker
22*c2e18aaaSAndroid Build Coastguard Workerfrom aidegen import constant
23*c2e18aaaSAndroid Build Coastguard Workerfrom aidegen.lib import aidegen_metrics
24*c2e18aaaSAndroid Build Coastguard Worker
25*c2e18aaaSAndroid Build Coastguard Worker
26*c2e18aaaSAndroid Build Coastguard Workertry:
27*c2e18aaaSAndroid Build Coastguard Worker    from atest.metrics import metrics
28*c2e18aaaSAndroid Build Coastguard Worker    from atest.metrics import metrics_utils
29*c2e18aaaSAndroid Build Coastguard Workerexcept ImportError:
30*c2e18aaaSAndroid Build Coastguard Worker    metrics = None
31*c2e18aaaSAndroid Build Coastguard Worker    metrics_utils = None
32*c2e18aaaSAndroid Build Coastguard Worker
33*c2e18aaaSAndroid Build Coastguard Worker
34*c2e18aaaSAndroid Build Coastguard Workerclass AidegenMetricsUnittests(unittest.TestCase):
35*c2e18aaaSAndroid Build Coastguard Worker    """Unit tests for aidegen_metrics.py."""
36*c2e18aaaSAndroid Build Coastguard Worker
37*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(metrics_utils, 'print_data_collection_notice')
38*c2e18aaaSAndroid Build Coastguard Worker    def test_starts_asuite_metrics(self, mock_print_data):
39*c2e18aaaSAndroid Build Coastguard Worker        """Test starts_asuite_metrics."""
40*c2e18aaaSAndroid Build Coastguard Worker        references = ['nothing']
41*c2e18aaaSAndroid Build Coastguard Worker        if not metrics:
42*c2e18aaaSAndroid Build Coastguard Worker            aidegen_metrics.starts_asuite_metrics(references)
43*c2e18aaaSAndroid Build Coastguard Worker            self.assertFalse(mock_print_data.called)
44*c2e18aaaSAndroid Build Coastguard Worker        else:
45*c2e18aaaSAndroid Build Coastguard Worker            with mock.patch.object(metrics_utils, 'get_start_time') as mk_get:
46*c2e18aaaSAndroid Build Coastguard Worker                with mock.patch.object(metrics, 'AtestStartEvent') as mk_start:
47*c2e18aaaSAndroid Build Coastguard Worker                    aidegen_metrics.starts_asuite_metrics(references)
48*c2e18aaaSAndroid Build Coastguard Worker                    self.assertTrue(mock_print_data.called)
49*c2e18aaaSAndroid Build Coastguard Worker                    self.assertTrue(mk_get.called)
50*c2e18aaaSAndroid Build Coastguard Worker                    self.assertTrue(mk_start.called)
51*c2e18aaaSAndroid Build Coastguard Worker
52*c2e18aaaSAndroid Build Coastguard Worker    def test_ends_asuite_metrics(self):
53*c2e18aaaSAndroid Build Coastguard Worker        """Test ends_asuite_metrics."""
54*c2e18aaaSAndroid Build Coastguard Worker        exit_code = constant.EXIT_CODE_NORMAL
55*c2e18aaaSAndroid Build Coastguard Worker        if metrics_utils:
56*c2e18aaaSAndroid Build Coastguard Worker            with mock.patch.object(metrics_utils, 'send_exit_event') as mk_send:
57*c2e18aaaSAndroid Build Coastguard Worker                aidegen_metrics.ends_asuite_metrics(exit_code)
58*c2e18aaaSAndroid Build Coastguard Worker                self.assertTrue(mk_send.called)
59*c2e18aaaSAndroid Build Coastguard Worker
60*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(aidegen_metrics, 'ends_asuite_metrics')
61*c2e18aaaSAndroid Build Coastguard Worker    def test_send_exception_metrics(self, mock_ends_metrics):
62*c2e18aaaSAndroid Build Coastguard Worker        """Test send_exception_metrics."""
63*c2e18aaaSAndroid Build Coastguard Worker        mock_ends_metrics.return_value = True
64*c2e18aaaSAndroid Build Coastguard Worker        aidegen_metrics.send_exception_metrics(1, '', '', 'err_test')
65*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(mock_ends_metrics.called)
66*c2e18aaaSAndroid Build Coastguard Worker
67*c2e18aaaSAndroid Build Coastguard Worker
68*c2e18aaaSAndroid Build Coastguard Workerif __name__ == '__main__':
69*c2e18aaaSAndroid Build Coastguard Worker    unittest.main()
70