All of lore.kernel.org
 help / color / mirror / Atom feed
From: Rae Moar <rmoar@google.com>
To: shuah@kernel.org, davidgow@google.com, dlatypov@google.com,
	brendan.higgins@linux.dev
Cc: linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com,
	linux-kernel@vger.kernel.org, keescook@chromium.org,
	linux-hardening@vger.kernel.org, jstultz@google.com,
	tglx@linutronix.de, sboyd@kernel.org, Rae Moar <rmoar@google.com>
Subject: [RFC v1 2/6] kunit: Add speed attribute
Date: Sat, 10 Jun 2023 00:51:45 +0000	[thread overview]
Message-ID: <20230610005149.1145665-3-rmoar@google.com> (raw)
In-Reply-To: <20230610005149.1145665-1-rmoar@google.com>

Add speed attribute to the test attribute API. This attribute will allow
users to mark tests with a category of speed.

Currently the categories of speed proposed are: fast, normal, slow, and
very_slow. These are outlined in the enum kunit_speed. Note the speed
attribute can also be left as unset and then, will act as the default which
is "normal", during filtering.

Note speed is intended to be marked based on relative speeds rather than
quantitative speeds of KUnit tests. This is because tests may run on
various architectures at different speeds.

Add the macro KUNIT_CASE_SLOW to set a test as slow, as this is likely a
common use of the attributes API.

Add an example of marking a slow test to kunit-example-test.c.

Signed-off-by: Rae Moar <rmoar@google.com>
---
 include/kunit/test.h           | 31 ++++++++++++++++++++++-
 lib/kunit/attributes.c         | 45 +++++++++++++++++++++++++++++++++-
 lib/kunit/kunit-example-test.c |  9 +++++++
 3 files changed, 83 insertions(+), 2 deletions(-)

diff --git a/include/kunit/test.h b/include/kunit/test.h
index 1fc9155988e9..3d684723ae57 100644
--- a/include/kunit/test.h
+++ b/include/kunit/test.h
@@ -63,8 +63,26 @@ enum kunit_status {
 	KUNIT_SKIPPED,
 };
 
+/* Attribute struct/enum definitions */
+
+/*
+ * Speed Attribute is stored as an enum and separated into categories of
+ * speed: very_slowm, slow, normal, and fast. These speeds are relative
+ * to other KUnit tests.
+ */
+enum kunit_speed {
+	KUNIT_SPEED_UNSET,
+	KUNIT_SPEED_VERY_SLOW,
+	KUNIT_SPEED_SLOW,
+	KUNIT_SPEED_NORMAL,
+	KUNIT_SPEED_FAST,
+	KUNIT_SPEED_MAX = KUNIT_SPEED_FAST,
+};
+
 /* Holds attributes for each test case and suite */
-struct kunit_attributes {};
+struct kunit_attributes {
+	enum kunit_speed speed;
+};
 
 /**
  * struct kunit_case - represents an individual test case.
@@ -150,6 +168,17 @@ static inline char *kunit_status_to_ok_not_ok(enum kunit_status status)
 		{ .run_case = test_name, .name = #test_name,	\
 		  .attr = attributes }
 
+/**
+ * KUNIT_CASE_SLOW - A helper for creating a &struct kunit_case
+ * with the slow attribute
+ *
+ * @test_name: a reference to a test case function.
+ */
+
+#define KUNIT_CASE_SLOW(test_name)			\
+		{ .run_case = test_name, .name = #test_name,	\
+		  .attr.speed = KUNIT_SPEED_SLOW }
+
 /**
  * KUNIT_CASE_PARAM - A helper for creation a parameterized &struct kunit_case
  *
diff --git a/lib/kunit/attributes.c b/lib/kunit/attributes.c
index 0ea641be795f..e17889f94693 100644
--- a/lib/kunit/attributes.c
+++ b/lib/kunit/attributes.c
@@ -28,9 +28,52 @@ struct kunit_attr {
 	void *attr_default;
 };
 
+/* String Lists for enum Attributes */
+
+static const char * const speed_str_list[] = {"unset", "very_slow", "slow", "normal", "fast"};
+
+/* To String Methods */
+
+static const char *attr_enum_to_string(void *attr, const char * const str_list[], bool *to_free)
+{
+	long val = (long)attr;
+
+	*to_free = false;
+	if (!val)
+		return NULL;
+	return str_list[val];
+}
+
+static const char *attr_speed_to_string(void *attr, bool *to_free)
+{
+	return attr_enum_to_string(attr, speed_str_list, to_free);
+}
+
+/* Get Attribute Methods */
+
+static void *attr_speed_get(void *test_or_suite, bool is_test)
+{
+	struct kunit_suite *suite = is_test ? NULL : test_or_suite;
+	struct kunit_case *test = is_test ? test_or_suite : NULL;
+
+	if (test)
+		return ((void *) test->attr.speed);
+	else
+		return ((void *) suite->attr.speed);
+}
+
+/* Attribute Struct Definitions */
+
+static const struct kunit_attr speed_attr = {
+	.name = "speed",
+	.get_attr = attr_speed_get,
+	.to_string = attr_speed_to_string,
+	.attr_default = (void *)KUNIT_SPEED_NORMAL,
+};
+
 /* List of all Test Attributes */
 
