Archive CompOS VM logs in odsign_e2e_test

Bug: 222508899
Test: atest com.android.tests.odsign.CompOs{Signing,Denial}HostTest
      # See their logs in the tradered output directory

Change-Id: Ia031e91ae7e2969870d4f4f4b02ce1694316d4da
diff --git a/test/odsign/test-src/com/android/tests/odsign/CompOsDenialHostTest.java b/test/odsign/test-src/com/android/tests/odsign/CompOsDenialHostTest.java
index 1dca339..442e190 100644
--- a/test/odsign/test-src/com/android/tests/odsign/CompOsDenialHostTest.java
+++ b/test/odsign/test-src/com/android/tests/odsign/CompOsDenialHostTest.java
@@ -17,6 +17,7 @@
 package com.android.tests.odsign;
 
 import static com.android.tests.odsign.CompOsTestUtils.PENDING_ARTIFACTS_DIR;
+import static com.android.tradefed.testtype.DeviceJUnit4ClassRunner.TestLogData;
 
 import static com.google.common.truth.Truth.assertThat;
 
@@ -29,6 +30,7 @@
 import com.android.tradefed.testtype.junit4.BeforeClassWithInfo;
 
 import org.junit.Before;
+import org.junit.Rule;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
@@ -46,6 +48,8 @@
     private OdsignTestUtils mTestUtils;
     private String mFirstArch;
 
+    @Rule public TestLogData mTestLogs = new TestLogData();
+
     @BeforeClassWithInfo
     public static void beforeClassWithDevice(TestInformation testInfo) throws Exception {
         ITestDevice device = testInfo.getDevice();
@@ -94,6 +98,18 @@
     }
 
     @Test
+    public void vmLogCollector() throws Exception {
+        // This is not a test. The purpose is to collect VM's log, which is generated once per
+        // class, in beforeClassWithDevice before any tests run. It's implemented as a test methond
+        // because TestLogData doesn't seem to work in a class method.
+        OdsignTestUtils testUtils = new OdsignTestUtils(getTestInformation());
+        testUtils.archiveLogThenDelete(mTestLogs, CompOsTestUtils.APEXDATA_DIR + "/vm.log",
+                "vm.log-CompOsDenialHostTest");
+        testUtils.archiveLogThenDelete(mTestLogs, CompOsTestUtils.APEXDATA_DIR + "/vm_console.log",
+                "vm_console.log-CompOsDenialHostTest");
+    }
+
+    @Test
     public void denyDueToInconsistentFileName() throws Exception {
         // Attack emulation: swap file names
         String[] paths = getAllPendingOdexPaths();
diff --git a/test/odsign/test-src/com/android/tests/odsign/CompOsSigningHostTest.java b/test/odsign/test-src/com/android/tests/odsign/CompOsSigningHostTest.java
index 1178130..090fb3a 100644
--- a/test/odsign/test-src/com/android/tests/odsign/CompOsSigningHostTest.java
+++ b/test/odsign/test-src/com/android/tests/odsign/CompOsSigningHostTest.java
@@ -16,6 +16,8 @@
 
 package com.android.tests.odsign;
 
+import static com.android.tradefed.testtype.DeviceJUnit4ClassRunner.TestLogData;
+
 import static com.google.common.truth.Truth.assertThat;
 import static com.google.common.truth.Truth.assertWithMessage;
 
@@ -28,6 +30,7 @@
 import com.android.tradefed.testtype.junit4.AfterClassWithInfo;
 import com.android.tradefed.testtype.junit4.BeforeClassWithInfo;
 
+import org.junit.Rule;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
@@ -47,6 +50,8 @@
     private static final String TIMESTAMP_VM_START_KEY = "compos_test_timestamp_vm_start";
     private static final String TIMESTAMP_REBOOT_KEY = "compos_test_timestamp_reboot";
 
+    @Rule public TestLogData mTestLogs = new TestLogData();
+
     @BeforeClassWithInfo
     public static void beforeClassWithDevice(TestInformation testInfo) throws Exception {
         ITestDevice device = testInfo.getDevice();
@@ -84,6 +89,22 @@
     }
 
     @Test
+    public void vmLogCollector() throws Exception {
+        // This is not a test. The purpose is to collect VM's log, which is generated once per
+        // class. It's implemented as a test methond because TestLogData doesn't seem to work in a
+        // class method.
+
+        // The log files are currently only available through a rooted shell.
+        OdsignTestUtils testUtils = new OdsignTestUtils(getTestInformation());
+        testUtils.enableAdbRootOrSkipTest();
+
+        testUtils.archiveLogThenDelete(mTestLogs, CompOsTestUtils.APEXDATA_DIR + "/vm.log",
+                        "vm.log-CompOsSigningHostTest");
+        testUtils.archiveLogThenDelete(mTestLogs, CompOsTestUtils.APEXDATA_DIR + "/vm_console.log",
+                        "vm_console.log-CompOsSigningHostTest");
+    }
+
+    @Test
     public void checkFileChecksums() throws Exception {
         CompOsTestUtils compOsTestUtils = new CompOsTestUtils(getDevice());
         String actualChecksums = compOsTestUtils.checksumDirectoryContentPartial(
diff --git a/test/odsign/test-src/com/android/tests/odsign/CompOsTestUtils.java b/test/odsign/test-src/com/android/tests/odsign/CompOsTestUtils.java
index aacd0eb..c85f036 100644
--- a/test/odsign/test-src/com/android/tests/odsign/CompOsTestUtils.java
+++ b/test/odsign/test-src/com/android/tests/odsign/CompOsTestUtils.java
@@ -30,6 +30,8 @@
 import java.util.concurrent.TimeUnit;
 
 public class CompOsTestUtils {
+    public static final String APEXDATA_DIR = "/data/misc/apexdata/com.android.compos";
+
     public static final String PENDING_ARTIFACTS_DIR =
             "/data/misc/apexdata/com.android.art/compos-pending";
 
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 b985700..2676c0d 100644
--- a/test/odsign/test-src/com/android/tests/odsign/OdsignTestUtils.java
+++ b/test/odsign/test-src/com/android/tests/odsign/OdsignTestUtils.java
@@ -16,6 +16,8 @@
 
 package com.android.tests.odsign;
 
+import static com.android.tradefed.testtype.DeviceJUnit4ClassRunner.TestLogData;
+
 import static com.google.common.truth.Truth.assertWithMessage;
 
 import static org.junit.Assert.assertNotNull;
@@ -26,8 +28,11 @@
 import android.cts.install.lib.host.InstallUtilsHost;
 
 import com.android.tradefed.device.DeviceNotAvailableException;
+import com.android.tradefed.device.ITestDevice;
 import com.android.tradefed.device.ITestDevice.ApexInfo;
 import com.android.tradefed.invoker.TestInformation;
+import com.android.tradefed.result.FileInputStreamSource;
+import com.android.tradefed.result.LogDataType;
 import com.android.tradefed.util.CommandResult;
 
 import java.io.File;
@@ -356,4 +361,16 @@
         assertWithMessage(result.toString()).that(result.getExitCode()).isEqualTo(0);
         return result.getStdout().trim();
     }
+
+    public void archiveLogThenDelete(TestLogData logs, String remotePath, String localName)
+            throws DeviceNotAvailableException {
+        ITestDevice device = mTestInfo.getDevice();
+        File logFile = device.pullFile(remotePath);
+        if (logFile != null) {
+            logs.addTestLog(localName, LogDataType.TEXT, new FileInputStreamSource(logFile));
+            // Delete to avoid confusing logs from a previous run, just in case.
+            device.deleteFile(remotePath);
+        }
+    }
+
 }