xref: /aosp_15_r20/tools/asuite/atest/bug_detector_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 bug_detector."""
18*c2e18aaaSAndroid Build Coastguard Worker
19*c2e18aaaSAndroid Build Coastguard Workerimport datetime
20*c2e18aaaSAndroid Build Coastguard Workerimport json
21*c2e18aaaSAndroid Build Coastguard Workerimport os
22*c2e18aaaSAndroid Build Coastguard Workerimport unittest
23*c2e18aaaSAndroid Build Coastguard Workerfrom unittest import mock
24*c2e18aaaSAndroid Build Coastguard Worker
25*c2e18aaaSAndroid Build Coastguard Workerfrom atest import bug_detector
26*c2e18aaaSAndroid Build Coastguard Workerfrom atest import constants
27*c2e18aaaSAndroid Build Coastguard Workerfrom atest import unittest_constants as uc
28*c2e18aaaSAndroid Build Coastguard Worker
29*c2e18aaaSAndroid Build Coastguard WorkerTEST_DICT = {
30*c2e18aaaSAndroid Build Coastguard Worker    'test1': {'latest_exit_code': 5, 'updated_at': ''},
31*c2e18aaaSAndroid Build Coastguard Worker    'test2': {'latest_exit_code': 0, 'updated_at': ''},
32*c2e18aaaSAndroid Build Coastguard Worker}
33*c2e18aaaSAndroid Build Coastguard Worker
34*c2e18aaaSAndroid Build Coastguard Worker
35*c2e18aaaSAndroid Build Coastguard Workerclass BugDetectorUnittest(unittest.TestCase):
36*c2e18aaaSAndroid Build Coastguard Worker  """Unit test for bug_detector.py"""
37*c2e18aaaSAndroid Build Coastguard Worker
38*c2e18aaaSAndroid Build Coastguard Worker  def setUp(self):
39*c2e18aaaSAndroid Build Coastguard Worker    """Set up stuff for testing."""
40*c2e18aaaSAndroid Build Coastguard Worker    self.history_file = os.path.join(uc.TEST_DATA_DIR, 'bug_detector.json')
41*c2e18aaaSAndroid Build Coastguard Worker    self.detector = bug_detector.BugDetector(['test1'], 5, self.history_file)
42*c2e18aaaSAndroid Build Coastguard Worker    self._reset_history_file()
43*c2e18aaaSAndroid Build Coastguard Worker    self.history_file2 = os.path.join(uc.TEST_DATA_DIR, 'bug_detector2.json')
44*c2e18aaaSAndroid Build Coastguard Worker
45*c2e18aaaSAndroid Build Coastguard Worker  def tearDown(self):
46*c2e18aaaSAndroid Build Coastguard Worker    """Run after execution of every test"""
47*c2e18aaaSAndroid Build Coastguard Worker    if os.path.isfile(self.history_file):
48*c2e18aaaSAndroid Build Coastguard Worker      os.remove(self.history_file)
49*c2e18aaaSAndroid Build Coastguard Worker    if os.path.isfile(self.history_file2):
50*c2e18aaaSAndroid Build Coastguard Worker      os.remove(self.history_file2)
51*c2e18aaaSAndroid Build Coastguard Worker
52*c2e18aaaSAndroid Build Coastguard Worker  def _reset_history_file(self):
53*c2e18aaaSAndroid Build Coastguard Worker    """Reset test history file."""
54*c2e18aaaSAndroid Build Coastguard Worker    with open(self.history_file, 'w') as outfile:
55*c2e18aaaSAndroid Build Coastguard Worker      json.dump(TEST_DICT, outfile)
56*c2e18aaaSAndroid Build Coastguard Worker
57*c2e18aaaSAndroid Build Coastguard Worker  def _make_test_file(self, file_size):
58*c2e18aaaSAndroid Build Coastguard Worker    temp_history = {}
59*c2e18aaaSAndroid Build Coastguard Worker    for i in range(file_size):
60*c2e18aaaSAndroid Build Coastguard Worker      latest_bug = {
61*c2e18aaaSAndroid Build Coastguard Worker          i: {
62*c2e18aaaSAndroid Build Coastguard Worker              'latest_exit_code': i,
63*c2e18aaaSAndroid Build Coastguard Worker              'updated_at': datetime.datetime.now().isoformat(),
64*c2e18aaaSAndroid Build Coastguard Worker          }
65*c2e18aaaSAndroid Build Coastguard Worker      }
66*c2e18aaaSAndroid Build Coastguard Worker      temp_history.update(latest_bug)
67*c2e18aaaSAndroid Build Coastguard Worker    with open(self.history_file2, 'w') as outfile:
68*c2e18aaaSAndroid Build Coastguard Worker      json.dump(temp_history, outfile, indent=0)
69*c2e18aaaSAndroid Build Coastguard Worker
70*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(bug_detector.BugDetector, 'update_history')
71*c2e18aaaSAndroid Build Coastguard Worker  def test_get_detect_key(self, _):
72*c2e18aaaSAndroid Build Coastguard Worker    """Test get_detect_key."""
73*c2e18aaaSAndroid Build Coastguard Worker    # argv without -v
74*c2e18aaaSAndroid Build Coastguard Worker    argv = ['test2', 'test1']
75*c2e18aaaSAndroid Build Coastguard Worker    want_key = 'test1 test2'
76*c2e18aaaSAndroid Build Coastguard Worker    dtr = bug_detector.BugDetector(argv, 0)
77*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(dtr.get_detect_key(argv), want_key)
78*c2e18aaaSAndroid Build Coastguard Worker
79*c2e18aaaSAndroid Build Coastguard Worker    # argv with -v
80*c2e18aaaSAndroid Build Coastguard Worker    argv = ['-v', 'test2', 'test1']
81*c2e18aaaSAndroid Build Coastguard Worker    want_key = 'test1 test2'
82*c2e18aaaSAndroid Build Coastguard Worker    dtr = bug_detector.BugDetector(argv, 0)
83*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(dtr.get_detect_key(argv), want_key)
84*c2e18aaaSAndroid Build Coastguard Worker
85*c2e18aaaSAndroid Build Coastguard Worker    # argv with --verbose
86*c2e18aaaSAndroid Build Coastguard Worker    argv = ['--verbose', 'test2', 'test3', 'test1']
87*c2e18aaaSAndroid Build Coastguard Worker    want_key = 'test1 test2 test3'
88*c2e18aaaSAndroid Build Coastguard Worker    dtr = bug_detector.BugDetector(argv, 0)
89*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(dtr.get_detect_key(argv), want_key)
90*c2e18aaaSAndroid Build Coastguard Worker
91*c2e18aaaSAndroid Build Coastguard Worker  def test_get_history(self):
92*c2e18aaaSAndroid Build Coastguard Worker    """Test get_history."""
93*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(self.detector.get_history(), TEST_DICT)
94*c2e18aaaSAndroid Build Coastguard Worker
95*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(bug_detector.BugDetector, 'update_history')
96*c2e18aaaSAndroid Build Coastguard Worker  def test_detect_bug_caught(self, _):
97*c2e18aaaSAndroid Build Coastguard Worker    """Test detect_bug_caught."""
98*c2e18aaaSAndroid Build Coastguard Worker    self._reset_history_file()
99*c2e18aaaSAndroid Build Coastguard Worker    dtr = bug_detector.BugDetector(['test1'], 0, self.history_file)
100*c2e18aaaSAndroid Build Coastguard Worker    success = 1
101*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(dtr.detect_bug_caught(), success)
102*c2e18aaaSAndroid Build Coastguard Worker
103*c2e18aaaSAndroid Build Coastguard Worker  def test_update_history(self):
104*c2e18aaaSAndroid Build Coastguard Worker    """Test update_history."""
105*c2e18aaaSAndroid Build Coastguard Worker    constants.UPPER_LIMIT = 10
106*c2e18aaaSAndroid Build Coastguard Worker    constants.TRIM_TO_SIZE = 3
107*c2e18aaaSAndroid Build Coastguard Worker
108*c2e18aaaSAndroid Build Coastguard Worker    mock_file_size = 0
109*c2e18aaaSAndroid Build Coastguard Worker    self._make_test_file(mock_file_size)
110*c2e18aaaSAndroid Build Coastguard Worker    dtr = bug_detector.BugDetector(['test1'], 0, self.history_file2)
111*c2e18aaaSAndroid Build Coastguard Worker    self.assertTrue('test1' in dtr.history)
112*c2e18aaaSAndroid Build Coastguard Worker
113*c2e18aaaSAndroid Build Coastguard Worker    # History is larger than constants.UPPER_LIMIT. Trim to size.
114*c2e18aaaSAndroid Build Coastguard Worker    mock_file_size = 10
115*c2e18aaaSAndroid Build Coastguard Worker    self._make_test_file(mock_file_size)
116*c2e18aaaSAndroid Build Coastguard Worker    dtr = bug_detector.BugDetector(['test1'], 0, self.history_file2)
117*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(len(dtr.history), constants.TRIM_TO_SIZE)
118*c2e18aaaSAndroid Build Coastguard Worker    keys = ['test1', '9', '8']
119*c2e18aaaSAndroid Build Coastguard Worker    for key in keys:
120*c2e18aaaSAndroid Build Coastguard Worker      self.assertTrue(key in dtr.history)
121*c2e18aaaSAndroid Build Coastguard Worker
122*c2e18aaaSAndroid Build Coastguard Worker    # History is not larger than constants.UPPER_LIMIT.
123*c2e18aaaSAndroid Build Coastguard Worker    mock_file_size = 5
124*c2e18aaaSAndroid Build Coastguard Worker    self._make_test_file(mock_file_size)
125*c2e18aaaSAndroid Build Coastguard Worker    dtr = bug_detector.BugDetector(['test1'], 0, self.history_file2)
126*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(len(dtr.history), mock_file_size + 1)
127*c2e18aaaSAndroid Build Coastguard Worker    keys = ['test1', '4', '3', '2', '1', '0']
128*c2e18aaaSAndroid Build Coastguard Worker    for key in keys:
129*c2e18aaaSAndroid Build Coastguard Worker      self.assertTrue(key in dtr.history)
130*c2e18aaaSAndroid Build Coastguard Worker
131*c2e18aaaSAndroid Build Coastguard Worker
132*c2e18aaaSAndroid Build Coastguard Workerif __name__ == '__main__':
133*c2e18aaaSAndroid Build Coastguard Worker  unittest.main()
134