All of lore.kernel.org
 help / color / mirror / Atom feed
From: Simon Glass <sjg@chromium.org>
To: u-boot@lists.denx.de
Subject: [PATCH 26/30] test: Use a local variable for test state
Date: Thu, 28 Jan 2021 08:12:07 -0700	[thread overview]
Message-ID: <20210128151211.879830-27-sjg@chromium.org> (raw)
In-Reply-To: <20210128151211.879830-1-sjg@chromium.org>

At present we use a global test state for all driver-model tests. Make use
of a local struct like we do with the other tests.

To make this work, add functions to get and set this state. When a test
starts, the state is set (so it can be used in the test). When a test
finishes, the state is unset, so it cannot be used by mistake.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 include/test/ut.h     | 14 ++++++++++++++
 test/dm/test-dm.c     |  4 +---
 test/dm/test-driver.c | 10 +++++++++-
 test/dm/test-uclass.c |  7 +++++--
 test/test-main.c      | 17 +++++++++++++++++
 5 files changed, 46 insertions(+), 6 deletions(-)

diff --git a/include/test/ut.h b/include/test/ut.h
index d06bc5089be..bed0e6eb5f6 100644
--- a/include/test/ut.h
+++ b/include/test/ut.h
@@ -367,6 +367,20 @@ void ut_unsilence_console(struct unit_test_state *uts);
  */
 void ut_set_skip_delays(struct unit_test_state *uts, bool skip_delays);
 
+/**
+ * test_get_state() - Get the active test state
+ *
+ * @return the currently active test state, or NULL if none
+ */
+struct unit_test_state *test_get_state(void);
+
+/**
+ * test_set_state() - Set the active test state
+ *
+ * @uts: Test state to use as currently active test state, or NULL if none
+ */
+void test_set_state(struct unit_test_state *uts);
+
 /**
  * ut_run_test_live_flat() - Run a test with both live and flat tree
  *
diff --git a/test/dm/test-dm.c b/test/dm/test-dm.c
index 48dca289de3..6f9906a510a 100644
--- a/test/dm/test-dm.c
+++ b/test/dm/test-dm.c
@@ -19,8 +19,6 @@
 
 DECLARE_GLOBAL_DATA_PTR;
 
-struct unit_test_state global_dm_test_state;
-
 static bool test_matches(const char *test_name, const char *find_name)
 {
 	if (!find_name)
@@ -43,7 +41,7 @@ int dm_test_run(const char *test_name)
 {
 	struct unit_test *tests = ll_entry_start(struct unit_test, dm_test);
 	const int n_ents = ll_entry_count(struct unit_test, dm_test);
-	struct unit_test_state *uts = &global_dm_test_state;
+	struct unit_test_state uts_s = { .fail_count = 0 }, *uts = &uts_s;
 	struct unit_test *test;
 	int found;
 
diff --git a/test/dm/test-driver.c b/test/dm/test-driver.c
index 49c1c310963..ad2b44c405c 100644
--- a/test/dm/test-driver.c
+++ b/test/dm/test-driver.c
@@ -18,7 +18,6 @@
 #include <test/ut.h>
 
 int dm_testdrv_op_count[DM_TEST_OP_COUNT];
-static struct unit_test_state *uts = &global_dm_test_state;
 
 static int testdrv_ping(struct udevice *dev, int pingval, int *pingret)
 {
@@ -37,6 +36,8 @@ static const struct test_ops test_ops = {
 
 static int test_bind(struct udevice *dev)
 {
+	struct unit_test_state *uts = test_get_state();
+
 	/* Private data should not be allocated */
 	ut_assert(!dev_get_priv(dev));
 
