Skip to contentSkip to conversation

lib: Convert UUID runtime test to KUnit

PATCH Superseded André Almeida 4 participants 7 messages Jun 10, 2021 – Jun 14, 2021

Subsystems (2)

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v3 cover lib: Convert UUID runtime test to KUnit

Hi,

This patch converts existing UUID runtime test to use KUnit framework.

Below, there's a comparison between the old output format and the new one. Keep in mind that even if KUnit seems very verbose, this is the corner case where _every_ test has failed.

* This is how the current output looks like in success:

test_uuid: all 18 tests passed

* And when it fails:

test_uuid: conversion test #1 failed on LE data: 'c33f4995-3701-450e-9fbf-206a2e98e576'
test_uuid: cmp test #2 failed on LE data: 'c33f4995-3701-450e-9fbf-206a2e98e576'
test_uuid: cmp test #2 actual data: 'c33f4995-3701-450e-9fbf-206a2e98e576'
test_uuid: conversion test #3 failed on BE data: 'c33f4995-3701-450e-9fbf-206a2e98e576'
test_uuid: cmp test #4 failed on BE data: 'c33f4995-3701-450e-9fbf-206a2e98e576'
test_uuid: cmp test #4 actual data: 'c33f4995-3701-450e-9fbf-206a2e98e576'
test_uuid: conversion test #5 failed on LE data: '64b4371c-77c1-48f9-8221-29f054fc023b'
test_uuid: cmp test #6 failed on LE data: '64b4371c-77c1-48f9-8221-29f054fc023b'
test_uuid: cmp test #6 actual data: '64b4371c-77c1-48f9-8221-29f054fc023b'
test_uuid: conversion test #7 failed on BE data: '64b4371c-77c1-48f9-8221-29f054fc023b'
test_uuid: cmp test #8 failed on BE data: '64b4371c-77c1-48f9-8221-29f054fc023b'
test_uuid: cmp test #8 actual data: '64b4371c-77c1-48f9-8221-29f054fc023b'
test_uuid: conversion test #9 failed on LE data: '0cb4ddff-a545-4401-9d06-688af53e7f84'
test_uuid: cmp test #10 failed on LE data: '0cb4ddff-a545-4401-9d06-688af53e7f84'
test_uuid: cmp test #10 actual data: '0cb4ddff-a545-4401-9d06-688af53e7f84'
test_uuid: conversion test #11 failed on BE data: '0cb4ddff-a545-4401-9d06-688af53e7f84'
test_uuid: cmp test #12 failed on BE data: '0cb4ddff-a545-4401-9d06-688af53e7f84'
test_uuid: cmp test #12 actual data: '0cb4ddff-a545-4401-9d06-688af53e7f84'
test_uuid: negative test #13 passed on wrong LE data: 'c33f4995-3701-450e-9fbf206a2e98e576 '
test_uuid: negative test #14 passed on wrong BE data: 'c33f4995-3701-450e-9fbf206a2e98e576 '
test_uuid: negative test #15 passed on wrong LE data: '64b4371c-77c1-48f9-8221-29f054XX023b'
test_uuid: negative test #16 passed on wrong BE data: '64b4371c-77c1-48f9-8221-29f054XX023b'
test_uuid: negative test #17 passed on wrong LE data: '0cb4ddff-a545-4401-9d06-688af53e'
test_uuid: negative test #18 passed on wrong BE data: '0cb4ddff-a545-4401-9d06-688af53e'
test_uuid: failed 18 out of 18 tests

* Now, here's how it looks like with KUnit:

======== [PASSED] uuid ========
[PASSED] uuid_correct_be
[PASSED] uuid_correct_le
[PASSED] uuid_wrong_be
[PASSED] uuid_wrong_le

* And if every test fail with KUnit:

======== [FAILED] uuid ========
[FAILED] uuid_correct_be

      # uuid_correct_be: ASSERTION FAILED at lib/test_uuid.c:57
      Expected uuid_parse(data->uuid, &be) == 1, but
          uuid_parse(data->uuid, &be) == 0

failed to parse 'c33f4995-3701-450e-9fbf-206a2e98e576'

      # uuid_correct_be: not ok 1 - c33f4995-3701-450e-9fbf-206a2e98e576
      # uuid_correct_be: ASSERTION FAILED at lib/test_uuid.c:57
      Expected uuid_parse(data->uuid, &be) == 1, but
          uuid_parse(data->uuid, &be) == 0

