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