Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2806155rwd; Fri, 9 Jun 2023 17:59:36 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6eTWA1742irsIlIAGwgVt29eqAVBG84Q1U14C4SU51ee6xXY2Mp6tFUbfN4GQ6kdn8WY7o X-Received: by 2002:a05:6a20:3d17:b0:10c:41a6:ac1f with SMTP id y23-20020a056a203d1700b0010c41a6ac1fmr2891189pzi.16.1686358776582; Fri, 09 Jun 2023 17:59:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686358776; cv=none; d=google.com; s=arc-20160816; b=uu2sX5Fja2q6vYCz4Hc7cr2Il14n6hTLqfL2gftzeBMdWBGmL2e8xL2H/coMsD9oXS 8dAUfmAyB3WpnVq1CsIjIMo2f1K1HzKFYviGGWgfeCVOZ4MWdKKFTruRdhnthdUHea6n /cX4/g8GmLpC+01usarO6h7z/D+vLA1nrYZoUX3mF+PIjq4sfwWimbcEpm5AxtevJ8gY nhJJjp3fiWqZVSCgHn1NSlvweCdsLl3pm/7WlHhFwflwqmuHEzBBW+D++llJ/PNJrQaZ 6P1fBRiK7K++IGdmj1IU4fyRzY8BbNrqhwVx9SujBgdEhWbOm1cLkUvhZ7Fuzn0U1ZQR gpjw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=kg21mZWTzPOwmlFSsLi8itlal2bzL2xDdn1UhMA3w20=; b=F3Oz7ARDRJrS0x4yuURxCBFMMqjgGyQKdLtQGJcoNWe3h3HwEDIf8NDb6vLkVD7I9r PtiJqfRtSs6mwmhFBVbEEBixiA6OB0UOnQjyA0Zlx1dBr6AKTbvJLi/pL6GhEM7FUDQz mqTgTmYGjGgf82FRESTdTgkSe2a1rKM0Ap/lkzISvZCOlhQkQxK8ljSeo0CTRw7Bgsf+ 4107RdlZgy/G5siyDb544d5EHxzklwZJlCbRBeoDufah5gUcIllEWdcdvRMZCVBLc3ur 1fg7WH0D2e0SPe2edYYKvJlzwdmuttc3vTHb+R34MjsVmLPp1h41NTYLrETG8YdaCDGl ZQ2w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=WswYlHq6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o28-20020a63731c000000b00534e67fd867si898706pgc.62.2023.06.09.17.59.24; Fri, 09 Jun 2023 17:59:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=WswYlHq6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232771AbjFJAwU (ORCPT + 99 others); Fri, 9 Jun 2023 20:52:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52272 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230418AbjFJAwN (ORCPT ); Fri, 9 Jun 2023 20:52:13 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 55BB71BE4 for ; Fri, 9 Jun 2023 17:52:11 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-56cd8338406so19061487b3.0 for ; Fri, 09 Jun 2023 17:52:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686358330; x=1688950330; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=kg21mZWTzPOwmlFSsLi8itlal2bzL2xDdn1UhMA3w20=; b=WswYlHq6/ZHkfHrmbeb7arHsRL5XXKC8w3ETG7ZaFzcMXt+WpH/QTKK+jz6PoG+QBq uSLDTqnyS2MGajKMA2iGh6RvicQOPQNCY9hKFYLRHors5KSEWpXfWHeYr5OioTJ9AZaR g0DySrOCSRUfojMwgtpCy9mXPqIxoJEepKcqLZhGTnqA3PQCrSwPR5Hsc5RogDCN6I7w l+YR05Q6aB93INFQnuCul5Nrk+j1ZgV0B8doglxFpMexoWkNgWPW74Kq2jJ5bEy+eIh7 b6O8jeGG3jRv3NtDrrmAR7MMIAZEBNe5HmVSmu2BiBOyacZdvyi+Qyfe7vjBpuIrhgnd Pl8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686358330; x=1688950330; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=kg21mZWTzPOwmlFSsLi8itlal2bzL2xDdn1UhMA3w20=; b=L0e0ERMAmYy2wxHXzy1Zyto9YA3pBl+UHXyxD+LlaX4Bdp+ftgte8b08UX1rugajhh QuLpcU9uqfDlkOfy68uvkxevta7x1voaLXTprct9SCvYNLRfmAliW5ZDKeFMYnFMnGeY Rrazex+2XFI/GzY8pixfifZpJ+U2Ym+EQ+pi7bTMVvPXTwXciHj2FMIywhIRD+pnN9+j tWQlip6BgRLNyYdWZVgHqJ5wuHbCqPm5TMCiHZb7l4IfJ3OBZto61nuKcMJ2zFACou0m MhoBKQp8bqUpyjsWiZ0x8Oxka0tHp7hv+f3SfCIvN/kIHUIoPrtvLbOCwi78wPK9sYnO /MHw== X-Gm-Message-State: AC+VfDw/P1EoIIMgNtr96oSK3Shv3ohDIEoWCn71DZ12ft3u/U9erqRx ZegzNraBfhy3ep3BfDIfPwgbg+1M3w== X-Received: from rmoar-specialist.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:45d3]) (user=rmoar job=sendgmr) by 2002:a05:690c:c01:b0:565:d5f6:a390 with SMTP id cl1-20020a05690c0c0100b00565d5f6a390mr2450767ywb.2.1686358330655; Fri, 09 Jun 2023 17:52:10 -0700 (PDT) Date: Sat, 10 Jun 2023 00:51:45 +0000 In-Reply-To: <20230610005149.1145665-1-rmoar@google.com> Mime-Version: 1.0 References: <20230610005149.1145665-1-rmoar@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Message-ID: <20230610005149.1145665-3-rmoar@google.com> Subject: [RFC v1 2/6] kunit: Add speed attribute From: Rae Moar 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 Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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