diff options
author | JW Wang <wangchun@google.com> | 2020-02-12 17:18:00 +0800 |
---|---|---|
committer | JW Wang <wangchun@google.com> | 2020-02-12 17:22:00 +0800 |
commit | d617d2ffbea9ad79ccd238c03780b61c89d0c065 (patch) | |
tree | b11ca4104dae0fe3ce18637b93e6b756d24f90f4 /tests/RollbackTest | |
parent | 3e7916ef1853a840d28b150d2929dbc2485ae8e8 (diff) |
Extract common code to a lib (4/n)
Bug: 147785893
Test: atest StagedRollbackTest NetworkStagedRollbackTest
Change-Id: I98da9f6287680691eb17afcd931b51c215da213c
Diffstat (limited to 'tests/RollbackTest')
4 files changed, 152 insertions, 189 deletions
diff --git a/tests/RollbackTest/Android.bp b/tests/RollbackTest/Android.bp index 89005da27709..24623fba5bff 100644 --- a/tests/RollbackTest/Android.bp +++ b/tests/RollbackTest/Android.bp @@ -29,7 +29,7 @@ java_test_host { name: "StagedRollbackTest", srcs: ["StagedRollbackTest/src/**/*.java"], libs: ["tradefed"], - static_libs: ["testng", "compatibility-tradefed"], + static_libs: ["testng", "compatibility-tradefed", "RollbackTestLib"], test_suites: ["general-tests"], test_config: "StagedRollbackTest.xml", data: [":com.android.apex.apkrollback.test_v1"], @@ -39,7 +39,7 @@ java_test_host { name: "NetworkStagedRollbackTest", srcs: ["NetworkStagedRollbackTest/src/**/*.java"], libs: ["tradefed"], - static_libs: ["testng"], + static_libs: ["testng", "RollbackTestLib"], test_suites: ["general-tests"], test_config: "NetworkStagedRollbackTest.xml", } @@ -52,6 +52,12 @@ java_test_host { test_config: "MultiUserRollbackTest.xml", } +java_library_host { + name: "RollbackTestLib", + srcs: ["lib/src/**/*.java"], + libs: ["tradefed"], +} + genrule { name: "com.android.apex.apkrollback.test.pem", out: ["com.android.apex.apkrollback.test.pem"], diff --git a/tests/RollbackTest/NetworkStagedRollbackTest/src/com/android/tests/rollback/host/NetworkStagedRollbackTest.java b/tests/RollbackTest/NetworkStagedRollbackTest/src/com/android/tests/rollback/host/NetworkStagedRollbackTest.java index a72bd38e5a83..d4e34f97a8d2 100644 --- a/tests/RollbackTest/NetworkStagedRollbackTest/src/com/android/tests/rollback/host/NetworkStagedRollbackTest.java +++ b/tests/RollbackTest/NetworkStagedRollbackTest/src/com/android/tests/rollback/host/NetworkStagedRollbackTest.java @@ -16,12 +16,12 @@ package com.android.tests.rollback.host; +import static com.android.tests.rollback.host.WatchdogEventLogger.watchdogEventOccurred; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.testng.Assert.assertThrows; -import com.android.tradefed.device.LogcatReceiver; -import com.android.tradefed.result.InputStreamSource; import com.android.tradefed.testtype.DeviceJUnit4ClassRunner; import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test; @@ -30,10 +30,6 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import java.io.BufferedReader; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.util.ArrayList; import java.util.List; /** @@ -58,19 +54,16 @@ public class NetworkStagedRollbackTest extends BaseHostJUnit4Test { private static final String ROLLBACK_INITIATE = "ROLLBACK_INITIATE"; private static final String ROLLBACK_BOOT_TRIGGERED = "ROLLBACK_BOOT_TRIGGERED"; - private LogcatReceiver mReceiver; + private WatchdogEventLogger mLogger = new WatchdogEventLogger(); @Before public void setUp() throws Exception { - mReceiver = new LogcatReceiver(getDevice(), "logcat -s WatchdogRollbackLogger", - getDevice().getOptions().getMaxLogcatDataSize(), 0); - mReceiver.start(); + mLogger.start(getDevice()); } @After public void tearDown() throws Exception { - mReceiver.stop(); - mReceiver.clear(); + mLogger.stop(); } /** @@ -94,16 +87,12 @@ public class NetworkStagedRollbackTest extends BaseHostJUnit4Test { getDevice().waitForDeviceAvailable(); // Verify rollback was executed after health check deadline runPhase("testNetworkFailedRollback_Phase4"); - InputStreamSource logcatStream = mReceiver.getLogcatData(); - try { - List<String> watchdogEvents = getWatchdogLoggingEvents(logcatStream); - assertTrue(watchdogEventOccurred(watchdogEvents, ROLLBACK_INITIATE, null, - REASON_EXPLICIT_HEALTH_CHECK, null)); - assertTrue(watchdogEventOccurred(watchdogEvents, ROLLBACK_BOOT_TRIGGERED, null, - null, null)); - } finally { - logcatStream.close(); - } + + List<String> watchdogEvents = mLogger.getWatchdogLoggingEvents(); + assertTrue(watchdogEventOccurred(watchdogEvents, ROLLBACK_INITIATE, null, + REASON_EXPLICIT_HEALTH_CHECK, null)); + assertTrue(watchdogEventOccurred(watchdogEvents, ROLLBACK_BOOT_TRIGGERED, null, + null, null)); } finally { // Reconnect internet again so we won't break tests which assume internet available getDevice().executeShellCommand("svc wifi enable"); @@ -133,66 +122,9 @@ public class NetworkStagedRollbackTest extends BaseHostJUnit4Test { // Verify rollback was not executed after health check deadline runPhase("testNetworkPassedDoesNotRollback_Phase3"); - InputStreamSource logcatStream = mReceiver.getLogcatData(); - try { - List<String> watchdogEvents = getWatchdogLoggingEvents(logcatStream); - assertEquals(watchdogEventOccurred(watchdogEvents, null, null, - REASON_EXPLICIT_HEALTH_CHECK, null), false); - } finally { - logcatStream.close(); - } - } - - /** - * Returns a list of all Watchdog logging events which have occurred. - */ - private List<String> getWatchdogLoggingEvents(InputStreamSource inputStreamSource) - throws Exception { - List<String> watchdogEvents = new ArrayList<>(); - InputStream inputStream = inputStreamSource.createInputStream(); - BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); - String line; - while ((line = reader.readLine()) != null) { - if (line.contains("Watchdog event occurred")) { - watchdogEvents.add(line); - } - } - return watchdogEvents; - } - /** - * Returns whether a Watchdog event has occurred that matches the given criteria. - * - * Check the value of all non-null parameters against the list of Watchdog events that have - * occurred, and return {@code true} if an event exists which matches all criteria. - */ - private boolean watchdogEventOccurred(List<String> loggingEvents, - String type, String logPackage, - String rollbackReason, String failedPackageName) throws Exception { - List<String> eventCriteria = new ArrayList<>(); - if (type != null) { - eventCriteria.add("type: " + type); - } - if (logPackage != null) { - eventCriteria.add("logPackage: " + logPackage); - } - if (rollbackReason != null) { - eventCriteria.add("rollbackReason: " + rollbackReason); - } - if (failedPackageName != null) { - eventCriteria.add("failedPackageName: " + failedPackageName); - } - for (String loggingEvent: loggingEvents) { - boolean matchesCriteria = true; - for (String criterion: eventCriteria) { - if (!loggingEvent.contains(criterion)) { - matchesCriteria = false; - } - } - if (matchesCriteria) { - return true; - } - } - return false; + List<String> watchdogEvents = mLogger.getWatchdogLoggingEvents(); + assertEquals(watchdogEventOccurred(watchdogEvents, null, null, + REASON_EXPLICIT_HEALTH_CHECK, null), false); } } diff --git a/tests/RollbackTest/StagedRollbackTest/src/com/android/tests/rollback/host/StagedRollbackTest.java b/tests/RollbackTest/StagedRollbackTest/src/com/android/tests/rollback/host/StagedRollbackTest.java index c3fd962ffa6d..8104d1dbb8ac 100644 --- a/tests/RollbackTest/StagedRollbackTest/src/com/android/tests/rollback/host/StagedRollbackTest.java +++ b/tests/RollbackTest/StagedRollbackTest/src/com/android/tests/rollback/host/StagedRollbackTest.java @@ -16,6 +16,8 @@ package com.android.tests.rollback.host; +import static com.android.tests.rollback.host.WatchdogEventLogger.watchdogEventOccurred; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; @@ -23,8 +25,6 @@ import static org.junit.Assume.assumeTrue; import static org.testng.Assert.assertThrows; import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper; -import com.android.tradefed.device.LogcatReceiver; -import com.android.tradefed.result.InputStreamSource; import com.android.tradefed.testtype.DeviceJUnit4ClassRunner; import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test; @@ -33,11 +33,7 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import java.io.BufferedReader; import java.io.File; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; @@ -83,12 +79,10 @@ public class StagedRollbackTest extends BaseHostJUnit4Test { private static final String ROLLBACK_INITIATE = "ROLLBACK_INITIATE"; private static final String ROLLBACK_BOOT_TRIGGERED = "ROLLBACK_BOOT_TRIGGERED"; - private LogcatReceiver mReceiver; + private WatchdogEventLogger mLogger = new WatchdogEventLogger(); @Before public void setUp() throws Exception { - mReceiver = new LogcatReceiver(getDevice(), "logcat -s WatchdogRollbackLogger", - getDevice().getOptions().getMaxLogcatDataSize(), 0); if (!getDevice().isAdbRoot()) { getDevice().enableAdbRoot(); } @@ -98,13 +92,12 @@ public class StagedRollbackTest extends BaseHostJUnit4Test { + "/data/apex/active/" + APK_IN_APEX_TESTAPEX_NAME + "*.apex"); getDevice().reboot(); runPhase("testCleanUp"); - mReceiver.start(); + mLogger.start(getDevice()); } @After public void tearDown() throws Exception { - mReceiver.stop(); - mReceiver.clear(); + mLogger.stop(); runPhase("testCleanUp"); if (!getDevice().isAdbRoot()) { @@ -134,16 +127,12 @@ public class StagedRollbackTest extends BaseHostJUnit4Test { getDevice().waitForDeviceAvailable(); runPhase("testBadApkOnly_Phase4"); - InputStreamSource logcatStream = mReceiver.getLogcatData(); - try { - List<String> watchdogEvents = getWatchdogLoggingEvents(logcatStream); - assertTrue(watchdogEventOccurred(watchdogEvents, ROLLBACK_INITIATE, null, - REASON_APP_CRASH, TESTAPP_A)); - assertTrue(watchdogEventOccurred(watchdogEvents, ROLLBACK_BOOT_TRIGGERED, null, - null, null)); - } finally { - logcatStream.close(); - } + + List<String> watchdogEvents = mLogger.getWatchdogLoggingEvents(); + assertTrue(watchdogEventOccurred(watchdogEvents, ROLLBACK_INITIATE, null, + REASON_APP_CRASH, TESTAPP_A)); + assertTrue(watchdogEventOccurred(watchdogEvents, ROLLBACK_BOOT_TRIGGERED, null, + null, null)); } @Test @@ -171,16 +160,12 @@ public class StagedRollbackTest extends BaseHostJUnit4Test { // verify rollback committed runPhase("testNativeWatchdogTriggersRollback_Phase3"); - InputStreamSource logcatStream = mReceiver.getLogcatData(); - try { - List<String> watchdogEvents = getWatchdogLoggingEvents(logcatStream); - assertTrue(watchdogEventOccurred(watchdogEvents, ROLLBACK_INITIATE, null, - REASON_NATIVE_CRASH, null)); - assertTrue(watchdogEventOccurred(watchdogEvents, ROLLBACK_BOOT_TRIGGERED, null, - null, null)); - } finally { - logcatStream.close(); - } + + List<String> watchdogEvents = mLogger.getWatchdogLoggingEvents(); + assertTrue(watchdogEventOccurred(watchdogEvents, ROLLBACK_INITIATE, null, + REASON_NATIVE_CRASH, null)); + assertTrue(watchdogEventOccurred(watchdogEvents, ROLLBACK_BOOT_TRIGGERED, null, + null, null)); } @Test @@ -215,16 +200,12 @@ public class StagedRollbackTest extends BaseHostJUnit4Test { // verify all available rollbacks have been committed runPhase("testNativeWatchdogTriggersRollbackForAll_Phase4"); - InputStreamSource logcatStream = mReceiver.getLogcatData(); - try { - List<String> watchdogEvents = getWatchdogLoggingEvents(logcatStream); - assertTrue(watchdogEventOccurred(watchdogEvents, ROLLBACK_INITIATE, null, - REASON_NATIVE_CRASH, null)); - assertTrue(watchdogEventOccurred(watchdogEvents, ROLLBACK_BOOT_TRIGGERED, null, - null, null)); - } finally { - logcatStream.close(); - } + + List<String> watchdogEvents = mLogger.getWatchdogLoggingEvents(); + assertTrue(watchdogEventOccurred(watchdogEvents, ROLLBACK_INITIATE, null, + REASON_NATIVE_CRASH, null)); + assertTrue(watchdogEventOccurred(watchdogEvents, ROLLBACK_BOOT_TRIGGERED, null, + null, null)); } /** @@ -290,16 +271,12 @@ public class StagedRollbackTest extends BaseHostJUnit4Test { getDevice().waitForDeviceAvailable(); // Verify rollback occurred due to crash of apk-in-apex runPhase("testRollbackApexWithApkCrashing_Phase3"); - InputStreamSource logcatStream = mReceiver.getLogcatData(); - try { - List<String> watchdogEvents = getWatchdogLoggingEvents(logcatStream); - assertTrue(watchdogEventOccurred(watchdogEvents, ROLLBACK_INITIATE, null, - REASON_APP_CRASH, TESTAPP_A)); - assertTrue(watchdogEventOccurred(watchdogEvents, ROLLBACK_BOOT_TRIGGERED, null, - null, null)); - } finally { - logcatStream.close(); - } + + List<String> watchdogEvents = mLogger.getWatchdogLoggingEvents(); + assertTrue(watchdogEventOccurred(watchdogEvents, ROLLBACK_INITIATE, null, + REASON_APP_CRASH, TESTAPP_A)); + assertTrue(watchdogEventOccurred(watchdogEvents, ROLLBACK_BOOT_TRIGGERED, null, + null, null)); } /** @@ -464,57 +441,4 @@ public class StagedRollbackTest extends BaseHostJUnit4Test { return false; } } - - /** - * Returns a list of all Watchdog logging events which have occurred. - */ - private List<String> getWatchdogLoggingEvents(InputStreamSource inputStreamSource) - throws Exception { - List<String> watchdogEvents = new ArrayList<>(); - InputStream inputStream = inputStreamSource.createInputStream(); - BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); - String line; - while ((line = reader.readLine()) != null) { - if (line.contains("Watchdog event occurred")) { - watchdogEvents.add(line); - } - } - return watchdogEvents; - } - - /** - * Returns whether a Watchdog event has occurred that matches the given criteria. - * - * Check the value of all non-null parameters against the list of Watchdog events that have - * occurred, and return {@code true} if an event exists which matches all criteria. - */ - private boolean watchdogEventOccurred(List<String> loggingEvents, - String type, String logPackage, - String rollbackReason, String failedPackageName) throws Exception { - List<String> eventCriteria = new ArrayList<>(); - if (type != null) { - eventCriteria.add("type: " + type); - } - if (logPackage != null) { - eventCriteria.add("logPackage: " + logPackage); - } - if (rollbackReason != null) { - eventCriteria.add("rollbackReason: " + rollbackReason); - } - if (failedPackageName != null) { - eventCriteria.add("failedPackageName: " + failedPackageName); - } - for (String loggingEvent: loggingEvents) { - boolean matchesCriteria = true; - for (String criterion: eventCriteria) { - if (!loggingEvent.contains(criterion)) { - matchesCriteria = false; - } - } - if (matchesCriteria) { - return true; - } - } - return false; - } } diff --git a/tests/RollbackTest/lib/src/com/android/tests/rollback/host/WatchdogEventLogger.java b/tests/RollbackTest/lib/src/com/android/tests/rollback/host/WatchdogEventLogger.java new file mode 100644 index 000000000000..317d67e3791e --- /dev/null +++ b/tests/RollbackTest/lib/src/com/android/tests/rollback/host/WatchdogEventLogger.java @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.tests.rollback.host; + +import com.android.tradefed.device.ITestDevice; +import com.android.tradefed.device.LogcatReceiver; +import com.android.tradefed.result.InputStreamSource; + +import java.io.BufferedReader; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.List; + +public class WatchdogEventLogger { + private LogcatReceiver mReceiver; + + public void start(ITestDevice device) { + mReceiver = new LogcatReceiver(device, "logcat -s WatchdogRollbackLogger", + device.getOptions().getMaxLogcatDataSize(), 0); + mReceiver.start(); + } + + public void stop() { + mReceiver.stop(); + mReceiver.clear(); + } + + /** + * Returns a list of all Watchdog logging events which have occurred. + */ + public List<String> getWatchdogLoggingEvents() throws Exception { + try (InputStreamSource logcatStream = mReceiver.getLogcatData()) { + return getWatchdogLoggingEvents(logcatStream); + } + } + + private static List<String> getWatchdogLoggingEvents(InputStreamSource inputStreamSource) + throws Exception { + List<String> watchdogEvents = new ArrayList<>(); + InputStream inputStream = inputStreamSource.createInputStream(); + BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); + String line; + while ((line = reader.readLine()) != null) { + if (line.contains("Watchdog event occurred")) { + watchdogEvents.add(line); + } + } + return watchdogEvents; + } + + /** + * Returns whether a Watchdog event has occurred that matches the given criteria. + * + * Check the value of all non-null parameters against the list of Watchdog events that have + * occurred, and return {@code true} if an event exists which matches all criteria. + */ + public static boolean watchdogEventOccurred(List<String> loggingEvents, + String type, String logPackage, + String rollbackReason, String failedPackageName) throws Exception { + List<String> eventCriteria = new ArrayList<>(); + if (type != null) { + eventCriteria.add("type: " + type); + } + if (logPackage != null) { + eventCriteria.add("logPackage: " + logPackage); + } + if (rollbackReason != null) { + eventCriteria.add("rollbackReason: " + rollbackReason); + } + if (failedPackageName != null) { + eventCriteria.add("failedPackageName: " + failedPackageName); + } + for (String loggingEvent: loggingEvents) { + boolean matchesCriteria = true; + for (String criterion: eventCriteria) { + if (!loggingEvent.contains(criterion)) { + matchesCriteria = false; + } + } + if (matchesCriteria) { + return true; + } + } + return false; + } +} |