xref: /aosp_15_r20/external/autotest/client/cros/update_engine/update_engine_event.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
1# Lint as: python2, python3
2# Copyright 2017 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6# Update event types.
7from __future__ import absolute_import
8from __future__ import division
9from __future__ import print_function
10import six
11
12EVENT_TYPE_DOWNLOAD_COMPLETE = 1
13EVENT_TYPE_INSTALL_COMPLETE = 2
14EVENT_TYPE_UPDATE_COMPLETE = 3
15EVENT_TYPE_DOWNLOAD_STARTED = 13
16EVENT_TYPE_DOWNLOAD_FINISHED = 14
17EVENT_TYPE_REBOOTED_AFTER_UPDATE = 54
18
19# Update event results.
20EVENT_RESULT_ERROR = 0
21EVENT_RESULT_SUCCESS = 1
22EVENT_RESULT_UPDATE_DEFERRED = 9
23
24EVENT_TYPE_DICT = {
25    EVENT_TYPE_DOWNLOAD_COMPLETE: 'download_complete',
26    EVENT_TYPE_INSTALL_COMPLETE: 'install_complete',
27    EVENT_TYPE_UPDATE_COMPLETE: 'update_complete',
28    EVENT_TYPE_DOWNLOAD_STARTED: 'download_started',
29    EVENT_TYPE_DOWNLOAD_FINISHED: 'download_finished',
30    EVENT_TYPE_REBOOTED_AFTER_UPDATE: 'rebooted_after_update',
31    None: 'initial_check'
32}
33
34
35def get_event_type(event_type_code):
36    """
37    Utility to look up the different event types by ID.
38
39    @param event_type_code: An integer event type code.
40    @returns: a string representation of the event type.
41
42    """
43    return EVENT_TYPE_DICT[event_type_code]
44
45
46class UpdateEngineEvent(object):
47    """This class represents a single EXPECTED update engine event.
48
49    This class's data will be compared against an ACTUAL event from a hostlog.
50    """
51
52    def __init__(self, event_type=None, event_result=None, version=None,
53                 previous_version=None, timeout=None):
54        """Initializes an event.
55
56        @param event_type: Expected event type.
57        @param event_result: Expected event result code.
58        @param version: Expected reported image version.
59        @param previous_version: Expected reported previous image version.
60        @param timeout: How many seconds max should it take to reach this event
61                        from the previous one.
62        """
63        self._expected_attrs = {
64            'event_type': event_type,
65            'event_result': event_result,
66            'version': version,
67            'previous_version': previous_version,
68        }
69        self._timeout = timeout
70
71
72    def __str__(self):
73        """Returns a comma separated list of the event data."""
74        return '{%s}' % ', '.join([
75                '%s:%s' % (k, v)
76                for k, v in six.iteritems(self._expected_attrs)
77        ])
78
79    def equals(self, actual_event):
80        """
81        Compares this expected event with an actual event from the hostlog.
82
83        We only compare values from the expected event that are not empty.
84        None values in the actual event are assumed to be missing and
85        non-matching.
86
87        @param actual_event: a hostlog event.
88        @return A list of mismatched attributes or None if events match.
89
90        """
91        mismatched_attrs = []
92        for expected_name, expected_val in six.iteritems(self._expected_attrs):
93            actual_val = actual_event.get(expected_name)
94            if (expected_val and (actual_val is None or
95                                  expected_val != actual_val)):
96                mismatched_attrs.append(expected_name)
97
98        return mismatched_attrs if mismatched_attrs else None
99