4 * Copyright Igalia, S.L. 2016
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 .instance_size
= sizeof(BlockJob
),
23 static void block_job_cb(void *opaque
, int ret
)
27 static BlockJob
*do_test_id(BlockBackend
*blk
, const char *id
,
33 job
= block_job_create(id
, &test_block_job_driver
, blk_bs(blk
), 0,
34 BLOCK_JOB_DEFAULT
, block_job_cb
, NULL
, &errp
);
37 g_assert_nonnull(job
);
39 g_assert_cmpstr(job
->id
, ==, id
);
41 g_assert_cmpstr(job
->id
, ==, blk_name(blk
));
44 g_assert_nonnull(errp
);
52 /* This creates a BlockBackend (optionally with a name) with a
53 * BlockDriverState inserted. */
54 static BlockBackend
*create_blk(const char *name
)
56 BlockBackend
*blk
= blk_new();
59 bs
= bdrv_open("null-co://", NULL
, NULL
, 0, &error_abort
);
62 blk_insert_bs(blk
, bs
);
67 monitor_add_blk(blk
, name
, &errp
);
74 /* This destroys the backend */
75 static void destroy_blk(BlockBackend
*blk
)
77 if (blk_name(blk
)[0] != '\0') {
78 monitor_remove_blk(blk
);
85 static void test_job_ids(void)
90 blk
[0] = create_blk(NULL
);
91 blk
[1] = create_blk("drive1");
92 blk
[2] = create_blk("drive2");
94 /* No job ID provided and the block backend has no name */
95 job
[0] = do_test_id(blk
[0], NULL
, false);
97 /* These are all invalid job IDs */
98 job
[0] = do_test_id(blk
[0], "0id", false);
99 job
[0] = do_test_id(blk
[0], "", false);
100 job
[0] = do_test_id(blk
[0], " ", false);
101 job
[0] = do_test_id(blk
[0], "123", false);
102 job
[0] = do_test_id(blk
[0], "_id", false);
103 job
[0] = do_test_id(blk
[0], "-id", false);
104 job
[0] = do_test_id(blk
[0], ".id", false);
105 job
[0] = do_test_id(blk
[0], "#id", false);
107 /* This one is valid */
108 job
[0] = do_test_id(blk
[0], "id0", true);
110 /* We cannot have two jobs in the same BDS */
111 do_test_id(blk
[0], "id1", false);
113 /* Duplicate job IDs are not allowed */
114 job
[1] = do_test_id(blk
[1], "id0", false);
116 /* But once job[0] finishes we can reuse its ID */
117 block_job_unref(job
[0]);
118 job
[1] = do_test_id(blk
[1], "id0", true);
120 /* No job ID specified, defaults to the backend name ('drive1') */
121 block_job_unref(job
[1]);
122 job
[1] = do_test_id(blk
[1], NULL
, true);
124 /* Duplicate job ID */
125 job
[2] = do_test_id(blk
[2], "drive1", false);
127 /* The ID of job[2] would default to 'drive2' but it is already in use */
128 job
[0] = do_test_id(blk
[0], "drive2", true);
129 job
[2] = do_test_id(blk
[2], NULL
, false);
131 /* This one is valid */
132 job
[2] = do_test_id(blk
[2], "id_2", true);
134 block_job_unref(job
[0]);
135 block_job_unref(job
[1]);
136 block_job_unref(job
[2]);
143 int main(int argc
, char **argv
)
145 qemu_init_main_loop(&error_abort
);
148 g_test_init(&argc
, &argv
, NULL
);
149 g_test_add_func("/blockjob/ids", test_job_ids
);