backup: Wire up qemu full pull backup commands over QMP
[libvirt/ericb.git] / tests / virlockspacetest.c
blob991e587db648d412da61fd18112905a94e78630e
1 /*
2 * Copyright (C) 2011, 2013, 2014 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; If not, see
16 * <http://www.gnu.org/licenses/>.
19 #include <config.h>
21 #include <signal.h>
22 #include <sys/stat.h>
24 #include "testutils.h"
25 #include "virutil.h"
26 #include "virerror.h"
27 #include "viralloc.h"
28 #include "virfile.h"
29 #include "virlog.h"
31 #include "virlockspace.h"
33 #define VIR_FROM_THIS VIR_FROM_RPC
35 VIR_LOG_INIT("tests.lockspacetest");
37 #define LOCKSPACE_DIR abs_builddir "/virlockspacedata"
39 static int testLockSpaceCreate(const void *args ATTRIBUTE_UNUSED)
41 virLockSpacePtr lockspace;
42 int ret = -1;
44 rmdir(LOCKSPACE_DIR);
46 if (!(lockspace = virLockSpaceNew(LOCKSPACE_DIR)))
47 goto cleanup;
49 if (!virFileIsDir(LOCKSPACE_DIR))
50 goto cleanup;
52 ret = 0;
54 cleanup:
55 virLockSpaceFree(lockspace);
56 rmdir(LOCKSPACE_DIR);
57 return ret;
61 static int testLockSpaceResourceLifecycle(const void *args ATTRIBUTE_UNUSED)
63 virLockSpacePtr lockspace;
64 int ret = -1;
66 rmdir(LOCKSPACE_DIR);
68 if (!(lockspace = virLockSpaceNew(LOCKSPACE_DIR)))
69 goto cleanup;
71 if (!virFileIsDir(LOCKSPACE_DIR))
72 goto cleanup;
74 if (virLockSpaceCreateResource(lockspace, "foo") < 0)
75 goto cleanup;
77 if (!virFileExists(LOCKSPACE_DIR "/foo"))
78 goto cleanup;
80 if (virLockSpaceDeleteResource(lockspace, "foo") < 0)
81 goto cleanup;
83 if (virFileExists(LOCKSPACE_DIR "/foo"))
84 goto cleanup;
86 ret = 0;
88 cleanup:
89 virLockSpaceFree(lockspace);
90 rmdir(LOCKSPACE_DIR);
91 return ret;
95 static int testLockSpaceResourceLockExcl(const void *args ATTRIBUTE_UNUSED)
97 virLockSpacePtr lockspace;
98 int ret = -1;
100 rmdir(LOCKSPACE_DIR);
102 if (!(lockspace = virLockSpaceNew(LOCKSPACE_DIR)))
103 goto cleanup;
105 if (!virFileIsDir(LOCKSPACE_DIR))
106 goto cleanup;
108 if (virLockSpaceCreateResource(lockspace, "foo") < 0)
109 goto cleanup;
111 if (virLockSpaceAcquireResource(lockspace, "foo", geteuid(), 0) < 0)
112 goto cleanup;
114 if (!virFileExists(LOCKSPACE_DIR "/foo"))
115 goto cleanup;
117 if (virLockSpaceAcquireResource(lockspace, "foo", geteuid(), 0) == 0)
118 goto cleanup;
120 if (virLockSpaceDeleteResource(lockspace, "foo") == 0)
121 goto cleanup;
123 if (virLockSpaceReleaseResource(lockspace, "foo", geteuid()) < 0)
124 goto cleanup;
126 if (virLockSpaceDeleteResource(lockspace, "foo") < 0)
127 goto cleanup;
129 if (virFileExists(LOCKSPACE_DIR "/foo"))
130 goto cleanup;
132 ret = 0;
134 cleanup:
135 virLockSpaceFree(lockspace);
136 rmdir(LOCKSPACE_DIR);
137 return ret;
141 static int testLockSpaceResourceLockExclAuto(const void *args ATTRIBUTE_UNUSED)
143 virLockSpacePtr lockspace;
144 int ret = -1;
146 rmdir(LOCKSPACE_DIR);
148 if (!(lockspace = virLockSpaceNew(LOCKSPACE_DIR)))
149 goto cleanup;
151 if (!virFileIsDir(LOCKSPACE_DIR))
152 goto cleanup;
154 if (virLockSpaceCreateResource(lockspace, "foo") < 0)
155 goto cleanup;
157 if (virLockSpaceAcquireResource(lockspace, "foo", geteuid(),
158 VIR_LOCK_SPACE_ACQUIRE_AUTOCREATE) < 0)
159 goto cleanup;
161 if (!virFileExists(LOCKSPACE_DIR "/foo"))
162 goto cleanup;
164 if (virLockSpaceReleaseResource(lockspace, "foo", geteuid()) < 0)
165 goto cleanup;
167 if (virFileExists(LOCKSPACE_DIR "/foo"))
168 goto cleanup;
170 ret = 0;
172 cleanup:
173 virLockSpaceFree(lockspace);
174 rmdir(LOCKSPACE_DIR);
175 return ret;
179 static int testLockSpaceResourceLockShr(const void *args ATTRIBUTE_UNUSED)
181 virLockSpacePtr lockspace;
182 int ret = -1;
184 rmdir(LOCKSPACE_DIR);
186 if (!(lockspace = virLockSpaceNew(LOCKSPACE_DIR)))
187 goto cleanup;
189 if (!virFileIsDir(LOCKSPACE_DIR))
190 goto cleanup;
192 if (virLockSpaceCreateResource(lockspace, "foo") < 0)
193 goto cleanup;
195 if (virLockSpaceAcquireResource(lockspace, "foo", geteuid(),
196 VIR_LOCK_SPACE_ACQUIRE_SHARED) < 0)
197 goto cleanup;
199 if (virLockSpaceAcquireResource(lockspace, "foo", geteuid(), 0) == 0)
200 goto cleanup;
202 if (virLockSpaceAcquireResource(lockspace, "foo", geteuid(),
203 VIR_LOCK_SPACE_ACQUIRE_SHARED) < 0)
204 goto cleanup;
206 if (virLockSpaceDeleteResource(lockspace, "foo") == 0)
207 goto cleanup;
209 if (virLockSpaceReleaseResource(lockspace, "foo", geteuid()) < 0)
210 goto cleanup;
212 if (virLockSpaceDeleteResource(lockspace, "foo") == 0)
213 goto cleanup;
215 if (virLockSpaceReleaseResource(lockspace, "foo", geteuid()) < 0)
216 goto cleanup;
218 if (virLockSpaceDeleteResource(lockspace, "foo") < 0)
219 goto cleanup;
221 if (virFileExists(LOCKSPACE_DIR "/foo"))
222 goto cleanup;
224 ret = 0;
226 cleanup:
227 virLockSpaceFree(lockspace);
228 rmdir(LOCKSPACE_DIR);
229 return ret;
233 static int testLockSpaceResourceLockShrAuto(const void *args ATTRIBUTE_UNUSED)
235 virLockSpacePtr lockspace;
236 int ret = -1;
238 rmdir(LOCKSPACE_DIR);
240 if (!(lockspace = virLockSpaceNew(LOCKSPACE_DIR)))
241 goto cleanup;
243 if (!virFileIsDir(LOCKSPACE_DIR))
244 goto cleanup;
246 if (virLockSpaceCreateResource(lockspace, "foo") < 0)
247 goto cleanup;
249 if (virLockSpaceAcquireResource(lockspace, "foo", geteuid(),
250 VIR_LOCK_SPACE_ACQUIRE_SHARED |
251 VIR_LOCK_SPACE_ACQUIRE_AUTOCREATE) < 0)
252 goto cleanup;
254 if (!virFileExists(LOCKSPACE_DIR "/foo"))
255 goto cleanup;
257 if (virLockSpaceAcquireResource(lockspace, "foo", geteuid(),
258 VIR_LOCK_SPACE_ACQUIRE_AUTOCREATE) == 0)
259 goto cleanup;
261 if (!virFileExists(LOCKSPACE_DIR "/foo"))
262 goto cleanup;
264 if (virLockSpaceAcquireResource(lockspace, "foo", geteuid(),
265 VIR_LOCK_SPACE_ACQUIRE_SHARED |
266 VIR_LOCK_SPACE_ACQUIRE_AUTOCREATE) < 0)
267 goto cleanup;
269 if (!virFileExists(LOCKSPACE_DIR "/foo"))
270 goto cleanup;
272 if (virLockSpaceReleaseResource(lockspace, "foo", geteuid()) < 0)
273 goto cleanup;
275 if (!virFileExists(LOCKSPACE_DIR "/foo"))
276 goto cleanup;
278 if (virLockSpaceReleaseResource(lockspace, "foo", geteuid()) < 0)
279 goto cleanup;
281 if (virFileExists(LOCKSPACE_DIR "/foo"))
282 goto cleanup;
284 ret = 0;
286 cleanup:
287 virLockSpaceFree(lockspace);
288 rmdir(LOCKSPACE_DIR);
289 return ret;
293 static int testLockSpaceResourceLockPath(const void *args ATTRIBUTE_UNUSED)
295 virLockSpacePtr lockspace;
296 int ret = -1;
298 rmdir(LOCKSPACE_DIR);
300 if (!(lockspace = virLockSpaceNew(NULL)))
301 goto cleanup;
303 if (mkdir(LOCKSPACE_DIR, 0700) < 0)
304 goto cleanup;
306 if (virLockSpaceCreateResource(lockspace, LOCKSPACE_DIR "/foo") < 0)
307 goto cleanup;
309 if (virLockSpaceAcquireResource(lockspace, LOCKSPACE_DIR "/foo", geteuid(), 0) < 0)
310 goto cleanup;
312 if (!virFileExists(LOCKSPACE_DIR "/foo"))
313 goto cleanup;
315 if (virLockSpaceAcquireResource(lockspace, LOCKSPACE_DIR "/foo", geteuid(), 0) == 0)
316 goto cleanup;
318 if (virLockSpaceDeleteResource(lockspace, LOCKSPACE_DIR "/foo") == 0)
319 goto cleanup;
321 if (virLockSpaceReleaseResource(lockspace, LOCKSPACE_DIR "/foo", geteuid()) < 0)
322 goto cleanup;
324 if (virLockSpaceDeleteResource(lockspace, LOCKSPACE_DIR "/foo") < 0)
325 goto cleanup;
327 if (virFileExists(LOCKSPACE_DIR "/foo"))
328 goto cleanup;
330 ret = 0;
332 cleanup:
333 virLockSpaceFree(lockspace);
334 rmdir(LOCKSPACE_DIR);
335 return ret;
340 static int
341 mymain(void)
343 int ret = 0;
345 signal(SIGPIPE, SIG_IGN);
347 if (virTestRun("Lockspace creation", testLockSpaceCreate, NULL) < 0)
348 ret = -1;
350 if (virTestRun("Lockspace res lifecycle", testLockSpaceResourceLifecycle, NULL) < 0)
351 ret = -1;
353 if (virTestRun("Lockspace res lock excl", testLockSpaceResourceLockExcl, NULL) < 0)
354 ret = -1;
356 if (virTestRun("Lockspace res lock shr", testLockSpaceResourceLockShr, NULL) < 0)
357 ret = -1;
359 if (virTestRun("Lockspace res lock excl auto", testLockSpaceResourceLockExclAuto, NULL) < 0)
360 ret = -1;
362 if (virTestRun("Lockspace res lock shr auto", testLockSpaceResourceLockShrAuto, NULL) < 0)
363 ret = -1;
365 if (virTestRun("Lockspace res full path", testLockSpaceResourceLockPath, NULL) < 0)
366 ret = -1;
368 return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
371 VIR_TEST_MAIN(mymain)