4 * Copyright Igalia, S.L. 2016
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 = {
21 .instance_size = sizeof(BlockJob),
22 .free = block_job_free,
23 .user_resume = block_job_user_resume,
24 .drain = block_job_drain,
28 static void block_job_cb(void *opaque, int ret)
32 static BlockJob *mk_job(BlockBackend *blk, const char *id,
33 const BlockJobDriver *drv, bool should_succeed,
39 job = block_job_create(id, drv, NULL, blk_bs(blk),
40 0, BLK_PERM_ALL, 0, flags, block_job_cb,
44 g_assert_nonnull(job);
46 g_assert_cmpstr(job->job.id, ==, id);
48 g_assert_cmpstr(job->job.id, ==, blk_name(blk));
51 g_assert_nonnull(errp);
59 static BlockJob *do_test_id(BlockBackend *blk, const char *id,
62 return mk_job(blk, id, &test_block_job_driver,
63 should_succeed, JOB_DEFAULT);
66 /* This creates a BlockBackend (optionally with a name) with a
67 * BlockDriverState inserted. */
68 static BlockBackend *create_blk(const char *name)
70 /* No I/O is performed on this device */
71 BlockBackend *blk = blk_new(0, BLK_PERM_ALL);
74 bs = bdrv_open("null-co://", NULL, NULL, 0, &error_abort);
77 blk_insert_bs(blk, bs, &error_abort);
82 monitor_add_blk(blk, name, &errp);
89 /* This destroys the backend */
90 static void destroy_blk(BlockBackend *blk)
92 if (blk_name(blk)[0] != '\0') {
93 monitor_remove_blk(blk);
100 static void test_job_ids(void)
102 BlockBackend *blk[3];
105 blk[0] = create_blk(NULL);
106 blk[1] = create_blk("drive1");
107 blk[2] = create_blk("drive2");
109 /* No job ID provided and the block backend has no name */
110 job[0] = do_test_id(blk[0], NULL, false);
112 /* These are all invalid job IDs */
113 job[0] = do_test_id(blk[0], "0id", false);
114 job[0] = do_test_id(blk[0], "", false);
115 job[0] = do_test_id(blk[0], " ", false);
116 job[0] = do_test_id(blk[0], "123", false);
117 job[0] = do_test_id(blk[0], "_id", false);
118 job[0] = do_test_id(blk[0], "-id", false);
119 job[0] = do_test_id(blk[0], ".id", false);
120 job[0] = do_test_id(blk[0], "#id", false);
122 /* This one is valid */
123 job[0] = do_test_id(blk[0], "id0", true);
125 /* We cannot have two jobs in the same BDS */
126 do_test_id(blk[0], "id1", false);
128 /* Duplicate job IDs are not allowed */
129 job[1] = do_test_id(blk[1], "id0", false);
131 /* But once job[0] finishes we can reuse its ID */
132 job_early_fail(&job[0]->job);
133 job[1] = do_test_id(blk[1], "id0", true);
135 /* No job ID specified, defaults to the backend name ('drive1') */
136 job_early_fail(&job[1]->job);
137 job[1] = do_test_id(blk[1], NULL, true);
139 /* Duplicate job ID */
140 job[2] = do_test_id(blk[2], "drive1", false);
142 /* The ID of job[2] would default to 'drive2' but it is already in use */
143 job[0] = do_test_id(blk[0], "drive2", true);
144 job[2] = do_test_id(blk[2], NULL, false);
146 /* This one is valid */
147 job[2] = do_test_id(blk[2], "id_2", true);
149 job_early_fail(&job[0]->job);
150 job_early_fail(&job[1]->job);
151 job_early_fail(&job[2]->job);
158 typedef struct CancelJob {
161 bool should_converge;
162 bool should_complete;
165 static void cancel_job_complete(Job *job, Error **errp)
167 CancelJob *s = container_of(job, CancelJob, common.job);
168 s->should_complete = true;
171 static int coroutine_fn cancel_job_run(Job *job, Error **errp)
173 CancelJob *s = container_of(job, CancelJob, common.job);
175 while (!s->should_complete) {
176 if (job_is_cancelled(&s->common.job)) {
180 if (!job_is_ready(&s->common.job) && s->should_converge) {
181 job_transition_to_ready(&s->common.job);
184 job_sleep_ns(&s->common.job, 100000);
190 static const BlockJobDriver test_cancel_driver = {
192 .instance_size = sizeof(CancelJob),
193 .free = block_job_free,
194 .user_resume = block_job_user_resume,
195 .drain = block_job_drain,
196 .run = cancel_job_run,
197 .complete = cancel_job_complete,
201 static CancelJob *create_common(Job **pjob)
208 blk = create_blk(NULL);
209 bjob = mk_job(blk, "Steve", &test_cancel_driver, true,
210 JOB_MANUAL_FINALIZE | JOB_MANUAL_DISMISS);
213 assert(job->status == JOB_STATUS_CREATED);
214 s = container_of(bjob, CancelJob, common);
221 static void cancel_common(CancelJob *s)
223 BlockJob *job = &s->common;
224 BlockBackend *blk = s->blk;
225 JobStatus sts = job->job.status;
227 job_cancel_sync(&job->job);
228 if (sts != JOB_STATUS_CREATED && sts != JOB_STATUS_CONCLUDED) {
229 Job *dummy = &job->job;
230 job_dismiss(&dummy, &error_abort);
232 assert(job->job.status == JOB_STATUS_NULL);
233 job_unref(&job->job);
237 static void test_cancel_created(void)
242 s = create_common(&job);
246 static void test_cancel_running(void)
251 s = create_common(&job);
254 assert(job->status == JOB_STATUS_RUNNING);
259 static void test_cancel_paused(void)
264 s = create_common(&job);
267 assert(job->status == JOB_STATUS_RUNNING);
269 job_user_pause(job, &error_abort);
271 assert(job->status == JOB_STATUS_PAUSED);
276 static void test_cancel_ready(void)
281 s = create_common(&job);
284 assert(job->status == JOB_STATUS_RUNNING);
286 s->should_converge = true;
288 assert(job->status == JOB_STATUS_READY);
293 static void test_cancel_standby(void)
298 s = create_common(&job);
301 assert(job->status == JOB_STATUS_RUNNING);
303 s->should_converge = true;
305 assert(job->status == JOB_STATUS_READY);
307 job_user_pause(job, &error_abort);
309 assert(job->status == JOB_STATUS_STANDBY);
314 static void test_cancel_pending(void)
319 s = create_common(&job);
322 assert(job->status == JOB_STATUS_RUNNING);
324 s->should_converge = true;
326 assert(job->status == JOB_STATUS_READY);
328 job_complete(job, &error_abort);
330 while (!job->deferred_to_main_loop) {
331 aio_poll(qemu_get_aio_context(), true);
333 assert(job->status == JOB_STATUS_READY);
334 aio_poll(qemu_get_aio_context(), true);
335 assert(job->status == JOB_STATUS_PENDING);
340 static void test_cancel_concluded(void)
345 s = create_common(&job);
348 assert(job->status == JOB_STATUS_RUNNING);
350 s->should_converge = true;
352 assert(job->status == JOB_STATUS_READY);
354 job_complete(job, &error_abort);
356 while (!job->deferred_to_main_loop) {
357 aio_poll(qemu_get_aio_context(), true);
359 assert(job->status == JOB_STATUS_READY);
360 aio_poll(qemu_get_aio_context(), true);
361 assert(job->status == JOB_STATUS_PENDING);
363 job_finalize(job, &error_abort);
364 assert(job->status == JOB_STATUS_CONCLUDED);
369 int main(int argc, char **argv)
371 qemu_init_main_loop(&error_abort);
374 g_test_init(&argc, &argv, NULL);
375 g_test_add_func("/blockjob/ids", test_job_ids);
376 g_test_add_func("/blockjob/cancel/created", test_cancel_created);
377 g_test_add_func("/blockjob/cancel/running", test_cancel_running);
378 g_test_add_func("/blockjob/cancel/paused", test_cancel_paused);
379 g_test_add_func("/blockjob/cancel/ready", test_cancel_ready);
380 g_test_add_func("/blockjob/cancel/standby", test_cancel_standby);
381 g_test_add_func("/blockjob/cancel/pending", test_cancel_pending);
382 g_test_add_func("/blockjob/cancel/concluded", test_cancel_concluded);