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
),
34 0, BLK_PERM_ALL
, 0, BLOCK_JOB_DEFAULT
, block_job_cb
,
38 g_assert_nonnull(job
);
40 g_assert_cmpstr(job
->id
, ==, id
);
42 g_assert_cmpstr(job
->id
, ==, blk_name(blk
));
45 g_assert_nonnull(errp
);
53 /* This creates a BlockBackend (optionally with a name) with a
54 * BlockDriverState inserted. */
55 static BlockBackend
*create_blk(const char *name
)
57 /* No I/O is performed on this device */
58 BlockBackend
*blk
= blk_new(0, BLK_PERM_ALL
);
61 bs
= bdrv_open("null-co://", NULL
, NULL
, 0, &error_abort
);
64 blk_insert_bs(blk
, bs
, &error_abort
);
69 monitor_add_blk(blk
, name
, &errp
);
76 /* This destroys the backend */
77 static void destroy_blk(BlockBackend
*blk
)
79 if (blk_name(blk
)[0] != '\0') {
80 monitor_remove_blk(blk
);
87 static void test_job_ids(void)
92 blk
[0] = create_blk(NULL
);
93 blk
[1] = create_blk("drive1");
94 blk
[2] = create_blk("drive2");
96 /* No job ID provided and the block backend has no name */
97 job
[0] = do_test_id(blk
[0], NULL
, false);
99 /* These are all invalid job IDs */
100 job
[0] = do_test_id(blk
[0], "0id", false);
101 job
[0] = do_test_id(blk
[0], "", false);
102 job
[0] = do_test_id(blk
[0], " ", false);
103 job
[0] = do_test_id(blk
[0], "123", false);
104 job
[0] = do_test_id(blk
[0], "_id", false);
105 job
[0] = do_test_id(blk
[0], "-id", false);
106 job
[0] = do_test_id(blk
[0], ".id", false);
107 job
[0] = do_test_id(blk
[0], "#id", false);
109 /* This one is valid */
110 job
[0] = do_test_id(blk
[0], "id0", true);
112 /* We cannot have two jobs in the same BDS */
113 do_test_id(blk
[0], "id1", false);
115 /* Duplicate job IDs are not allowed */
116 job
[1] = do_test_id(blk
[1], "id0", false);
118 /* But once job[0] finishes we can reuse its ID */
119 block_job_early_fail(job
[0]);
120 job
[1] = do_test_id(blk
[1], "id0", true);
122 /* No job ID specified, defaults to the backend name ('drive1') */
123 block_job_early_fail(job
[1]);
124 job
[1] = do_test_id(blk
[1], NULL
, true);
126 /* Duplicate job ID */
127 job
[2] = do_test_id(blk
[2], "drive1", false);
129 /* The ID of job[2] would default to 'drive2' but it is already in use */
130 job
[0] = do_test_id(blk
[0], "drive2", true);
131 job
[2] = do_test_id(blk
[2], NULL
, false);
133 /* This one is valid */
134 job
[2] = do_test_id(blk
[2], "id_2", true);
136 block_job_early_fail(job
[0]);
137 block_job_early_fail(job
[1]);
138 block_job_early_fail(job
[2]);
145 int main(int argc
, char **argv
)
147 qemu_init_main_loop(&error_abort
);
150 g_test_init(&argc
, &argv
, NULL
);
151 g_test_add_func("/blockjob/ids", test_job_ids
);