diff options
author | Mohammad Islam <samiul@google.com> | 2020-09-29 15:33:22 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2020-09-29 15:33:22 +0000 |
commit | bec812403aba01535fd1430e9d62e6cadd2eefda (patch) | |
tree | e7a4fff1c7ecadeb62fa5008eaa521f49df9e612 | |
parent | d7d0a6d48b47b15235d484f799a1ad84ff78b81b (diff) | |
parent | bcd39eca7d08967ad66b389bf7ad2a4f9970e274 (diff) |
Merge "Abandon sessions before/after running tests" am: 347e38a87b am: 6317806d3c am: 4327c4ec4b am: bcd39eca7d
Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1433441
Change-Id: I993f6f29e49e28d00e7a5ccb3066347851917ea7
3 files changed, 70 insertions, 0 deletions
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 61d7c763e8d7..fb4a2b209347 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 @@ -26,6 +26,7 @@ import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test; import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; @@ -57,6 +58,9 @@ public class NetworkStagedRollbackTest extends BaseHostJUnit4Test { private WatchdogEventLogger mLogger = new WatchdogEventLogger(); + @Rule + public AbandonSessionsRule mHostTestRule = new AbandonSessionsRule(this); + @Before public void setUp() throws Exception { runPhase("cleanUp"); 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 9169ef517bf7..be74e338d7ac 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 @@ -34,6 +34,7 @@ import com.android.tradefed.util.CommandStatus; import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; @@ -87,6 +88,9 @@ public class StagedRollbackTest extends BaseHostJUnit4Test { private WatchdogEventLogger mLogger = new WatchdogEventLogger(); + @Rule + public AbandonSessionsRule mHostTestRule = new AbandonSessionsRule(this); + @Before public void setUp() throws Exception { deleteFiles("/system/apex/" + APK_IN_APEX_TESTAPEX_NAME + "*.apex", diff --git a/tests/RollbackTest/lib/src/com/android/tests/rollback/host/AbandonSessionsRule.java b/tests/RollbackTest/lib/src/com/android/tests/rollback/host/AbandonSessionsRule.java new file mode 100644 index 000000000000..b08621314ee0 --- /dev/null +++ b/tests/RollbackTest/lib/src/com/android/tests/rollback/host/AbandonSessionsRule.java @@ -0,0 +1,62 @@ +/* + * 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.testtype.junit4.BaseHostJUnit4Test; + +import org.junit.rules.ExternalResource; + +public class AbandonSessionsRule extends ExternalResource { + private final BaseHostJUnit4Test mHost; + + public AbandonSessionsRule(BaseHostJUnit4Test host) { + mHost = host; + } + + @Override + protected void before() throws Throwable { + abandonSessions(mHost.getDevice()); + } + + @Override + protected void after() { + try { + abandonSessions(mHost.getDevice()); + } catch (Exception ignore) { + } + } + + /** + * Abandons all sessions to prevent interference in our tests. + */ + private static void abandonSessions(ITestDevice device) throws Exception { + // No point in abandoning applied or failed sessions. We care about ready sessions only. + String cmdListReadySessions = + "pm list staged-sessions --only-sessionid --only-parent --only-ready"; + String output = device.executeShellCommand(cmdListReadySessions); + if (output.trim().isEmpty()) { + // No sessions to abandon + return; + } + // Ensure we have sufficient privilege to abandon sessions from other apps + device.enableAdbRoot(); + device.executeShellCommand("for i in $(" + cmdListReadySessions + + "); do pm install-abandon $i; done"); + device.disableAdbRoot(); + } +} |