Linux-kselftest Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH] lib: Convert test_uuid.c to KUnit
@ 2020-07-30 12:16 Arpitha Raghunandan
  2020-07-31  5:50 ` Brendan Higgins
  2020-08-01  5:34 ` Arpitha Raghunandan
  0 siblings, 2 replies; 5+ messages in thread
From: Arpitha Raghunandan @ 2020-07-30 12:16 UTC (permalink / raw)
  To: brendanhiggins, skhan, andriy.shevchenko
  Cc: Arpitha Raghunandan, kunit-dev, linux-kselftest, linux-kernel,
	linux-kernel-mentees

Converts test lib/test_uuid.c to KUnit

Signed-off-by: Arpitha Raghunandan <98.arpi@gmail.com>
---
 lib/Kconfig.debug                 |  7 +--
 lib/Makefile                      |  2 +-
 lib/{test_uuid.c => uuid_kunit.c} | 84 +++++++++----------------------
 3 files changed, 28 insertions(+), 65 deletions(-)
 rename lib/{test_uuid.c => uuid_kunit.c} (48%)

diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index f174f8887ae7..330c0d1de50b 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -2070,9 +2070,6 @@ config TEST_BITFIELD
 
 	  If unsure, say N.
 
-config TEST_UUID
-	tristate "Test functions located in the uuid module at runtime"
-
 config TEST_XARRAY
 	tristate "Test the XArray code at runtime"
 
@@ -2273,6 +2270,10 @@ config BITS_TEST
 
 	  If unsure, say N.
 
+config UUID_KUNIT_TEST
+	tristate "KUnit test for functions located in the uuid module at runtime"
+	depends on KUNIT
+
 config TEST_UDELAY
 	tristate "udelay test driver"
 	help
diff --git a/lib/Makefile b/lib/Makefile
index 032cc6c71a3a..62ef383c7563 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -81,7 +81,6 @@ obj-$(CONFIG_TEST_PRINTF) += test_printf.o
 obj-$(CONFIG_TEST_BITMAP) += test_bitmap.o
 obj-$(CONFIG_TEST_STRSCPY) += test_strscpy.o
 obj-$(CONFIG_TEST_BITFIELD) += test_bitfield.o
-obj-$(CONFIG_TEST_UUID) += test_uuid.o
 obj-$(CONFIG_TEST_XARRAY) += test_xarray.o
 obj-$(CONFIG_TEST_PARMAN) += test_parman.o
 obj-$(CONFIG_TEST_KMOD) += test_kmod.o
@@ -342,5 +341,6 @@ obj-$(CONFIG_PLDMFW) += pldmfw/
 obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.o
 obj-$(CONFIG_LINEAR_RANGES_TEST) += test_linear_ranges.o
 obj-$(CONFIG_BITS_TEST) += test_bits.o
+obj-$(CONFIG_UUID_KUNIT_TEST) += uuid_kunit.o
 
 obj-$(CONFIG_GENERIC_LIB_DEVMEM_IS_ALLOWED) += devmem_is_allowed.o
diff --git a/lib/test_uuid.c b/lib/uuid_kunit.c
similarity index 48%
rename from lib/test_uuid.c
rename to lib/uuid_kunit.c
index cd819c397dc7..f7f219ddecc2 100644
--- a/lib/test_uuid.c
+++ b/lib/uuid_kunit.c
@@ -3,6 +3,7 @@
  */
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
+#include <kunit/test.h>
 #include <linux/init.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
@@ -39,95 +40,56 @@ static const char * const test_uuid_wrong_data[] = {
 	"0cb4ddff-a545-4401-9d06-688af53e",	/* not enough data */
 };
 
-static unsigned total_tests __initdata;
-static unsigned failed_tests __initdata;
-
-static void __init test_uuid_failed(const char *prefix, bool wrong, bool be,
-				    const char *data, const char *actual)
-{
-	pr_err("%s test #%u %s %s data: '%s'\n",
-	       prefix,
-	       total_tests,
-	       wrong ? "passed on wrong" : "failed on",
-	       be ? "BE" : "LE",
-	       data);
-	if (actual && *actual)
-		pr_err("%s test #%u actual data: '%s'\n",
-		       prefix,
-		       total_tests,
-		       actual);
-	failed_tests++;
-}
-
-static void __init test_uuid_test(const struct test_uuid_data *data)
+static void test_uuid_test(struct kunit *test, const struct test_uuid_data *data)
 {
 	guid_t le;
 	uuid_t be;
 	char buf[48];
 
 	/* LE */
-	total_tests++;
-	if (guid_parse(data->uuid, &le))
-		test_uuid_failed("conversion", false, false, data->uuid, NULL);
-
-	total_tests++;
-	if (!guid_equal(&data->le, &le)) {
-		sprintf(buf, "%pUl", &le);
-		test_uuid_failed("cmp", false, false, data->uuid, buf);
-	}
+	KUNIT_EXPECT_EQ(test, 0, guid_parse(data->uuid, &le));
+	KUNIT_EXPECT_TRUE(test, guid_equal(&data->le, &le));
 
 	/* BE */
-	total_tests++;
-	if (uuid_parse(data->uuid, &be))
-		test_uuid_failed("conversion", false, true, data->uuid, NULL);
-
-	total_tests++;
-	if (!uuid_equal(&data->be, &be)) {
-		sprintf(buf, "%pUb", &be);
-		test_uuid_failed("cmp", false, true, data->uuid, buf);
-	}
+	KUNIT_EXPECT_EQ(test, 0, uuid_parse(data->uuid, &be));
+	KUNIT_EXPECT_TRUE(test, uuid_equal(&data->be, &be));
 }
 
-static void __init test_uuid_wrong(const char *data)
+static void test_uuid_wrong(struct kunit *test, const char *data)
 {
 	guid_t le;
 	uuid_t be;
 
 	/* LE */
-	total_tests++;
-	if (!guid_parse(data, &le))
-		test_uuid_failed("negative", true, false, data, NULL);
+	KUNIT_EXPECT_NE(test, 0, guid_parse(data, &le));
 
 	/* BE */
-	total_tests++;
-	if (!uuid_parse(data, &be))
-		test_uuid_failed("negative", true, true, data, NULL);
+	KUNIT_EXPECT_NE(test, 0, uuid_parse(data, &be));
 }
 
-static int __init test_uuid_init(void)
+static void test_uuid_init(struct kunit *test)
 {
 	unsigned int i;
 
 	for (i = 0; i < ARRAY_SIZE(test_uuid_test_data); i++)
-		test_uuid_test(&test_uuid_test_data[i]);
+		test_uuid_test(test, &test_uuid_test_data[i]);
 
 	for (i = 0; i < ARRAY_SIZE(test_uuid_wrong_data); i++)
-		test_uuid_wrong(test_uuid_wrong_data[i]);
+		test_uuid_wrong(test, test_uuid_wrong_data[i]);
+}
 
-	if (failed_tests == 0)
-		pr_info("all %u tests passed\n", total_tests);
-	else
-		pr_err("failed %u out of %u tests\n", failed_tests, total_tests);
+static struct kunit_case uuid_test_cases[] = {
+	KUNIT_CASE(test_uuid_init),
+	{}
+};
 
-	return failed_tests ? -EINVAL : 0;
-}
-module_init(test_uuid_init);
+static struct kunit_suite uuid_test_suite = {
+	.name = "uuid-kunit-test",
+	.test_cases = uuid_test_cases,
+};
+
+kunit_test_suite(uuid_test_suite);
 
-static void __exit test_uuid_exit(void)
-{
-	/* do nothing */
-}
-module_exit(test_uuid_exit);
 
 MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
 MODULE_LICENSE("Dual BSD/GPL");
-- 
2.25.1


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] lib: Convert test_uuid.c to KUnit
  2020-07-30 12:16 [PATCH] lib: Convert test_uuid.c to KUnit Arpitha Raghunandan
@ 2020-07-31  5:50 ` Brendan Higgins
  2020-07-31 10:02   ` Arpitha Raghunandan
  2020-08-01  5:34 ` Arpitha Raghunandan
  1 sibling, 1 reply; 5+ messages in thread
From: Brendan Higgins @ 2020-07-31  5:50 UTC (permalink / raw)
  To: Arpitha Raghunandan
  Cc: Shuah Khan, Andy Shevchenko, KUnit Development,
	open list:KERNEL SELFTEST FRAMEWORK, Linux Kernel Mailing List,
	linux-kernel-mentees

On Thu, Jul 30, 2020 at 5:18 AM Arpitha Raghunandan <98.arpi@gmail.com> wrote:
>
> Converts test lib/test_uuid.c to KUnit

Can you add some more detail to the commit message? Maybe link to
KUnit and say something about why this change is beneficial.

> Signed-off-by: Arpitha Raghunandan <98.arpi@gmail.com>

Change mostly looks good to me.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] lib: Convert test_uuid.c to KUnit
  2020-07-31  5:50 ` Brendan Higgins
