Rewrite how we abandon sessions
The original code is flawed in that `pm install-abandon` only abandons
the 1st session returned by `pm get-stagedsessions ...`.
1. move AbandonSessionsRule to be shared by multiple host tests
2. use AbandonSessionsRule to do the job
Bug: 160754072
Test: StagedInstallInternalTest
Change-Id: Ib7b32fbd7b1133ac6a8e6782234a4fe2c5a782bd
Merged-In: Ib7b32fbd7b1133ac6a8e6782234a4fe2c5a782bd
(cherry picked from commit 3ac333f2d2)
This commit is contained in:
committed by
Mohammad Samiul Islam
parent
fd9c90f4dc
commit
e3a71c6295
@@ -29,7 +29,12 @@ java_test_host {
|
|||||||
name: "StagedRollbackTest",
|
name: "StagedRollbackTest",
|
||||||
srcs: ["StagedRollbackTest/src/**/*.java"],
|
srcs: ["StagedRollbackTest/src/**/*.java"],
|
||||||
libs: ["tradefed"],
|
libs: ["tradefed"],
|
||||||
static_libs: ["testng", "compatibility-tradefed", "RollbackTestLib"],
|
static_libs: [
|
||||||
|
"compatibility-tradefed",
|
||||||
|
"frameworks-base-hostutils",
|
||||||
|
"RollbackTestLib",
|
||||||
|
"testng",
|
||||||
|
],
|
||||||
test_suites: ["general-tests"],
|
test_suites: ["general-tests"],
|
||||||
test_config: "StagedRollbackTest.xml",
|
test_config: "StagedRollbackTest.xml",
|
||||||
data: [":com.android.apex.apkrollback.test_v1"],
|
data: [":com.android.apex.apkrollback.test_v1"],
|
||||||
@@ -39,7 +44,7 @@ java_test_host {
|
|||||||
name: "NetworkStagedRollbackTest",
|
name: "NetworkStagedRollbackTest",
|
||||||
srcs: ["NetworkStagedRollbackTest/src/**/*.java"],
|
srcs: ["NetworkStagedRollbackTest/src/**/*.java"],
|
||||||
libs: ["tradefed"],
|
libs: ["tradefed"],
|
||||||
static_libs: ["RollbackTestLib"],
|
static_libs: ["RollbackTestLib", "frameworks-base-hostutils"],
|
||||||
test_suites: ["general-tests"],
|
test_suites: ["general-tests"],
|
||||||
test_config: "NetworkStagedRollbackTest.xml",
|
test_config: "NetworkStagedRollbackTest.xml",
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,62 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -28,6 +28,7 @@ java_test_host {
|
|||||||
"testng",
|
"testng",
|
||||||
"compatibility-tradefed",
|
"compatibility-tradefed",
|
||||||
"module_test_util",
|
"module_test_util",
|
||||||
|
"frameworks-base-hostutils",
|
||||||
],
|
],
|
||||||
data: [
|
data: [
|
||||||
":com.android.apex.cts.shim.v2_prebuilt",
|
":com.android.apex.cts.shim.v2_prebuilt",
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import static org.junit.Assert.assertTrue;
|
|||||||
import static org.junit.Assume.assumeTrue;
|
import static org.junit.Assume.assumeTrue;
|
||||||
|
|
||||||
import com.android.ddmlib.Log;
|
import com.android.ddmlib.Log;
|
||||||
|
import com.android.tests.rollback.host.AbandonSessionsRule;
|
||||||
import com.android.tests.util.ModuleTestUtils;
|
import com.android.tests.util.ModuleTestUtils;
|
||||||
import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
|
import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
|
||||||
import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
|
import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
|
||||||
@@ -29,6 +30,7 @@ import com.android.tradefed.util.ProcessInfo;
|
|||||||
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
@@ -39,7 +41,9 @@ public class StagedInstallInternalTest extends BaseHostJUnit4Test {
|
|||||||
|
|
||||||
private static final String TAG = StagedInstallInternalTest.class.getSimpleName();
|
private static final String TAG = StagedInstallInternalTest.class.getSimpleName();
|
||||||
private static final long SYSTEM_SERVER_TIMEOUT_MS = 60 * 1000;
|
private static final long SYSTEM_SERVER_TIMEOUT_MS = 60 * 1000;
|
||||||
private boolean mWasRoot = false;
|
|
||||||
|
@Rule
|
||||||
|
public AbandonSessionsRule mHostTestRule = new AbandonSessionsRule(this);
|
||||||
|
|
||||||
private static final String SHIM_V2 = "com.android.apex.cts.shim.v2.apex";
|
private static final String SHIM_V2 = "com.android.apex.cts.shim.v2.apex";
|
||||||
private static final String APK_A = "TestAppAv1.apk";
|
private static final String APK_A = "TestAppAv1.apk";
|
||||||
@@ -71,21 +75,11 @@ public class StagedInstallInternalTest extends BaseHostJUnit4Test {
|
|||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
mWasRoot = getDevice().isAdbRoot();
|
|
||||||
if (!mWasRoot) {
|
|
||||||
getDevice().enableAdbRoot();
|
|
||||||
}
|
|
||||||
cleanUp();
|
cleanUp();
|
||||||
// Abandon all staged sessions
|
|
||||||
getDevice().executeShellCommand("pm install-abandon $(pm get-stagedsessions --only-ready "
|
|
||||||
+ "--only-parent --only-sessionid)");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void tearDown() throws Exception {
|
public void tearDown() throws Exception {
|
||||||
if (!mWasRoot) {
|
|
||||||
getDevice().disableAdbRoot();
|
|
||||||
}
|
|
||||||
cleanUp();
|
cleanUp();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -151,7 +145,10 @@ public class StagedInstallInternalTest extends BaseHostJUnit4Test {
|
|||||||
private void restartSystemServer() throws Exception {
|
private void restartSystemServer() throws Exception {
|
||||||
// Restart the system server
|
// Restart the system server
|
||||||
long oldStartTime = getDevice().getProcessByName("system_server").getStartTime();
|
long oldStartTime = getDevice().getProcessByName("system_server").getStartTime();
|
||||||
|
|
||||||
|
getDevice().enableAdbRoot(); // Need root to restart system server
|
||||||
assertThat(getDevice().executeShellCommand("am restart")).contains("Restart the system");
|
assertThat(getDevice().executeShellCommand("am restart")).contains("Restart the system");
|
||||||
|
getDevice().disableAdbRoot();
|
||||||
|
|
||||||
// Wait for new system server process to start
|
// Wait for new system server process to start
|
||||||
long start = System.currentTimeMillis();
|
long start = System.currentTimeMillis();
|
||||||
|
|||||||
@@ -16,14 +16,12 @@
|
|||||||
|
|
||||||
package com.android.tests.rollback.host;
|
package com.android.tests.rollback.host;
|
||||||
|
|
||||||
import com.android.ddmlib.Log;
|
|
||||||
import com.android.tradefed.device.ITestDevice;
|
import com.android.tradefed.device.ITestDevice;
|
||||||
import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
|
import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
|
||||||
|
|
||||||
import org.junit.rules.ExternalResource;
|
import org.junit.rules.ExternalResource;
|
||||||
|
|
||||||
public final class AbandonSessionsRule extends ExternalResource {
|
public class AbandonSessionsRule extends ExternalResource {
|
||||||
private static final String TAG = "AbandonSessionsRule";
|
|
||||||
private final BaseHostJUnit4Test mHost;
|
private final BaseHostJUnit4Test mHost;
|
||||||
|
|
||||||
public AbandonSessionsRule(BaseHostJUnit4Test host) {
|
public AbandonSessionsRule(BaseHostJUnit4Test host) {
|
||||||
@@ -39,9 +37,7 @@ public final class AbandonSessionsRule extends ExternalResource {
|
|||||||
protected void after() {
|
protected void after() {
|
||||||
try {
|
try {
|
||||||
abandonSessions(mHost.getDevice());
|
abandonSessions(mHost.getDevice());
|
||||||
} catch (Exception e) {
|
} catch (Exception ignore) {
|
||||||
mHost.getDevice().logOnDevice(TAG, Log.LogLevel.ERROR,
|
|
||||||
"%s", "Failed to abandon sessions");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user