diff options
| author | Yuexi Ma <yuexima@google.com> | 2021-02-11 02:30:34 -0800 |
|---|---|---|
| committer | Yuexi Ma <yuexima@google.com> | 2021-03-01 23:50:16 +0000 |
| commit | 81d8abd58a048e495731c783f5403c2a0bee5ec3 (patch) | |
| tree | 514ed66fc918d43ce02b96617eea06446a92fb12 | |
| parent | e11386db17efd68eba8cb13da397cc2fcf882cf6 (diff) | |
| download | platform_test_app_compat_csuite-81d8abd58a048e495731c783f5403c2a0bee5ec3.tar.gz platform_test_app_compat_csuite-81d8abd58a048e495731c783f5403c2a0bee5ec3.tar.bz2 platform_test_app_compat_csuite-81d8abd58a048e495731c783f5403c2a0bee5ec3.zip | |
Attempts to recover the package manager during test setup
Because the app uninstaller requires the package manager to be available, it should check attempts to recover the package manager before uninstalling apps.
Test: atest :presubmit
Bug: 179101382
Change-Id: Ia83bd05ab77bb00ab14157b40eb87296e6e90f02
| -rw-r--r-- | harness/src/main/java/com/android/csuite/core/SystemPackageUninstaller.java | 21 | ||||
| -rw-r--r-- | harness/src/test/java/com/android/csuite/core/SystemAppUninstallerTest.java | 44 |
2 files changed, 56 insertions, 9 deletions
diff --git a/harness/src/main/java/com/android/csuite/core/SystemPackageUninstaller.java b/harness/src/main/java/com/android/csuite/core/SystemPackageUninstaller.java index b878578..4ed6efe 100644 --- a/harness/src/main/java/com/android/csuite/core/SystemPackageUninstaller.java +++ b/harness/src/main/java/com/android/csuite/core/SystemPackageUninstaller.java @@ -44,11 +44,27 @@ public final class SystemPackageUninstaller { static final String SYSPROP_SYS_BOOT_COMPLETED = "sys.boot_completed"; static final long WAIT_FOR_BOOT_COMPLETE_TIMEOUT_MILLIS = 1000 * 60; @VisibleForTesting static final int MAX_NUMBER_OF_UPDATES = 100; + @VisibleForTesting static final String PM_CHECK_COMMAND = "pm path android"; public static void uninstallPackage(String packageName, ITestDevice device) throws TargetSetupError, DeviceNotAvailableException { checkNotNull(packageName); + if (!isPackageManagerRunning(device)) { + CLog.w( + "Package manager is not available on the device." + + " Attempting to recover it by restarting the framework."); + runAsRoot( + device, + () -> { + stopFramework(device); + startFramework(device); + }); + if (!isPackageManagerRunning(device)) { + throw new TargetSetupError("The package manager failed to start."); + } + } + if (!isPackageInstalled(packageName, device)) { CLog.i("Package %s is not installed.", packageName); return; @@ -236,6 +252,11 @@ public final class SystemPackageUninstaller { "Failed to remove system app data %s from %s", packageName, dataPath)); } + private static boolean isPackageManagerRunning(ITestDevice device) + throws DeviceNotAvailableException { + return device.executeShellV2Command(PM_CHECK_COMMAND).getStatus() == CommandStatus.SUCCESS; + } + private static boolean isPackageInstalled(String packageName, ITestDevice device) throws TargetSetupError, DeviceNotAvailableException { CommandResult commandResult = diff --git a/harness/src/test/java/com/android/csuite/core/SystemAppUninstallerTest.java b/harness/src/test/java/com/android/csuite/core/SystemAppUninstallerTest.java index 2af9d9c..30b77db 100644 --- a/harness/src/test/java/com/android/csuite/core/SystemAppUninstallerTest.java +++ b/harness/src/test/java/com/android/csuite/core/SystemAppUninstallerTest.java @@ -47,20 +47,28 @@ public final class SystemAppUninstallerTest { public void uninstallPackage_packageNameIsNull_throws() throws Exception { assertThrows( NullPointerException.class, - () -> SystemPackageUninstaller.uninstallPackage(TEST_PACKAGE_NAME, null)); + () -> + SystemPackageUninstaller.uninstallPackage( + null, createGoodDeviceWithAppNotInstalled())); } @Test - public void uninstallPackage_packageIsNotInstalled_doesNotRemove() throws Exception { - ITestDevice device = createGoodDeviceWithSystemAppInstalled(); - // Mock the device as if the test package does not exist on device - CommandResult commandResult = createSuccessfulCommandResult(); - commandResult.setStdout(""); + public void uninstallPackage_frameworkNotRunning_startsFrameworkOrThrows() throws Exception { + ITestDevice device = createGoodDeviceWithAppNotInstalled(); Mockito.when( device.executeShellV2Command( - ArgumentMatchers.startsWith( - CHECK_PACKAGE_INSTALLED_COMMAND_PREFIX))) - .thenReturn(commandResult); + ArgumentMatchers.eq(SystemPackageUninstaller.PM_CHECK_COMMAND))) + .thenReturn(createFailedCommandResult()); + + assertThrows( + TargetSetupError.class, + () -> SystemPackageUninstaller.uninstallPackage(TEST_PACKAGE_NAME, device)); + Mockito.verify(device, Mockito.times(1)).executeShellV2Command(Mockito.eq("start")); + } + + @Test + public void uninstallPackage_packageIsNotInstalled_doesNotRemove() throws Exception { + ITestDevice device = createGoodDeviceWithAppNotInstalled(); SystemPackageUninstaller.uninstallPackage(TEST_PACKAGE_NAME, device); @@ -306,6 +314,18 @@ public final class SystemAppUninstallerTest { return device; } + private ITestDevice createGoodDeviceWithAppNotInstalled() throws Exception { + ITestDevice device = createGoodDeviceWithSystemAppInstalled(); + CommandResult commandResult = createSuccessfulCommandResult(); + commandResult.setStdout(""); + Mockito.when( + device.executeShellV2Command( + ArgumentMatchers.startsWith( + CHECK_PACKAGE_INSTALLED_COMMAND_PREFIX))) + .thenReturn(commandResult); + return device; + } + private ITestDevice createGoodDeviceWithSystemAppInstalled() throws Exception { return createGoodDeviceWithSystemAppInstalled(1); } @@ -315,6 +335,12 @@ public final class SystemAppUninstallerTest { ITestDevice device = Mockito.mock(ITestDevice.class); CommandResult commandResult; + // Is framework running + Mockito.when( + device.executeShellV2Command( + ArgumentMatchers.eq(SystemPackageUninstaller.PM_CHECK_COMMAND))) + .thenReturn(createSuccessfulCommandResult()); + // Uninstall updates String uninstallFailureMessage = "Failure [DELETE_FAILED_INTERNAL_ERROR]"; if (numberOfUpdatesInstalled == 0) { |
