• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 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
5# Update event types.
6EVENT_TYPE_DOWNLOAD_COMPLETE = 1
7EVENT_TYPE_INSTALL_COMPLETE = 2
8EVENT_TYPE_UPDATE_COMPLETE = 3
9EVENT_TYPE_DOWNLOAD_STARTED = 13
10EVENT_TYPE_DOWNLOAD_FINISHED = 14
11EVENT_TYPE_REBOOTED_AFTER_UPDATE = 54
12
13# Update event results.
14EVENT_RESULT_ERROR = 0
15EVENT_RESULT_SUCCESS = 1
16EVENT_RESULT_UPDATE_DEFERRED = 9
17
18EVENT_TYPE_DICT = {
19    EVENT_TYPE_DOWNLOAD_COMPLETE: 'download_complete',
20    EVENT_TYPE_INSTALL_COMPLETE: 'install_complete',
21    EVENT_TYPE_UPDATE_COMPLETE: 'update_complete',
22    EVENT_TYPE_DOWNLOAD_STARTED: 'download_started',
23    EVENT_TYPE_DOWNLOAD_FINISHED: 'download_finished',
24    EVENT_TYPE_REBOOTED_AFTER_UPDATE: 'rebooted_after_update',
25    None: 'initial_check'
26}
27
28
29def get_event_type(event_type_code):
30    """
31    Utility to look up the different event types by ID.
32
33    @param event_type_code: An integer event type code.
34    @returns: a string representation of the event type.
35
36    """
37    return EVENT_TYPE_DICT[event_type_code]
38
39
40class UpdateEngineEvent(object):
41    """This class represents a single EXPECTED update engine event.
42
43    This class's data will be compared against an ACTUAL event from a hostlog.
44    """
45
46    def __init__(self, event_type=None, event_result=None, version=None,
47                 previous_version=None, timeout=None):
48        """Initializes an event.
49
50        @param event_type: Expected event type.
51        @param event_result: Expected event result code.
52        @param version: Expected reported image version.
53        @param previous_version: Expected reported previous image version.
54        @param timeout: How many seconds max should it take to reach this event
55                        from the previous one.
56        """
57        self._expected_attrs = {
58            'event_type': event_type,
59            'event_result': event_result,
60            'version': version,
61            'previous_version': previous_version,
62        }
63        self._timeout = timeout
64
65
66    def __str__(self):
67        """Returns a comma separated list of the event data."""
68        return '{%s}' % ', '.join(['%s:%s' % (k, v) for k, v in
69                                   self._expected_attrs.iteritems()])
70
71    def equals(self, actual_event):
72        """
73        Compares this expected event with an actual event from the hostlog.
74
75        We only compare values from the expected event that are not empty.
76        None values in the actual event are assumed to be missing and
77        non-matching.
78
79        @param actual_event: a hostlog event.
80        @return A list of mismatched attributes or None if events match.
81
82        """
83        mismatched_attrs = []
84        for expected_name, expected_val in self._expected_attrs.iteritems():
85            actual_val = actual_event.get(expected_name)
86            if (expected_val and (actual_val is None or
87                                  expected_val != actual_val)):
88                mismatched_attrs.append(expected_name)
89
90        return mismatched_attrs if mismatched_attrs else None
91
92