public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] kunit: add tests for using current KUnit test field
@ 2023-04-03 20:19 Rae Moar
  2023-04-03 23:17 ` Daniel Latypov
  2023-04-04  0:36 ` David Gow
  0 siblings, 2 replies; 3+ messages in thread
From: Rae Moar @ 2023-04-03 20:19 UTC (permalink / raw)
  To: brendanhiggins, davidgow, dlatypov
  Cc: skhan, kunit-dev, linux-kernel, linux-kselftest, Rae Moar

Create test suite called "kunit_current" to add test coverage for the use
of current->kunit_test, which returns the current KUnit test.

Add two test cases:
- kunit_current_test to test current->kunit_test and the method
  kunit_get_current_test(), which utilizes current->kunit_test.

- kunit_current_fail_test to test the method
  kunit_fail_current_test(), which utilizes current->kunit_test.

Signed-off-by: Rae Moar <rmoar@google.com>
---

Changes from v1->v2:
- Combine two test cases to test both ways of getting current test in
  kunit_current_test.
- Changes to comments.
- Add kunit_cleanup to kunit_current_fail_test.

 lib/kunit/kunit-test.c | 42 +++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 41 insertions(+), 1 deletion(-)

diff --git a/lib/kunit/kunit-test.c b/lib/kunit/kunit-test.c
index b63595d3e241..42e44caa1bdd 100644
--- a/lib/kunit/kunit-test.c
+++ b/lib/kunit/kunit-test.c
@@ -6,6 +6,7 @@
  * Author: Brendan Higgins <brendanhiggins@google.com>
  */
 #include <kunit/test.h>
+#include <kunit/test-bug.h>
 
 #include "try-catch-impl.h"
 
@@ -532,7 +533,46 @@ static struct kunit_suite kunit_status_test_suite = {
 	.test_cases = kunit_status_test_cases,
 };
 
+static void kunit_current_test(struct kunit *test)
+{
+	/* Check results of both current->kunit_test and
+	 * kunit_get_current_test() are equivalent to current test.
+	 */
+	KUNIT_EXPECT_PTR_EQ(test, test, current->kunit_test);
+	KUNIT_EXPECT_PTR_EQ(test, test, kunit_get_current_test());
+}
+
+static void kunit_current_fail_test(struct kunit *test)
+{
+	struct kunit fake;
+
+	kunit_init_test(&fake, "fake test", NULL);
+	KUNIT_EXPECT_EQ(test, fake.status, KUNIT_SUCCESS);
+
+	/* Set current->kunit_test to fake test. */
+	current->kunit_test = &fake;
+
+	kunit_fail_current_test("This should make `fake` test fail.");
+	KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_FAILURE);
+	kunit_cleanup(&fake);
+
+	/* Reset current->kunit_test to current test. */
+	current->kunit_test = test;
+}
+
+static struct kunit_case kunit_current_test_cases[] = {
+	KUNIT_CASE(kunit_current_test),
+	KUNIT_CASE(kunit_current_fail_test),
+	{}
+};
+
+static struct kunit_suite kunit_current_test_suite = {
+	.name = "kunit_current",
+	.test_cases = kunit_current_test_cases,
+};
+
 kunit_test_suites(&kunit_try_catch_test_suite, &kunit_resource_test_suite,
-		  &kunit_log_test_suite, &kunit_status_test_suite);
+		  &kunit_log_test_suite, &kunit_status_test_suite,
+		  &kunit_current_test_suite);
 
 MODULE_LICENSE("GPL v2");

base-commit: 7232282dd47cce6a780c9414bd9baccf232c7686
-- 
2.40.0.348.gf938b09366-goog


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

* Re: [PATCH v2] kunit: add tests for using current KUnit test field
  2023-04-03 20:19 [PATCH v2] kunit: add tests for using current KUnit test field Rae Moar
@ 2023-04-03 23:17 ` Daniel Latypov
  2023-04-04  0:36 ` David Gow
  1 sibling, 0 replies; 3+ messages in thread
From: Daniel Latypov @ 2023-04-03 23:17 UTC (permalink / raw)
  To: Rae Moar
  Cc: brendanhiggins, davidgow, skhan, kunit-dev, linux-kernel,
	linux-kselftest