-static struct kunit_attr kunit_attr_list[1] = {};
+static struct kunit_attr kunit_attr_list[1] = {speed_attr};
 
 /* Helper Functions to Access Attributes */
 
diff --git a/lib/kunit/kunit-example-test.c b/lib/kunit/kunit-example-test.c
index b69b689ea850..01a769f35e1d 100644
--- a/lib/kunit/kunit-example-test.c
+++ b/lib/kunit/kunit-example-test.c
@@ -220,6 +220,14 @@ static void example_params_test(struct kunit *test)
 	KUNIT_EXPECT_EQ(test, param->value % param->value, 0);
 }
 
+/*
+ * This test should always pass. Can be used to practice filtering attributes.
+ */
+static void example_slow_test(struct kunit *test)
+{
+	KUNIT_EXPECT_EQ(test, 1 + 1, 2);
+}
+
 /*
  * Here we make a list of all the test cases we want to add to the test suite
  * below.
@@ -237,6 +245,7 @@ static struct kunit_case example_test_cases[] = {
 	KUNIT_CASE(example_all_expect_macros_test),
 	KUNIT_CASE(example_static_stub_test),
 	KUNIT_CASE_PARAM(example_params_test, example_gen_params),
+	KUNIT_CASE_SLOW(example_slow_test),
 	{}
 };
 
-- 
2.41.0.162.gfafddb0af9-goog


  parent reply	other threads:[~2023-06-10  0:52 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-10  0:51 [RFC v1 0/6] kunit: Add test attributes API Rae Moar
2023-06-10  0:51 ` [RFC v1 1/6] kunit: Add test attributes API structure Rae Moar
2023-06-10  8:29   ` David Gow
2023-06-13 20:36     ` Rae Moar
2023-06-10  0:51 ` Rae Moar [this message]
2023-06-10  3:13   ` [RFC v1 2/6] kunit: Add speed attribute kernel test robot
2023-06-10  8:29   ` David Gow
2023-06-13 20:37     ` Rae Moar
2023-06-10  0:51 ` [RFC v1 3/6] kunit: Add ability to filter attributes Rae Moar
2023-06-10  3:57   ` kernel test robot
2023-06-10  8:29   ` David Gow
2023-06-13 20:42     ` Rae Moar
2023-06-13 20:26   ` Kees Cook
2023-06-13 20:58     ` Rae Moar
2023-06-10  0:51 ` [RFC v1 4/6] kunit: tool: Add command line interface to filter and report attributes Rae Moar
2023-06-10  8:29   ` David Gow
2023-06-13 20:44     ` Rae Moar
2023-06-10  0:51 ` [RFC v1 5/6] kunit: memcpy: Mark tests as slow using test attributes Rae Moar
2023-06-10  8:29   ` David Gow
2023-06-13 20:44     ` Rae Moar
2023-06-10  0:51 ` [RFC v1 6/6] kunit: time: Mark test " Rae Moar
2023-06-10  8:29 ` [RFC v1 0/6] kunit: Add test attributes API David Gow
2023-06-13 20:34   ` Rae Moar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230610005149.1145665-3-rmoar@google.com \
    --to=rmoar@google.com \
    --cc=brendan.higgins@linux.dev \
    --cc=davidgow@google.com \
    --cc=dlatypov@google.com \
    --cc=jstultz@google.com \
    --cc=keescook@chromium.org \
    --cc=kunit-dev@googlegroups.com \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=sboyd@kernel.org \
    --cc=shuah@kernel.org \
    --cc=tglx@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.