Skip tests instead of failing when running on unsupported devices

... in preparation for running tests in presubmit builds

Bug: 181615964
Test: watch TH

Change-Id: Id2544a3324efd09521b6a60f392e9bba200886b9
diff --git a/tests/AndroidTest.xml b/tests/AndroidTest.xml
index f170f48..b56c0e8 100644
--- a/tests/AndroidTest.xml
+++ b/tests/AndroidTest.xml
@@ -19,19 +19,6 @@
       a test-only permission, run it without selinux -->
     <target_preparer class="com.android.tradefed.targetprep.DisableSELinuxTargetPreparer"/>
 
-    <!-- Basic checks that the device has all the prerequisites. -->
-    <target_preparer class="com.android.tradefed.targetprep.RunCommandTargetPreparer">
-        <option name="throw-if-cmd-fail" value="true" />
-        <!-- Kernel has KVM enabled. -->
-        <option name="run-command" value="ls /dev/kvm" />
-        <!-- Kernel has vhost-vsock enabled. -->
-        <option name="run-command" value="ls /dev/vhost-vsock" />
-        <!-- CrosVM is installed. -->
-        <option name="run-command" value="ls /apex/com.android.virt/bin/crosvm" />
-        <!-- VirtualizationService is installed. -->
-        <option name="run-command" value="ls /apex/com.android.virt/bin/virtualizationservice" />
-    </target_preparer>
-
     <!-- Push test binaries to the device. -->
     <target_preparer class="com.android.tradefed.targetprep.PushFilePreparer">
         <option name="cleanup" value="true" />
diff --git a/tests/vsock_test.cc b/tests/vsock_test.cc
index 84827d8..923c1ef 100644
--- a/tests/vsock_test.cc
+++ b/tests/vsock_test.cc
@@ -20,6 +20,8 @@
 // Needs to be included after sys/socket.h
 #include <linux/vm_sockets.h>
 
+#include <algorithm>
+#include <array>
 #include <iostream>
 #include <optional>
 
@@ -41,7 +43,22 @@
 static constexpr const char kVmParams[] = "rdinit=/bin/init bin/vsock_client 2 45678 HelloWorld";
 static constexpr const char kTestMessage[] = "HelloWorld";
 
+bool isVmSupported() {
+    const std::array<const char *, 4> needed_files = {
+            "/dev/kvm",
+            "/dev/vhost-vsock",
+            "/apex/com.android.virt/bin/crosvm",
+            "/apex/com.android.virt/bin/virtualizationservice",
+    };
+    return std::all_of(needed_files.begin(), needed_files.end(),
+                       [](const char *file) { return access(file, F_OK) == 0; });
+}
+
 TEST_F(VirtualizationTest, TestVsock) {
+    if (!isVmSupported()) {
+        GTEST_SKIP() << "Device doesn't support VM.";
+    }
+
     binder::Status status;
 
     unique_fd server_fd(TEMP_FAILURE_RETRY(socket(AF_VSOCK, SOCK_STREAM, 0)));