crash_detector.py revision 3525950d28ff8ea429fbbf1aa2f9db4d25cfa782
1# Copyright (c) 2017 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4# The objective of this test is to identify crashes outlined in the below
5# directories while tests run.
6
7import logging
8
9class CrashDetector(object):
10    """ Identifies all crash files outlined in the below (crash_paths)
11    directories while tests run and get_crash_files finally retuns the list of
12    crash files found so that the count/file names can be used for further
13    analysis. At present this test looks for .meta and .kcrash files as defined
14    in crash_signs variable"""
15    version = 1
16
17    def __init__(self, host):
18        """Initilize variables and lists used in script
19
20        @param host: this function runs on client or as defined
21
22        """
23        self.client = host
24        self.crash_paths = ['/var/spool', '/home/chronos', '/home/chronos/u-*']
25        self.crash_signs = ('\( -name "*.meta*" -o -name "*.kcrash*" \) '
26                            '-printf "%f \n"')
27        self.files_found = []
28        self.crash_files_list = []
29
30
31    def add_crash_files(self, crash_files, crash_path):
32        """Checks if files list is empty and then adds to files_list
33
34        @param crash_files: list of crash files found in crash_path
35        @param crash_path: the path under which crash files are searched for
36
37        """
38        for crash_file in crash_files:
39            if crash_file is not '':
40                self.files_found.append(crash_file)
41                logging.info('CRASH DETECTED in %s/crash: %s',
42                             crash_path, crash_file)
43
44
45    def find_crash_files(self):
46        """Gets crash files from crash_paths and adds them to list."""
47        for crash_path in self.crash_paths:
48            if str(self.client.run('ls %s' % crash_path,
49                   ignore_status=True)).find('crash') != -1:
50                crash_out = self.client.run("find {0} {1} ".format(crash_path,
51                                    self.crash_signs),ignore_status=True).stdout
52                crash_files = crash_out.strip().split('\n')
53                self.add_crash_files(crash_files, crash_path)
54
55
56    def is_new_crash_present(self):
57        """Checks for kernel, browser, process crashes on host
58
59        @returns False if there are no crashes; True otherwise
60
61        """
62        is_new_crash = False
63        self.find_crash_files()
64
65        files_collected = set(self.files_found)
66        crash_files = set(self.crash_files_list)
67
68        files_diff = list(files_collected.difference(crash_files))
69
70        if files_diff:
71            is_new_crash = True
72            self.crash_files_list.extend(files_diff)
73
74        return is_new_crash
75
76
77    def get_crash_files(self):
78        """Gets the list of crash_files collected during the tests whenever
79        is_new_crash_present method is called.
80
81        @returns the list of crash files collected duing the test run
82        """
83        self.is_new_crash_present()
84        return self.crash_files_list
85