Update odsign_e2e_tests to better simulate the real odsign workflow.

Since ag/16330322, odsign calls "odrefresh --check" before calling
"odrefresh --compile". This CL updates odsign_e2e_tests to reflect that
change so that issues like b/241512616 can be caught by the test.

Bug: 241512616
Test: atest odsign_e2e_tests_full passes on the latest ART version.
Test: atest odsign_e2e_tests_full fails if aosp/1951947 is reverted.
Change-Id: I4c2faf095a778612357ab1de6f3c56f85ad3f1d9
diff --git a/test/odsign/test-src/com/android/tests/odsign/OdrefreshHostTest.java b/test/odsign/test-src/com/android/tests/odsign/OdrefreshHostTest.java
index 731ea38..e67822e 100644
--- a/test/odsign/test-src/com/android/tests/odsign/OdrefreshHostTest.java
+++ b/test/odsign/test-src/com/android/tests/odsign/OdrefreshHostTest.java
@@ -27,6 +27,7 @@
 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
 import com.android.tradefed.testtype.junit4.BeforeClassWithInfo;
 
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -46,10 +47,6 @@
     private static final String CACHE_INFO_FILE =
             OdsignTestUtils.ART_APEX_DALVIK_CACHE_DIRNAME + "/cache-info.xml";
     private static final String ODREFRESH_BIN = "odrefresh";
-    private static final String ODREFRESH_COMMAND =
-            ODREFRESH_BIN + " --partial-compilation --no-refresh --compile";
-    private static final String ODREFRESH_MINIMAL_COMMAND =
-            ODREFRESH_BIN + " --partial-compilation --no-refresh --minimal --compile";
 
     private static final String TAG = "OdrefreshHostTest";
     private static final String ZYGOTE_ARTIFACTS_KEY = TAG + ":ZYGOTE_ARTIFACTS";
@@ -87,11 +84,27 @@
         mTestUtils = new OdsignTestUtils(getTestInformation());
     }
 
