Run VirtualizationTestCases for presubmit builds

Bug: 192204736
Test: watch TH
Change-Id: Id4cd15b551c3cd8db20276401cfc95b1010f9db6
diff --git a/TEST_MAPPING b/TEST_MAPPING
index 5218abb..b805d03 100644
--- a/TEST_MAPPING
+++ b/TEST_MAPPING
@@ -5,12 +5,7 @@
     },
     {
       "name": "ComposHostTestCases"
-    }
-  ],
-  "postsubmit": [
-    // TODO(jiyong): promote this to presubmit. That currently doesn't work because
-    // this test is skipped for cf_x86_64_phone (not aosp_cf_x86_64_phone), but tradefed
-    // somehow thinks that the test wasn't executed at all and reports it as a failure.
+    },
     {
       "name": "VirtualizationTestCases"
     }
diff --git a/tests/common.cc b/tests/common.cc
index a9f0807..9602283 100644
--- a/tests/common.cc
+++ b/tests/common.cc
@@ -16,9 +16,28 @@
 
 #include "virt/VirtualizationTest.h"
 
+namespace {
+
+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; });
+}
+
+} // namespace
+
 namespace virt {
 
 void VirtualizationTest::SetUp() {
+    if (!isVmSupported()) {
+        GTEST_SKIP() << "Device doesn't support KVM.";
+    }
+
     mVirtualizationService = waitForService<IVirtualizationService>(
             String16("android.system.virtualizationservice"));
     ASSERT_NE(mVirtualizationService, nullptr);
diff --git a/tests/vsock_test.cc b/tests/vsock_test.cc
index c5643ec..a594e6d 100644
--- a/tests/vsock_test.cc
+++ b/tests/vsock_test.cc
@@ -48,17 +48,6 @@
 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; });
-}
-
 /** Returns true if the kernel supports Protected KVM. */
 bool isPkvmSupported() {
     unique_fd kvm_fd(open("/dev/kvm", O_NONBLOCK | O_CLOEXEC));
@@ -66,6 +55,10 @@
 }
 
 void runTest(sp<IVirtualizationService> virtualization_service, bool protected_vm) {
+    if (protected_vm && !isPkvmSupported()) {
+        GTEST_SKIP() << "Skipping as pKVM is not supported on this device.";
+    }
+
     binder::Status status;
 
     unique_fd server_fd(TEMP_FAILURE_RETRY(socket(AF_VSOCK, SOCK_STREAM, 0)));
@@ -117,20 +110,10 @@
 }
 
 TEST_F(VirtualizationTest, TestVsock) {
-    if (!isVmSupported()) {
-        GTEST_SKIP() << "Device doesn't support KVM.";
-    }
-
     runTest(mVirtualizationService, false);
 }
 
 TEST_F(VirtualizationTest, TestVsockProtected) {
-    if (!isVmSupported()) {
-        GTEST_SKIP() << "Device doesn't support KVM.";
-    } else if (!isPkvmSupported()) {
-        GTEST_SKIP() << "Skipping as pKVM is not supported on this device.";
-    }
-
     runTest(mVirtualizationService, true);
 }