sm501: Do not clear read only bits when writing registers
[qemu/rayw.git] / tests / test-blockjob.c
blobcb42f06e61483df0b50461aa6e0ecb611e216ddd
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"
19 static const BlockJobDriver test_block_job_driver = {
20 .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,
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(0, BLK_PERM_ALL);
72 BlockDriverState *bs;
74 bs = bdrv_open("null-co://", NULL, NULL, 0, &error_abort);
75 g_assert_nonnull(bs);
77 blk_insert_bs(blk, bs, &error_abort);
78 bdrv_unref(bs);
80 if (name) {
81 Error *errp = NULL;
82 monitor_add_blk(blk, name, &errp);
83 g_assert_null(errp);
86 return blk;
89 /* This destroys the backend */
90 static void destroy_blk(BlockBackend *blk)
92 if (blk_name(blk)[0] != '\0') {
93 monitor_remove_blk(blk);
96 blk_remove_bs(blk);
97 blk_unref(blk);
100 static void test_job_ids(void)
102 BlockBackend *blk[3];
103 BlockJob *job[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);
153 destroy_blk(blk[0]);
154 destroy_blk(blk[1]);
155 destroy_blk(blk[2]);
158 typedef struct CancelJob {
159 BlockJob common;
160 BlockBackend *blk;
161 bool should_converge;
162 bool should_complete;
163 bool completed;
164 } CancelJob;
166 static void cancel_job_completed(Job *job, void *opaque)
168 CancelJob *s = opaque;
169 s->completed = true;
170 job_completed(job, 0, NULL);
173 static void cancel_job_complete(Job *job, Error **errp)
175 CancelJob *s = container_of(job, CancelJob, common.job);
176 s->should_complete = true;
179 static void coroutine_fn cancel_job_start(void *opaque)
181 CancelJob *s = opaque;
183 while (!s->should_complete) {
184 if (job_is_cancelled(&s->common.job)) {
185 goto defer;
188 if (!job_is_ready(&s->common.job) && s->should_converge) {
189 job_transition_to_ready(&s->common.job);
192 job_sleep_ns(&s->common.job, 100000);
195 defer:
196 job_defer_to_main_loop(&s->common.job, cancel_job_completed, s);
199 static const BlockJobDriver test_cancel_driver = {
200 .job_driver = {
201 .instance_size = sizeof(CancelJob),
202 .free = block_job_free,
203 .user_resume = block_job_user_resume,
204 .drain = block_job_drain,
205 .start = cancel_job_start,
206 .complete = cancel_job_complete,
210 static CancelJob *create_common(BlockJob **pjob)
212 BlockBackend *blk;
213 BlockJob *job;
214 CancelJob *s;
216 blk = create_blk(NULL);
217 job = mk_job(blk, "Steve", &test_cancel_driver, true,
218 JOB_MANUAL_FINALIZE | JOB_MANUAL_DISMISS);
219 job_ref(&job->job);
220 assert(job->job.status == JOB_STATUS_CREATED);
221 s = container_of(job, CancelJob, common);
222 s->blk = blk;
224 *pjob = job;
225 return s;
228 static void cancel_common(CancelJob *s)
230 BlockJob *job = &s->common;
231 BlockBackend *blk = s->blk;
232 JobStatus sts = job->job.status;
234 job_cancel_sync(&job->job);
235 if (sts != JOB_STATUS_CREATED && sts != JOB_STATUS_CONCLUDED) {
236 Job *dummy = &job->job;
237 job_dismiss(&dummy, &error_abort);
239 assert(job->job.status == JOB_STATUS_NULL);
240 job_unref(&job->job);
241 destroy_blk(blk);
244 static void test_cancel_created(void)
246 BlockJob *job;
247 CancelJob *s;
249 s = create_common(&job);
250 cancel_common(s);
253 static void test_cancel_running(void)
255 BlockJob *job;
256 CancelJob *s;
258 s = create_common(&job);
260 job_start(&job->job);
261 assert(job->job.status == JOB_STATUS_RUNNING);
263 cancel_common(s);
266 static void test_cancel_paused(void)
268 BlockJob *job;
269 CancelJob *s;
271 s = create_common(&job);
273 job_start(&job->job);
274 assert(job->job.status == JOB_STATUS_RUNNING);
276 job_user_pause(&job->job, &error_abort);
277 job_enter(&job->job);
278 assert(job->job.status == JOB_STATUS_PAUSED);
280 cancel_common(s);
283 static void test_cancel_ready(void)
285 BlockJob *job;
286 CancelJob *s;
288 s = create_common(&job);
290 job_start(&job->job);
291 assert(job->job.status == JOB_STATUS_RUNNING);
293 s->should_converge = true;
294 job_enter(&job->job);
295 assert(job->job.status == JOB_STATUS_READY);
297 cancel_common(s);
300 static void test_cancel_standby(void)
302 BlockJob *job;
303 CancelJob *s;
305 s = create_common(&job);
307 job_start(&job->job);
308 assert(job->job.status == JOB_STATUS_RUNNING);
310 s->should_converge = true;
311 job_enter(&job->job);
312 assert(job->job.status == JOB_STATUS_READY);
314 job_user_pause(&job->job, &error_abort);
315 job_enter(&job->job);
316 assert(job->job.status == JOB_STATUS_STANDBY);
318 cancel_common(s);
321 static void test_cancel_pending(void)
323 BlockJob *job;
324 CancelJob *s;
326 s = create_common(&job);
328 job_start(&job->job);
329 assert(job->job.status == JOB_STATUS_RUNNING);
331 s->should_converge = true;
332 job_enter(&job->job);
333 assert(job->job.status == JOB_STATUS_READY);
335 job_complete(&job->job, &error_abort);
336 job_enter(&job->job);
337 while (!s->completed) {
338 aio_poll(qemu_get_aio_context(), true);
340 assert(job->job.status == JOB_STATUS_PENDING);
342 cancel_common(s);
345 static void test_cancel_concluded(void)
347 BlockJob *job;
348 CancelJob *s;
350 s = create_common(&job);
352 job_start(&job->job);
353 assert(job->job.status == JOB_STATUS_RUNNING);
355 s->should_converge = true;
356 job_enter(&job->job);
357 assert(job->job.status == JOB_STATUS_READY);
359 job_complete(&job->job, &error_abort);
360 job_enter(&job->job);
361 while (!s->completed) {
362 aio_poll(qemu_get_aio_context(), true);
364 assert(job->job.status == JOB_STATUS_PENDING);
366 job_finalize(&job->job, &error_abort);
367 assert(job->job.status == JOB_STATUS_CONCLUDED);
369 cancel_common(s);
372 int main(int argc, char **argv)
374 qemu_init_main_loop(&error_abort);
375 bdrv_init();
377 g_test_init(&argc, &argv, NULL);
378 g_test_add_func("/blockjob/ids", test_job_ids);
379 g_test_add_func("/blockjob/cancel/created", test_cancel_created);
380 g_test_add_func("/blockjob/cancel/running", test_cancel_running);
381 g_test_add_func("/blockjob/cancel/paused", test_cancel_paused);
382 g_test_add_func("/blockjob/cancel/ready", test_cancel_ready);
383 g_test_add_func("/blockjob/cancel/standby", test_cancel_standby);
384 g_test_add_func("/blockjob/cancel/pending", test_cancel_pending);
385 g_test_add_func("/blockjob/cancel/concluded", test_cancel_concluded);
386 return g_test_run();