KVM: selftests: Add a macro to define a test with one vcpu
authorThomas Huth <thuth@redhat.com>
Thu, 8 Feb 2024 20:48:40 +0000 (21:48 +0100)
committerSean Christopherson <seanjc@google.com>
Wed, 28 Feb 2024 20:58:12 +0000 (20:58 +0000)
Most tests are currently not giving any proper output for the user
to see how much sub-tests have already been run, or whether new
sub-tests are part of a binary or not. So it would be good to
support TAP output in the KVM selftests. There is already a nice
framework for this in the kselftest_harness.h header which we can
use. But since we also need a vcpu in most KVM selftests, it also
makes sense to introduce our own wrapper around this which takes
care of creating a VM with one vcpu, so we don't have to repeat
this boilerplate in each and every test. Thus let's introduce
a KVM_ONE_VCPU_TEST() macro here which takes care of this.

Suggested-by: Sean Christopherson <seanjc@google.com>
Link: https://lore.kernel.org/all/Y2v+B3xxYKJSM%2FfH@google.com/
Signed-off-by: Thomas Huth <thuth@redhat.com>
Link: https://lore.kernel.org/r/20240208204844.119326-5-thuth@redhat.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
tools/testing/selftests/kvm/include/kvm_test_harness.h [new file with mode: 0644]

diff --git a/tools/testing/selftests/kvm/include/kvm_test_harness.h b/tools/testing/selftests/kvm/include/kvm_test_harness.h
new file mode 100644 (file)
index 0000000..8f7c685
--- /dev/null
@@ -0,0 +1,36 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Macros for defining a KVM test
+ *
+ * Copyright (C) 2022, Google LLC.
+ */
+
+#ifndef SELFTEST_KVM_TEST_HARNESS_H
+#define SELFTEST_KVM_TEST_HARNESS_H
+
+#include "kselftest_harness.h"
+
+#define KVM_ONE_VCPU_TEST_SUITE(name)                                  \
+       FIXTURE(name) {                                                 \
+               struct kvm_vcpu *vcpu;                                  \
+       };                                                              \
+                                                                       \
+       FIXTURE_SETUP(name) {                                           \
+               (void)vm_create_with_one_vcpu(&self->vcpu, NULL);       \
+       }                                                               \
+                                                                       \
+       FIXTURE_TEARDOWN(name) {                                        \
+               kvm_vm_free(self->vcpu->vm);                            \
+       }
+
+#define KVM_ONE_VCPU_TEST(suite, test, guestcode)                      \
+static void __suite##_##test(struct kvm_vcpu *vcpu);                   \
+                                                                       \
+TEST_F(suite, test)                                                    \
+{                                                                      \
+       vcpu_arch_set_entry_point(self->vcpu, guestcode);               \
+       __suite##_##test(self->vcpu);                                   \
+}                                                                      \
+static void __suite##_##test(struct kvm_vcpu *vcpu)
+
+#endif /* SELFTEST_KVM_TEST_HARNESS_H */