Acceptance tests: drop left over usage of ":avocado: enable"
[qemu/ar7.git] / tests / test-blockjob.c
blob68a0819495ee50ef4306ff454eec54f4faf279ba
1 /*
2 * Blockjob tests
4 * Copyright Igalia, S.L. 2016
6 * Authors:
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"
18 #include "qapi/qmp/qdict.h"
20 static const BlockJobDriver test_block_job_driver = {
21 .job_driver = {
22 .instance_size = sizeof(BlockJob),
23 .free = block_job_free,
24 .user_resume = block_job_user_resume,
25 .drain = block_job_drain,
29 static void block_job_cb(void *opaque, int ret)
33 static BlockJob *mk_job(BlockBackend *blk, const char *id,
34 const BlockJobDriver *drv, bool should_succeed,
35 int flags)
37 BlockJob *job;
38 Error *errp = NULL;
40 job = block_job_create(id, drv, NULL, blk_bs(blk),
41 0, BLK_PERM_ALL, 0, flags, block_job_cb,
42 NULL, &errp);
43 if (should_succeed) {
44 g_assert_null(errp);
45 g_assert_nonnull(job);
46 if (id) {
47 g_assert_cmpstr(job->job.id, ==, id);
48 } else {
49 g_assert_cmpstr(job->job.id, ==, blk_name(blk));
51 } else {
52 g_assert_nonnull(errp);
53 g_assert_null(job);
54 error_free(errp);
57 return job;
60 static BlockJob *do_test_id(BlockBackend *blk, const char *id,
61 bool should_succeed)
63 return mk_job(blk, id, &test_block_job_driver,
64 should_succeed, JOB_DEFAULT);
67 /* This creates a BlockBackend (optionally with a name) with a
68 * BlockDriverState inserted. */
69 static BlockBackend *create_blk(const char *name)
71 /* No I/O is performed on this device */
72 BlockBackend *blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL);
73 BlockDriverState *bs;
75 QDict *opt = qdict_new();
76 qdict_put_str(opt, "file.read-zeroes", "on");
77 bs = bdrv_open("null-co://", NULL, opt, 0, &error_abort);
78 g_assert_nonnull(bs);
80 blk_insert_bs(blk, bs, &error_abort);
81 bdrv_unref(bs);
83 if (name) {
84 Error *errp = NULL;
85 monitor_add_blk(blk, name, &errp);
86 g_assert_null(errp);
89 return blk;
92 /* This destroys the backend */
93 static void destroy_blk(BlockBackend *blk)
95 if (blk_name(blk)[0] != '\0') {
96 monitor_remove_blk(blk);
99 blk_remove_bs(blk);
100 blk_unref(blk);
103 static void test_job_ids(void)
105 BlockBackend *blk[3];
106 BlockJob *job[3];
108 blk[0] = create_blk(NULL);
109 blk[1] = create_blk("drive1");
110 blk[2] = create_blk("drive2");
112 /* No job ID provided and the block backend has no name */
113 job[0] = do_test_id(blk[0], NULL, false);
115 /* These are all invalid job IDs */
116 job[0] = do_test_id(blk[0], "0id", false);
117 job[0] = do_test_id(blk[0], "", false);
118 job[0] = do_test_id(blk[0], " ", false);
119 job[0] = do_test_id(blk[0], "123", false);
120 job[0] = do_test_id(blk[0], "_id", false);
121 job[0] = do_test_id(blk[0], "-id", false);
122 job[0] = do_test_id(blk[0], ".id", false);
123 job[0] = do_test_id(blk[0], "#id", false);
125 /* This one is valid */
126 job[0] = do_test_id(blk[0], "id0", true);
128 /* We can have two jobs in the same BDS */
129 job[1] = do_test_id(blk[0], "id1", true);
130 job_early_fail(&job[1]->job);
132 /* Duplicate job IDs are not allowed */
133 job[1] = do_test_id(blk[1], "id0", false);
135 /* But once job[0] finishes we can reuse its ID */
136 job_early_fail(&job[0]->job);
137 job[1] = do_test_id(blk[1], "id0", true);
139 /* No job ID specified, defaults to the backend name ('drive1') */
140 job_early_fail(&job[1]->job);
141 job[1] = do_test_id(blk[1], NULL, true);
143 /* Duplicate job ID */
144 job[2] = do_test_id(blk[2], "drive1", false);
146 /* The ID of job[2] would default to 'drive2' but it is already in use */
147 job[0] = do_test_id(blk[0], "drive2", true);
148 job[2] = do_test_id(blk[2], NULL, false);
150 /* This one is valid */
151 job[2] = do_test_id(blk[2], "id_2", true);
153 job_early_fail(&job[0]->job);
154 job_early_fail(&job[1]->job);
155 job_early_fail(&job[2]->job);
157 destroy_blk(blk[0]);
158 destroy_blk(blk[1]);
159 destroy_blk(blk[2]);
162 typedef struct CancelJob {
163 BlockJob common;
164 BlockBackend *blk;
165 bool should_converge;
166 bool should_complete;
167 } CancelJob;
169 static void cancel_job_complete(Job *job, Error **errp)
171 CancelJob *s = container_of(job, CancelJob, common.job);
172 s->should_complete = true;
175 static int coroutine_fn cancel_job_run(Job *job, Error **errp)
177 CancelJob *s = container_of(job, CancelJob, common.job);
179 while (!s->should_complete) {
180 if (job_is_cancelled(&s->common.job)) {
181 return 0;
184 if (!job_is_ready(&s->common.job) && s->should_converge) {
185 job_transition_to_ready(&s->common.job);
188 job_sleep_ns(&s->common.job, 100000);
191 return 0;
194 static const BlockJobDriver test_cancel_driver = {
195 .job_driver = {
196 .instance_size = sizeof(CancelJob),
197 .free = block_job_free,
198 .user_resume = block_job_user_resume,
199 .drain = block_job_drain,
200 .run = cancel_job_run,
201 .complete = cancel_job_complete,
205 static CancelJob *create_common(Job **pjob)
207 BlockBackend *blk;
208 Job *job;
209 BlockJob *bjob;
210 CancelJob *s;
212 blk = create_blk(NULL);
213 bjob = mk_job(blk, "Steve", &test_cancel_driver, true,
214 JOB_MANUAL_FINALIZE | JOB_MANUAL_DISMISS);
215 job = &bjob->job;
216 job_ref(job);
217 assert(job->status == JOB_STATUS_CREATED);
218 s = container_of(bjob, CancelJob, common);
219 s->blk = blk;
221 *pjob = job;
222 return s;
225 static void cancel_common(CancelJob *s)
227 BlockJob *job = &s->common;
228 BlockBackend *blk = s->blk;
229 JobStatus sts = job->job.status;
230 AioContext *ctx;
232 ctx = job->job.aio_context;
233 aio_context_acquire(ctx);
235 job_cancel_sync(&job->job);
236 if (sts != JOB_STATUS_CREATED && sts != JOB_STATUS_CONCLUDED) {
237 Job *dummy = &job->job;
238 job_dismiss(&dummy, &error_abort);
240 assert(job->job.status == JOB_STATUS_NULL);
241 job_unref(&job->job);
242 destroy_blk(blk);
244 aio_context_release(ctx);
247 static void test_cancel_created(void)
249 Job *job;
250 CancelJob *s;
252 s = create_common(&job);
253 cancel_common(s);
256 static void test_cancel_running(void)
258 Job *job;
259 CancelJob *s;
261 s = create_common(&job);
263 job_start(job);
264 assert(job->status == JOB_STATUS_RUNNING);
266 cancel_common(s);
269 static void test_cancel_paused(void)
271 Job *job;
272 CancelJob *s;
274 s = create_common(&job);
276 job_start(job);
277 assert(job->status == JOB_STATUS_RUNNING);
279 job_user_pause(job, &error_abort);
280 job_enter(job);
281 assert(job->status == JOB_STATUS_PAUSED);
283 cancel_common(s);
286 static void test_cancel_ready(void)
288 Job *job;
289 CancelJob *s;
291 s = create_common(&job);
293 job_start(job);
294 assert(job->status == JOB_STATUS_RUNNING);
296 s->should_converge = true;
297 job_enter(job);
298 assert(job->status == JOB_STATUS_READY);
300 cancel_common(s);
303 static void test_cancel_standby(void)
305 Job *job;
306 CancelJob *s;
308 s = create_common(&job);
310 job_start(job);
311 assert(job->status == JOB_STATUS_RUNNING);
313 s->should_converge = true;
314 job_enter(job);
315 assert(job->status == JOB_STATUS_READY);
317 job_user_pause(job, &error_abort);
318 job_enter(job);
319 assert(job->status == JOB_STATUS_STANDBY);
321 cancel_common(s);
324 static void test_cancel_pending(void)
326 Job *job;
327 CancelJob *s;
329 s = create_common(&job);
331 job_start(job);
332 assert(job->status == JOB_STATUS_RUNNING);
334 s->should_converge = true;
335 job_enter(job);
336 assert(job->status == JOB_STATUS_READY);
338 job_complete(job, &error_abort);
339 job_enter(job);
340 while (!job->deferred_to_main_loop) {
341 aio_poll(qemu_get_aio_context(), true);
343 assert(job->status == JOB_STATUS_READY);
344 aio_poll(qemu_get_aio_context(), true);
345 assert(job->status == JOB_STATUS_PENDING);
347 cancel_common(s);
350 static void test_cancel_concluded(void)
352 Job *job;
353 CancelJob *s;
355 s = create_common(&job);
357 job_start(job);
358 assert(job->status == JOB_STATUS_RUNNING);
360 s->should_converge = true;
361 job_enter(job);
362 assert(job->status == JOB_STATUS_READY);
364 job_complete(job, &error_abort);
365 job_enter(job);
366 while (!job->deferred_to_main_loop) {
367 aio_poll(qemu_get_aio_context(), true);
369 assert(job->status == JOB_STATUS_READY);
370 aio_poll(qemu_get_aio_context(), true);
371 assert(job->status == JOB_STATUS_PENDING);
373 job_finalize(job, &error_abort);
374 assert(job->status == JOB_STATUS_CONCLUDED);
376 cancel_common(s);
379 int main(int argc, char **argv)
381 qemu_init_main_loop(&error_abort);
382 bdrv_init();
384 g_test_init(&argc, &argv, NULL);
385 g_test_add_func("/blockjob/ids", test_job_ids);
386 g_test_add_func("/blockjob/cancel/created", test_cancel_created);
387 g_test_add_func("/blockjob/cancel/running", test_cancel_running);
388 g_test_add_func("/blockjob/cancel/paused", test_cancel_paused);
389 g_test_add_func("/blockjob/cancel/ready", test_cancel_ready);
390 g_test_add_func("/blockjob/cancel/standby", test_cancel_standby);
391 g_test_add_func("/blockjob/cancel/pending", test_cancel_pending);
392 g_test_add_func("/blockjob/cancel/concluded", test_cancel_concluded);
393 return g_test_run();