Disable AVF debug policies for tests
AVF debug policy enabled devices behave differently from end-user's
devices, so some tests (mostly for benchmarks) couldn't have been tested
the real world situation.
Specified DisableMicrodroidDebugPolicyPreparer for some tests that better
to be run without AVF debug policy. We can add the preparer for more tests,
but let me start with a small subset.
Test: atest. Benchmark stats were improved as expected.
Bug: 272496125
Change-Id: I829111d38bb16ee9b78c20ea8cec4dce7240a1ed
diff --git a/tests/benchmark/AndroidTest.xml b/tests/benchmark/AndroidTest.xml
index 29bc95a..8c8bfbe 100644
--- a/tests/benchmark/AndroidTest.xml
+++ b/tests/benchmark/AndroidTest.xml
@@ -30,6 +30,7 @@
<option name="post-push" value="chmod 755 /data/local/tmp/perf-setup.sh;/data/local/tmp/perf-setup.sh" />
<option name="cleanup" value="true" />
</target_preparer>
+ <target_preparer class="com.android.microdroid.test.preparer.DisableMicrodroidDebugPolicyPreparer" />
<test class="com.android.tradefed.testtype.AndroidJUnitTest" >
<option name="package" value="com.android.microdroid.benchmark" />
<option name="runner" value="androidx.test.runner.AndroidJUnitRunner" />
diff --git a/tests/benchmark_hostside/AndroidTest.xml b/tests/benchmark_hostside/AndroidTest.xml
index 5161269..7a998b1 100644
--- a/tests/benchmark_hostside/AndroidTest.xml
+++ b/tests/benchmark_hostside/AndroidTest.xml
@@ -18,7 +18,9 @@
<option name="force-root" value="true" />
</target_preparer>
+ <target_preparer class="com.android.microdroid.test.preparer.DisableMicrodroidDebugPolicyPreparer" />
+
<test class="com.android.compatibility.common.tradefed.testtype.JarHostTest" >
<option name="jar" value="AVFHostTestCases.jar" />
</test>
-</configuration>
\ No newline at end of file
+</configuration>
diff --git a/tests/helper/Android.bp b/tests/helper/Android.bp
index f36a15d..6f07efd 100644
--- a/tests/helper/Android.bp
+++ b/tests/helper/Android.bp
@@ -21,3 +21,15 @@
],
sdk_version: "test_current",
}
+
+java_test_helper_library {
+ name: "MicrodroidTestPreparer",
+ srcs: ["src/java/com/android/microdroid/test/preparer/*.java"],
+ libs: ["tradefed"],
+ test_suites: [
+ "cts",
+ "general-tests",
+ ],
+ host_supported: true,
+ device_supported: false,
+}
diff --git a/tests/helper/src/java/com/android/microdroid/test/device/MicrodroidDeviceTestBase.java b/tests/helper/src/java/com/android/microdroid/test/device/MicrodroidDeviceTestBase.java
index cf17e5b..4e1d238 100644
--- a/tests/helper/src/java/com/android/microdroid/test/device/MicrodroidDeviceTestBase.java
+++ b/tests/helper/src/java/com/android/microdroid/test/device/MicrodroidDeviceTestBase.java
@@ -97,15 +97,6 @@
}
}
- public final boolean getDebugPolicyBoolean(String debugPolicy) throws IOException {
- Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
- UiAutomation uiAutomation = instrumentation.getUiAutomation();
- String debugPolicyFilePath = "/proc/device-tree" + debugPolicy;
- String cmd = "su root xxd -p " + debugPolicyFilePath;
- String dp = runInShell(TAG, uiAutomation, cmd).trim();
- return "00000001".equals(dp);
- }
-
private Context mCtx;
private boolean mProtectedVm;
diff --git a/tests/helper/src/java/com/android/microdroid/test/preparer/DisableMicrodroidDebugPolicyPreparer.java b/tests/helper/src/java/com/android/microdroid/test/preparer/DisableMicrodroidDebugPolicyPreparer.java
new file mode 100644
index 0000000..47be8b8
--- /dev/null
+++ b/tests/helper/src/java/com/android/microdroid/test/preparer/DisableMicrodroidDebugPolicyPreparer.java
@@ -0,0 +1,93 @@
+/*
+ * Copyright 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.microdroid.test.preparer;
+
+import com.android.tradefed.config.Option;
+import com.android.tradefed.config.OptionClass;
+import com.android.tradefed.device.DeviceNotAvailableException;
+import com.android.tradefed.device.ITestDevice;
+import com.android.tradefed.invoker.TestInformation;
+import com.android.tradefed.log.LogUtil.CLog;
+import com.android.tradefed.targetprep.BaseTargetPreparer;
+import com.android.tradefed.targetprep.BuildError;
+import com.android.tradefed.targetprep.TargetSetupError;
+
+/**
+ * Target preparer that disables microdroid's device policy for future VMs. This requires adb root
+ * for configuring the relevant sysprop.
+ *
+ * <p>Will restore back to original value on tear down. adb will be also unrooted if it wasn't root.
+ */
+@OptionClass(alias = "disable-microdroid-debug-policy-preparer")
+public final class DisableMicrodroidDebugPolicyPreparer extends BaseTargetPreparer {
+ private static final String SYSPROP_CUSTOM_DEBUG_POLICY_PATH =
+ "hypervisor.virtualizationmanager.debug_policy.path";
+
+ private boolean mWasRoot = false;
+ private String mOldDebugPolicyPath;
+
+ @Option(
+ name = "debug-policy-path",
+ description = "Debug policy path for sysprop " + SYSPROP_CUSTOM_DEBUG_POLICY_PATH)
+ private String mDebugPolicyPath = "/data/local/tmp/virt/stub_debug_policy.dts";
+
+ @Override
+ public void setUp(TestInformation testInfo)
+ throws TargetSetupError, BuildError, DeviceNotAvailableException {
+ ITestDevice device = testInfo.getDevice();
+ mWasRoot = device.isAdbRoot();
+ if (!mWasRoot && !device.enableAdbRoot()) {
+ throw new TargetSetupError("Failed to adb root device", device.getDeviceDescriptor());
+ }
+
+ try {
+ CLog.d("Bypassing micrdroid debug policy");
+ mOldDebugPolicyPath = device.getProperty(SYSPROP_CUSTOM_DEBUG_POLICY_PATH);
+ boolean result = device.setProperty(SYSPROP_CUSTOM_DEBUG_POLICY_PATH, mDebugPolicyPath);
+ if (!result) {
+ throw new TargetSetupError(
+ "Bypassing microdroid debug policy failed", device.getDeviceDescriptor());
+ }
+ } finally {
+ if (!mWasRoot) {
+ device.disableAdbRoot();
+ }
+ }
+ }
+
+ @Override
+ public void tearDown(TestInformation testInfo, Throwable e) throws DeviceNotAvailableException {
+ ITestDevice device = testInfo.getDevice();
+ if (e instanceof DeviceNotAvailableException) {
+ CLog.d("device not available: skipping teardown");
+ return;
+ }
+
+ if (!mWasRoot) {
+ device.enableAdbRoot();
+ }
+
+ CLog.d("Resetting microdroid debug policy");
+ device.setProperty(
+ SYSPROP_CUSTOM_DEBUG_POLICY_PATH,
+ mOldDebugPolicyPath == null ? "" : mOldDebugPolicyPath);
+
+ if (!mWasRoot) {
+ device.disableAdbRoot();
+ }
+ }
+}
diff --git a/tests/hostside/AndroidTest.xml b/tests/hostside/AndroidTest.xml
index 18728ad..429d910 100644
--- a/tests/hostside/AndroidTest.xml
+++ b/tests/hostside/AndroidTest.xml
@@ -25,6 +25,8 @@
<option name="force-root" value="false"/>
</target_preparer>
+ <target_preparer class="com.android.microdroid.test.preparer.DisableMicrodroidDebugPolicyPreparer" />
+
<test class="com.android.compatibility.common.tradefed.testtype.JarHostTest" >
<option name="jar" value="MicrodroidHostTestCases.jar" />
</test>
diff --git a/tests/testapk/AndroidTest.xml b/tests/testapk/AndroidTest.xml
index 929dd31..725a1e4 100644
--- a/tests/testapk/AndroidTest.xml
+++ b/tests/testapk/AndroidTest.xml
@@ -23,6 +23,7 @@
<option name="test-file-name" value="MicrodroidTestApp.apk" />
<option name="test-file-name" value="MicrodroidVmShareApp.apk" />
</target_preparer>
+ <target_preparer class="com.android.microdroid.test.preparer.DisableMicrodroidDebugPolicyPreparer" />
<test class="com.android.tradefed.testtype.AndroidJUnitTest" >
<option name="package" value="com.android.microdroid.test" />
<option name="runner" value="androidx.test.runner.AndroidJUnitRunner" />
diff --git a/tests/testapk/src/java/com/android/microdroid/test/MicrodroidTests.java b/tests/testapk/src/java/com/android/microdroid/test/MicrodroidTests.java
index 7044ae7..13738e5 100644
--- a/tests/testapk/src/java/com/android/microdroid/test/MicrodroidTests.java
+++ b/tests/testapk/src/java/com/android/microdroid/test/MicrodroidTests.java
@@ -1583,22 +1583,6 @@
}
}
- private boolean isConsoleOutputEnabledByDebugPolicy() {
- if (isUserBuild()) {
- Log.i(
- TAG,
- "Debug policy is inaccessible in user build. Assumes that console output is"
- + " disabled");
- return false;
- }
- try {
- return getDebugPolicyBoolean("/avf/guest/common/log");
- } catch (IOException e) {
- Log.w(TAG, "Fail to read debug policy. Assumes false", e);
- return false;
- }
- }
-
private boolean checkVmOutputIsRedirectedToLogcat(boolean debuggable) throws Exception {
String time =
LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));
@@ -1632,9 +1616,6 @@
@Test
public void outputIsRedirectedToLogcatIfNotCaptured() throws Exception {
assumeSupportedDevice();
- assumeFalse(
- "Debug policy would turn on console output. Perhaps userdebug build?",
- isConsoleOutputEnabledByDebugPolicy());
assertThat(checkVmOutputIsRedirectedToLogcat(true)).isTrue();
}
@@ -1642,9 +1623,6 @@
@Test
public void outputIsNotRedirectedToLogcatIfNotDebuggable() throws Exception {
assumeSupportedDevice();
- assumeFalse(
- "Debug policy would turn on console output. Perhaps userdebug build?",
- isConsoleOutputEnabledByDebugPolicy());
assertThat(checkVmOutputIsRedirectedToLogcat(false)).isFalse();
}