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