@@ -46,6 +47,7 @@ static int test_bind(struct udevice *dev)
 
 static int test_probe(struct udevice *dev)
 {
+	struct unit_test_state *uts = test_get_state();
 	struct dm_test_priv *priv = dev_get_priv(dev);
 
 	/* Private data should be allocated */
@@ -58,6 +60,8 @@ static int test_probe(struct udevice *dev)
 
 static int test_remove(struct udevice *dev)
 {
+	struct unit_test_state *uts = test_get_state();
+
 	/* Private data should still be allocated */
 	ut_assert(dev_get_priv(dev));
 
@@ -67,6 +71,8 @@ static int test_remove(struct udevice *dev)
 
 static int test_unbind(struct udevice *dev)
 {
+	struct unit_test_state *uts = test_get_state();
+
 	/* Private data should not be allocated */
 	ut_assert(!dev_get_priv(dev));
 
@@ -116,6 +122,8 @@ static int test_manual_bind(struct udevice *dev)
 
 static int test_manual_probe(struct udevice *dev)
 {
+	struct unit_test_state *uts = test_get_state();
+
 	dm_testdrv_op_count[DM_TEST_OP_PROBE]++;
 	if (!uts->force_fail_alloc)
 		dev_set_priv(dev, calloc(1, sizeof(struct dm_test_priv)));
diff --git a/test/dm/test-uclass.c b/test/dm/test-uclass.c
index f4b540c9278..067701734a0 100644
--- a/test/dm/test-uclass.c
+++ b/test/dm/test-uclass.c
@@ -17,8 +17,6 @@
 #include <test/test.h>
 #include <test/ut.h>
 
-static struct unit_test_state *uts = &global_dm_test_state;
-
 int test_ping(struct udevice *dev, int pingval, int *pingret)
 {
 	const struct test_ops *ops = device_get_ops(dev);
@@ -31,6 +29,7 @@ int test_ping(struct udevice *dev, int pingval, int *pingret)
 
 static int test_post_bind(struct udevice *dev)
 {
+	struct unit_test_state *uts = test_get_state();
 	struct dm_test_perdev_uc_pdata *uc_pdata;
 
 	dm_testdrv_op_count[DM_TEST_OP_POST_BIND]++;
@@ -56,6 +55,7 @@ static int test_pre_unbind(struct udevice *dev)
 static int test_pre_probe(struct udevice *dev)
 {
 	struct dm_test_uclass_perdev_priv *priv = dev_get_uclass_priv(dev);
+	struct unit_test_state *uts = test_get_state();
 
 	dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]++;
 	ut_assert(priv);
@@ -66,6 +66,7 @@ static int test_pre_probe(struct udevice *dev)
 
 static int test_post_probe(struct udevice *dev)
 {
+	struct unit_test_state *uts = test_get_state();
 	struct udevice *prev = list_entry(dev->uclass_node.prev,
 					    struct udevice, uclass_node);
 
@@ -100,6 +101,8 @@ static int test_pre_remove(struct udevice *dev)
 
 static int test_init(struct uclass *uc)
 {
+	struct unit_test_state *uts = test_get_state();
+
 	dm_testdrv_op_count[DM_TEST_OP_INIT]++;
 	ut_assert(uclass_get_priv(uc));
 
diff --git a/test/test-main.c b/test/test-main.c
index 2dc2ac02866..b911c669e54 100644
--- a/test/test-main.c
+++ b/test/test-main.c
@@ -16,6 +16,18 @@
 
 DECLARE_GLOBAL_DATA_PTR;
 
+static struct unit_test_state *cur_test_state;
+
+struct unit_test_state *test_get_state(void)
+{
+	return cur_test_state;
+}
+
+void test_set_state(struct unit_test_state *uts)
+{
+	cur_test_state = uts;
+}
+
 /**
  * dm_test_pre_run() - Get ready to run a driver model test
  *
@@ -177,6 +189,9 @@ static int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
 		note = " (flat tree)";
 	printf("Test: %s: %s%s\n", test_name, fname, note);
 
+	/* Allow access to test state from drivers */
+	cur_test_state = uts;
+
 	ret = test_pre_run(uts, test);
 	if (ret == -EAGAIN)
 		return -EAGAIN;
@@ -189,6 +204,8 @@ static int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
 	if (ret)
 		return ret;
 
+	cur_test_state = NULL;
+
 	return 0;
 }
 
-- 
2.30.0.280.ga3ce27912f-goog

  parent reply	other threads:[~2021-01-28 15:12 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-28 15:11 [PATCH 00/30] test: Refactor tests to have a single test runner Simon Glass
2021-01-28 15:11 ` [PATCH 01/30] doc: Tidy up testing section Simon Glass
2021-01-28 16:18   ` Heinrich Schuchardt
2021-01-28 15:11 ` [PATCH 02/30] doc: Document make tcheck Simon Glass
2021-01-28 16:22   ` Heinrich Schuchardt
2021-01-28 15:11 ` [PATCH 03/30] sandbox: Drop the 'starting...' message unless testing Simon Glass
2021-01-28 15:11 ` [PATCH 04/30] doc: Explain how to run tests without pytest Simon Glass
2021-01-28 15:11 ` [PATCH 05/30] doc: Document how sandbox_spl_tests are run Simon Glass
2021-01-28 16:33   ` Heinrich Schuchardt
2021-01-28 15:11 ` [PATCH 06/30] test: Correct setexpr test prefix Simon Glass
2021-01-28 15:11 ` [PATCH 07/30] test: Mark all driver model tests with a flag Simon Glass
2021-01-28 15:11 ` [PATCH 08/30] test: Rename test-main.c to test-dm.c Simon Glass
2021-01-28 15:11 ` [PATCH 09/30] test: Add an overall test runner Simon Glass
2021-01-28 15:11 ` [PATCH 10/30] test: Create pre/post-run functions Simon Glass
2021-01-28 15:11 ` [PATCH 11/30] test: Call test_pre/post_run() from driver model tests Simon Glass
2021-01-28 15:11 ` [PATCH 12/30] test: Move dm_extended_scan() to test_pre_run() Simon Glass
2021-01-28 15:11 ` [PATCH 13/30] test: Move do_autoprobe() " Simon Glass
2021-01-28 15:11 ` [PATCH 14/30] test: Move dm_scan_plat() " Simon Glass
2021-01-28 15:11 ` [PATCH 15/30] test: Drop mallinfo() work-around Simon Glass
2021-01-28 15:11 ` [PATCH 16/30] test: Move console silencing to test_pre_run() Simon Glass
2021-01-28 15:11 ` [PATCH 17/30] test: Move delay skipping " Simon Glass
2021-01-28 15:11 ` [PATCH 18/30] test: Handle driver model reinit in test_pre_run() Simon Glass
2021-01-28 15:12 ` [PATCH 19/30] test: Drop struct dm_test_state Simon Glass
2021-01-28 15:12 ` [PATCH 20/30] test: Move dm_test_init() into test-main.c Simon Glass
2021-01-28 15:12 ` [PATCH 21/30] test: Move dm_test_destroy() " Simon Glass
2021-01-28 15:12 ` [PATCH 22/30] test: Move test running into a separate function Simon Glass
2021-01-28 15:12 ` [PATCH 23/30] test: Use ut_run_test() to run driver model tests Simon Glass
2021-01-28 15:12 ` [PATCH 24/30] test: Drop dm_do_test() Simon Glass
2021-01-28 15:12 ` [PATCH 25/30] test: Add ut_run_test_live_flat() to run tests twice Simon Glass
2021-01-28 15:12 ` Simon Glass [this message]
2021-01-28 15:12 ` [PATCH 27/30] test: Run driver-model tests using ut_run_list() Simon Glass
2021-01-28 15:12 ` [PATCH 28/30] test: Use return values in dm_test_run() Simon Glass
2021-01-28 15:12 ` [PATCH 29/30] test: Move the devicetree check into ut_run_list() Simon Glass
2021-01-28 15:12 ` [PATCH 30/30] test: Move restoring of driver model state to ut_run_list() Simon Glass

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=20210128151211.879830-27-sjg@chromium.org \
    --to=sjg@chromium.org \
    --cc=u-boot@lists.denx.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.