• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 remove_crash_files(self):
32        """Delete crash files from host."""
33        for crash_path in self.crash_paths:
34            self.client.run('rm -rf %s/crash' % crash_path,
35                          ignore_status=True)
36
37
38    def add_crash_files(self, crash_files, crash_path):
39        """Checks if files list is empty and then adds to files_list
40
41        @param crash_files: list of crash files found in crash_path
42        @param crash_path: the path under which crash files are searched for
43
44        """
45        for crash_file in crash_files:
46            if crash_file is not '':
47                self.files_found.append(crash_file)
48                logging.info('CRASH DETECTED in %s/crash: %s',
49                             crash_path, crash_file)
50
51
52    def find_crash_files(self):
53        """Gets crash files from crash_paths and adds them to list."""
54        for crash_path in self.crash_paths:
55            if str(self.client.run('ls %s' % crash_path,
56                   ignore_status=True)).find('crash') != -1:
57                crash_out = self.client.run("find {0} {1} ".format(crash_path,
58                                    self.crash_signs),ignore_status=True).stdout
59                crash_files = crash_out.strip().split('\n')
60                self.add_crash_files(crash_files, crash_path)
61
62
63    def get_new_crash_files(self):
64        """ Gets the newly generated files since last .
65
66        @returns list of newly generated crashes
67        """
68        self.find_crash_files()
69        files_collected = set(self.files_found)
70        crash_files = set(self.crash_files_list)
71
72        diff = list(files_collected.difference(crash_files))
73        if diff:
74            self.crash_files_list.extend(diff)
75        return diff
76
77
78    def is_new_crash_present(self):
79        """Checks for kernel, browser, process crashes on host
80
81        @returns False if there are no crashes; True otherwise
82
83        """
84        if self.get_new_crash_files():
85            return True
86        return False
87
88
89    def get_crash_files(self):
90        """Gets the list of crash_files collected during the tests whenever
91        is_new_crash_present method is called.
92
93        @returns the list of crash files collected duing the test run
94        """
95        self.is_new_crash_present()
96        return self.crash_files_list
97