+    @After
+    public void tearDown() throws Exception {
+        Set<String> artifacts = new HashSet<>();
+        artifacts.addAll(getZygoteArtifacts());
+        artifacts.addAll(getSystemServerArtifacts());
+
+        for (String artifact : artifacts) {
+            if (!getDevice().doesFileExist(artifact)) {
+                // Things went wrong during the test. Run odrefresh to revert to a normal state.
+                mTestUtils.removeCompilationLogToAvoidBackoff();
+                runOdrefresh();
+                break;
+            }
+        }
+    }
+
     @Test
     public void verifyArtSamegradeUpdateTriggersCompilation() throws Exception {
         simulateArtApexUpgrade();
         long timeMs = mTestUtils.getCurrentTimeMs();
-        getDevice().executeShellV2Command(ODREFRESH_COMMAND);
+        runOdrefresh();
 
         assertArtifactsModifiedAfter(getZygoteArtifacts(), timeMs);
         assertArtifactsModifiedAfter(getSystemServerArtifacts(), timeMs);
@@ -101,7 +114,7 @@
     public void verifyOtherApexSamegradeUpdateTriggersCompilation() throws Exception {
         simulateApexUpgrade();
         long timeMs = mTestUtils.getCurrentTimeMs();
-        getDevice().executeShellV2Command(ODREFRESH_COMMAND);
+        runOdrefresh();
 
         assertArtifactsNotModifiedAfter(getZygoteArtifacts(), timeMs);
         assertArtifactsModifiedAfter(getSystemServerArtifacts(), timeMs);
@@ -111,7 +124,7 @@
     public void verifyBootClasspathOtaTriggersCompilation() throws Exception {
         simulateBootClasspathOta();
         long timeMs = mTestUtils.getCurrentTimeMs();
-        getDevice().executeShellV2Command(ODREFRESH_COMMAND);
+        runOdrefresh();
 
         assertArtifactsModifiedAfter(getZygoteArtifacts(), timeMs);
         assertArtifactsModifiedAfter(getSystemServerArtifacts(), timeMs);
@@ -121,7 +134,7 @@
     public void verifySystemServerOtaTriggersCompilation() throws Exception {
         simulateSystemServerOta();
         long timeMs = mTestUtils.getCurrentTimeMs();
-        getDevice().executeShellV2Command(ODREFRESH_COMMAND);
+        runOdrefresh();
 
         assertArtifactsNotModifiedAfter(getZygoteArtifacts(), timeMs);
         assertArtifactsModifiedAfter(getSystemServerArtifacts(), timeMs);
@@ -137,7 +150,7 @@
 
         mTestUtils.removeCompilationLogToAvoidBackoff();
         long timeMs = mTestUtils.getCurrentTimeMs();
-        getDevice().executeShellV2Command(ODREFRESH_COMMAND);
+        runOdrefresh();
 
         assertArtifactsNotModifiedAfter(remainingArtifacts, timeMs);
         assertArtifactsModifiedAfter(missingArtifacts, timeMs);
@@ -158,7 +171,7 @@
                     "device_config put runtime_native_boot enable_uffd_gc false");
 
             long timeMs = mTestUtils.getCurrentTimeMs();
-            getDevice().executeShellV2Command(ODREFRESH_COMMAND);
+            runOdrefresh();
 
             // Artifacts should not be re-compiled.
             assertArtifactsNotModifiedAfter(getZygoteArtifacts(), timeMs);
@@ -169,7 +182,7 @@
                     "device_config put runtime_native_boot enable_uffd_gc true");
 
             timeMs = mTestUtils.getCurrentTimeMs();
-            getDevice().executeShellV2Command(ODREFRESH_COMMAND);
+            runOdrefresh();
 
             // Artifacts should be re-compiled.
             assertArtifactsModifiedAfter(getZygoteArtifacts(), timeMs);
@@ -177,7 +190,7 @@
 
             // Run odrefresh again with the flag unchanged.
             timeMs = mTestUtils.getCurrentTimeMs();
-            getDevice().executeShellV2Command(ODREFRESH_COMMAND);
+            runOdrefresh();
 
             // Artifacts should not be re-compiled.
             assertArtifactsNotModifiedAfter(getZygoteArtifacts(), timeMs);
@@ -188,7 +201,7 @@
                     "device_config put runtime_native_boot enable_uffd_gc false");
 
             timeMs = mTestUtils.getCurrentTimeMs();
-            getDevice().executeShellV2Command(ODREFRESH_COMMAND);
+            runOdrefresh();
 
             // Artifacts should be re-compiled.
             assertArtifactsModifiedAfter(getZygoteArtifacts(), timeMs);
@@ -205,7 +218,7 @@
         // Change a system property from empty to a value.
         getDevice().setProperty("dalvik.vm.foo", "1");
         long timeMs = mTestUtils.getCurrentTimeMs();
-        getDevice().executeShellV2Command(ODREFRESH_COMMAND);
+        runOdrefresh();
 
         // Artifacts should be re-compiled.
         assertArtifactsModifiedAfter(getZygoteArtifacts(), timeMs);
@@ -213,7 +226,7 @@
 
         // Run again with the same value.
         timeMs = mTestUtils.getCurrentTimeMs();
-        getDevice().executeShellV2Command(ODREFRESH_COMMAND);
+        runOdrefresh();
 
         // Artifacts should not be re-compiled.
         assertArtifactsNotModifiedAfter(getZygoteArtifacts(), timeMs);
@@ -222,7 +235,7 @@
         // Change the system property to another value.
         getDevice().setProperty("dalvik.vm.foo", "2");
         timeMs = mTestUtils.getCurrentTimeMs();
-        getDevice().executeShellV2Command(ODREFRESH_COMMAND);
+        runOdrefresh();
 
         // Artifacts should be re-compiled.
         assertArtifactsModifiedAfter(getZygoteArtifacts(), timeMs);
@@ -230,7 +243,7 @@
 
         // Run again with the same value.
         timeMs = mTestUtils.getCurrentTimeMs();
-        getDevice().executeShellV2Command(ODREFRESH_COMMAND);
+        runOdrefresh();
 
         // Artifacts should not be re-compiled.
         assertArtifactsNotModifiedAfter(getZygoteArtifacts(), timeMs);
@@ -239,7 +252,7 @@
         // Change the system property to empty.
         getDevice().setProperty("dalvik.vm.foo", "");
         timeMs = mTestUtils.getCurrentTimeMs();
-        getDevice().executeShellV2Command(ODREFRESH_COMMAND);
+        runOdrefresh();
 
         // Artifacts should be re-compiled.
         assertArtifactsModifiedAfter(getZygoteArtifacts(), timeMs);
@@ -247,7 +260,7 @@
 
         // Run again with the same value.
         timeMs = mTestUtils.getCurrentTimeMs();
-        getDevice().executeShellV2Command(ODREFRESH_COMMAND);
+        runOdrefresh();
 
         // Artifacts should not be re-compiled.
         assertArtifactsNotModifiedAfter(getZygoteArtifacts(), timeMs);
@@ -258,7 +271,7 @@
     public void verifyNoCompilationWhenCacheIsGood() throws Exception {
         mTestUtils.removeCompilationLogToAvoidBackoff();
         long timeMs = mTestUtils.getCurrentTimeMs();
-        getDevice().executeShellV2Command(ODREFRESH_COMMAND);
+        runOdrefresh();
 
         assertArtifactsNotModifiedAfter(getZygoteArtifacts(), timeMs);
         assertArtifactsNotModifiedAfter(getSystemServerArtifacts(), timeMs);
@@ -267,8 +280,8 @@
     @Test
     public void verifyUnexpectedFilesAreCleanedUp() throws Exception {
         String unexpected = OdsignTestUtils.ART_APEX_DALVIK_CACHE_DIRNAME + "/unexpected";
-        getDevice().pushString(/*contents=*/"", unexpected);
-        getDevice().executeShellV2Command(ODREFRESH_COMMAND);
+        getDevice().pushString("" /* contents */, unexpected);
+        runOdrefresh();
 
         assertFalse(getDevice().doesFileExist(unexpected));
     }
@@ -292,9 +305,7 @@
         mTestUtils.removeCompilationLogToAvoidBackoff();
         simulateApexUpgrade();
         long timeMs = mTestUtils.getCurrentTimeMs();
-        getDevice().executeShellV2Command(
-                ODREFRESH_BIN + " --no-refresh --partial-compilation"
-                        + " --compilation-os-mode --compile");
+        runOdrefresh("--compilation-os-mode");
 
         assertArtifactsNotModifiedAfter(getZygoteArtifacts(), timeMs);
         assertArtifactsModifiedAfter(getSystemServerArtifacts(), timeMs);
@@ -307,7 +318,7 @@
 
         // Simulate the odrefresh invocation on the next boot.
         timeMs = mTestUtils.getCurrentTimeMs();
-        getDevice().executeShellV2Command(ODREFRESH_COMMAND);
+        runOdrefresh();
 
         // odrefresh should not re-compile anything.
         assertArtifactsNotModifiedAfter(getZygoteArtifacts(), timeMs);
@@ -319,7 +330,7 @@
         mTestUtils.removeCompilationLogToAvoidBackoff();
         getDevice().executeShellV2Command(
             "rm -rf " + OdsignTestUtils.ART_APEX_DALVIK_CACHE_DIRNAME);
-        getDevice().executeShellV2Command(ODREFRESH_MINIMAL_COMMAND);
+        runOdrefresh("--minimal");
 
         mTestUtils.restartZygote();
 
@@ -330,21 +341,14 @@
         // Running the command again should not overwrite the minimal boot image.
         mTestUtils.removeCompilationLogToAvoidBackoff();
         long timeMs = mTestUtils.getCurrentTimeMs();
-        getDevice().executeShellV2Command(ODREFRESH_MINIMAL_COMMAND);
-
-        assertArtifactsNotModifiedAfter(minimalZygoteArtifacts, timeMs);
-
-        // `odrefresh --check` should keep the minimal boot image.
-        mTestUtils.removeCompilationLogToAvoidBackoff();
-        timeMs = mTestUtils.getCurrentTimeMs();
-        getDevice().executeShellV2Command(ODREFRESH_BIN + " --check");
+        runOdrefresh("--minimal");
 
         assertArtifactsNotModifiedAfter(minimalZygoteArtifacts, timeMs);
 
         // A normal odrefresh invocation should replace the minimal boot image with a full one.
         mTestUtils.removeCompilationLogToAvoidBackoff();
         timeMs = mTestUtils.getCurrentTimeMs();
-        getDevice().executeShellV2Command(ODREFRESH_COMMAND);
+        runOdrefresh();
 
         for (String artifact : minimalZygoteArtifacts) {
             assertFalse(
@@ -488,4 +492,14 @@
     private Set<String> getSystemServerArtifacts() {
         return getColonSeparatedSet(SYSTEM_SERVER_ARTIFACTS_KEY);
     }
+
+    private void runOdrefresh() throws Exception {
+        runOdrefresh("" /* extraArgs */);
+    }
+
+    private void runOdrefresh(String extraArgs) throws Exception {
+        getDevice().executeShellV2Command(ODREFRESH_BIN + " --check");
+        getDevice().executeShellV2Command(
+                ODREFRESH_BIN + " --partial-compilation --no-refresh " + extraArgs + " --compile");
+    }
 }
diff --git a/test/odsign/test-src/com/android/tests/odsign/OdsignTestUtils.java b/test/odsign/test-src/com/android/tests/odsign/OdsignTestUtils.java
index caf94a7..5951600 100644
--- a/test/odsign/test-src/com/android/tests/odsign/OdsignTestUtils.java
+++ b/test/odsign/test-src/com/android/tests/odsign/OdsignTestUtils.java
@@ -314,18 +314,14 @@
         // We can't use the "-c '%.3Y'" flag when to get the timestamp because the Toybox's `stat`
         // implementation truncates the timestamp to seconds, which is not accurate enough, so we
         // use "-c '%%y'" and parse the time ourselves.
-        String dateTimeStr = mTestInfo.getDevice()
-                .executeShellCommand(String.format("stat -c '%%y' '%s'", filename))
-                .trim();
+        String dateTimeStr = assertCommandSucceeds(String.format("stat -c '%%y' '%s'", filename));
         return parseFormattedDateTime(dateTimeStr);
     }
 
     public long getCurrentTimeMs() throws Exception {
         // We can't use getDevice().getDeviceDate() because it truncates the timestamp to seconds,
         // which is not accurate enough.
-        String dateTimeStr = mTestInfo.getDevice()
-                .executeShellCommand("date +'%Y-%m-%d %H:%M:%S.%N %z'")
-                .trim();
+        String dateTimeStr = assertCommandSucceeds("date +'%Y-%m-%d %H:%M:%S.%N %z'");
         return parseFormattedDateTime(dateTimeStr);
     }