On Mon, Apr 3, 2023 at 1:19 PM Rae Moar <rmoar@google.com> wrote:
>
> Create test suite called "kunit_current" to add test coverage for the use
> of current->kunit_test, which returns the current KUnit test.
>
> Add two test cases:
> - kunit_current_test to test current->kunit_test and the method
>   kunit_get_current_test(), which utilizes current->kunit_test.
>
> - kunit_current_fail_test to test the method
>   kunit_fail_current_test(), which utilizes current->kunit_test.
>
> Signed-off-by: Rae Moar <rmoar@google.com>

Reviewed-by: Daniel Latypov <dlatypov@google.com>

Looks good and runs well here.
It's nice to have a test for this given kunit_fail_current_test() went
from a simple function call to now using `kunit_hooks` to do an
indirect call and relying on the `kunit_running` static key.


> ---
>
> Changes from v1->v2:
> - Combine two test cases to test both ways of getting current test in
>   kunit_current_test.
> - Changes to comments.
> - Add kunit_cleanup to kunit_current_fail_test.
>
>  lib/kunit/kunit-test.c | 42 +++++++++++++++++++++++++++++++++++++++++-
>  1 file changed, 41 insertions(+), 1 deletion(-)
>
> diff --git a/lib/kunit/kunit-test.c b/lib/kunit/kunit-test.c
> index b63595d3e241..42e44caa1bdd 100644
> --- a/lib/kunit/kunit-test.c
> +++ b/lib/kunit/kunit-test.c
> @@ -6,6 +6,7 @@
>   * Author: Brendan Higgins <brendanhiggins@google.com>
>   */
>  #include <kunit/test.h>
> +#include <kunit/test-bug.h>
>
>  #include "try-catch-impl.h"
>
> @@ -532,7 +533,46 @@ static struct kunit_suite kunit_status_test_suite = {
>         .test_cases = kunit_status_test_cases,
>  };
>
> +static void kunit_current_test(struct kunit *test)
> +{
> +       /* Check results of both current->kunit_test and
> +        * kunit_get_current_test() are equivalent to current test.
> +        */
> +       KUNIT_EXPECT_PTR_EQ(test, test, current->kunit_test);
> +       KUNIT_EXPECT_PTR_EQ(test, test, kunit_get_current_test());
> +}
> +
> +static void kunit_current_fail_test(struct kunit *test)
> +{
> +       struct kunit fake;
> +
> +       kunit_init_test(&fake, "fake test", NULL);
> +       KUNIT_EXPECT_EQ(test, fake.status, KUNIT_SUCCESS);
> +
> +       /* Set current->kunit_test to fake test. */
> +       current->kunit_test = &fake;
> +
> +       kunit_fail_current_test("This should make `fake` test fail.");
> +       KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_FAILURE);
> +       kunit_cleanup(&fake);
> +
> +       /* Reset current->kunit_test to current test. */
> +       current->kunit_test = test;
> +}
> +
> +static struct kunit_case kunit_current_test_cases[] = {
> +       KUNIT_CASE(kunit_current_test),
> +       KUNIT_CASE(kunit_current_fail_test),
> +       {}
> +};
> +
> +static struct kunit_suite kunit_current_test_suite = {
> +       .name = "kunit_current",
> +       .test_cases = kunit_current_test_cases,
> +};
> +
>  kunit_test_suites(&kunit_try_catch_test_suite, &kunit_resource_test_suite,
> -                 &kunit_log_test_suite, &kunit_status_test_suite);
> +                 &kunit_log_test_suite, &kunit_status_test_suite,
> +                 &kunit_current_test_suite);
>
>  MODULE_LICENSE("GPL v2");
>
> base-commit: 7232282dd47cce6a780c9414bd9baccf232c7686
> --
> 2.40.0.348.gf938b09366-goog
>

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

* Re: [PATCH v2] kunit: add tests for using current KUnit test field
  2023-04-03 20:19 [PATCH v2] kunit: add tests for using current KUnit test field Rae Moar
  2023-04-03 23:17 ` Daniel Latypov
@ 2023-04-04  0:36 ` David Gow
  1 sibling, 0 replies; 3+ messages in thread
From: David Gow @ 2023-04-04  0:36 UTC (permalink / raw)
  To: Rae Moar
  Cc: brendanhiggins, dlatypov, skhan, kunit-dev, linux-kernel,
	linux-kselftest