@ 2020-07-31 10:02   ` Arpitha Raghunandan
  2020-07-31 20:53     ` Brendan Higgins
  0 siblings, 1 reply; 5+ messages in thread
From: Arpitha Raghunandan @ 2020-07-31 10:02 UTC (permalink / raw)
  To: Brendan Higgins
  Cc: Shuah Khan, Andy Shevchenko, KUnit Development,
	open list:KERNEL SELFTEST FRAMEWORK, Linux Kernel Mailing List,
	linux-kernel-mentees

On 31/07/20 11:20 am, Brendan Higgins wrote:
> On Thu, Jul 30, 2020 at 5:18 AM Arpitha Raghunandan <98.arpi@gmail.com> wrote:
>>
>> Converts test lib/test_uuid.c to KUnit
> 
> Can you add some more detail to the commit message? Maybe link to
> KUnit and say something about why this change is beneficial.
> 
Sure, I will make this change.
>> Signed-off-by: Arpitha Raghunandan <98.arpi@gmail.com>
> 
> Change mostly looks good to me.
> Thank you for the review.

On failure, the original test calls another function which prints a detailed error. I removed it when converting to KUnit. Is this required?

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] lib: Convert test_uuid.c to KUnit
  2020-07-31 10:02   ` Arpitha Raghunandan
@ 2020-07-31 20:53     ` Brendan Higgins
  0 siblings, 0 replies; 5+ messages in thread
From: Brendan Higgins @ 2020-07-31 20:53 UTC (permalink / raw)
  To: Arpitha Raghunandan
  Cc: Shuah Khan, Andy Shevchenko, KUnit Development,
	open list:KERNEL SELFTEST FRAMEWORK, Linux Kernel Mailing List,
	linux-kernel-mentees

On Fri, Jul 31, 2020 at 3:02 AM Arpitha Raghunandan <98.arpi@gmail.com> wrote:
>
> On 31/07/20 11:20 am, Brendan Higgins wrote:
> > On Thu, Jul 30, 2020 at 5:18 AM Arpitha Raghunandan <98.arpi@gmail.com> wrote:
> >>
> >> Converts test lib/test_uuid.c to KUnit
> >
> > Can you add some more detail to the commit message? Maybe link to
> > KUnit and say something about why this change is beneficial.
> >
> Sure, I will make this change.
> >> Signed-off-by: Arpitha Raghunandan <98.arpi@gmail.com>
> >
> > Change mostly looks good to me.
> > Thank you for the review.
>
> On failure, the original test calls another function which prints a detailed error. I removed it when converting to KUnit. Is this required?

I think that's probably Andy's call. Do you mind calling out the
segment of code in question?

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] lib: Convert test_uuid.c to KUnit
  2020-07-30 12:16 [PATCH] lib: Convert test_uuid.c to KUnit Arpitha Raghunandan
  2020-07-31  5:50 ` Brendan Higgins
@ 2020-08-01  5:34 ` Arpitha Raghunandan
  1 sibling, 0 replies; 5+ messages in thread
From: Arpitha Raghunandan @ 2020-08-01  5:34 UTC (permalink / raw)
  To: brendanhiggins, skhan, andriy.shevchenko
  Cc: kunit-dev, linux-kselftest, linux-kernel, linux-kernel-mentees