failed to parse '64b4371c-77c1-48f9-8221-29f054fc023b'

      # uuid_correct_be: not ok 2 - 64b4371c-77c1-48f9-8221-29f054fc023b
      # uuid_correct_be: ASSERTION FAILED at lib/test_uuid.c:57
      Expected uuid_parse(data->uuid, &be) == 1, but
          uuid_parse(data->uuid, &be) == 0

failed to parse '0cb4ddff-a545-4401-9d06-688af53e7f84'
# uuid_correct_be: not ok 3 - 0cb4ddff-a545-4401-9d06-688af53e7f84 not ok 1 - uuid_correct_be

[FAILED] uuid_correct_le

      # uuid_correct_le: ASSERTION FAILED at lib/test_uuid.c:46
      Expected guid_parse(data->uuid, &le) == 1, but
          guid_parse(data->uuid, &le) == 0

failed to parse 'c33f4995-3701-450e-9fbf-206a2e98e576'

      # uuid_correct_le: not ok 1 - c33f4995-3701-450e-9fbf-206a2e98e576
      # uuid_correct_le: ASSERTION FAILED at lib/test_uuid.c:46
      Expected guid_parse(data->uuid, &le) == 1, but
          guid_parse(data->uuid, &le) == 0

failed to parse '64b4371c-77c1-48f9-8221-29f054fc023b'

      # uuid_correct_le: not ok 2 - 64b4371c-77c1-48f9-8221-29f054fc023b
      # uuid_correct_le: ASSERTION FAILED at lib/test_uuid.c:46
      Expected guid_parse(data->uuid, &le) == 1, but
          guid_parse(data->uuid, &le) == 0

failed to parse '0cb4ddff-a545-4401-9d06-688af53e7f84'
# uuid_correct_le: not ok 3 - 0cb4ddff-a545-4401-9d06-688af53e7f84 not ok 2 - uuid_correct_le

[FAILED] uuid_wrong_be

      # uuid_wrong_be: ASSERTION FAILED at lib/test_uuid.c:77
      Expected uuid_parse(*data, &be) == 0, but
          uuid_parse(*data, &be) == -22

parsing of 'c33f4995-3701-450e-9fbf206a2e98e576 ' should've failed

      # uuid_wrong_be: not ok 1 - c33f4995-3701-450e-9fbf206a2e98e576
      # uuid_wrong_be: ASSERTION FAILED at lib/test_uuid.c:77
      Expected uuid_parse(*data, &be) == 0, but
          uuid_parse(*data, &be) == -22

parsing of '64b4371c-77c1-48f9-8221-29f054XX023b' should've failed

      # uuid_wrong_be: not ok 2 - 64b4371c-77c1-48f9-8221-29f054XX023b
      # uuid_wrong_be: ASSERTION FAILED at lib/test_uuid.c:77
      Expected uuid_parse(*data, &be) == 0, but
          uuid_parse(*data, &be) == -22

parsing of '0cb4ddff-a545-4401-9d06-688af53e' should've failed
# uuid_wrong_be: not ok 3 - 0cb4ddff-a545-4401-9d06-688af53e not ok 3 - uuid_wrong_be

[FAILED] uuid_wrong_le

      # uuid_wrong_le: ASSERTION FAILED at lib/test_uuid.c:68
      Expected guid_parse(*data, &le) == 0, but
          guid_parse(*data, &le) == -22

parsing of 'c33f4995-3701-450e-9fbf206a2e98e576 ' should've failed

      # uuid_wrong_le: not ok 1 - c33f4995-3701-450e-9fbf206a2e98e576
      # uuid_wrong_le: ASSERTION FAILED at lib/test_uuid.c:68
      Expected guid_parse(*data, &le) == 0, but
          guid_parse(*data, &le) == -22

parsing of '64b4371c-77c1-48f9-8221-29f054XX023b' should've failed

      # uuid_wrong_le: not ok 2 - 64b4371c-77c1-48f9-8221-29f054XX023b
      # uuid_wrong_le: ASSERTION FAILED at lib/test_uuid.c:68
      Expected guid_parse(*data, &le) == 0, but
          guid_parse(*data, &le) == -22

parsing of '0cb4ddff-a545-4401-9d06-688af53e' should've failed
# uuid_wrong_le: not ok 3 - 0cb4ddff-a545-4401-9d06-688af53e not ok 4 - uuid_wrong_le

Changes from v2:
- Clarify in commit message the new test cases setup
v2: https://lore.kernel.org/lkml/[email protected]/

Changes from v1:
- Test suite name: uuid_test -> uuid
- Config name: TEST_UUID -> UUID_KUNIT_TEST
- Config entry in the Kconfig file left where it is
- Converted tests to use _MSG variant
v1: https://lore.kernel.org/lkml/[email protected]/

