• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2019, The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Classes for bug events history."""
16
17# pylint: disable=line-too-long
18
19import datetime
20import logging
21import json
22import os
23
24from atest import atest_utils
25from atest import constants
26
27from atest.metrics import metrics_utils
28
29_META_FILE = os.path.join(atest_utils.get_misc_dir(),
30                          '.config', 'asuite', 'atest_history.json')
31_DETECT_OPTION_FILTER = ['-v', '--verbose']
32_DETECTED_SUCCESS = 1
33_DETECTED_FAIL = 0
34# constants of history key
35_LATEST_EXIT_CODE = 'latest_exit_code'
36_UPDATED_AT = 'updated_at'
37
38class BugDetector:
39    """Class for handling if a bug is detected by comparing test history."""
40
41    def __init__(self, argv, exit_code, history_file=None):
42        """BugDetector constructor
43
44        Args:
45            argv: A list of arguments.
46            exit_code: An integer of exit code.
47            history_file: A string of a given history file path.
48        """
49        self.detect_key = self.get_detect_key(argv)
50        self.exit_code = exit_code
51        self.file = history_file if history_file else _META_FILE
52        self.history = self.get_history()
53        self.caught_result = self.detect_bug_caught()
54        self.update_history()
55
56    def get_detect_key(self, argv):
57        """Get the key for history searching.
58
59        1. remove '-v' in argv to argv_no_verbose
60        2. sort the argv_no_verbose
61
62        Args:
63            argv: A list of arguments.
64
65        Returns:
66            A string of ordered command line.
67        """
68        argv_without_option = [x for x in argv if x not in _DETECT_OPTION_FILTER]
69        argv_without_option.sort()
70        return ' '.join(argv_without_option)
71
72    def get_history(self):
73        """Get a history object from a history file.
74
75        e.g.
76        {
77            "SystemUITests:.ScrimControllerTest":{
78                "latest_exit_code": 5, "updated_at": "2019-01-26T15:33:08.305026"},
79            "--host hello_world_test ":{
80                "latest_exit_code": 0, "updated_at": "2019-02-26T15:33:08.305026"},
81        }
82
83        Returns:
84            An object of loading from a history.
85        """
86        history = {}
87        if os.path.exists(self.file):
88            with open(self.file) as json_file:
89                try:
90                    history = json.load(json_file)
91                except ValueError as e:
92                    logging.debug(e)
93                    metrics_utils.handle_exc_and_send_exit_event(
94                        constants.ACCESS_HISTORY_FAILURE)
95        return history
96
97    def detect_bug_caught(self):
98        """Detection of catching bugs.
99
100        When latest_exit_code and current exit_code are different, treat it
101        as a bug caught.
102
103        Returns:
104            A integer of detecting result, e.g.
105            1: success
106            0: fail
107        """
108        if not self.history:
109            return _DETECTED_FAIL
110        latest = self.history.get(self.detect_key, {})
111        if latest.get(_LATEST_EXIT_CODE, self.exit_code) == self.exit_code:
112            return _DETECTED_FAIL
113        return _DETECTED_SUCCESS
114
115    def update_history(self):
116        """Update the history file.
117
118        1. update latest_bug result to history cache.
119        2. trim history cache to size from oldest updated time.
120        3. write to the file.
121        """
122        latest_bug = {
123            self.detect_key: {
124                _LATEST_EXIT_CODE: self.exit_code,
125                _UPDATED_AT: datetime.datetime.now().isoformat()
126            }
127        }
128        self.history.update(latest_bug)
129        num_history = len(self.history)
130        if num_history > constants.UPPER_LIMIT:
131            sorted_history = sorted(self.history.items(),
132                                    key=lambda kv: kv[1][_UPDATED_AT])
133            self.history = dict(
134                sorted_history[(num_history - constants.TRIM_TO_SIZE):])
135        if not os.path.exists(os.path.dirname(self.file)):
136            os.makedirs(os.path.dirname(self.file))
137        with open(self.file, 'w') as outfile:
138            try:
139                json.dump(self.history, outfile, indent=0)
140            except ValueError as e:
141                logging.debug(e)
142                metrics_utils.handle_exc_and_send_exit_event(
143                    constants.ACCESS_HISTORY_FAILURE)
144