Skip to contentSkip to conversation

tests/9pfs: Code refactoring

PATCH Superseded Greg Kurz 2 participants 9 messages Oct 20, 2020

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Greg Kurz

author
#0 lore raw

cover tests/9pfs: Code refactoring

Some code refactoring to have a clear distinction between top level test functions and helper functions.

---

Greg Kurz (5):
tests/9pfs: Factor out do_fs_version() helper tests/9pfs: Turn fs_readdir_split() into a helper tests/9pfs: Set alloc in fs_create_dir() tests/9pfs: Factor out do_fs_attach() helper tests/9pfs: Turn fs_mkdir() into a helper

 tests/qtest/virtio-9p-test.c |   62 +++++++++++++++++++++++-------------------
 1 file changed, 34 insertions(+), 28 deletions(-)

--
Greg

author
#1 lore raw

PATCH 1/5 tests/9pfs: Factor out do_fs_version() helper

fs_version() is a top level test function. Factor out the sugar to a separate helper instead of hijacking it in other tests.

---
 tests/qtest/virtio-9p-test.c |   14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
tests/qtest/virtio-9p-test.c +9 -5
index c15908f27b3d..63f91aaf77e6 100644
@@ -567,10 +567,8 @@ static void v9fs_rflush(P9Req *req)
     v9fs_req_free(req);
 }
 
-static void fs_version(void *obj, void *data, QGuestAllocator *t_alloc)
+static void do_fs_version(QVirtio9P *v9p)
 {
-    QVirtio9P *v9p = obj;
-    alloc = t_alloc;
     const char *version = "9P2000.L";
     uint16_t server_len;
     char *server_version;
@@ -585,13 +583,19 @@ static void fs_version(void *obj, void *data, QGuestAllocator *t_alloc)
     g_free(server_version);
 }
 
+static void fs_version(void *obj, void *data, QGuestAllocator *t_alloc)
+{
+    alloc = t_alloc;
+    do_fs_version(obj);
+}
+
 static void fs_attach(void *obj, void *data, QGuestAllocator *t_alloc)
 {
     QVirtio9P *v9p = obj;
     alloc = t_alloc;
     P9Req *req;
 
-    fs_version(v9p, NULL, t_alloc);
+    do_fs_version(v9p);
     req = v9fs_tattach(v9p, 0, getuid(), 0);
     v9fs_req_wait_for_reply(req, NULL);
     v9fs_rattach(req, NULL);
@@ -831,7 +835,7 @@ static void fs_walk_dotdot(void *obj, void *data, QGuestAllocator *t_alloc)
     v9fs_qid root_qid, *wqid;
     P9Req *req;
 
-    fs_version(v9p, NULL, t_alloc);
+    do_fs_version(v9p);
     req = v9fs_tattach(v9p, 0, getuid(), 0);
     v9fs_req_wait_for_reply(req, NULL);
     v9fs_rattach(req, &root_qid);
#2 lore raw

Re: [PATCH 1/5] tests/9pfs: Factor out do_fs_version() helper

Quoted 25 lines · On Dienstag, 20. Oktober 2020 17:11:05 CEST Greg Kurz wrote:

fs_version() is a top level test function. Factor out the sugar to a separate helper instead of hijacking it in other tests.

---
 tests/qtest/virtio-9p-test.c |   14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
tests/qtest/virtio-9p-test.c +1 -3
index c15908f27b3d..63f91aaf77e6 100644
@@ -567,10 +567,8 @@ static void v9fs_rflush(P9Req *req)
     v9fs_req_free(req);
 }
 
-static void fs_version(void *obj, void *data, QGuestAllocator *t_alloc)
+static void do_fs_version(QVirtio9P *v9p)
 {
-    QVirtio9P *v9p = obj;
-    alloc = t_alloc;
     const char *version = "9P2000.L";
     uint16_t server_len;
     char *server_version;

tests/qtest/virtio-9p-test.c static void fs_version(void *obj, void *data, · line 584

Quoted 2 lines
tests/qtest/virtio-9p-test.c +0 -0
@@ -585,13 +583,19 @@ static void fs_version(void *obj, void *data,
 QGuestAllocator *t_alloc) g_free(server_version);
 }

So the naming convention from now on shall be do_fs_*() for non-toplevel functions there. Not that I care too much about the precise prefix, but how about just do_*() for them instead?

Except of that, your patches look fine to me.

Best regards,
Christian Schoenebeck

author
#3 lore raw

Re: [PATCH 1/5] tests/9pfs: Factor out do_fs_version() helper

Quoted 17 lines · On Tue, 20 Oct 2020 17:34:05 +0200 Christian Schoenebeck…
Quoted 16 lines · On Dienstag, 20. Oktober 2020 17:11:05 CEST Greg Kurz wrote:

fs_version() is a top level test function. Factor out the sugar to a separate helper instead of hijacking it in other tests.

---
 tests/qtest/virtio-9p-test.c |   14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
tests/qtest/virtio-9p-test.c +0 -0
index c15908f27b3d..63f91aaf77e6 100644
@@ -567,10 +567,8 @@ static void v9fs_rflush(P9Req *req)
     v9fs_req_free(req);
 }
 
