xics: Link ICP_PROP_XICS property to ICPState::xics pointer
[qemu/ar7.git] / tests / test-blockjob.c
blob7844c9ffcb7fc21d3436840d64df9d6aabce60a0
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,
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,
34 int flags)
36 BlockJob *job;
37 Error *errp = NULL;
39 job = block_job_create(id, drv, NULL, blk_bs(blk),
40 0, BLK_PERM_ALL, 0, flags, block_job_cb,
41 NULL, &errp);
42 if (should_succeed) {
43 g_assert_null(errp);
44 g_assert_nonnull(job);
45 if (id) {
46 g_assert_cmpstr(job->job.id, ==, id);
47 } else {
48 g_assert_cmpstr(job->job.id, ==, blk_name(blk));
50 } else {
51 g_assert_nonnull(errp);
52 g_assert_null(job);
53 error_free(errp);
56 return job;
59 static BlockJob *do_test_id(BlockBackend *blk, const char *id,
60 bool should_succeed)
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(qemu_get_aio_context(), 0, BLK_PERM_ALL);
72 BlockDriverState *bs;
74 QDict *opt = qdict_new();
75 qdict_put_str(opt, "file.read-zeroes", "on");
76 bs = bdrv_open("null-co://", NULL, opt, 0, &error_abort);
77 g_assert_nonnull(bs);
79 blk_insert_bs(blk, bs, &error_abort);
80 bdrv_unref(bs);
82 if (name) {
83 Error *errp = NULL;
84 monitor_add_blk(blk, name, &errp);
85 g_assert_null(errp);
88 return blk;
91 /* This destroys the backend */
92 static void destroy_blk(BlockBackend *blk)
94 if (blk_name(blk)[0] != '\0') {
95 monitor_remove_blk(blk);
98 blk_remove_bs(blk);
99 blk_unref(blk);
102 static void test_job_ids(void)
104 BlockBackend *blk[3];
105 BlockJob *job[3];
107 blk[0] = create_blk(NULL);
108 blk[1] = create_blk("drive1");
109 blk[2] = create_blk("drive2");
111 /* No job ID provided and the block backend has no name */
112 job[0] = do_test_id(blk[0], NULL, false);
114 /* These are all invalid job IDs */
115 job[0] = do_test_id(blk[0], "0id", false);
116 job[0] = do_test_id(blk[0], "", false);
117 job[0] = do_test_id(blk[0], " ", false);
118 job[0] = do_test_id(blk[0], "123", false);
119 job[0] = do_test_id(blk[0], "_id", 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);
124 /* This one is valid */
125 job[0] = do_test_id(blk[0], "id0", true);
127 /* We can have two jobs in the same BDS */
128 job[1] = do_test_id(blk[0], "id1", true);
129 job_early_fail(&job[1]->job);
131 /* Duplicate job IDs are not allowed */
132 job[1] = do_test_id(blk[1], "id0", false);
134 /* But once job[0] finishes we can reuse its ID */
135 job_early_fail(&job[0]->job);
136 job[1] = do_test_id(blk[1], "id0", true);
138 /* No job ID specified, defaults to the backend name ('drive1') */
139 job_early_fail(&job[1]->job);
140 job[1] = do_test_id(blk[1], NULL, true);
142 /* Duplicate job ID */
143 job[2] = do_test_id(blk[2], "drive1", false);
145 /* The ID of job[2] would default to 'drive2' but it is already in use */
146 job[0] = do_test_id(blk[0], "drive2", true);
147 job[2] = do_test_id(blk[2], NULL, false);
149 /* This one is valid */
150 job[2] = do_test_id(blk[2], "id_2", true);
152 job_early_fail(&job[0]->job);
153 job_early_fail(&job[1]->job);
154 job_early_fail(&job[2]->job);
156 destroy_blk(blk[0]);
157 destroy_blk(blk[1]);
158 destroy_blk(blk[2]);
161 typedef struct CancelJob {
162 BlockJob common;
163 BlockBackend *blk;
164 bool should_converge;
165 bool should_complete;
166 } CancelJob;
168 static void cancel_job_complete(Job *job, Error **errp)
170 CancelJob *s = container_of(job, CancelJob, common.job);
171 s->should_complete = true;
174 static int coroutine_fn cancel_job_run(Job *job, Error **errp)
176 CancelJob *s = container_of(job, CancelJob, common.job);
178 while (!s->should_complete) {
179 if (job_is_cancelled(&s->common.job)) {
180 return 0;
183 if (!job_is_ready(&s->common.job) && s->should_converge) {
184 job_transition_to_ready(&s->common.job);
187 job_sleep_ns(&s->common.job, 100000);
190 return 0;
193 static const BlockJobDriver test_cancel_driver = {
194 .job_driver = {
195 .instance_size = sizeof(CancelJob),
196 .free = block_job_free,
197 .user_resume = block_job_user_resume,
198 .run = cancel_job_run,
199 .complete = cancel_job_complete,
203 static CancelJob *create_common(Job **pjob)
205 BlockBackend *blk;
206 Job *job;
207 BlockJob *bjob;
208 CancelJob *s;
210 blk = create_blk(NULL);
211 bjob = mk_job(blk, "Steve", &test_cancel_driver, true,
212 JOB_MANUAL_FINALIZE | JOB_MANUAL_DISMISS);
213 job = &bjob->job;
214 job_ref(job);
215 assert(job->status == JOB_STATUS_CREATED);
216 s = container_of(bjob, CancelJob, common);
217 s->blk = blk;
219 *pjob = job;
220 return s;
223 static void cancel_common(CancelJob *s)
225 BlockJob *job = &s->common;
226 BlockBackend *blk = s->blk;
227 JobStatus sts = job->job.status;
228 AioContext *ctx;
230 ctx = job->job.aio_context;
231 aio_context_acquire(ctx);
233 job_cancel_sync(&job->job);
234 if (sts != JOB_STATUS_CREATED && sts != JOB_STATUS_CONCLUDED) {
235 Job *dummy = &job->job;
236 job_dismiss(&dummy, &error_abort);
238 assert(job->job.status == JOB_STATUS_NULL);
239 job_unref(&job->job);
240 destroy_blk(blk);
242 aio_context_release(ctx);
245 static void test_cancel_created(void)
247 Job *job;
248 CancelJob *s;
250 s = create_common(&job);
251 cancel_common(s);
254 static void test_cancel_running(void)
256 Job *job;
257 CancelJob *s;
259 s = create_common(&job);
261 job_start(job);
262 assert(job->status == JOB_STATUS_RUNNING);
264 cancel_common(s);
267 static void test_cancel_paused(void)
269 Job *job;
270 CancelJob *s;
272 s = create_common(&job);
274 job_start(job);
275 assert(job->status == JOB_STATUS_RUNNING);
277 job_user_pause(job, &error_abort);
278 job_enter(job);
279 assert(job->status == JOB_STATUS_PAUSED);
281 cancel_common(s);
284 static void test_cancel_ready(void)
286 Job *job;
287 CancelJob *s;
289 s = create_common(&job);
291 job_start(job);
292 assert(job->status == JOB_STATUS_RUNNING);
294 s->should_converge = true;
295 job_enter(job);
296 assert(job->status == JOB_STATUS_READY);
298 cancel_common(s);
301 static void test_cancel_standby(void)
303 Job *job;
304 CancelJob *s;
306 s = create_common(&job);
308 job_start(job);
309 assert(job->status == JOB_STATUS_RUNNING);
311 s->should_converge = true;
312 job_enter(job);
313 assert(job->status == JOB_STATUS_READY);
315 job_user_pause(job, &error_abort);
316 job_enter(job);
317 assert(job->status == JOB_STATUS_STANDBY);
319 cancel_common(s);
322 static void test_cancel_pending(void)
324 Job *job;
325 CancelJob *s;
327 s = create_common(&job);
329 job_start(job);
330 assert(job->status == JOB_STATUS_RUNNING);
332 s->should_converge = true;
333 job_enter(job);
334 assert(job->status == JOB_STATUS_READY);
336 job_complete(job, &error_abort);
337 job_enter(job);
338 while (!job->deferred_to_main_loop) {
339 aio_poll(qemu_get_aio_context(), true);
341 assert(job->status == JOB_STATUS_READY);
342 aio_poll(qemu_get_aio_context(), true);
343 assert(job->status == JOB_STATUS_PENDING);
345 cancel_common(s);
348 static void test_cancel_concluded(void)
350 Job *job;
351 CancelJob *s;
353 s = create_common(&job);
355 job_start(job);
356 assert(job->status == JOB_STATUS_RUNNING);
358 s->should_converge = true;
359 job_enter(job);
360 assert(job->status == JOB_STATUS_READY);
362 job_complete(job, &error_abort);
363 job_enter(job);
364 while (!job->deferred_to_main_loop) {
365 aio_poll(qemu_get_aio_context(), true);
367 assert(job->status == JOB_STATUS_READY);
368 aio_poll(qemu_get_aio_context(), true);
369 assert(job->status == JOB_STATUS_PENDING);
371 job_finalize(job, &error_abort);
372 assert(job->status == JOB_STATUS_CONCLUDED);
374 cancel_common(s);
377 int main(int argc, char **argv)
379 qemu_init_main_loop(&error_abort);
380 bdrv_init();
382 g_test_init(&argc, &argv, NULL);
383 g_test_add_func("/blockjob/ids", test_job_ids);
384 g_test_add_func("/blockjob/cancel/created", test_cancel_created);
385 g_test_add_func("/blockjob/cancel/running", test_cancel_running);
386 g_test_add_func("/blockjob/cancel/paused", test_cancel_paused);
387 g_test_add_func("/blockjob/cancel/ready", test_cancel_ready);
388 g_test_add_func("/blockjob/cancel/standby", test_cancel_standby);
389 g_test_add_func("/blockjob/cancel/pending", test_cancel_pending);
390 g_test_add_func("/blockjob/cancel/concluded", test_cancel_concluded);
391 return g_test_run();