]> Git Repo - qemu.git/blob - tests/test-blockjob.c
virtio-scsi-test: Use scsi-hd instead of legacy scsi-disk
[qemu.git] / tests / test-blockjob.c
1 /*
2  * Blockjob tests
3  *
4  * Copyright Igalia, S.L. 2016
5  *
6  * Authors:
7  *  Alberto Garcia   <[email protected]>
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"
16 #include "block/blockjob_int.h"
17 #include "sysemu/block-backend.h"
18
19 static const BlockJobDriver test_block_job_driver = {
20     .instance_size = sizeof(BlockJob),
21 };
22
23 static void block_job_cb(void *opaque, int ret)
24 {
25 }
26
27 static BlockJob *do_test_id(BlockBackend *blk, const char *id,
28                             bool should_succeed)
29 {
30     BlockJob *job;
31     Error *errp = NULL;
32
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,
35                            NULL, &errp);
36     if (should_succeed) {
37         g_assert_null(errp);
38         g_assert_nonnull(job);
39         if (id) {
40             g_assert_cmpstr(job->id, ==, id);
41         } else {
42             g_assert_cmpstr(job->id, ==, blk_name(blk));
43         }
44     } else {
45         g_assert_nonnull(errp);
46         g_assert_null(job);
47         error_free(errp);
48     }
49
50     return job;
51 }
52
53 /* This creates a BlockBackend (optionally with a name) with a
54  * BlockDriverState inserted. */
55 static BlockBackend *create_blk(const char *name)
56 {
57     /* No I/O is performed on this device */
58     BlockBackend *blk = blk_new(0, BLK_PERM_ALL);
59     BlockDriverState *bs;
60
61     bs = bdrv_open("null-co://", NULL, NULL, 0, &error_abort);
62     g_assert_nonnull(bs);
63
64     blk_insert_bs(blk, bs, &error_abort);
65     bdrv_unref(bs);
66
67     if (name) {
68         Error *errp = NULL;
69         monitor_add_blk(blk, name, &errp);
70         g_assert_null(errp);
71     }
72
73     return blk;
74 }
75
76 /* This destroys the backend */
77 static void destroy_blk(BlockBackend *blk)
78 {
79     if (blk_name(blk)[0] != '\0') {
80         monitor_remove_blk(blk);
81     }
82
83     blk_remove_bs(blk);
84     blk_unref(blk);
85 }
86
87 static void test_job_ids(void)
88 {
89     BlockBackend *blk[3];
90     BlockJob *job[3];
91
92     blk[0] = create_blk(NULL);
93     blk[1] = create_blk("drive1");
94     blk[2] = create_blk("drive2");
95
96     /* No job ID provided and the block backend has no name */
97     job[0] = do_test_id(blk[0], NULL, false);
98
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);
108
109     /* This one is valid */
110     job[0] = do_test_id(blk[0], "id0", true);
111
112     /* We cannot have two jobs in the same BDS */
113     do_test_id(blk[0], "id1", false);
114
115     /* Duplicate job IDs are not allowed */
116     job[1] = do_test_id(blk[1], "id0", false);
117
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);
121
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);
125
126     /* Duplicate job ID */
127     job[2] = do_test_id(blk[2], "drive1", false);
128
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);
132
133     /* This one is valid */
134     job[2] = do_test_id(blk[2], "id_2", true);
135
136     block_job_early_fail(job[0]);
137     block_job_early_fail(job[1]);
138     block_job_early_fail(job[2]);
139
140     destroy_blk(blk[0]);
141     destroy_blk(blk[1]);
142     destroy_blk(blk[2]);
143 }
144
145 int main(int argc, char **argv)
146 {
147     qemu_init_main_loop(&error_abort);
148     bdrv_init();
149
150     g_test_init(&argc, &argv, NULL);
151     g_test_add_func("/blockjob/ids", test_job_ids);
152     return g_test_run();
153 }
This page took 0.032872 seconds and 4 git commands to generate.