From 5507664a34bcb0b1de7b83b74c0e9b108290d714 Mon Sep 17 00:00:00 2001 From: Victor Hsieh Date: Wed, 9 Mar 2022 23:03:40 +0000 Subject: [PATCH] Archive VM logs for test cases Each test class needs to decide when to collect the logs, since they know when the VM is shutdown. Bug: 222508899 Test: atest MicrodroidHostTestCases ComposHostTestCases AuthFsHostTest # See logs like... # vm_console.log-testOdrefreshSpeedProfile_11308753452704528515.txt # vm_recent.log-testReadWithFsverityVerification_RemoteSmallerFile_15718173342691838983.txt Change-Id: I37b6983a854846eea74a3cf8215be671a6b2d720 --- .../src/com/android/fs/AuthFsHostTest.java | 19 +++++++++++++++++-- .../android/compos/test/ComposTestCase.java | 14 ++++++++++++++ .../virt/test/VirtualizationTestCaseBase.java | 17 ++++++++++++++++- .../android/virt/test/MicrodroidTestCase.java | 12 ++++++++++-- 4 files changed, 57 insertions(+), 5 deletions(-) diff --git a/authfs/tests/java/src/com/android/fs/AuthFsHostTest.java b/authfs/tests/java/src/com/android/fs/AuthFsHostTest.java index 5cd4af89..5d36f16e 100644 --- a/authfs/tests/java/src/com/android/fs/AuthFsHostTest.java +++ b/authfs/tests/java/src/com/android/fs/AuthFsHostTest.java @@ -16,6 +16,8 @@ package com.android.virt.fs; +import static com.android.tradefed.testtype.DeviceJUnit4ClassRunner.TestLogData; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -40,7 +42,9 @@ import com.android.tradefed.util.CommandStatus; import org.junit.After; import org.junit.AssumptionViolatedException; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TestName; import org.junit.runner.RunWith; import java.util.Optional; @@ -100,6 +104,9 @@ public final class AuthFsHostTest extends VirtualizationTestCaseBase { private ExecutorService mThreadPool = Executors.newCachedThreadPool(); + @Rule public TestLogData mTestLogs = new TestLogData(); + @Rule public TestName mTestName = new TestName(); + @BeforeClassWithInfo public static void beforeClassWithDevice(TestInformation testInfo) throws Exception { assertNotNull(testInfo.getDevice()); @@ -170,10 +177,18 @@ public final class AuthFsHostTest extends VirtualizationTestCaseBase { @After public void tearDown() throws Exception { sAndroid.tryRun("killall fd_server"); - sAndroid.run("rm -rf " + TEST_OUTPUT_DIR); - tryRunOnMicrodroid("killall authfs"); tryRunOnMicrodroid("umount " + MOUNT_DIR); + + // Even though we only run one VM for the whole class, and could have collect the VM log + // after all tests are done, TestLogData doesn't seem to work at class level. Hence, + // collect recent logs manually for each test method. + String vmRecentLog = TEST_OUTPUT_DIR + "/vm_recent.log"; + sAndroid.tryRun("tail -n 50 " + LOG_PATH + " > " + vmRecentLog); + archiveLogThenDelete(mTestLogs, getDevice(), vmRecentLog, + "vm_recent.log-" + mTestName.getMethodName()); + + sAndroid.run("rm -rf " + TEST_OUTPUT_DIR); } @Test diff --git a/compos/tests/java/android/compos/test/ComposTestCase.java b/compos/tests/java/android/compos/test/ComposTestCase.java index cfe72cbd..6773eb7f 100644 --- a/compos/tests/java/android/compos/test/ComposTestCase.java +++ b/compos/tests/java/android/compos/test/ComposTestCase.java @@ -16,6 +16,8 @@ package android.compos.test; +import static com.android.tradefed.testtype.DeviceJUnit4ClassRunner.TestLogData; + import static com.google.common.truth.Truth.assertThat; import android.platform.test.annotations.RootPermissionTest; @@ -28,7 +30,9 @@ import com.android.tradefed.util.CommandResult; import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TestName; import org.junit.runner.RunWith; @RootPermissionTest @@ -41,6 +45,8 @@ public final class ComposTestCase extends VirtualizationTestCaseBase { private static final String COMPOS_VERIFY_BIN = "/apex/com.android.compos/bin/compos_verify"; + private static final String COMPOS_APEXDATA_DIR = "/data/misc/apexdata/com.android.compos"; + /** Output directory of odrefresh */ private static final String TEST_ARTIFACTS_DIR = "test-artifacts"; @@ -61,6 +67,9 @@ public final class ComposTestCase extends VirtualizationTestCaseBase { "dalvik.vm.systemservercompilerfilter"; private String mBackupSystemServerCompilerFilter; + @Rule public TestLogData mTestLogs = new TestLogData(); + @Rule public TestName mTestName = new TestName(); + @Before public void setUp() throws Exception { testIfDeviceIsCapable(getDevice()); @@ -77,6 +86,11 @@ public final class ComposTestCase extends VirtualizationTestCaseBase { public void tearDown() throws Exception { killVmAndReconnectAdb(); + archiveLogThenDelete(mTestLogs, getDevice(), COMPOS_APEXDATA_DIR + "/vm_console.log", + "vm_console.log-" + mTestName.getMethodName()); + archiveLogThenDelete(mTestLogs, getDevice(), COMPOS_APEXDATA_DIR + "/vm.log", + "vm.log-" + mTestName.getMethodName()); + CommandRunner android = new CommandRunner(getDevice()); // Clear up any CompOS instance files we created diff --git a/tests/hostside/helper/java/android/virt/test/VirtualizationTestCaseBase.java b/tests/hostside/helper/java/android/virt/test/VirtualizationTestCaseBase.java index 8e86fd19..40be2482 100644 --- a/tests/hostside/helper/java/android/virt/test/VirtualizationTestCaseBase.java +++ b/tests/hostside/helper/java/android/virt/test/VirtualizationTestCaseBase.java @@ -16,6 +16,8 @@ package android.virt.test; +import static com.android.tradefed.testtype.DeviceJUnit4ClassRunner.TestLogData; + import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; @@ -28,6 +30,8 @@ import com.android.tradefed.device.DeviceNotAvailableException; import com.android.tradefed.device.ITestDevice; import com.android.tradefed.device.TestDevice; import com.android.tradefed.log.LogUtil.CLog; +import com.android.tradefed.result.FileInputStreamSource; +import com.android.tradefed.result.LogDataType; import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test; import com.android.tradefed.util.CommandResult; import com.android.tradefed.util.CommandStatus; @@ -46,6 +50,7 @@ import java.util.regex.Pattern; public abstract class VirtualizationTestCaseBase extends BaseHostJUnit4Test { protected static final String TEST_ROOT = "/data/local/tmp/virt/"; protected static final String VIRT_APEX = "/apex/com.android.virt/"; + protected static final String LOG_PATH = TEST_ROOT + "log.txt"; private static final int TEST_VM_ADB_PORT = 8000; private static final String MICRODROID_SERIAL = "localhost:" + TEST_VM_ADB_PORT; private static final String INSTANCE_IMG = "instance.img"; @@ -105,6 +110,16 @@ public abstract class VirtualizationTestCaseBase extends BaseHostJUnit4Test { assumeTrue("Requires VM support", testDevice.supportsMicrodroid()); } + public static void archiveLogThenDelete(TestLogData logs, ITestDevice device, String remotePath, + String localName) throws DeviceNotAvailableException { + 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); + } + } + // Run an arbitrary command in the host side and returns the result private static String runOnHost(String... cmd) { return runOnHostWithTimeout(10000, cmd); @@ -270,7 +285,7 @@ public abstract class VirtualizationTestCaseBase extends BaseHostJUnit4Test { final String outApkIdsigPath = TEST_ROOT + apkName + ".idsig"; final String instanceImg = TEST_ROOT + INSTANCE_IMG; - final String logPath = TEST_ROOT + "log.txt"; + final String logPath = LOG_PATH; final String debugFlag = debug ? "--debug full" : ""; // Run the VM diff --git a/tests/hostside/java/android/virt/test/MicrodroidTestCase.java b/tests/hostside/java/android/virt/test/MicrodroidTestCase.java index 51d62cb2..f3053729 100644 --- a/tests/hostside/java/android/virt/test/MicrodroidTestCase.java +++ b/tests/hostside/java/android/virt/test/MicrodroidTestCase.java @@ -16,6 +16,8 @@ package android.virt.test; +import static com.android.tradefed.testtype.DeviceJUnit4ClassRunner.TestLogData; + import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertEquals; @@ -40,7 +42,9 @@ import org.json.JSONException; import org.json.JSONObject; import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TestName; import org.junit.runner.RunWith; import java.io.File; @@ -65,6 +69,9 @@ public class MicrodroidTestCase extends VirtualizationTestCaseBase { private static final int NUM_VCPUS = 3; private static final String CPU_AFFINITY = "0,1,2"; + @Rule public TestLogData mTestLogs = new TestLogData(); + @Rule public TestName mTestName = new TestName(); + // TODO(b/176805428): remove this private boolean isCuttlefish() throws Exception { String productName = getDevice().getProperty("ro.product.name"); @@ -257,7 +264,7 @@ public class MicrodroidTestCase extends VirtualizationTestCaseBase { final String configPath = TEST_ROOT + "raw_config.json"; getDevice().pushString(config.toString(), configPath); - final String logPath = TEST_ROOT + "log"; + final String logPath = LOG_PATH; final String ret = android.runWithTimeout( 60 * 1000, VIRT_APEX + "bin/vm run", @@ -436,6 +443,7 @@ public class MicrodroidTestCase extends VirtualizationTestCaseBase { public void shutdown() throws Exception { cleanUpVirtualizationTestSetup(getDevice()); - getDevice().uninstallPackage(PACKAGE_NAME); + archiveLogThenDelete(mTestLogs, getDevice(), LOG_PATH, + "vm.log-" + mTestName.getMethodName()); } }