On 30/07/20 5:46 pm, Arpitha Raghunandan wrote:
> Converts test lib/test_uuid.c to KUnit
> 
> Signed-off-by: Arpitha Raghunandan <98.arpi@gmail.com>
> ---
>  lib/Kconfig.debug                 |  7 +--
>  lib/Makefile                      |  2 +-
>  lib/{test_uuid.c => uuid_kunit.c} | 84 +++++++++----------------------
>  3 files changed, 28 insertions(+), 65 deletions(-)
>  rename lib/{test_uuid.c => uuid_kunit.c} (48%)
> 
> diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
> index f174f8887ae7..330c0d1de50b 100644
> --- a/lib/Kconfig.debug
> +++ b/lib/Kconfig.debug
> @@ -2070,9 +2070,6 @@ config TEST_BITFIELD
>  
>  	  If unsure, say N.
>  
> -config TEST_UUID
> -	tristate "Test functions located in the uuid module at runtime"
> -
>  config TEST_XARRAY
>  	tristate "Test the XArray code at runtime"
>  
> @@ -2273,6 +2270,10 @@ config BITS_TEST
>  
>  	  If unsure, say N.
>  
> +config UUID_KUNIT_TEST
> +	tristate "KUnit test for functions located in the uuid module at runtime"
> +	depends on KUNIT
> +
>  config TEST_UDELAY
>  	tristate "udelay test driver"
>  	help
> diff --git a/lib/Makefile b/lib/Makefile
> index 032cc6c71a3a..62ef383c7563 100644
> --- a/lib/Makefile
> +++ b/lib/Makefile
> @@ -81,7 +81,6 @@ obj-$(CONFIG_TEST_PRINTF) += test_printf.o
>  obj-$(CONFIG_TEST_BITMAP) += test_bitmap.o
>  obj-$(CONFIG_TEST_STRSCPY) += test_strscpy.o
>  obj-$(CONFIG_TEST_BITFIELD) += test_bitfield.o
> -obj-$(CONFIG_TEST_UUID) += test_uuid.o
>  obj-$(CONFIG_TEST_XARRAY) += test_xarray.o
>  obj-$(CONFIG_TEST_PARMAN) += test_parman.o
>  obj-$(CONFIG_TEST_KMOD) += test_kmod.o
> @@ -342,5 +341,6 @@ obj-$(CONFIG_PLDMFW) += pldmfw/
>  obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.o
>  obj-$(CONFIG_LINEAR_RANGES_TEST) += test_linear_ranges.o
>  obj-$(CONFIG_BITS_TEST) += test_bits.o
> +obj-$(CONFIG_UUID_KUNIT_TEST) += uuid_kunit.o
>  
>  obj-$(CONFIG_GENERIC_LIB_DEVMEM_IS_ALLOWED) += devmem_is_allowed.o
> diff --git a/lib/test_uuid.c b/lib/uuid_kunit.c
> similarity index 48%
> rename from lib/test_uuid.c
> rename to lib/uuid_kunit.c
> index cd819c397dc7..f7f219ddecc2 100644
> --- a/lib/test_uuid.c
> +++ b/lib/uuid_kunit.c
> @@ -3,6 +3,7 @@
>   */
>  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
>  
> +#include <kunit/test.h>
>  #include <linux/init.h>
>  #include <linux/kernel.h>
>  #include <linux/module.h>
> @@ -39,95 +40,56 @@ static const char * const test_uuid_wrong_data[] = {
>  	"0cb4ddff-a545-4401-9d06-688af53e",	/* not enough data */
>  };
>  
> -static unsigned total_tests __initdata;
> -static unsigned failed_tests __initdata;
> -
> -static void __init test_uuid_failed(const char *prefix, bool wrong, bool be,
> -				    const char *data, const char *actual)
> -{
> -	pr_err("%s test #%u %s %s data: '%s'\n",
> -	       prefix,
> -	       total_tests,
> -	       wrong ? "passed on wrong" : "failed on",
> -	       be ? "BE" : "LE",
> -	       data);
> -	if (actual && *actual)
> -		pr_err("%s test #%u actual data: '%s'\n",
> -		       prefix,
> -		       total_tests,
> -		       actual);
> -	failed_tests++;
> -}
> -


I have removed the above test_uuid_failed function while converting to KUnit, as mentioned earlier. Is this required?


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, back to index

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-30 12:16 [PATCH] lib: Convert test_uuid.c to KUnit Arpitha Raghunandan
2020-07-31  5:50 ` Brendan Higgins
2020-07-31 10:02   ` Arpitha Raghunandan
2020-07-31 20:53     ` Brendan Higgins
2020-08-01  5:34 ` Arpitha Raghunandan

Linux-kselftest Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-kselftest/0 linux-kselftest/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-kselftest linux-kselftest/ https://lore.kernel.org/linux-kselftest \
		linux-kselftest@vger.kernel.org
	public-inbox-index linux-kselftest

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-kselftest


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git