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_int.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
*mk_job(BlockBackend
*blk
, const char *id
,
28 const BlockJobDriver
*drv
, bool should_succeed
,
34 job
= block_job_create(id
, drv
, NULL
, blk_bs(blk
),
35 0, BLK_PERM_ALL
, 0, flags
, block_job_cb
,
39 g_assert_nonnull(job
);
41 g_assert_cmpstr(job
->id
, ==, id
);
43 g_assert_cmpstr(job
->id
, ==, blk_name(blk
));
46 g_assert_nonnull(errp
);
54 static BlockJob
*do_test_id(BlockBackend
*blk
, const char *id
,
57 return mk_job(blk
, id
, &test_block_job_driver
,
58 should_succeed
, BLOCK_JOB_DEFAULT
);
61 /* This creates a BlockBackend (optionally with a name) with a
62 * BlockDriverState inserted. */
63 static BlockBackend
*create_blk(const char *name
)
65 /* No I/O is performed on this device */
66 BlockBackend
*blk
= blk_new(0, BLK_PERM_ALL
);
69 bs
= bdrv_open("null-co://", NULL
, NULL
, 0, &error_abort
);
72 blk_insert_bs(blk
, bs
, &error_abort
);
77 monitor_add_blk(blk
, name
, &errp
);
84 /* This destroys the backend */
85 static void destroy_blk(BlockBackend
*blk
)
87 if (blk_name(blk
)[0] != '\0') {
88 monitor_remove_blk(blk
);
95 static void test_job_ids(void)
100 blk
[0] = create_blk(NULL
);
101 blk
[1] = create_blk("drive1");
102 blk
[2] = create_blk("drive2");
104 /* No job ID provided and the block backend has no name */
105 job
[0] = do_test_id(blk
[0], NULL
, false);
107 /* These are all invalid job IDs */
108 job
[0] = do_test_id(blk
[0], "0id", false);
109 job
[0] = do_test_id(blk
[0], "", false);
110 job
[0] = do_test_id(blk
[0], " ", false);
111 job
[0] = do_test_id(blk
[0], "123", false);
112 job
[0] = do_test_id(blk
[0], "_id", false);
113 job
[0] = do_test_id(blk
[0], "-id", false);
114 job
[0] = do_test_id(blk
[0], ".id", false);
115 job
[0] = do_test_id(blk
[0], "#id", false);
117 /* This one is valid */
118 job
[0] = do_test_id(blk
[0], "id0", true);
120 /* We cannot have two jobs in the same BDS */
121 do_test_id(blk
[0], "id1", false);
123 /* Duplicate job IDs are not allowed */
124 job
[1] = do_test_id(blk
[1], "id0", false);
126 /* But once job[0] finishes we can reuse its ID */
127 block_job_early_fail(job
[0]);
128 job
[1] = do_test_id(blk
[1], "id0", true);
130 /* No job ID specified, defaults to the backend name ('drive1') */
131 block_job_early_fail(job
[1]);
132 job
[1] = do_test_id(blk
[1], NULL
, true);
134 /* Duplicate job ID */
135 job
[2] = do_test_id(blk
[2], "drive1", false);
137 /* The ID of job[2] would default to 'drive2' but it is already in use */
138 job
[0] = do_test_id(blk
[0], "drive2", true);
139 job
[2] = do_test_id(blk
[2], NULL
, false);
141 /* This one is valid */
142 job
[2] = do_test_id(blk
[2], "id_2", true);
144 block_job_early_fail(job
[0]);
145 block_job_early_fail(job
[1]);
146 block_job_early_fail(job
[2]);
153 typedef struct CancelJob
{
156 bool should_converge
;
157 bool should_complete
;
161 static void cancel_job_completed(BlockJob
*job
, void *opaque
)
163 CancelJob
*s
= opaque
;
165 block_job_completed(job
, 0);
168 static void cancel_job_complete(BlockJob
*job
, Error
**errp
)
170 CancelJob
*s
= container_of(job
, CancelJob
, common
);
171 s
->should_complete
= true;
174 static void coroutine_fn
cancel_job_start(void *opaque
)
176 CancelJob
*s
= opaque
;
178 while (!s
->should_complete
) {
179 if (block_job_is_cancelled(&s
->common
)) {
183 if (!s
->common
.ready
&& s
->should_converge
) {
184 block_job_event_ready(&s
->common
);
187 block_job_sleep_ns(&s
->common
, 100000);
191 block_job_defer_to_main_loop(&s
->common
, cancel_job_completed
, s
);
194 static const BlockJobDriver test_cancel_driver
= {
195 .instance_size
= sizeof(CancelJob
),
196 .start
= cancel_job_start
,
197 .complete
= cancel_job_complete
,
200 static CancelJob
*create_common(BlockJob
**pjob
)
206 blk
= create_blk(NULL
);
207 job
= mk_job(blk
, "Steve", &test_cancel_driver
, true,
208 BLOCK_JOB_MANUAL_FINALIZE
| BLOCK_JOB_MANUAL_DISMISS
);
210 assert(job
->status
== BLOCK_JOB_STATUS_CREATED
);
211 s
= container_of(job
, CancelJob
, common
);
218 static void cancel_common(CancelJob
*s
)
220 BlockJob
*job
= &s
->common
;
221 BlockBackend
*blk
= s
->blk
;
222 BlockJobStatus sts
= job
->status
;
224 block_job_cancel_sync(job
);
225 if ((sts
!= BLOCK_JOB_STATUS_CREATED
) &&
226 (sts
!= BLOCK_JOB_STATUS_CONCLUDED
)) {
227 BlockJob
*dummy
= job
;
228 block_job_dismiss(&dummy
, &error_abort
);
230 assert(job
->status
== BLOCK_JOB_STATUS_NULL
);
231 block_job_unref(job
);
235 static void test_cancel_created(void)
240 s
= create_common(&job
);
244 static void test_cancel_running(void)
249 s
= create_common(&job
);
251 block_job_start(job
);
252 assert(job
->status
== BLOCK_JOB_STATUS_RUNNING
);
257 static void test_cancel_paused(void)
262 s
= create_common(&job
);
264 block_job_start(job
);
265 assert(job
->status
== BLOCK_JOB_STATUS_RUNNING
);
267 block_job_user_pause(job
, &error_abort
);
268 block_job_enter(job
);
269 assert(job
->status
== BLOCK_JOB_STATUS_PAUSED
);
274 static void test_cancel_ready(void)
279 s
= create_common(&job
);
281 block_job_start(job
);
282 assert(job
->status
== BLOCK_JOB_STATUS_RUNNING
);
284 s
->should_converge
= true;
285 block_job_enter(job
);
286 assert(job
->status
== BLOCK_JOB_STATUS_READY
);
291 static void test_cancel_standby(void)
296 s
= create_common(&job
);
298 block_job_start(job
);
299 assert(job
->status
== BLOCK_JOB_STATUS_RUNNING
);
301 s
->should_converge
= true;
302 block_job_enter(job
);
303 assert(job
->status
== BLOCK_JOB_STATUS_READY
);
305 block_job_user_pause(job
, &error_abort
);
306 block_job_enter(job
);
307 assert(job
->status
== BLOCK_JOB_STATUS_STANDBY
);
312 static void test_cancel_pending(void)
317 s
= create_common(&job
);
319 block_job_start(job
);
320 assert(job
->status
== BLOCK_JOB_STATUS_RUNNING
);
322 s
->should_converge
= true;
323 block_job_enter(job
);
324 assert(job
->status
== BLOCK_JOB_STATUS_READY
);
326 block_job_complete(job
, &error_abort
);
327 block_job_enter(job
);
328 while (!s
->completed
) {
329 aio_poll(qemu_get_aio_context(), true);
331 assert(job
->status
== BLOCK_JOB_STATUS_PENDING
);
336 static void test_cancel_concluded(void)
341 s
= create_common(&job
);
343 block_job_start(job
);
344 assert(job
->status
== BLOCK_JOB_STATUS_RUNNING
);
346 s
->should_converge
= true;
347 block_job_enter(job
);
348 assert(job
->status
== BLOCK_JOB_STATUS_READY
);
350 block_job_complete(job
, &error_abort
);
351 block_job_enter(job
);
352 while (!s
->completed
) {
353 aio_poll(qemu_get_aio_context(), true);
355 assert(job
->status
== BLOCK_JOB_STATUS_PENDING
);
357 block_job_finalize(job
, &error_abort
);
358 assert(job
->status
== BLOCK_JOB_STATUS_CONCLUDED
);
363 int main(int argc
, char **argv
)
365 qemu_init_main_loop(&error_abort
);
368 g_test_init(&argc
, &argv
, NULL
);
369 g_test_add_func("/blockjob/ids", test_job_ids
);
370 g_test_add_func("/blockjob/cancel/created", test_cancel_created
);
371 g_test_add_func("/blockjob/cancel/running", test_cancel_running
);
372 g_test_add_func("/blockjob/cancel/paused", test_cancel_paused
);
373 g_test_add_func("/blockjob/cancel/ready", test_cancel_ready
);
374 g_test_add_func("/blockjob/cancel/standby", test_cancel_standby
);
375 g_test_add_func("/blockjob/cancel/pending", test_cancel_pending
);
376 g_test_add_func("/blockjob/cancel/concluded", test_cancel_concluded
);