André Almeida (1):
lib: Convert UUID runtime test to KUnit

 lib/Kconfig.debug |  11 +++-
 lib/Makefile      |   2 +-
 lib/test_uuid.c   | 137 +++++++++++++++++++---------------------------
 3 files changed, 67 insertions(+), 83 deletions(-)
Signature · 1 line
2.31.1
#1 lore raw

PATCH v3 1/1 lib: Convert UUID runtime test to KUnit

Remove custom functions for testing and use KUnit framework. Keep the tested functions and test data the same.

Current test threat (g/u)uid_parse and (g/u)uid_equal as different test cases. Make both functions being part of the same test case, given the dependency regarding their results. This reduces the tests cases from 6 cases to 4, while keeping the test coverage the same. Given that we have 3 strings for each test case, current test output notifies 18 tests results, and the KUnit output announces 12 results.

---
 lib/Kconfig.debug |  11 +++-
 lib/Makefile      |   2 +-
 lib/test_uuid.c   | 137 +++++++++++++++++++---------------------------
 3 files changed, 67 insertions(+), 83 deletions(-)
lib/Kconfig.debug +9 -2
index 678c13967580..1d879197f303 100644
@@ -2188,8 +2188,15 @@ config TEST_BITMAP
 
 	  If unsure, say N.
 
-config TEST_UUID
-	tristate "Test functions located in the uuid module at runtime"
+config UUID_KUNIT_TEST
+	tristate "Unit test for UUID" if !KUNIT_ALL_TESTS
+	depends on KUNIT
+	default KUNIT_ALL_TESTS
+	help
+	  This builds the UUID unit test.
+	  Tests parsing functions for UUID/GUID strings.
+
+	  If unsure, say N.
 
 config TEST_XARRAY
 	tristate "Test the XArray code at runtime"
lib/Makefile +1 -1
index 2cc359ec1fdd..cc19048961c0 100644
@@ -85,7 +85,6 @@ obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_key_base.o
 obj-$(CONFIG_TEST_PRINTF) += test_printf.o
 obj-$(CONFIG_TEST_BITMAP) += test_bitmap.o
 obj-$(CONFIG_TEST_STRSCPY) += test_strscpy.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
@@ -354,5 +353,6 @@ 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_CMDLINE_KUNIT_TEST) += cmdline_kunit.o
+obj-$(CONFIG_UUID_KUNIT_TEST) += test_uuid.o
 
 obj-$(CONFIG_GENERIC_LIB_DEVMEM_IS_ALLOWED) += devmem_is_allowed.o
lib/test_uuid.c +57 -80

179 lines not on this page · raw message

Signature · 1 line
2.32.0
#2 lore raw

lib/Kconfig.debug config TEST_BITMAP · line 2196

Quoted 6 lines
lib/Kconfig.debug +6 -0
+config UUID_KUNIT_TEST
+	tristate "Unit test for UUID" if !KUNIT_ALL_TESTS
+	depends on KUNIT
+	default KUNIT_ALL_TESTS
+	help
+	  This builds the UUID unit test.

Does this first help line really add any value if we have this second line:

lib/Kconfig.debug config TEST_BITMAP · line 2197

Quoted 1 line
lib/Kconfig.debug +1 -0
+	  Tests parsing functions for UUID/GUID strings.

?

lib/Kconfig.debug config TEST_BITMAP · line 2199

Quoted 1 line
lib/Kconfig.debug +1 -0
+	  If unsure, say N.

Not specific to this case, but IMHO we can drop this line for all kunit tests as it is completely obvious.

lib/Makefile obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.o · line 356

Quoted 5 lines
lib/Makefile +1 -0
@@ -354,5 +353,6 @@ 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_CMDLINE_KUNIT_TEST) += cmdline_kunit.o
+obj-$(CONFIG_UUID_KUNIT_TEST) += test_uuid.o

Another meta-comment on the kunit tests: Wouldn't it make more sense to name them all as CONFIG_KUNIT_TEST_FOO to allow for easier grepping?

lib/test_uuid.c line 17