Quoted 16 lines

tests/qtest/virtio-9p-test.c static void fs_version(void *obj, void *data, · line 584

Quoted 11 lines
tests/qtest/virtio-9p-test.c +1 -3
-static void fs_version(void *obj, void *data, QGuestAllocator *t_alloc)
+static void do_fs_version(QVirtio9P *v9p)
 {
-    QVirtio9P *v9p = obj;
-    alloc = t_alloc;
     const char *version = "9P2000.L";
     uint16_t server_len;
     char *server_version;
@@ -585,13 +583,19 @@ static void fs_version(void *obj, void *data,
 QGuestAllocator *t_alloc) g_free(server_version);
 }

So the naming convention from now on shall be do_fs_*() for non-toplevel functions there. Not that I care too much about the precise prefix, but how about just do_*() for them instead?

I've prepended "do_" to the existing names by pure laziness but I'm fine with any prefix or naming convention actually.

So just tell me what you prefer and I'll send a v2.

Quoted 6 lines

Except of that, your patches look fine to me.

Best regards,
Christian Schoenebeck

#4 lore raw

Re: [PATCH 1/5] tests/9pfs: Factor out do_fs_version() helper

Quoted 45 lines · On Dienstag, 20. Oktober 2020 17:41:56 CEST Greg Kurz wrote:

On Tue, 20 Oct 2020 17:34:05 +0200

Quoted 35 lines · Christian Schoenebeck
Quoted 34 lines · On Dienstag, 20. Oktober 2020 17:11:05 CEST Greg Kurz wrote:

fs_version() is a top level test function. Factor out the sugar to a separate helper instead of hijacking it in other tests.

---

 tests/qtest/virtio-9p-test.c |   14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
tests/qtest/virtio-9p-test.c +1 -3
index c15908f27b3d..63f91aaf77e6 100644
@@ -567,10 +567,8 @@ static void v9fs_rflush(P9Req *req)
 
     v9fs_req_free(req);
 
 }
 
-static void fs_version(void *obj, void *data, QGuestAllocator *t_alloc)
+static void do_fs_version(QVirtio9P *v9p)
 
 {
 
-    QVirtio9P *v9p = obj;
-    alloc = t_alloc;
 
     const char *version = "9P2000.L";
     uint16_t server_len;
     char *server_version;
@@ -585,13 +583,19 @@ static void fs_version(void *obj, void *data,
 QGuestAllocator *t_alloc) g_free(server_version);
 
Quoted 6 lines

tests/qtest/virtio-9p-test.c static void fs_version(void *obj, void *data, · line 585

Quoted 1 line
tests/qtest/virtio-9p-test.c +0 -0
 }

So the naming convention from now on shall be do_fs_*() for non-toplevel functions there. Not that I care too much about the precise prefix, but how
about just do_*() for them instead?

Quoted 4 lines

I've prepended "do_" to the existing names by pure laziness but I'm fine with any prefix or naming convention actually.

So just tell me what you prefer and I'll send a v2.

It's really just more pleasant for the eye to have the prefix a bit shorter. So use do_*() or any other kind of xx_*() or xxx_*() prefix that comes to your mind. It will be fine with me.

Thanks Greg!

Best regards,
Christian Schoenebeck

author
#5 lore raw

PATCH 2/5 tests/9pfs: Turn fs_readdir_split() into a helper

In reply to Greg Kurz

fs_readdir_split() isn't a top level test function and thus shouldn't take the "void *obj, void *data, QGuestAllocator *t_alloc" arguments. Turn it into a helper to be used by test functions.

---
 tests/qtest/virtio-9p-test.c |   14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
tests/qtest/virtio-9p-test.c +7 -7
index 63f91aaf77e6..d0cdc14fee3e 100644
@@ -727,11 +727,8 @@ static void fs_readdir(void *obj, void *data, QGuestAllocator *t_alloc)
 }
 
 /* readdir test where overall request is split over several messages */
