]> git.proxmox.com Git - mirror_qemu.git/blame - tests/test-blockjob.c
Merge remote-tracking branch 'remotes/lersek/tags/edk2-pull-2019-04-22' into staging
[mirror_qemu.git] / tests / test-blockjob.c
CommitLineData
9ef8112a
AG
1/*
2 * Blockjob tests
3 *
4 * Copyright Igalia, S.L. 2016
5 *
6 * Authors:
7 * Alberto Garcia <berto@igalia.com>
8 *
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.
11 */
12
13#include "qemu/osdep.h"
14#include "qapi/error.h"
15#include "qemu/main-loop.h"
c87621ea 16#include "block/blockjob_int.h"
9ef8112a
AG
17#include "sysemu/block-backend.h"
18
19static const BlockJobDriver test_block_job_driver = {
33e9e9bd
KW
20 .job_driver = {
21 .instance_size = sizeof(BlockJob),
80fa2c75 22 .free = block_job_free,
b15de828 23 .user_resume = block_job_user_resume,
b69f777d 24 .drain = block_job_drain,
33e9e9bd 25 },
9ef8112a
AG
26};
27
28static void block_job_cb(void *opaque, int ret)
29{
30}
31
fb367e03
JS
32static BlockJob *mk_job(BlockBackend *blk, const char *id,
33 const BlockJobDriver *drv, bool should_succeed,
34 int flags)
9ef8112a
AG
35{
36 BlockJob *job;
37 Error *errp = NULL;
38
fb367e03
JS
39 job = block_job_create(id, drv, NULL, blk_bs(blk),
40 0, BLK_PERM_ALL, 0, flags, block_job_cb,
c6cc12bf 41 NULL, &errp);
9ef8112a
AG
42 if (should_succeed) {
43 g_assert_null(errp);
44 g_assert_nonnull(job);
45 if (id) {
33e9e9bd 46 g_assert_cmpstr(job->job.id, ==, id);
9ef8112a 47 } else {
33e9e9bd 48 g_assert_cmpstr(job->job.id, ==, blk_name(blk));
9ef8112a
AG
49 }
50 } else {
51 g_assert_nonnull(errp);
52 g_assert_null(job);
53 error_free(errp);
54 }
55
56 return job;
57}
58
fb367e03
JS
59static BlockJob *do_test_id(BlockBackend *blk, const char *id,
60 bool should_succeed)
61{
62 return mk_job(blk, id, &test_block_job_driver,
bb02b65c 63 should_succeed, JOB_DEFAULT);
fb367e03
JS
64}
65
9ef8112a
AG
66/* This creates a BlockBackend (optionally with a name) with a
67 * BlockDriverState inserted. */
68static BlockBackend *create_blk(const char *name)
69{
2807c0cd 70 /* No I/O is performed on this device */
6d0eb64d 71 BlockBackend *blk = blk_new(0, BLK_PERM_ALL);
d185cf0e
KW
72 BlockDriverState *bs;
73
74 bs = bdrv_open("null-co://", NULL, NULL, 0, &error_abort);
75 g_assert_nonnull(bs);
9ef8112a 76
d7086422 77 blk_insert_bs(blk, bs, &error_abort);
9ef8112a
AG
78 bdrv_unref(bs);
79
80 if (name) {
81 Error *errp = NULL;
82 monitor_add_blk(blk, name, &errp);
83 g_assert_null(errp);
84 }
85
86 return blk;
87}
88
89/* This destroys the backend */
90static void destroy_blk(BlockBackend *blk)
91{
92 if (blk_name(blk)[0] != '\0') {
93 monitor_remove_blk(blk);
94 }
95
96 blk_remove_bs(blk);
97 blk_unref(blk);
98}
99
100static void test_job_ids(void)
101{
102 BlockBackend *blk[3];
103 BlockJob *job[3];
104
105 blk[0] = create_blk(NULL);
106 blk[1] = create_blk("drive1");
107 blk[2] = create_blk("drive2");
108
109 /* No job ID provided and the block backend has no name */
110 job[0] = do_test_id(blk[0], NULL, false);
111
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);
121
122 /* This one is valid */
123 job[0] = do_test_id(blk[0], "id0", true);
124
125 /* We cannot have two jobs in the same BDS */
126 do_test_id(blk[0], "id1", false);
127
128 /* Duplicate job IDs are not allowed */
129 job[1] = do_test_id(blk[1], "id0", false);
130
131 /* But once job[0] finishes we can reuse its ID */
4ad35181 132 job_early_fail(&job[0]->job);
9ef8112a
AG
133 job[1] = do_test_id(blk[1], "id0", true);
134
135 /* No job ID specified, defaults to the backend name ('drive1') */
4ad35181 136 job_early_fail(&job[1]->job);
9ef8112a
AG
137 job[1] = do_test_id(blk[1], NULL, true);
138
139 /* Duplicate job ID */
140 job[2] = do_test_id(blk[2], "drive1", false);
141
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);
145
146 /* This one is valid */
147 job[2] = do_test_id(blk[2], "id_2", true);
148
4ad35181
KW
149 job_early_fail(&job[0]->job);
150 job_early_fail(&job[1]->job);
151 job_early_fail(&job[2]->job);
9ef8112a
AG
152
153 destroy_blk(blk[0]);
154 destroy_blk(blk[1]);
155 destroy_blk(blk[2]);
156}
157
fb367e03
JS
158typedef struct CancelJob {
159 BlockJob common;
160 BlockBackend *blk;
161 bool should_converge;
162 bool should_complete;
fb367e03
JS
163} CancelJob;
164
3453d972 165static void cancel_job_complete(Job *job, Error **errp)
fb367e03 166{
3453d972 167 CancelJob *s = container_of(job, CancelJob, common.job);
fb367e03
JS
168 s->should_complete = true;
169}
170
f67432a2 171static int coroutine_fn cancel_job_run(Job *job, Error **errp)
fb367e03 172{
f67432a2 173 CancelJob *s = container_of(job, CancelJob, common.job);
fb367e03
JS
174
175 while (!s->should_complete) {
daa7f2f9 176 if (job_is_cancelled(&s->common.job)) {
eb23654d 177 return 0;
fb367e03
JS
178 }
179
df956ae2 180 if (!job_is_ready(&s->common.job) && s->should_converge) {
2e1795b5 181 job_transition_to_ready(&s->common.job);
fb367e03
JS
182 }
183
5d43e86e 184 job_sleep_ns(&s->common.job, 100000);
fb367e03
JS
185 }
186
f67432a2 187 return 0;
fb367e03
JS
188}
189
190static const BlockJobDriver test_cancel_driver = {
33e9e9bd
KW
191 .job_driver = {
192 .instance_size = sizeof(CancelJob),
80fa2c75 193 .free = block_job_free,
b15de828 194 .user_resume = block_job_user_resume,
b69f777d 195 .drain = block_job_drain,
f67432a2 196 .run = cancel_job_run,
3453d972 197 .complete = cancel_job_complete,
33e9e9bd 198 },
fb367e03
JS
199};
200
0cc4643b 201static CancelJob *create_common(Job **pjob)
fb367e03
JS
202{
203 BlockBackend *blk;
0cc4643b
JS
204 Job *job;
205 BlockJob *bjob;
fb367e03
JS
206 CancelJob *s;
207
208 blk = create_blk(NULL);
0cc4643b
JS
209 bjob = mk_job(blk, "Steve", &test_cancel_driver, true,
210 JOB_MANUAL_FINALIZE | JOB_MANUAL_DISMISS);
211 job = &bjob->job;
212 job_ref(job);
213 assert(job->status == JOB_STATUS_CREATED);
214 s = container_of(bjob, CancelJob, common);
fb367e03
JS
215 s->blk = blk;
216
217 *pjob = job;
218 return s;
219}
220
221static void cancel_common(CancelJob *s)
222{
223 BlockJob *job = &s->common;
224 BlockBackend *blk = s->blk;
a50c2ab8 225 JobStatus sts = job->job.status;
30c070a5
KW
226 AioContext *ctx;
227
228 ctx = job->job.aio_context;
229 aio_context_acquire(ctx);
fb367e03 230
3d70ff53 231 job_cancel_sync(&job->job);
a50c2ab8 232 if (sts != JOB_STATUS_CREATED && sts != JOB_STATUS_CONCLUDED) {
5f9a6a08
KW
233 Job *dummy = &job->job;
234 job_dismiss(&dummy, &error_abort);
fb367e03 235 }
a50c2ab8 236 assert(job->job.status == JOB_STATUS_NULL);
80fa2c75 237 job_unref(&job->job);
fb367e03 238 destroy_blk(blk);
30c070a5
KW
239
240 aio_context_release(ctx);
fb367e03
JS
241}
242
243static void test_cancel_created(void)
244{
0cc4643b 245 Job *job;
fb367e03
JS
246 CancelJob *s;
247
248 s = create_common(&job);
249 cancel_common(s);
250}
251
252static void test_cancel_running(void)
253{
0cc4643b 254 Job *job;
fb367e03
JS
255 CancelJob *s;
256
257 s = create_common(&job);
258
0cc4643b
JS
259 job_start(job);
260 assert(job->status == JOB_STATUS_RUNNING);
fb367e03
JS
261
262 cancel_common(s);
263}
264
265static void test_cancel_paused(void)
266{
0cc4643b 267 Job *job;
fb367e03
JS
268 CancelJob *s;
269
270 s = create_common(&job);
271
0cc4643b
JS
272 job_start(job);
273 assert(job->status == JOB_STATUS_RUNNING);
fb367e03 274
0cc4643b
JS
275 job_user_pause(job, &error_abort);
276 job_enter(job);
277 assert(job->status == JOB_STATUS_PAUSED);
fb367e03
JS
278
279 cancel_common(s);
280}
281
282static void test_cancel_ready(void)
283{
0cc4643b 284 Job *job;
fb367e03
JS
285 CancelJob *s;
286
287 s = create_common(&job);
288
0cc4643b
JS
289 job_start(job);
290 assert(job->status == JOB_STATUS_RUNNING);
fb367e03
JS
291
292 s->should_converge = true;
0cc4643b
JS
293 job_enter(job);
294 assert(job->status == JOB_STATUS_READY);
fb367e03
JS
295
296 cancel_common(s);
297}
298
299static void test_cancel_standby(void)
300{
0cc4643b 301 Job *job;
fb367e03
JS
302 CancelJob *s;
303
304 s = create_common(&job);
305
0cc4643b
JS
306 job_start(job);
307 assert(job->status == JOB_STATUS_RUNNING);
fb367e03
JS
308
309 s->should_converge = true;
0cc4643b
JS
310 job_enter(job);
311 assert(job->status == JOB_STATUS_READY);
fb367e03 312
0cc4643b
JS
313 job_user_pause(job, &error_abort);
314 job_enter(job);
315 assert(job->status == JOB_STATUS_STANDBY);
fb367e03
JS
316
317 cancel_common(s);
318}
319
320static void test_cancel_pending(void)
321{
0cc4643b 322 Job *job;
fb367e03
JS
323 CancelJob *s;
324
325 s = create_common(&job);
326
0cc4643b
JS
327 job_start(job);
328 assert(job->status == JOB_STATUS_RUNNING);
fb367e03
JS
329
330 s->should_converge = true;
0cc4643b
JS
331 job_enter(job);
332 assert(job->status == JOB_STATUS_READY);
fb367e03 333
0cc4643b
JS
334 job_complete(job, &error_abort);
335 job_enter(job);
977d26fd 336 while (!job->deferred_to_main_loop) {
fb367e03
JS
337 aio_poll(qemu_get_aio_context(), true);
338 }
977d26fd
JS
339 assert(job->status == JOB_STATUS_READY);
340 aio_poll(qemu_get_aio_context(), true);
0cc4643b 341 assert(job->status == JOB_STATUS_PENDING);
fb367e03
JS
342
343 cancel_common(s);
344}
345
346static void test_cancel_concluded(void)
347{
0cc4643b 348 Job *job;
fb367e03
JS
349 CancelJob *s;
350
351 s = create_common(&job);
352
0cc4643b
JS
353 job_start(job);
354 assert(job->status == JOB_STATUS_RUNNING);
fb367e03
JS
355
356 s->should_converge = true;
0cc4643b
JS
357 job_enter(job);
358 assert(job->status == JOB_STATUS_READY);
fb367e03 359
0cc4643b
JS
360 job_complete(job, &error_abort);
361 job_enter(job);
977d26fd 362 while (!job->deferred_to_main_loop) {
fb367e03
JS
363 aio_poll(qemu_get_aio_context(), true);
364 }
977d26fd
JS
365 assert(job->status == JOB_STATUS_READY);
366 aio_poll(qemu_get_aio_context(), true);
0cc4643b 367 assert(job->status == JOB_STATUS_PENDING);
fb367e03 368
0cc4643b
JS
369 job_finalize(job, &error_abort);
370 assert(job->status == JOB_STATUS_CONCLUDED);
fb367e03
JS
371
372 cancel_common(s);
373}
374
9ef8112a
AG
375int main(int argc, char **argv)
376{
377 qemu_init_main_loop(&error_abort);
d185cf0e 378 bdrv_init();
9ef8112a
AG
379
380 g_test_init(&argc, &argv, NULL);
381 g_test_add_func("/blockjob/ids", test_job_ids);
fb367e03
JS
382 g_test_add_func("/blockjob/cancel/created", test_cancel_created);
383 g_test_add_func("/blockjob/cancel/running", test_cancel_running);
384 g_test_add_func("/blockjob/cancel/paused", test_cancel_paused);
385 g_test_add_func("/blockjob/cancel/ready", test_cancel_ready);
386 g_test_add_func("/blockjob/cancel/standby", test_cancel_standby);
387 g_test_add_func("/blockjob/cancel/pending", test_cancel_pending);
388 g_test_add_func("/blockjob/cancel/concluded", test_cancel_concluded);
9ef8112a
AG
389 return g_test_run();
390}