]> git.proxmox.com Git - mirror_lxc.git/blame - src/tests/parse_config_file.c
confile_utils: add new file
[mirror_lxc.git] / src / tests / parse_config_file.c
CommitLineData
9c61a6d6
CB
1/* liblxcapi
2 *
3 * Copyright © 2017 Christian Brauner <christian.brauner@ubuntu.com>.
4 * Copyright © 2017 Canonical Ltd.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2, as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19#include <lxc/lxccontainer.h>
20
21#include <unistd.h>
22#include <signal.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <sys/types.h>
26#include <sys/wait.h>
27#include <stdlib.h>
28#include <errno.h>
29#include <string.h>
30
31#include "lxc/state.h"
32#include "lxctest.h"
33
ae4ad10d
CB
34static int set_get_compare_clear_save_load(struct lxc_container *c,
35 const char *key, const char *value,
36 const char *config_file,
37 bool compare)
9c61a6d6
CB
38{
39 char retval[4096] = {0};
40 int ret;
41
42 if (!c->set_config_item(c, key, value)) {
43 lxc_error("failed to set config item \"%s\" to \"%s\"\n", key,
44 value);
45 return -1;
46 }
47
48 ret = c->get_config_item(c, key, retval, sizeof(retval));
49 if (ret < 0) {
50 lxc_error("failed to get config item \"%s\"\n", key);
51 return -1;
52 }
53
ae4ad10d
CB
54 if (compare) {
55 ret = strcmp(retval, value);
56 if (ret != 0) {
57 lxc_error(
58 "expected value \"%s\" and retrieved value \"%s\" "
59 "for config key \"%s\" do not match\n",
60 value, retval, key);
61 return -1;
62 }
63 }
64
9c61a6d6
CB
65 if (config_file) {
66 if (!c->save_config(c, config_file)) {
67 lxc_error("%s\n", "failed to save config file");
68 return -1;
69 }
70
71 c->clear_config(c);
72 c->lxc_conf = NULL;
73
74 if (!c->load_config(c, config_file)) {
75 lxc_error("%s\n", "failed to load config file");
76 return -1;
77 }
78 }
79
80 if (!c->clear_config_item(c, key)) {
81 lxc_error("failed to clear config item \"%s\"\n", key);
82 return -1;
83 }
84
ae4ad10d 85 if (config_file) {
9c61a6d6
CB
86 if (!c->save_config(c, config_file)) {
87 lxc_error("%s\n", "failed to save config file");
88 return -1;
89 }
90
91 c->clear_config(c);
92 c->lxc_conf = NULL;
93
94 if (!c->load_config(c, config_file)) {
95 lxc_error("%s\n", "failed to load config file");
96 return -1;
97 }
98 }
99
100 c->clear_config(c);
101 c->lxc_conf = NULL;
102
103 return 0;
104}
105
106int main(int argc, char *argv[])
107{
108 struct lxc_container *c;
109 int fd = -1;
110 int ret = EXIT_FAILURE;
111 char tmpf[] = "lxc-parse-config-file-XXXXXX";
112 char retval[4096] = {0};
113
114 c = lxc_container_new("lxc-parse-config-file-testxyz", NULL);
115 if (!c) {
116 lxc_error("%s\n", "failed to create new container");
117 exit(EXIT_FAILURE);
118 }
119
120 fd = mkstemp(tmpf);
121 if (fd < 0) {
122 lxc_error("%s\n", "Could not create temporary file");
123 goto non_test_error;
124 }
125 close(fd);
126
127 /* lxc.arch */
ae4ad10d 128 if (set_get_compare_clear_save_load(c, "lxc.arch", "x86_64", tmpf, true) < 0) {
9c61a6d6
CB
129 lxc_error("%s\n", "lxc.arch");
130 goto non_test_error;
131 }
132
133 /* lxc.pts */
ae4ad10d 134 if (set_get_compare_clear_save_load(c, "lxc.pts", "1000", tmpf, true) < 0) {
9c61a6d6
CB
135 lxc_error("%s\n", "lxc.pts");
136 goto non_test_error;
137 }
138
139 /* lxc.tty */
ae4ad10d 140 if (set_get_compare_clear_save_load(c, "lxc.tty", "4", tmpf, true) < 0) {
9c61a6d6
CB
141 lxc_error("%s\n", "lxc.tty");
142 goto non_test_error;
143 }
144
145 /* lxc.devttydir */
ae4ad10d 146 if (set_get_compare_clear_save_load(c, "lxc.devttydir", "not-dev", tmpf, true) < 0) {
9c61a6d6
CB
147 lxc_error("%s\n", "lxc.devttydir");
148 goto non_test_error;
149 }
150
151 /* lxc.kmsg */
ae4ad10d 152 if (set_get_compare_clear_save_load(c, "lxc.kmsg", "1", tmpf, true) < 0) {
9c61a6d6
CB
153 lxc_error("%s\n", "lxc.kmsg");
154 goto non_test_error;
155 }
156
157 /* lxc.aa_profile */
ae4ad10d 158 if (set_get_compare_clear_save_load(c, "lxc.aa_profile", "unconfined", tmpf, true) <
9c61a6d6
CB
159 0) {
160 lxc_error("%s\n", "lxc.aa_profile");
161 goto non_test_error;
162 }
163
164 /* lxc.aa_allow_incomplete */
ae4ad10d 165 if (set_get_compare_clear_save_load(c, "lxc.aa_allow_incomplete", "1", tmpf, true) < 0) {
9c61a6d6
CB
166 lxc_error("%s\n", "lxc.aa_allow_incomplete");
167 goto non_test_error;
168 }
169
170 /* lxc.cgroup.cpuset.cpus */
ae4ad10d 171 if (set_get_compare_clear_save_load(c, "lxc.cgroup.cpuset.cpus", "1-100", tmpf, false) < 0) {
9c61a6d6
CB
172 lxc_error("%s\n", "lxc.cgroup.cpuset.cpus");
173 goto non_test_error;
174 }
175
176 /* lxc.cgroup */
177 if (!c->set_config_item(c, "lxc.cgroup.cpuset.cpus", "1-100")) {
178 lxc_error("%s\n", "failed to set config item "
179 "\"lxc.cgroup.cpuset.cpus\" to \"1-100\"");
180 return -1;
181 }
182
183 if (!c->set_config_item(c, "lxc.cgroup.memory.limit_in_bytes",
184 "123456789")) {
185 lxc_error(
186 "%s\n",
187 "failed to set config item "
188 "\"lxc.cgroup.memory.limit_in_bytes\" to \"123456789\"");
189 return -1;
190 }
191
192 if (!c->get_config_item(c, "lxc.cgroup", retval, sizeof(retval))) {
193 lxc_error("%s\n", "failed to get config item \"lxc.cgroup\"");
194 return -1;
195 }
196
197 c->clear_config(c);
198 c->lxc_conf = NULL;
199
200 /* lxc.id_map
201 * We can't really save the config here since save_config() wants to
202 * chown the container's directory but we haven't created an on-disk
203 * container. So let's test set-get-clear.
204 */
ae4ad10d
CB
205 if (set_get_compare_clear_save_load(c, "lxc.id_map", "u 0 100000 1000000000",
206 NULL, false) < 0) {
9c61a6d6
CB
207 lxc_error("%s\n", "lxc.id_map");
208 goto non_test_error;
209 }
210
211 if (!c->set_config_item(c, "lxc.id_map", "u 1 100000 10000000")) {
212 lxc_error("%s\n", "failed to set config item "
213 "\"lxc.id_map\" to \"u 1 100000 10000000\"");
214 return -1;
215 }
216
217 if (!c->set_config_item(c, "lxc.id_map", "g 1 100000 10000000")) {
218 lxc_error("%s\n", "failed to set config item "
219 "\"lxc.id_map\" to \"g 1 100000 10000000\"");
220 return -1;
221 }
222
223 if (!c->get_config_item(c, "lxc.id_map", retval, sizeof(retval))) {
224 lxc_error("%s\n", "failed to get config item \"lxc.cgroup\"");
225 return -1;
226 }
227
228 c->clear_config(c);
229 c->lxc_conf = NULL;
230
231 /* lxc.loglevel */
ae4ad10d 232 if (set_get_compare_clear_save_load(c, "lxc.loglevel", "DEBUG", tmpf, true) < 0) {
9c61a6d6
CB
233 lxc_error("%s\n", "lxc.loglevel");
234 goto non_test_error;
235 }
236
237 /* lxc.logfile */
ae4ad10d 238 if (set_get_compare_clear_save_load(c, "lxc.logfile", "/some/path", tmpf, true) < 0) {
9c61a6d6
CB
239 lxc_error("%s\n", "lxc.logfile");
240 goto non_test_error;
241 }
242
243 /* lxc.mount */
ae4ad10d 244 if (set_get_compare_clear_save_load(c, "lxc.mount", "/some/path", NULL, true) < 0) {
9c61a6d6
CB
245 lxc_error("%s\n", "lxc.mount");
246 goto non_test_error;
247 }
248
ae4ad10d
CB
249 /* lxc.mount.auto
250 * Note that we cannot compare the values since the getter for
251 * lxc.mount.auto does not preserve ordering.
252 */
253 if (set_get_compare_clear_save_load(c, "lxc.mount.auto", "proc:rw sys:rw cgroup-full:rw", tmpf, false) < 0) {
9c61a6d6
CB
254 lxc_error("%s\n", "lxc.mount.auto");
255 goto non_test_error;
256 }
257
ae4ad10d
CB
258 /* lxc.mount.entry
259 * Note that we cannot compare the values since the getter for
260 * lxc.mount.entry appends newlines.
261 */
262 if (set_get_compare_clear_save_load(
9c61a6d6 263 c, "lxc.mount.entry",
ae4ad10d 264 "/dev/dri dev/dri none bind,optional,create=dir", tmpf, false) < 0) {
9c61a6d6
CB
265 lxc_error("%s\n", "lxc.mount.entry");
266 goto non_test_error;
267 }
268
269 /* lxc.rootfs */
ae4ad10d 270 if (set_get_compare_clear_save_load(c, "lxc.rootfs", "/some/path", tmpf, true) < 0) {
9c61a6d6
CB
271 lxc_error("%s\n", "lxc.rootfs");
272 goto non_test_error;
273 }
274
275 /* lxc.rootfs.mount */
ae4ad10d 276 if (set_get_compare_clear_save_load(c, "lxc.rootfs.mount", "/some/path", tmpf, true) < 0) {
9c61a6d6
CB
277 lxc_error("%s\n", "lxc.rootfs.mount");
278 goto non_test_error;
279 }
280
281 /* lxc.rootfs.options */
ae4ad10d 282 if (set_get_compare_clear_save_load(c, "lxc.rootfs.options", "ext4,discard", tmpf, true) < 0) {
9c61a6d6
CB
283 lxc_error("%s\n", "lxc.rootfs.options");
284 goto non_test_error;
285 }
286
287 /* lxc.rootfs.backend */
ae4ad10d 288 if (set_get_compare_clear_save_load(c, "lxc.rootfs.backend", "btrfs", tmpf, true) < 0) {
9c61a6d6
CB
289 lxc_error("%s\n", "lxc.rootfs.backend");
290 goto non_test_error;
291 }
292
293 /* lxc.utsname */
ae4ad10d 294 if (set_get_compare_clear_save_load(c, "lxc.utsname", "the-shire", tmpf, true) < 0) {
9c61a6d6
CB
295 lxc_error("%s\n", "lxc.utsname");
296 goto non_test_error;
297 }
298
299 /* lxc.hook.pre-start */
ae4ad10d 300 if (set_get_compare_clear_save_load(c, "lxc.hook.pre-start", "/some/pre-start", tmpf, false) < 0) {
9c61a6d6
CB
301 lxc_error("%s\n", "lxc.hook.pre-start");
302 goto non_test_error;
303 }
304
305 /* lxc.hook.pre-mount */
ae4ad10d 306 if (set_get_compare_clear_save_load(c, "lxc.hook.pre-mount", "/some/pre-mount", tmpf, false) < 0) {
9c61a6d6
CB
307 lxc_error("%s\n", "lxc.hook.pre-mount");
308 goto non_test_error;
309 }
310
311 /* lxc.hook.mount */
ae4ad10d 312 if (set_get_compare_clear_save_load(c, "lxc.hook.mount", "/some/mount", tmpf, false) < 0) {
9c61a6d6
CB
313 lxc_error("%s\n", "lxc.hook.mount");
314 goto non_test_error;
315 }
316
317 /* lxc.hook.autodev */
ae4ad10d 318 if (set_get_compare_clear_save_load(c, "lxc.hook.autodev", "/some/autodev", tmpf, false) < 0) {
9c61a6d6
CB
319 lxc_error("%s\n", "lxc.hook.autodev");
320 goto non_test_error;
321 }
322
323 /* lxc.hook.start */
ae4ad10d 324 if (set_get_compare_clear_save_load(c, "lxc.hook.start", "/some/start", tmpf, false) < 0) {
9c61a6d6
CB
325 lxc_error("%s\n", "lxc.hook.start");
326 goto non_test_error;
327 }
328
329 /* lxc.hook.stop */
ae4ad10d 330 if (set_get_compare_clear_save_load(c, "lxc.hook.stop", "/some/stop", tmpf, false) < 0) {
9c61a6d6
CB
331 lxc_error("%s\n", "lxc.hook.stop");
332 goto non_test_error;
333 }
334
335 /* lxc.hook.post-stop */
ae4ad10d 336 if (set_get_compare_clear_save_load(c, "lxc.hook.post-stop", "/some/post-stop", tmpf, false) < 0) {
9c61a6d6
CB
337 lxc_error("%s\n", "lxc.hook.post-stop");
338 goto non_test_error;
339 }
340
341 /* lxc.hook.clone */
ae4ad10d 342 if (set_get_compare_clear_save_load(c, "lxc.hook.clone", "/some/clone", tmpf, false) < 0) {
9c61a6d6
CB
343 lxc_error("%s\n", "lxc.hook.clone");
344 goto non_test_error;
345 }
346
347 /* lxc.hook.destroy */
ae4ad10d 348 if (set_get_compare_clear_save_load(c, "lxc.hook.destroy", "/some/destroy", tmpf, false) < 0) {
9c61a6d6
CB
349 lxc_error("%s\n", "lxc.hook.destroy");
350 goto non_test_error;
351 }
352
353 /* lxc.cap.drop */
ae4ad10d 354 if (set_get_compare_clear_save_load(c, "lxc.cap.drop", "sys_module mknod setuid net_raw", tmpf, false) < 0) {
9c61a6d6
CB
355 lxc_error("%s\n", "lxc.cap.drop");
356 goto non_test_error;
357 }
358
359 /* lxc.cap.keep */
ae4ad10d 360 if (set_get_compare_clear_save_load(c, "lxc.cap.keep", "sys_module mknod setuid net_raw", tmpf, false) < 0) {
9c61a6d6
CB
361 lxc_error("%s\n", "lxc.cap.keep");
362 goto non_test_error;
363 }
364
365 /* lxc.console */
ae4ad10d 366 if (set_get_compare_clear_save_load(c, "lxc.console", "none", tmpf, true) < 0) {
9c61a6d6
CB
367 lxc_error("%s\n", "lxc.console");
368 goto non_test_error;
369 }
370
371 /* lxc.console.logfile */
ae4ad10d 372 if (set_get_compare_clear_save_load(c, "lxc.console.logfile", "/some/logfile", tmpf, true) < 0) {
9c61a6d6
CB
373 lxc_error("%s\n", "lxc.console.logfile");
374 goto non_test_error;
375 }
376
377 /* lxc.seccomp */
ae4ad10d 378 if (set_get_compare_clear_save_load(c, "lxc.seccomp", "/some/seccomp/file", tmpf, true) < 0) {
9c61a6d6
CB
379 lxc_error("%s\n", "lxc.seccomp");
380 goto non_test_error;
381 }
382
383 /* lxc.autodev */
ae4ad10d 384 if (set_get_compare_clear_save_load(c, "lxc.autodev", "1", tmpf, true) < 0) {
9c61a6d6
CB
385 lxc_error("%s\n", "lxc.autodev");
386 goto non_test_error;
387 }
388
389 /* lxc.haltsignal */
ae4ad10d 390 if (set_get_compare_clear_save_load(c, "lxc.haltsignal", "1", tmpf, true) < 0) {
9c61a6d6
CB
391 lxc_error("%s\n", "lxc.haltsignal");
392 goto non_test_error;
393 }
394
395 /* lxc.rebootsignal */
ae4ad10d 396 if (set_get_compare_clear_save_load(c, "lxc.rebootsignal", "1", tmpf, true) < 0) {
9c61a6d6
CB
397 lxc_error("%s\n", "lxc.rebootsignal");
398 goto non_test_error;
399 }
400
401 /* lxc.stopsignal */
ae4ad10d 402 if (set_get_compare_clear_save_load(c, "lxc.stopsignal", "1", tmpf, true) < 0) {
9c61a6d6
CB
403 lxc_error("%s\n", "lxc.stopsignal");
404 goto non_test_error;
405 }
406
407 /* lxc.start.auto */
ae4ad10d 408 if (set_get_compare_clear_save_load(c, "lxc.start.auto", "1", tmpf, true) < 0) {
9c61a6d6
CB
409 lxc_error("%s\n", "lxc.start.auto");
410 goto non_test_error;
411 }
412
413 /* lxc.start.delay */
ae4ad10d 414 if (set_get_compare_clear_save_load(c, "lxc.start.delay", "5", tmpf, true) < 0) {
9c61a6d6
CB
415 lxc_error("%s\n", "lxc.start.delay");
416 goto non_test_error;
417 }
418
419 /* lxc.start.order */
ae4ad10d 420 if (set_get_compare_clear_save_load(c, "lxc.start.order", "1", tmpf, true) < 0) {
9c61a6d6
CB
421 lxc_error("%s\n", "lxc.start.order");
422 goto non_test_error;
423 }
424
425 /* lxc.syslog */
ae4ad10d 426 if (set_get_compare_clear_save_load(c, "lxc.syslog", "local0", tmpf, true) < 0) {
9c61a6d6
CB
427 lxc_error("%s\n", "lxc.syslog");
428 goto non_test_error;
429 }
430
431 /* lxc.utsname */
ae4ad10d 432 if (set_get_compare_clear_save_load(c, "lxc.utsname", "get-schwifty", tmpf, true) <
9c61a6d6
CB
433 0) {
434 lxc_error("%s\n", "lxc.utsname");
435 goto non_test_error;
436 }
437
438 /* lxc.monitor.unshare */
ae4ad10d 439 if (set_get_compare_clear_save_load(c, "lxc.monitor.unshare", "1", tmpf, true) < 0) {
9c61a6d6
CB
440 lxc_error("%s\n", "lxc.monitor.unshare");
441 goto non_test_error;
442 }
443
444 /* lxc.group */
ae4ad10d 445 if (set_get_compare_clear_save_load(c, "lxc.group", "some,container,groups", tmpf, false) < 0) {
9c61a6d6
CB
446 lxc_error("%s\n", "lxc.group");
447 goto non_test_error;
448 }
449
450 /* lxc.environment */
ae4ad10d 451 if (set_get_compare_clear_save_load(c, "lxc.environment", "FOO=BAR", tmpf, false) < 0) {
9c61a6d6
CB
452 lxc_error("%s\n", "lxc.environment");
453 goto non_test_error;
454 }
455
456 /* lxc.init_cmd */
ae4ad10d 457 if (set_get_compare_clear_save_load(c, "lxc.init_cmd", "/bin/bash", tmpf, true) < 0) {
9c61a6d6
CB
458 lxc_error("%s\n", "lxc.init_cmd");
459 goto non_test_error;
460 }
461
462 /* lxc.init_uid */
ae4ad10d 463 if (set_get_compare_clear_save_load(c, "lxc.init_uid", "1000", tmpf, true) < 0) {
9c61a6d6
CB
464 lxc_error("%s\n", "lxc.init_uid");
465 goto non_test_error;
466 }
467
468 /* lxc.init_gid */
ae4ad10d 469 if (set_get_compare_clear_save_load(c, "lxc.init_gid", "1000", tmpf, true) < 0) {
9c61a6d6
CB
470 lxc_error("%s\n", "lxc.init_gid");
471 goto non_test_error;
472 }
473
474 /* lxc.ephemeral */
ae4ad10d 475 if (set_get_compare_clear_save_load(c, "lxc.ephemeral", "1", tmpf, true) < 0) {
9c61a6d6
CB
476 lxc_error("%s\n", "lxc.ephemeral");
477 goto non_test_error;
478 }
479
480 /* lxc.no_new_privs */
ae4ad10d 481 if (set_get_compare_clear_save_load(c, "lxc.no_new_privs", "1", tmpf, true) < 0) {
9c61a6d6
CB
482 lxc_error("%s\n", "lxc.no_new_privs");
483 goto non_test_error;
484 }
485
486 /* lxc.limit.nofile */
ae4ad10d 487 if (set_get_compare_clear_save_load(c, "lxc.limit.nofile", "65536", tmpf, true) < 0) {
9c61a6d6
CB
488 lxc_error("%s\n", "lxc.limit.nofile");
489 goto non_test_error;
490 }
491
492 ret = EXIT_SUCCESS;
493non_test_error:
494 c->destroy(c);
495 lxc_container_put(c);
496 exit(ret);
497}