Use log file instead of micrdroid logcat for tests

This reduces flakiness by reducing the commands run by host on microdroid shell (such as locat, grep). Log files are more reliable.

------
Mostly a cherrypick of r.android.com/2147670 + change required to
resolve merge conflict.

Two reasons
1) log file is more reliable than microdroid logcat
2) we're going to make microdroid logcat a bootstrap binary. Then shell
   can't execute logcat freely, because shell doesn't have access to the
   bootstrap binaries

Bug: 238135989, 239849383
Test: atest MicrodroidHostTestCases
Merged-In: I0a477fcdf5b674da171cadc307d99b4d4a50d46c
Change-Id: I0a477fcdf5b674da171cadc307d99b4d4a50d46c
diff --git a/tests/hostside/java/android/virt/test/MicrodroidTestCase.java b/tests/hostside/java/android/virt/test/MicrodroidTestCase.java
index 67f48a3..364464f 100644
--- a/tests/hostside/java/android/virt/test/MicrodroidTestCase.java
+++ b/tests/hostside/java/android/virt/test/MicrodroidTestCase.java
@@ -101,6 +101,10 @@
                 false);
     }
 
+    private void waitForBootComplete() {
+        runOnMicrodroidForResult("watch -e \"getprop dev.bootcomplete | grep '^0$'\"");
+    }
+
     // Wait until logd-init starts. The service is one of the last services that are started in
     // the microdroid boot procedure. Therefore, waiting for the service means that we wait for
     // the boot to complete. TODO: we need a better marker eventually.
@@ -380,17 +384,25 @@
                         Optional.of(NUM_VCPUS),
                         Optional.of(CPU_AFFINITY));
         adbConnectToMicrodroid(getDevice(), cid);
-        waitForLogdInit();
-        runOnMicrodroid("logcat -c");
+        waitForBootComplete();
+        runOnMicrodroidRetryingOnFailure(
+                MICRODROID_COMMAND_TIMEOUT_MILLIS, MICRODROID_ADB_CONNECT_MAX_ATTEMPTS, "true");
         // We need root permission to write to /data/tombstones/
         rootMicrodroid();
         // Write a test tombstone file in /data/tombstones
         runOnMicrodroid("echo -n \'Test tombstone in VM with 34 bytes\'"
                     + "> /data/tombstones/transmit.txt");
-        // check if the tombstone have been tranferred from VM
-        assertNotEquals(runOnMicrodroid("timeout 15s logcat | grep -m 1 "
-                            + "'tombstone_transmit.microdroid:.*data/tombstones/transmit.txt'"),
-                "");
+        // check if the tombstone have been tranferred from VM. This is a bit flaky - increasing
+        // timeout to 30s can result in SIGKILL inside microdroid due to logcat memory issue
+        CommandRunner android = new CommandRunner(getDevice());
+        android.runWithTimeout(
+                15000,
+                "grep",
+                "-m",
+                "1",
+                "'tombstone_transmit.microdroid:.*data/tombstones/transmit.txt'",
+                LOG_PATH);
+
         // Confirm that tombstone is received (from host logcat)
         assertNotEquals(runOnHost("adb", "-s", getDevice().getSerialNumber(),
                             "logcat", "-d", "-e",
@@ -435,7 +447,9 @@
         assertThat(runOnMicrodroid("ls", "-Z", testLib), is(label + " " + testLib));
 
         // Check that no denials have happened so far
-        assertThat(runOnMicrodroid("logcat -d -e 'avc:[[:space:]]{1,2}denied'"), is(""));
+        CommandRunner android = new CommandRunner(getDevice());
+        assertThat(android.tryRun("egrep", "'avc:[[:space:]]{1,2}denied'", LOG_PATH),
+                is(nullValue()));
 
         assertThat(runOnMicrodroid("cat /proc/cpuinfo | grep processor | wc -l"),
                 is(Integer.toString(NUM_VCPUS)));