[-- Attachment #1: Type: text/plain, Size: 3091 bytes --]

On Tue, 4 Apr 2023 at 04:19, Rae Moar <rmoar@google.com> wrote:
>
> Create test suite called "kunit_current" to add test coverage for the use
> of current->kunit_test, which returns the current KUnit test.
>
> Add two test cases:
> - kunit_current_test to test current->kunit_test and the method
>   kunit_get_current_test(), which utilizes current->kunit_test.
>
> - kunit_current_fail_test to test the method
>   kunit_fail_current_test(), which utilizes current->kunit_test.
>
> Signed-off-by: Rae Moar <rmoar@google.com>
> ---

Looks good to me, thanks!

Reviewed-by: David Gow <davidgow@google.com>

Cheers,
-- David

>
> Changes from v1->v2:
> - Combine two test cases to test both ways of getting current test in
>   kunit_current_test.
> - Changes to comments.
> - Add kunit_cleanup to kunit_current_fail_test.
>
>  lib/kunit/kunit-test.c | 42 +++++++++++++++++++++++++++++++++++++++++-
>  1 file changed, 41 insertions(+), 1 deletion(-)
>
> diff --git a/lib/kunit/kunit-test.c b/lib/kunit/kunit-test.c
> index b63595d3e241..42e44caa1bdd 100644
> --- a/lib/kunit/kunit-test.c
> +++ b/lib/kunit/kunit-test.c
> @@ -6,6 +6,7 @@
>   * Author: Brendan Higgins <brendanhiggins@google.com>
>   */
>  #include <kunit/test.h>
> +#include <kunit/test-bug.h>
>
>  #include "try-catch-impl.h"
>
> @@ -532,7 +533,46 @@ static struct kunit_suite kunit_status_test_suite = {
>         .test_cases = kunit_status_test_cases,
>  };
>
> +static void kunit_current_test(struct kunit *test)
> +{
> +       /* Check results of both current->kunit_test and
> +        * kunit_get_current_test() are equivalent to current test.
> +        */
> +       KUNIT_EXPECT_PTR_EQ(test, test, current->kunit_test);
> +       KUNIT_EXPECT_PTR_EQ(test, test, kunit_get_current_test());
> +}
> +
> +static void kunit_current_fail_test(struct kunit *test)
> +{
> +       struct kunit fake;
> +
> +       kunit_init_test(&fake, "fake test", NULL);
> +       KUNIT_EXPECT_EQ(test, fake.status, KUNIT_SUCCESS);
> +
> +       /* Set current->kunit_test to fake test. */
> +       current->kunit_test = &fake;
> +
> +       kunit_fail_current_test("This should make `fake` test fail.");
> +       KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_FAILURE);
> +       kunit_cleanup(&fake);
> +
> +       /* Reset current->kunit_test to current test. */
> +       current->kunit_test = test;
> +}
> +
> +static struct kunit_case kunit_current_test_cases[] = {
> +       KUNIT_CASE(kunit_current_test),
> +       KUNIT_CASE(kunit_current_fail_test),
> +       {}
> +};
> +
> +static struct kunit_suite kunit_current_test_suite = {
> +       .name = "kunit_current",
> +       .test_cases = kunit_current_test_cases,
> +};
> +
>  kunit_test_suites(&kunit_try_catch_test_suite, &kunit_resource_test_suite,
> -                 &kunit_log_test_suite, &kunit_status_test_suite);
> +                 &kunit_log_test_suite, &kunit_status_test_suite,
> +                 &kunit_current_test_suite);
>
>  MODULE_LICENSE("GPL v2");
>
> base-commit: 7232282dd47cce6a780c9414bd9baccf232c7686
> --
> 2.40.0.348.gf938b09366-goog
>

[-- Attachment #2: S/MIME Cryptographic Signature --]
[-- Type: application/pkcs7-signature, Size: 4003 bytes --]

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

end of thread, other threads:[~2023-04-04  0:36 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-04-03 20:19 [PATCH v2] kunit: add tests for using current KUnit test field Rae Moar
2023-04-03 23:17 ` Daniel Latypov
2023-04-04  0:36 ` David Gow

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox