• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.cts_root.rollback.host;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import com.android.tradefed.device.ITestDevice;
22 import com.android.tradefed.log.LogUtil.CLog;
23 
24 import com.google.common.truth.FailureMetadata;
25 import com.google.common.truth.Truth;
26 
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29 
30 public class WatchdogEventLogger {
31 
32     private ITestDevice mDevice;
33 
updateTestSysProp(boolean enabled)34     private void updateTestSysProp(boolean enabled) throws Exception {
35         assertThat(mDevice.setProperty(
36                 "persist.sys.rollbacktest.enabled", String.valueOf(enabled))).isTrue();
37     }
38 
start(ITestDevice device)39     public void start(ITestDevice device) throws Exception {
40         mDevice = device;
41         updateTestSysProp(true);
42     }
43 
stop()44     public void stop() throws Exception {
45         if (mDevice != null) {
46             updateTestSysProp(false);
47         }
48     }
49 
verifyEventContainsVal(String watchdogEvent, String expectedVal)50     private boolean verifyEventContainsVal(String watchdogEvent, String expectedVal) {
51         return expectedVal == null || watchdogEvent.contains(expectedVal);
52     }
53 
54     /**
55      * Returns whether a Watchdog event has occurred that matches the given criteria.
56      *
57      * Check the value of all non-null parameters against the list of Watchdog events that have
58      * occurred, and return {@code true} if an event exists which matches all criteria.
59      */
watchdogEventOccurred(String type, String logPackage, String rollbackReason, String failedPackageName)60     public boolean watchdogEventOccurred(String type, String logPackage,
61             String rollbackReason, String failedPackageName) {
62         String watchdogEvent = getEventForRollbackType(type);
63         return (watchdogEvent != null)
64                 && verifyEventContainsVal(watchdogEvent, logPackage)
65                 && verifyEventContainsVal(watchdogEvent, rollbackReason)
66                 && verifyEventContainsVal(watchdogEvent, failedPackageName);
67     }
68 
69     /** Returns last matched event for rollbackType **/
getEventForRollbackType(String rollbackType)70     private String getEventForRollbackType(String rollbackType) {
71         String lastMatchedEvent = null;
72         try {
73             String rollbackDump = mDevice.executeShellCommand("dumpsys rollback");
74             String eventRegex = ".*%s%s(.*)\\n";
75             String eventPrefix = "Watchdog event occurred with type: ";
76 
77             final Pattern pattern = Pattern.compile(
78                     String.format(eventRegex, eventPrefix, rollbackType));
79             final Matcher matcher = pattern.matcher(rollbackDump);
80             while (matcher.find()) {
81                 lastMatchedEvent = matcher.group(1);
82             }
83             CLog.d("Found watchdogEvent: " + lastMatchedEvent + " for type: " + rollbackType);
84         } catch (Exception e) {
85             CLog.e("Unable to find event for type: " + rollbackType, e);
86         }
87         return lastMatchedEvent;
88     }
89 
90     static class Subject extends com.google.common.truth.Subject {
91         private final WatchdogEventLogger mActual;
92 
Subject(FailureMetadata failureMetadata, WatchdogEventLogger subject)93         private Subject(FailureMetadata failureMetadata, WatchdogEventLogger subject) {
94             super(failureMetadata, subject);
95             mActual = subject;
96         }
97 
98         private static Factory<Subject,
loggers()99                 WatchdogEventLogger> loggers() {
100             return Subject::new;
101         }
102 
assertThat(WatchdogEventLogger actual)103         static Subject assertThat(WatchdogEventLogger actual) {
104             return Truth.assertAbout(loggers()).that(actual);
105         }
106 
eventOccurred(String type, String logPackage, String rollbackReason, String failedPackageName)107         void eventOccurred(String type, String logPackage, String rollbackReason,
108                 String failedPackageName) {
109             check("watchdogEventOccurred(type=%s, logPackage=%s, rollbackReason=%s, "
110                     + "failedPackageName=%s)", type, logPackage, rollbackReason, failedPackageName)
111                     .that(mActual.watchdogEventOccurred(type, logPackage, rollbackReason,
112                             failedPackageName)).isTrue();
113         }
114     }
115 }
116