Quoted 9 lines
lib/test_uuid.c +1 -1
-struct test_uuid_data {
+struct test_data {
 	const char *uuid;
lib/test_uuid.c +1 -1
 	guid_t le;
 	uuid_t be;
 };
 
-static const struct test_uuid_data test_uuid_test_data[] = {
+static const struct test_data correct_data[] = {

What is the reason for these renames? Is this a pattern used for other kunit tests?

lib/test_uuid.c static const struct test_uuid_data test_uuid_test_data[] = { · line 44

Quoted 4 lines
lib/test_uuid.c +3 -0
+static void uuid_correct_le(struct kunit *test)
 {
+	guid_t le;
+	const struct test_data *data = (const struct test_data *)(test->param_value);

Overly long line. But as far as I can tell there is no need for the case that causes this mess anyway given that param_value is a "const void *".

Same for all the other instances of this.

lib/test_uuid.c static const struct test_uuid_data test_uuid_test_data[] = { · line 66

Quoted 4 lines
lib/test_uuid.c +2 -0
+static void uuid_wrong_le(struct kunit *test)
 {
 	guid_t le;
+	const char **data = (const char **)(test->param_value);

No need for the second pair of braces. Same for various other instances.

#3 lore raw
Quoted 20 lines · Christoph Hellwig

lib/Kconfig.debug config TEST_BITMAP · line 2196

Quoted 7 lines
lib/Kconfig.debug +6 -0
+config UUID_KUNIT_TEST
+     tristate "Unit test for UUID" if !KUNIT_ALL_TESTS
+     depends on KUNIT
+     default KUNIT_ALL_TESTS
+     help
+       This builds the UUID unit test.

Does this first help line really add any value if we have this second line:

Quoted 1 line

+ Tests parsing functions for UUID/GUID strings.

?

Quoted 1 line

+ If unsure, say N.

Not specific to this case, but IMHO we can drop this line for all kunit tests as it is completely obvious.

Quoted 8 lines

lib/Makefile obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.o · line 356

Quoted 5 lines
lib/Makefile +1 -0
@@ -354,5 +353,6 @@ 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_CMDLINE_KUNIT_TEST) += cmdline_kunit.o
+obj-$(CONFIG_UUID_KUNIT_TEST) += test_uuid.o

Another meta-comment on the kunit tests: Wouldn't it make more sense to name them all as CONFIG_KUNIT_TEST_FOO to allow for easier grepping?

But putting them in a "kunit namespace" by prefixing them as such would be misleading, IMO.
The tests live adjacent to the code they test and are owned by the same maintainers, or at least that's the intent.

And if the goal is just to find configs, then I don't see much difference between "config.*KUNIT_TEST" and "config KUNIT_TEST.*"

Quoted 8 lines
Quoted 8 lines
lib/test_uuid.c +1 -1
-struct test_uuid_data {
+struct test_data {
      const char *uuid;
      guid_t le;
      uuid_t be;

};

Quoted 5 lines

lib/test_uuid.c line 17

Quoted 2 lines
lib/test_uuid.c +1 -1
-static const struct test_uuid_data test_uuid_test_data[] = {
+static const struct test_data correct_data[] = {

What is the reason for these renames? Is this a pattern used for other kunit tests?

No, this is not a pattern.
The structs can be renamed back.

Quoted 9 lines

lib/test_uuid.c static const struct test_uuid_data test_uuid_test_data[] = { · line 44

Quoted 5 lines
lib/test_uuid.c +3 -0
+static void uuid_correct_le(struct kunit *test)
 {
+     guid_t le;
+     const struct test_data *data = (const struct test_data *)(test->param_value);

Overly long line. But as far as I can tell there is no need for the case that causes this mess anyway given that param_value is a "const void *".

There is no need for the cast or the brace, yes.
This is my fault.

The documentation has both since I had thought that would make how it works more clear:
https://www.kernel.org/doc/html/latest/dev-tools/kunit/usage.html#parameterized-testing
I don't really understand my past thought process...

Quoted 9 lines

Same for all the other instances of this.

lib/test_uuid.c static const struct test_uuid_data test_uuid_test_data[] = { · line 66

Quoted 4 lines
lib/test_uuid.c +2 -0
+static void uuid_wrong_le(struct kunit *test)
 {
      guid_t le;
+     const char **data = (const char **)(test->param_value);

No need for the second pair of braces. Same for various other instances.

#4 lore raw

Às 13:55 de 14/06/21, Daniel Latypov escreveu:

Quoted 46 lines · Christoph Hellwig
Quoted 20 lines

lib/Kconfig.debug config TEST_BITMAP · line 2196

Quoted 7 lines
lib/Kconfig.debug +6 -0
+config UUID_KUNIT_TEST
+     tristate "Unit test for UUID" if !KUNIT_ALL_TESTS
+     depends on KUNIT
+     default KUNIT_ALL_TESTS
+     help
+       This builds the UUID unit test.

Does this first help line really add any value if we have this second line:

Quoted 1 line

+ Tests parsing functions for UUID/GUID strings.

?

Quoted 1 line

+ If unsure, say N.

Not specific to this case, but IMHO we can drop this line for all kunit tests as it is completely obvious.

Quoted 8 lines

lib/Makefile obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.o · line 356

Quoted 5 lines
lib/Makefile +1 -0
@@ -354,5 +353,6 @@ 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_CMDLINE_KUNIT_TEST) += cmdline_kunit.o
+obj-$(CONFIG_UUID_KUNIT_TEST) += test_uuid.o

Another meta-comment on the kunit tests: Wouldn't it make more sense to name them all as CONFIG_KUNIT_TEST_FOO to allow for easier grepping?

But putting them in a "kunit namespace" by prefixing them as such would be misleading, IMO.
The tests live adjacent to the code they test and are owned by the same maintainers, or at least that's the intent.

And if the goal is just to find configs, then I don't see much difference between "config.*KUNIT_TEST" and "config KUNIT_TEST.*"

Quoted 8 lines
Quoted 8 lines
lib/test_uuid.c +1 -1
-struct test_uuid_data {
+struct test_data {
      const char *uuid;
      guid_t le;
      uuid_t be;

};

Quoted 9 lines
Quoted 5 lines

lib/test_uuid.c line 17

Quoted 2 lines
lib/test_uuid.c +1 -1
-static const struct test_uuid_data test_uuid_test_data[] = {
+static const struct test_data correct_data[] = {

What is the reason for these renames? Is this a pattern used for other kunit tests?

No, this is not a pattern.
The structs can be renamed back.

The idea behind this renaming is to be more explicit about what this data is about: correct UUIDs inputs.

Quoted 10 lines
Quoted 9 lines

lib/test_uuid.c static const struct test_uuid_data test_uuid_test_data[] = { · line 44

Quoted 5 lines
lib/test_uuid.c +3 -0
+static void uuid_correct_le(struct kunit *test)
 {
+     guid_t le;
+     const struct test_data *data = (const struct test_data *)(test->param_value);

Overly long line. But as far as I can tell there is no need for the case that causes this mess anyway given that param_value is a "const void *".

Quoted 8 lines

There is no need for the cast or the brace, yes.
This is my fault.

The documentation has both since I had thought that would make how it works more clear:
https://www.kernel.org/doc/html/latest/dev-tools/kunit/usage.html#parameterized-testing
I don't really understand my past thought process...

Ok, I'll change my code to remove the cast and braces. I can also send a patch to rework this part of documentation.

Quoted 9 lines
Quoted 9 lines

Same for all the other instances of this.

lib/test_uuid.c static const struct test_uuid_data test_uuid_test_data[] = { · line 66

Quoted 4 lines
lib/test_uuid.c +2 -0
+static void uuid_wrong_le(struct kunit *test)
 {
      guid_t le;
+     const char **data = (const char **)(test->param_value);

No need for the second pair of braces. Same for various other instances.

#5 lore raw
In reply to André Almeida
Quoted 7 lines · André Almeida

Hi,

This patch converts existing UUID runtime test to use KUnit framework.

Below, there's a comparison between the old output format and the new one. Keep in mind that even if KUnit seems very verbose, this is the corner case where _every_ test has failed.

Btw, do we have test coverage statistics?

I mean since we reduced 18 test cases to 12, do we still have the same / better test coverage?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw

Hi Andy,

Às 06:55 de 11/06/21, Andy Shevchenko escreveu:

Quoted 5 lines · André Almeida
Quoted 4 lines

Hi,

This patch converts existing UUID runtime test to use KUnit framework.

Quoted 9 lines
Quoted 3 lines

Below, there's a comparison between the old output format and the new one. Keep in mind that even if KUnit seems very verbose, this is the corner case where _every_ test has failed.

Btw, do we have test coverage statistics?

I mean since we reduced 18 test cases to 12, do we still have the same / better test coverage?

I don't think we have automated statistics, but I can assure you that the coverage it's exactly the same. We are testing two correlated functions with the same input, in a single test case, instead of having a single case for each one, so that's why the number of cases is reduced.

For example, instead of:

	total_tests++;
	if (guid_parse(data->uuid, &le))


	total_tests++;
	if (!guid_equal(&data->le, &le))

We now have:

	KUNIT_ASSERT_EQ(guid_parse(data->guid, &le), 0)
	KUNIT_EXPECT_TRUE(guid_equal(&data->le, &le))

That will count as a single test.