4 * Copyright Igalia, S.L. 2016
7 * Alberto Garcia <berto@igalia.com>
9 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
10 * See the COPYING.LIB file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "qapi/error.h"
15 #include "qemu/main-loop.h"
16 #include "block/blockjob.h"
17 #include "sysemu/block-backend.h"
19 static const BlockJobDriver test_block_job_driver
= {
20 .instance_size
= sizeof(BlockJob
),
23 static void block_job_cb(void *opaque
, int ret
)
27 static BlockJob
*do_test_id(BlockBackend
*blk
, const char *id
,
33 job
= block_job_create(id
, &test_block_job_driver
, blk_bs(blk
), 0,
34 block_job_cb
, NULL
, &errp
);
37 g_assert_nonnull(job
);
39 g_assert_cmpstr(job
->id
, ==, id
);
41 g_assert_cmpstr(job
->id
, ==, blk_name(blk
));
44 g_assert_nonnull(errp
);
52 /* This creates a BlockBackend (optionally with a name) with a
53 * BlockDriverState inserted. */
54 static BlockBackend
*create_blk(const char *name
)
56 BlockBackend
*blk
= blk_new();
57 BlockDriverState
*bs
= bdrv_new();
59 blk_insert_bs(blk
, bs
);
64 monitor_add_blk(blk
, name
, &errp
);
71 /* This destroys the backend */
72 static void destroy_blk(BlockBackend
*blk
)
74 if (blk_name(blk
)[0] != '\0') {
75 monitor_remove_blk(blk
);
82 static void test_job_ids(void)
87 blk
[0] = create_blk(NULL
);
88 blk
[1] = create_blk("drive1");
89 blk
[2] = create_blk("drive2");
91 /* No job ID provided and the block backend has no name */
92 job
[0] = do_test_id(blk
[0], NULL
, false);
94 /* These are all invalid job IDs */
95 job
[0] = do_test_id(blk
[0], "0id", false);
96 job
[0] = do_test_id(blk
[0], "", false);
97 job
[0] = do_test_id(blk
[0], " ", false);
98 job
[0] = do_test_id(blk
[0], "123", false);
99 job
[0] = do_test_id(blk
[0], "_id", false);
100 job
[0] = do_test_id(blk
[0], "-id", false);
101 job
[0] = do_test_id(blk
[0], ".id", false);
102 job
[0] = do_test_id(blk
[0], "#id", false);
104 /* This one is valid */
105 job
[0] = do_test_id(blk
[0], "id0", true);
107 /* We cannot have two jobs in the same BDS */
108 do_test_id(blk
[0], "id1", false);
110 /* Duplicate job IDs are not allowed */
111 job
[1] = do_test_id(blk
[1], "id0", false);
113 /* But once job[0] finishes we can reuse its ID */
114 block_job_unref(job
[0]);
115 job
[1] = do_test_id(blk
[1], "id0", true);
117 /* No job ID specified, defaults to the backend name ('drive1') */
118 block_job_unref(job
[1]);
119 job
[1] = do_test_id(blk
[1], NULL
, true);
121 /* Duplicate job ID */
122 job
[2] = do_test_id(blk
[2], "drive1", false);
124 /* The ID of job[2] would default to 'drive2' but it is already in use */
125 job
[0] = do_test_id(blk
[0], "drive2", true);
126 job
[2] = do_test_id(blk
[2], NULL
, false);
128 /* This one is valid */
129 job
[2] = do_test_id(blk
[2], "id_2", true);
131 block_job_unref(job
[0]);
132 block_job_unref(job
[1]);
133 block_job_unref(job
[2]);
140 int main(int argc
, char **argv
)
142 qemu_init_main_loop(&error_abort
);
144 g_test_init(&argc
, &argv
, NULL
);
145 g_test_add_func("/blockjob/ids", test_job_ids
);