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/>.
24 #include "testutils.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
;
46 if (!(lockspace
= virLockSpaceNew(LOCKSPACE_DIR
)))
49 if (!virFileIsDir(LOCKSPACE_DIR
))
55 virLockSpaceFree(lockspace
);
61 static int testLockSpaceResourceLifecycle(const void *args ATTRIBUTE_UNUSED
)
63 virLockSpacePtr lockspace
;
68 if (!(lockspace
= virLockSpaceNew(LOCKSPACE_DIR
)))
71 if (!virFileIsDir(LOCKSPACE_DIR
))
74 if (virLockSpaceCreateResource(lockspace
, "foo") < 0)
77 if (!virFileExists(LOCKSPACE_DIR
"/foo"))
80 if (virLockSpaceDeleteResource(lockspace
, "foo") < 0)
83 if (virFileExists(LOCKSPACE_DIR
"/foo"))
89 virLockSpaceFree(lockspace
);
95 static int testLockSpaceResourceLockExcl(const void *args ATTRIBUTE_UNUSED
)
97 virLockSpacePtr lockspace
;
100 rmdir(LOCKSPACE_DIR
);
102 if (!(lockspace
= virLockSpaceNew(LOCKSPACE_DIR
)))
105 if (!virFileIsDir(LOCKSPACE_DIR
))
108 if (virLockSpaceCreateResource(lockspace
, "foo") < 0)
111 if (virLockSpaceAcquireResource(lockspace
, "foo", geteuid(), 0) < 0)
114 if (!virFileExists(LOCKSPACE_DIR
"/foo"))
117 if (virLockSpaceAcquireResource(lockspace
, "foo", geteuid(), 0) == 0)
120 if (virLockSpaceDeleteResource(lockspace
, "foo") == 0)
123 if (virLockSpaceReleaseResource(lockspace
, "foo", geteuid()) < 0)
126 if (virLockSpaceDeleteResource(lockspace
, "foo") < 0)
129 if (virFileExists(LOCKSPACE_DIR
"/foo"))
135 virLockSpaceFree(lockspace
);
136 rmdir(LOCKSPACE_DIR
);
141 static int testLockSpaceResourceLockExclAuto(const void *args ATTRIBUTE_UNUSED
)
143 virLockSpacePtr lockspace
;
146 rmdir(LOCKSPACE_DIR
);
148 if (!(lockspace
= virLockSpaceNew(LOCKSPACE_DIR
)))
151 if (!virFileIsDir(LOCKSPACE_DIR
))
154 if (virLockSpaceCreateResource(lockspace
, "foo") < 0)
157 if (virLockSpaceAcquireResource(lockspace
, "foo", geteuid(),
158 VIR_LOCK_SPACE_ACQUIRE_AUTOCREATE
) < 0)
161 if (!virFileExists(LOCKSPACE_DIR
"/foo"))
164 if (virLockSpaceReleaseResource(lockspace
, "foo", geteuid()) < 0)
167 if (virFileExists(LOCKSPACE_DIR
"/foo"))
173 virLockSpaceFree(lockspace
);
174 rmdir(LOCKSPACE_DIR
);
179 static int testLockSpaceResourceLockShr(const void *args ATTRIBUTE_UNUSED
)
181 virLockSpacePtr lockspace
;
184 rmdir(LOCKSPACE_DIR
);
186 if (!(lockspace
= virLockSpaceNew(LOCKSPACE_DIR
)))
189 if (!virFileIsDir(LOCKSPACE_DIR
))
192 if (virLockSpaceCreateResource(lockspace
, "foo") < 0)
195 if (virLockSpaceAcquireResource(lockspace
, "foo", geteuid(),
196 VIR_LOCK_SPACE_ACQUIRE_SHARED
) < 0)
199 if (virLockSpaceAcquireResource(lockspace
, "foo", geteuid(), 0) == 0)
202 if (virLockSpaceAcquireResource(lockspace
, "foo", geteuid(),
203 VIR_LOCK_SPACE_ACQUIRE_SHARED
) < 0)
206 if (virLockSpaceDeleteResource(lockspace
, "foo") == 0)
209 if (virLockSpaceReleaseResource(lockspace
, "foo", geteuid()) < 0)
212 if (virLockSpaceDeleteResource(lockspace
, "foo") == 0)
215 if (virLockSpaceReleaseResource(lockspace
, "foo", geteuid()) < 0)
218 if (virLockSpaceDeleteResource(lockspace
, "foo") < 0)
221 if (virFileExists(LOCKSPACE_DIR
"/foo"))
227 virLockSpaceFree(lockspace
);
228 rmdir(LOCKSPACE_DIR
);
233 static int testLockSpaceResourceLockShrAuto(const void *args ATTRIBUTE_UNUSED
)
235 virLockSpacePtr lockspace
;
238 rmdir(LOCKSPACE_DIR
);
240 if (!(lockspace
= virLockSpaceNew(LOCKSPACE_DIR
)))
243 if (!virFileIsDir(LOCKSPACE_DIR
))
246 if (virLockSpaceCreateResource(lockspace
, "foo") < 0)
249 if (virLockSpaceAcquireResource(lockspace
, "foo", geteuid(),
250 VIR_LOCK_SPACE_ACQUIRE_SHARED
|
251 VIR_LOCK_SPACE_ACQUIRE_AUTOCREATE
) < 0)
254 if (!virFileExists(LOCKSPACE_DIR
"/foo"))
257 if (virLockSpaceAcquireResource(lockspace
, "foo", geteuid(),
258 VIR_LOCK_SPACE_ACQUIRE_AUTOCREATE
) == 0)
261 if (!virFileExists(LOCKSPACE_DIR
"/foo"))
264 if (virLockSpaceAcquireResource(lockspace
, "foo", geteuid(),
265 VIR_LOCK_SPACE_ACQUIRE_SHARED
|
266 VIR_LOCK_SPACE_ACQUIRE_AUTOCREATE
) < 0)
269 if (!virFileExists(LOCKSPACE_DIR
"/foo"))
272 if (virLockSpaceReleaseResource(lockspace
, "foo", geteuid()) < 0)
275 if (!virFileExists(LOCKSPACE_DIR
"/foo"))
278 if (virLockSpaceReleaseResource(lockspace
, "foo", geteuid()) < 0)
281 if (virFileExists(LOCKSPACE_DIR
"/foo"))
287 virLockSpaceFree(lockspace
);
288 rmdir(LOCKSPACE_DIR
);
293 static int testLockSpaceResourceLockPath(const void *args ATTRIBUTE_UNUSED
)
295 virLockSpacePtr lockspace
;
298 rmdir(LOCKSPACE_DIR
);
300 if (!(lockspace
= virLockSpaceNew(NULL
)))
303 if (mkdir(LOCKSPACE_DIR
, 0700) < 0)
306 if (virLockSpaceCreateResource(lockspace
, LOCKSPACE_DIR
"/foo") < 0)
309 if (virLockSpaceAcquireResource(lockspace
, LOCKSPACE_DIR
"/foo", geteuid(), 0) < 0)
312 if (!virFileExists(LOCKSPACE_DIR
"/foo"))
315 if (virLockSpaceAcquireResource(lockspace
, LOCKSPACE_DIR
"/foo", geteuid(), 0) == 0)
318 if (virLockSpaceDeleteResource(lockspace
, LOCKSPACE_DIR
"/foo") == 0)
321 if (virLockSpaceReleaseResource(lockspace
, LOCKSPACE_DIR
"/foo", geteuid()) < 0)
324 if (virLockSpaceDeleteResource(lockspace
, LOCKSPACE_DIR
"/foo") < 0)
327 if (virFileExists(LOCKSPACE_DIR
"/foo"))
333 virLockSpaceFree(lockspace
);
334 rmdir(LOCKSPACE_DIR
);
345 signal(SIGPIPE
, SIG_IGN
);
347 if (virTestRun("Lockspace creation", testLockSpaceCreate
, NULL
) < 0)
350 if (virTestRun("Lockspace res lifecycle", testLockSpaceResourceLifecycle
, NULL
) < 0)
353 if (virTestRun("Lockspace res lock excl", testLockSpaceResourceLockExcl
, NULL
) < 0)
356 if (virTestRun("Lockspace res lock shr", testLockSpaceResourceLockShr
, NULL
) < 0)
359 if (virTestRun("Lockspace res lock excl auto", testLockSpaceResourceLockExclAuto
, NULL
) < 0)
362 if (virTestRun("Lockspace res lock shr auto", testLockSpaceResourceLockShrAuto
, NULL
) < 0)
365 if (virTestRun("Lockspace res full path", testLockSpaceResourceLockPath
, NULL
) < 0)
368 return ret
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
371 VIR_TEST_MAIN(mymain
)