• 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
17import datetime
18import json
19import logging
20import os
21
22from atest import atest_utils
23from atest import constants
24from atest.metrics import metrics_utils
25
26_META_FILE = os.path.join(
27    atest_utils.get_misc_dir(), '.config', 'asuite', 'atest_history.json'
28)
29_DETECT_OPTION_FILTER = ['-v', '--verbose']
30_DETECTED_SUCCESS = 1
31_DETECTED_FAIL = 0
32# constants of history key
33_LATEST_EXIT_CODE = 'latest_exit_code'
34_UPDATED_AT = 'updated_at'
35
36
37class BugDetector:
38  """Class for handling if a bug is detected by comparing test history."""
39
40  def __init__(self, argv, exit_code, history_file=None):
41    """BugDetector constructor
42
43    Args:
44        argv: A list of arguments.
45        exit_code: An integer of exit code.
46        history_file: A string of a given history file path.
47    """
48    self.detect_key = self.get_detect_key(argv)
49    self.exit_code = exit_code
50    self.file = history_file if history_file else _META_FILE
51    self.history = self.get_history()
52    self.caught_result = self.detect_bug_caught()
53    self.update_history()
54
55  def get_detect_key(self, argv):
56    """Get the key for history searching.
57
58    1. remove '-v' in argv to argv_no_verbose
59    2. sort the argv_no_verbose
60
61    Args:
62        argv: A list of arguments.
63
64    Returns:
65        A string of ordered command line.
66    """
67    argv_without_option = [x for x in argv if x not in _DETECT_OPTION_FILTER]
68    argv_without_option.sort()
69    return ' '.join(argv_without_option)
70
71  def get_history(self):
72    """Get a history object from a history file.
73
74    e.g.
75    {
76        "SystemUITests:.ScrimControllerTest":{
77            "latest_exit_code": 5, "updated_at": "2019-01-26T15:33:08.305026"},
78        "--host hello_world_test ":{
79            "latest_exit_code": 0, "updated_at": "2019-02-26T15:33:08.305026"},
80    }
81
82    Returns:
83        An object of loading from a history.
84    """
85    history = atest_utils.load_json_safely(self.file)
86    return history
87
88  def detect_bug_caught(self):
89    """Detection of catching bugs.
90
91    When latest_exit_code and current exit_code are different, treat it
92    as a bug caught.
93
94    Returns:
95        A integer of detecting result, e.g.
96        1: success
97        0: fail
98    """
99    if not self.history:
100      return _DETECTED_FAIL
101    latest = self.history.get(self.detect_key, {})
102    if latest.get(_LATEST_EXIT_CODE, self.exit_code) == self.exit_code:
103      return _DETECTED_FAIL
104    return _DETECTED_SUCCESS
105
106  def update_history(self):
107    """Update the history file.
108
109    1. update latest_bug result to history cache.
110    2. trim history cache to size from oldest updated time.
111    3. write to the file.
112    """
113    latest_bug = {
114        self.detect_key: {
115            _LATEST_EXIT_CODE: self.exit_code,
116            _UPDATED_AT: datetime.datetime.now().isoformat(),
117        }
118    }
119    self.history.update(latest_bug)
120    num_history = len(self.history)
121    if num_history > constants.UPPER_LIMIT:
122      sorted_history = sorted(
123          self.history.items(), key=lambda kv: kv[1][_UPDATED_AT]
124      )
125      self.history = dict(
126          sorted_history[(num_history - constants.TRIM_TO_SIZE) :]
127      )
128    if not os.path.exists(os.path.dirname(self.file)):
129      os.makedirs(os.path.dirname(self.file))
130    with open(self.file, 'w') as outfile:
131      try:
132        json.dump(self.history, outfile, indent=0)
133      except ValueError as e:
134        logging.debug(e)
135        metrics_utils.handle_exc_and_send_exit_event(
136            constants.ACCESS_HISTORY_FAILURE
137        )
138