-static void fs_readdir_split(void *obj, void *data, QGuestAllocator *t_alloc,
-                             uint32_t count)
+static void do_fs_readdir_split(QVirtio9P *v9p, uint32_t count)
 {
-    QVirtio9P *v9p = obj;
-    alloc = t_alloc;
     char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_READDIR_DIR) };
     uint16_t nqid;
     v9fs_qid qid;
@@ -998,19 +995,22 @@ static void fs_mkdir(void *obj, void *data, QGuestAllocator *t_alloc,
 static void fs_readdir_split_128(void *obj, void *data,
                                  QGuestAllocator *t_alloc)
 {
-    fs_readdir_split(obj, data, t_alloc, 128);
+    alloc = t_alloc;
+    do_fs_readdir_split(obj, 128);
 }
 
 static void fs_readdir_split_256(void *obj, void *data,
                                  QGuestAllocator *t_alloc)
 {
-    fs_readdir_split(obj, data, t_alloc, 256);
+    alloc = t_alloc;
+    do_fs_readdir_split(obj, 256);
 }
 
 static void fs_readdir_split_512(void *obj, void *data,
                                  QGuestAllocator *t_alloc)
 {
-    fs_readdir_split(obj, data, t_alloc, 512);
+    alloc = t_alloc;
+    do_fs_readdir_split(obj, 512);
 }
 
 
author
#6 lore raw

PATCH 3/5 tests/9pfs: Set alloc in fs_create_dir()

In reply to Greg Kurz

fs_create_dir() is a top level test function. It should set alloc.

---
 tests/qtest/virtio-9p-test.c |    1 +
 1 file changed, 1 insertion(+)
tests/qtest/virtio-9p-test.c +1 -0
index d0cdc14fee3e..95638662e14d 100644
@@ -1019,6 +1019,7 @@ static void fs_readdir_split_512(void *obj, void *data,
 static void fs_create_dir(void *obj, void *data, QGuestAllocator *t_alloc)
 {
     QVirtio9P *v9p = obj;
+    alloc = t_alloc;
     struct stat st;
     char *root_path = virtio_9p_test_path("");
     char *new_dir = virtio_9p_test_path("01");
author
#7 lore raw

PATCH 4/5 tests/9pfs: Factor out do_fs_attach() helper

In reply to Greg Kurz

fs_attach() is a top level test function. Factor out the sugar to a separate helper instead of hijacking it in other tests.

---
 tests/qtest/virtio-9p-test.c |   28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)
tests/qtest/virtio-9p-test.c +16 -12
index 95638662e14d..1e1b1433014b 100644
@@ -589,10 +589,8 @@ static void fs_version(void *obj, void *data, QGuestAllocator *t_alloc)
     do_fs_version(obj);
 }
 
-static void fs_attach(void *obj, void *data, QGuestAllocator *t_alloc)
+static void do_fs_attach(QVirtio9P *v9p)
 {
-    QVirtio9P *v9p = obj;
-    alloc = t_alloc;
     P9Req *req;
 
     do_fs_version(v9p);
@@ -601,6 +599,12 @@ static void fs_attach(void *obj, void *data, QGuestAllocator *t_alloc)
     v9fs_rattach(req, NULL);
 }
 
+static void fs_attach(void *obj, void *data, QGuestAllocator *t_alloc)
+{
+    alloc = t_alloc;
+    do_fs_attach(obj);
+}
+
 static void fs_walk(void *obj, void *data, QGuestAllocator *t_alloc)
 {
     QVirtio9P *v9p = obj;
@@ -615,7 +619,7 @@ static void fs_walk(void *obj, void *data, QGuestAllocator *t_alloc)
         wnames[i] = g_strdup_printf(QTEST_V9FS_SYNTH_WALK_FILE, i);
     }
 
-    fs_attach(v9p, NULL, t_alloc);
+    do_fs_attach(v9p);
     req = v9fs_twalk(v9p, 0, 1, P9_MAXWELEM, wnames, 0);
     v9fs_req_wait_for_reply(req, NULL);
     v9fs_rwalk(req, &nwqid, &wqid);
@@ -684,7 +688,7 @@ static void fs_readdir(void *obj, void *data, QGuestAllocator *t_alloc)
     struct V9fsDirent *entries = NULL;
     P9Req *req;
 
-    fs_attach(v9p, NULL, t_alloc);
+    do_fs_attach(v9p);
     req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
     v9fs_req_wait_for_reply(req, NULL);
     v9fs_rwalk(req, &nqid, NULL);
@@ -738,7 +742,7 @@ static void do_fs_readdir_split(QVirtio9P *v9p, uint32_t count)
     int fid;
     uint64_t offset;
 
-    fs_attach(v9p, NULL, t_alloc);
+    do_fs_attach(v9p);
 
     fid = 1;
     offset = 0;
@@ -814,7 +818,7 @@ static void fs_walk_no_slash(void *obj, void *data, QGuestAllocator *t_alloc)
     P9Req *req;
     uint32_t err;
 
-    fs_attach(v9p, NULL, t_alloc);
+    do_fs_attach(v9p);
     req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
     v9fs_req_wait_for_reply(req, NULL);
     v9fs_rlerror(req, &err);
@@ -854,7 +858,7 @@ static void fs_lopen(void *obj, void *data, QGuestAllocator *t_alloc)
     char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_LOPEN_FILE) };
     P9Req *req;
 
