Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7fabc936 authored by Richard Uhler's avatar Richard Uhler
Browse files

Add test for staged rollback of apex only install.

Bug: 124043688
Test: atest StagedRollbackTest

Change-Id: I5383c533483e48d3f300e66e9554677d6057c494
parent 67c3746c
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -76,13 +76,13 @@ LOCAL_PACKAGE_NAME := RollbackTest
LOCAL_MODULE_TAGS := tests
LOCAL_STATIC_JAVA_LIBRARIES := android-support-test
LOCAL_COMPATIBILITY_SUITE := general-tests
LOCAL_COMPATIBILITY_SUPPORT_FILES := $(ROLLBACK_TEST_APEX_V1)
LOCAL_JAVA_RESOURCE_FILES := \
  $(ROLLBACK_TEST_APP_AV1) \
  $(ROLLBACK_TEST_APP_AV2) \
  $(ROLLBACK_TEST_APP_A_CRASHING_V2) \
  $(ROLLBACK_TEST_APP_BV1) \
  $(ROLLBACK_TEST_APP_BV2) \
  $(ROLLBACK_TEST_APEX_V1) \
  $(ROLLBACK_TEST_APEX_V2)
LOCAL_MANIFEST_FILE := RollbackTest/AndroidManifest.xml
LOCAL_SDK_VERSION := system_current
+11 −5
Original line number Diff line number Diff line
@@ -66,7 +66,7 @@ class RollbackTestUtils {
        Context context = InstrumentationRegistry.getContext();
        PackageManager pm = context.getPackageManager();
        try {
            PackageInfo info = pm.getPackageInfo(packageName, 0);
            PackageInfo info = pm.getPackageInfo(packageName, PackageManager.MATCH_APEX);
            return info.getLongVersionCode();
        } catch (PackageManager.NameNotFoundException e) {
            return -1;
@@ -142,7 +142,7 @@ class RollbackTestUtils {
        session = packageInstaller.openSession(sessionId);

        ClassLoader loader = RollbackTest.class.getClassLoader();
        try (OutputStream packageInSession = session.openWrite("package", 0, -1);
        try (OutputStream packageInSession = session.openWrite(resourceName, 0, -1);
             InputStream is = loader.getResourceAsStream(resourceName);) {
            byte[] buffer = new byte[4096];
            int n;
@@ -168,7 +168,8 @@ class RollbackTestUtils {
    }

    /**
     * Installs the apks with the given resource names as an atomic set.
     * Installs the APKs or APEXs with the given resource names as an atomic
     * set. A resource is assumed to be an APEX if it has the .apex extension.
     * <p>
     * In case of staged installs, this function will return succesfully after
     * the staged install has been committed and is ready for the device to
@@ -206,6 +207,9 @@ class RollbackTestUtils {
            if (staged) {
                params.setStaged();
            }
            if (resourceName.endsWith(".apex")) {
                params.setInstallAsApex();
            }
            if (enableRollback) {
                params.setEnableRollback();
            }
@@ -213,7 +217,7 @@ class RollbackTestUtils {
            session = packageInstaller.openSession(sessionId);

            ClassLoader loader = RollbackTest.class.getClassLoader();
            try (OutputStream packageInSession = session.openWrite("package", 0, -1);
            try (OutputStream packageInSession = session.openWrite(resourceName, 0, -1);
                 InputStream is = loader.getResourceAsStream(resourceName);) {
                byte[] buffer = new byte[4096];
                int n;
@@ -247,7 +251,9 @@ class RollbackTestUtils {
    }

    /**
     * Installs the apks with the given resource names as a staged atomic set.
     * Installs the APKs or APEXs with the given resource names as a staged
     * atomic set. A resource is assumed to be an APEX if it has the .apex
     * extension.
     *
     * @param enableRollback if rollback should be enabled.
     * @param resourceNames names of the class loader resource for the apks to
+85 −0
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import android.content.rollback.RollbackManager;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import org.junit.After;
@@ -46,6 +47,11 @@ public class StagedRollbackTest {

    private static final String TAG = "RollbackTest";
    private static final String TEST_APP_A = "com.android.tests.rollback.testapp.A";
    private static final String TEST_APEX_PKG = "com.android.tests.rollback.testapex";
    private static final String TEST_APEX_V1 =
            "com.android.tests.rollback.testapex.RollbackTestApexV1.apex";
    private static final String TEST_APEX_V2 =
            "com.android.tests.rollback.testapex.RollbackTestApexV2.apex";

    /**
     * Adopts common shell permissions needed for rollback tests.
@@ -131,4 +137,83 @@ public class StagedRollbackTest {
        assertTrue(rollback.isStaged());
        assertNotEquals(-1, rollback.getCommittedSessionId());
    }

    /**
     * Test rollbacks of staged installs involving only apex.
     * Prepare apex phase.
     */
    @Test
    public void testApexOnlyPrepareApex() throws Exception {
        // Note: We can't uninstall the apex if it is already on device,
        // because that isn't supported yet (b/123667725). As long as nothing
        // is failing, this should be fine because we don't expect the tests
        // to leave the device with v2 of the apex installed.
        RollbackTestUtils.installStaged(false, TEST_APEX_V1);

        // At this point, the host test driver will reboot the device and run
        // testApexOnlyEnableRollback().
    }

    /**
     * Test rollbacks of staged installs involving only apex.
     * Enable rollback phase.
     */
    @Test
    public void testApexOnlyEnableRollback() throws Exception {
        assertEquals(1, RollbackTestUtils.getInstalledVersion(TEST_APEX_PKG));
        RollbackTestUtils.installStaged(true, TEST_APEX_V2);

        // At this point, the host test driver will reboot the device and run
        // testApexOnlyCommitRollback().
    }

    /**
     * Test rollbacks of staged installs involving only apks.
     * Commit rollback phase.
     */
    @Test
    public void testApexOnlyCommitRollback() throws Exception {
        assertEquals(2, RollbackTestUtils.getInstalledVersion(TEST_APEX_PKG));

        RollbackManager rm = RollbackTestUtils.getRollbackManager();
        RollbackInfo rollback = getUniqueRollbackInfoForPackage(
                rm.getAvailableRollbacks(), TEST_APEX_PKG);
        assertRollbackInfoEquals(TEST_APEX_PKG, 2, 1, rollback);
        assertTrue(rollback.isStaged());

        RollbackTestUtils.rollback(rollback.getRollbackId());

        // Note: We can't use getUniqueRollbackInfoForPackage for the apex,
        // because we can't uninstall the apex (b/123667725), which means
        // there's no way to clear info about rollbacks from previous tests
        // run on the device. Look up the info by rollback id instead.
        RollbackInfo committed = null;
        for (RollbackInfo info : rm.getRecentlyCommittedRollbacks()) {
            if (info.getRollbackId() == rollback.getRollbackId()) {
                assertNull(committed);
                committed = info;
                break;
            }
        }
        assertRollbackInfoEquals(TEST_APEX_PKG, 2, 1, committed);
        assertTrue(committed.isStaged());
        assertNotEquals(-1, committed.getCommittedSessionId());

        RollbackTestUtils.waitForSessionReady(committed.getCommittedSessionId());

        // The apex should not be rolled back until after reboot.
        assertEquals(2, RollbackTestUtils.getInstalledVersion(TEST_APEX_PKG));

        // At this point, the host test driver will reboot the device and run
        // testApexOnlyConfirmRollback().
    }

    /**
     * Test rollbacks of staged installs involving only apks.
     * Confirm rollback phase.
     */
    @Test
    public void testApexOnlyConfirmRollback() throws Exception {
        assertEquals(1, RollbackTestUtils.getInstalledVersion(TEST_APEX_PKG));
    }
}
+14 −0
Original line number Diff line number Diff line
@@ -53,4 +53,18 @@ public class StagedRollbackTest extends BaseHostJUnit4Test {
        getDevice().reboot();
        runPhase("testApkOnlyConfirmRollback");
    }

    /**
     * Tests staged rollbacks involving only apex.
     */
    @Test
    public void testApexOnly() throws Exception {
        runPhase("testApexOnlyPrepareApex");
        getDevice().reboot();
        runPhase("testApexOnlyEnableRollback");
        getDevice().reboot();
        runPhase("testApexOnlyCommitRollback");
        getDevice().reboot();
        runPhase("testApexOnlyConfirmRollback");
    }
}