diff options
| author | Peter Maydell <peter.maydell@linaro.org> | 2016-08-08 15:21:33 +0100 |
|---|---|---|
| committer | Peter Maydell <peter.maydell@linaro.org> | 2016-08-08 15:21:33 +0100 |
| commit | b8dc0fcff10702ab38db2d318f8fa2b8fc5c61b0 (patch) | |
| tree | cd5e7e1214f1c96ac16af8021d79495fba869190 /tests/test-blockjob.c | |
| parent | 684b6b26af02a58692bb98578ff2653cd60dcb9c (diff) | |
| parent | a752e4786c19b0b368f4521a5dcbcce14882c3b1 (diff) | |
| download | focaccia-qemu-b8dc0fcff10702ab38db2d318f8fa2b8fc5c61b0.tar.gz focaccia-qemu-b8dc0fcff10702ab38db2d318f8fa2b8fc5c61b0.zip | |
Merge remote-tracking branch 'remotes/kevin/tags/for-upstream' into staging
More block layer patches for 2.7.0-rc2 # gpg: Signature made Mon 08 Aug 2016 12:51:30 BST # gpg: using RSA key 0x7F09B272C88F2FD6 # gpg: Good signature from "Kevin Wolf <kwolf@redhat.com>" # Primary key fingerprint: DC3D EB15 9A9A F95D 3D74 56FE 7F09 B272 C88F 2FD6 * remotes/kevin/tags/for-upstream: iotests: fix 109 mirror: finish earlier on error tests: Test blockjob IDs block/qdev: Let 'drive' property fall back to node name Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Diffstat (limited to 'tests/test-blockjob.c')
| -rw-r--r-- | tests/test-blockjob.c | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/tests/test-blockjob.c b/tests/test-blockjob.c new file mode 100644 index 0000000000..5b0e934a0c --- /dev/null +++ b/tests/test-blockjob.c @@ -0,0 +1,147 @@ +/* + * Blockjob tests + * + * Copyright Igalia, S.L. 2016 + * + * Authors: + * Alberto Garcia <berto@igalia.com> + * + * This work is licensed under the terms of the GNU LGPL, version 2 or later. + * See the COPYING.LIB file in the top-level directory. + */ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "qemu/main-loop.h" +#include "block/blockjob.h" +#include "sysemu/block-backend.h" + +static const BlockJobDriver test_block_job_driver = { + .instance_size = sizeof(BlockJob), +}; + +static void block_job_cb(void *opaque, int ret) +{ +} + +static BlockJob *do_test_id(BlockBackend *blk, const char *id, + bool should_succeed) +{ + BlockJob *job; + Error *errp = NULL; + + job = block_job_create(id, &test_block_job_driver, blk_bs(blk), 0, + block_job_cb, NULL, &errp); + if (should_succeed) { + g_assert_null(errp); + g_assert_nonnull(job); + if (id) { + g_assert_cmpstr(job->id, ==, id); + } else { + g_assert_cmpstr(job->id, ==, blk_name(blk)); + } + } else { + g_assert_nonnull(errp); + g_assert_null(job); + error_free(errp); + } + + return job; +} + +/* This creates a BlockBackend (optionally with a name) with a + * BlockDriverState inserted. */ +static BlockBackend *create_blk(const char *name) +{ + BlockBackend *blk = blk_new(); + BlockDriverState *bs = bdrv_new(); + + blk_insert_bs(blk, bs); + bdrv_unref(bs); + + if (name) { + Error *errp = NULL; + monitor_add_blk(blk, name, &errp); + g_assert_null(errp); + } + + return blk; +} + +/* This destroys the backend */ +static void destroy_blk(BlockBackend *blk) +{ + if (blk_name(blk)[0] != '\0') { + monitor_remove_blk(blk); + } + + blk_remove_bs(blk); + blk_unref(blk); +} + +static void test_job_ids(void) +{ + BlockBackend *blk[3]; + BlockJob *job[3]; + + blk[0] = create_blk(NULL); + blk[1] = create_blk("drive1"); + blk[2] = create_blk("drive2"); + + /* No job ID provided and the block backend has no name */ + job[0] = do_test_id(blk[0], NULL, false); + + /* These are all invalid job IDs */ + job[0] = do_test_id(blk[0], "0id", false); + job[0] = do_test_id(blk[0], "", false); + job[0] = do_test_id(blk[0], " ", false); + job[0] = do_test_id(blk[0], "123", false); + job[0] = do_test_id(blk[0], "_id", false); + job[0] = do_test_id(blk[0], "-id", false); + job[0] = do_test_id(blk[0], ".id", false); + job[0] = do_test_id(blk[0], "#id", false); + + /* This one is valid */ + job[0] = do_test_id(blk[0], "id0", true); + + /* We cannot have two jobs in the same BDS */ + do_test_id(blk[0], "id1", false); + + /* Duplicate job IDs are not allowed */ + job[1] = do_test_id(blk[1], "id0", false); + + /* But once job[0] finishes we can reuse its ID */ + block_job_unref(job[0]); + job[1] = do_test_id(blk[1], "id0", true); + + /* No job ID specified, defaults to the backend name ('drive1') */ + block_job_unref(job[1]); + job[1] = do_test_id(blk[1], NULL, true); + + /* Duplicate job ID */ + job[2] = do_test_id(blk[2], "drive1", false); + + /* The ID of job[2] would default to 'drive2' but it is already in use */ + job[0] = do_test_id(blk[0], "drive2", true); + job[2] = do_test_id(blk[2], NULL, false); + + /* This one is valid */ + job[2] = do_test_id(blk[2], "id_2", true); + + block_job_unref(job[0]); + block_job_unref(job[1]); + block_job_unref(job[2]); + + destroy_blk(blk[0]); + destroy_blk(blk[1]); + destroy_blk(blk[2]); +} + +int main(int argc, char **argv) +{ + qemu_init_main_loop(&error_abort); + + g_test_init(&argc, &argv, NULL); + g_test_add_func("/blockjob/ids", test_job_ids); + return g_test_run(); +} |