-    fs_attach(v9p, NULL, t_alloc);
+    do_fs_attach(v9p);
     req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
     v9fs_req_wait_for_reply(req, NULL);
     v9fs_rwalk(req, NULL, NULL);
@@ -876,7 +880,7 @@ static void fs_write(void *obj, void *data, QGuestAllocator *t_alloc)
     uint32_t count;
     P9Req *req;
 
-    fs_attach(v9p, NULL, t_alloc);
+    do_fs_attach(v9p);
     req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
     v9fs_req_wait_for_reply(req, NULL);
     v9fs_rwalk(req, NULL, NULL);
@@ -903,7 +907,7 @@ static void fs_flush_success(void *obj, void *data, QGuestAllocator *t_alloc)
     uint32_t reply_len;
     uint8_t should_block;
 
-    fs_attach(v9p, NULL, t_alloc);
+    do_fs_attach(v9p);
     req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
     v9fs_req_wait_for_reply(req, NULL);
     v9fs_rwalk(req, NULL, NULL);
@@ -940,7 +944,7 @@ static void fs_flush_ignored(void *obj, void *data, QGuestAllocator *t_alloc)
     uint32_t count;
     uint8_t should_block;
 
-    fs_attach(v9p, NULL, t_alloc);
+    do_fs_attach(v9p);
     req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
     v9fs_req_wait_for_reply(req, NULL);
     v9fs_rwalk(req, NULL, NULL);
@@ -1026,7 +1030,7 @@ static void fs_create_dir(void *obj, void *data, QGuestAllocator *t_alloc)
 
     g_assert(root_path != NULL);
 
-    fs_attach(v9p, NULL, t_alloc);
+    do_fs_attach(v9p);
     fs_mkdir(v9p, data, t_alloc, "/", "01");
 
     /* check if created directory really exists now ... */
author
#8 lore raw

PATCH 5/5 tests/9pfs: Turn fs_mkdir() into a helper

In reply to Greg Kurz

fs_mkdir() isn't a top level test function and thus shouldn't take the "void *obj, void *data, QGuestAllocator *t_alloc" arguments. Turn it into a helper to be used by test functions.

---
 tests/qtest/virtio-9p-test.c |    7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
tests/qtest/virtio-9p-test.c +2 -5
index 1e1b1433014b..155d327fd9f5 100644
@@ -972,11 +972,8 @@ static void fs_flush_ignored(void *obj, void *data, QGuestAllocator *t_alloc)
     g_free(wnames[0]);
 }
 
-static void fs_mkdir(void *obj, void *data, QGuestAllocator *t_alloc,
-                     const char *path, const char *cname)
+static void do_fs_mkdir(QVirtio9P *v9p, const char *path, const char *cname)
 {
-    QVirtio9P *v9p = obj;
-    alloc = t_alloc;
     char **wnames;
     char *const name = g_strdup(cname);
     P9Req *req;
@@ -1031,7 +1028,7 @@ static void fs_create_dir(void *obj, void *data, QGuestAllocator *t_alloc)
     g_assert(root_path != NULL);
 
     do_fs_attach(v9p);
-    fs_mkdir(v9p, data, t_alloc, "/", "01");
+    do_fs_mkdir(v9p, "/", "01");
 
     /* check if created directory really exists now ... */
     g_assert(stat(new_dir, &st) == 0);