]>
git.proxmox.com Git - mirror_lxc.git/blob - src/tests/parse_config_file.c
3 * Copyright © 2017 Christian Brauner <christian.brauner@ubuntu.com>.
4 * Copyright © 2017 Canonical Ltd.
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.
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.
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.
19 #include <lxc/lxccontainer.h>
25 #include <sys/types.h>
31 #include "lxc/state.h"
34 static int set_get_compare_clear_save_load(struct lxc_container
*c
,
35 const char *key
, const char *value
,
36 const char *config_file
,
39 char retval
[4096] = {0};
42 if (!c
->set_config_item(c
, key
, value
)) {
43 lxc_error("failed to set config item \"%s\" to \"%s\"\n", key
,
48 ret
= c
->get_config_item(c
, key
, retval
, sizeof(retval
));
50 lxc_error("failed to get config item \"%s\"\n", key
);
55 ret
= strcmp(retval
, value
);
58 "expected value \"%s\" and retrieved value \"%s\" "
59 "for config key \"%s\" do not match\n",
66 if (!c
->save_config(c
, config_file
)) {
67 lxc_error("%s\n", "failed to save config file");
74 if (!c
->load_config(c
, config_file
)) {
75 lxc_error("%s\n", "failed to load config file");
80 if (!c
->clear_config_item(c
, key
)) {
81 lxc_error("failed to clear config item \"%s\"\n", key
);
86 if (!c
->save_config(c
, config_file
)) {
87 lxc_error("%s\n", "failed to save config file");
94 if (!c
->load_config(c
, config_file
)) {
95 lxc_error("%s\n", "failed to load config file");
106 int main(int argc
, char *argv
[])
108 struct lxc_container
*c
;
110 int ret
= EXIT_FAILURE
;
111 char tmpf
[] = "lxc-parse-config-file-XXXXXX";
112 char retval
[4096] = {0};
114 c
= lxc_container_new("lxc-parse-config-file-testxyz", NULL
);
116 lxc_error("%s\n", "failed to create new container");
122 lxc_error("%s\n", "Could not create temporary file");
128 if (set_get_compare_clear_save_load(c
, "lxc.arch", "x86_64", tmpf
, true) < 0) {
129 lxc_error("%s\n", "lxc.arch");
134 if (set_get_compare_clear_save_load(c
, "lxc.pts", "1000", tmpf
, true) < 0) {
135 lxc_error("%s\n", "lxc.pts");
140 if (set_get_compare_clear_save_load(c
, "lxc.tty", "4", tmpf
, true) < 0) {
141 lxc_error("%s\n", "lxc.tty");
146 if (set_get_compare_clear_save_load(c
, "lxc.devttydir", "not-dev", tmpf
, true) < 0) {
147 lxc_error("%s\n", "lxc.devttydir");
152 if (set_get_compare_clear_save_load(c
, "lxc.kmsg", "1", tmpf
, true) < 0) {
153 lxc_error("%s\n", "lxc.kmsg");
158 if (set_get_compare_clear_save_load(c
, "lxc.aa_profile", "unconfined", tmpf
, true) <
160 lxc_error("%s\n", "lxc.aa_profile");
164 /* lxc.aa_allow_incomplete */
165 if (set_get_compare_clear_save_load(c
, "lxc.aa_allow_incomplete", "1", tmpf
, true) < 0) {
166 lxc_error("%s\n", "lxc.aa_allow_incomplete");
170 /* lxc.cgroup.cpuset.cpus */
171 if (set_get_compare_clear_save_load(c
, "lxc.cgroup.cpuset.cpus", "1-100", tmpf
, false) < 0) {
172 lxc_error("%s\n", "lxc.cgroup.cpuset.cpus");
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\"");
183 if (!c
->set_config_item(c
, "lxc.cgroup.memory.limit_in_bytes",
187 "failed to set config item "
188 "\"lxc.cgroup.memory.limit_in_bytes\" to \"123456789\"");
192 if (!c
->get_config_item(c
, "lxc.cgroup", retval
, sizeof(retval
))) {
193 lxc_error("%s\n", "failed to get config item \"lxc.cgroup\"");
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.
205 if (set_get_compare_clear_save_load(c
, "lxc.id_map", "u 0 100000 1000000000",
207 lxc_error("%s\n", "lxc.id_map");
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\"");
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\"");
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\"");
232 if (set_get_compare_clear_save_load(c
, "lxc.loglevel", "DEBUG", tmpf
, true) < 0) {
233 lxc_error("%s\n", "lxc.loglevel");
238 if (set_get_compare_clear_save_load(c
, "lxc.logfile", "/some/path", tmpf
, true) < 0) {
239 lxc_error("%s\n", "lxc.logfile");
244 if (set_get_compare_clear_save_load(c
, "lxc.mount", "/some/path", NULL
, true) < 0) {
245 lxc_error("%s\n", "lxc.mount");
250 * Note that we cannot compare the values since the getter for
251 * lxc.mount.auto does not preserve ordering.
253 if (set_get_compare_clear_save_load(c
, "lxc.mount.auto", "proc:rw sys:rw cgroup-full:rw", tmpf
, false) < 0) {
254 lxc_error("%s\n", "lxc.mount.auto");
259 * Note that we cannot compare the values since the getter for
260 * lxc.mount.entry appends newlines.
262 if (set_get_compare_clear_save_load(
263 c
, "lxc.mount.entry",
264 "/dev/dri dev/dri none bind,optional,create=dir", tmpf
, false) < 0) {
265 lxc_error("%s\n", "lxc.mount.entry");
270 if (set_get_compare_clear_save_load(c
, "lxc.rootfs", "/some/path", tmpf
, true) < 0) {
271 lxc_error("%s\n", "lxc.rootfs");
275 /* lxc.rootfs.mount */
276 if (set_get_compare_clear_save_load(c
, "lxc.rootfs.mount", "/some/path", tmpf
, true) < 0) {
277 lxc_error("%s\n", "lxc.rootfs.mount");
281 /* lxc.rootfs.options */
282 if (set_get_compare_clear_save_load(c
, "lxc.rootfs.options", "ext4,discard", tmpf
, true) < 0) {
283 lxc_error("%s\n", "lxc.rootfs.options");
287 /* lxc.rootfs.backend */
288 if (set_get_compare_clear_save_load(c
, "lxc.rootfs.backend", "btrfs", tmpf
, true) < 0) {
289 lxc_error("%s\n", "lxc.rootfs.backend");
294 if (set_get_compare_clear_save_load(c
, "lxc.utsname", "the-shire", tmpf
, true) < 0) {
295 lxc_error("%s\n", "lxc.utsname");
299 /* lxc.hook.pre-start */
300 if (set_get_compare_clear_save_load(c
, "lxc.hook.pre-start", "/some/pre-start", tmpf
, false) < 0) {
301 lxc_error("%s\n", "lxc.hook.pre-start");
305 /* lxc.hook.pre-mount */
306 if (set_get_compare_clear_save_load(c
, "lxc.hook.pre-mount", "/some/pre-mount", tmpf
, false) < 0) {
307 lxc_error("%s\n", "lxc.hook.pre-mount");
312 if (set_get_compare_clear_save_load(c
, "lxc.hook.mount", "/some/mount", tmpf
, false) < 0) {
313 lxc_error("%s\n", "lxc.hook.mount");
317 /* lxc.hook.autodev */
318 if (set_get_compare_clear_save_load(c
, "lxc.hook.autodev", "/some/autodev", tmpf
, false) < 0) {
319 lxc_error("%s\n", "lxc.hook.autodev");
324 if (set_get_compare_clear_save_load(c
, "lxc.hook.start", "/some/start", tmpf
, false) < 0) {
325 lxc_error("%s\n", "lxc.hook.start");
330 if (set_get_compare_clear_save_load(c
, "lxc.hook.stop", "/some/stop", tmpf
, false) < 0) {
331 lxc_error("%s\n", "lxc.hook.stop");
335 /* lxc.hook.post-stop */
336 if (set_get_compare_clear_save_load(c
, "lxc.hook.post-stop", "/some/post-stop", tmpf
, false) < 0) {
337 lxc_error("%s\n", "lxc.hook.post-stop");
342 if (set_get_compare_clear_save_load(c
, "lxc.hook.clone", "/some/clone", tmpf
, false) < 0) {
343 lxc_error("%s\n", "lxc.hook.clone");
347 /* lxc.hook.destroy */
348 if (set_get_compare_clear_save_load(c
, "lxc.hook.destroy", "/some/destroy", tmpf
, false) < 0) {
349 lxc_error("%s\n", "lxc.hook.destroy");
354 if (set_get_compare_clear_save_load(c
, "lxc.cap.drop", "sys_module mknod setuid net_raw", tmpf
, false) < 0) {
355 lxc_error("%s\n", "lxc.cap.drop");
360 if (set_get_compare_clear_save_load(c
, "lxc.cap.keep", "sys_module mknod setuid net_raw", tmpf
, false) < 0) {
361 lxc_error("%s\n", "lxc.cap.keep");
366 if (set_get_compare_clear_save_load(c
, "lxc.console", "none", tmpf
, true) < 0) {
367 lxc_error("%s\n", "lxc.console");
371 /* lxc.console.logfile */
372 if (set_get_compare_clear_save_load(c
, "lxc.console.logfile", "/some/logfile", tmpf
, true) < 0) {
373 lxc_error("%s\n", "lxc.console.logfile");
378 if (set_get_compare_clear_save_load(c
, "lxc.seccomp", "/some/seccomp/file", tmpf
, true) < 0) {
379 lxc_error("%s\n", "lxc.seccomp");
384 if (set_get_compare_clear_save_load(c
, "lxc.autodev", "1", tmpf
, true) < 0) {
385 lxc_error("%s\n", "lxc.autodev");
390 if (set_get_compare_clear_save_load(c
, "lxc.haltsignal", "1", tmpf
, true) < 0) {
391 lxc_error("%s\n", "lxc.haltsignal");
395 /* lxc.rebootsignal */
396 if (set_get_compare_clear_save_load(c
, "lxc.rebootsignal", "1", tmpf
, true) < 0) {
397 lxc_error("%s\n", "lxc.rebootsignal");
402 if (set_get_compare_clear_save_load(c
, "lxc.stopsignal", "1", tmpf
, true) < 0) {
403 lxc_error("%s\n", "lxc.stopsignal");
408 if (set_get_compare_clear_save_load(c
, "lxc.start.auto", "1", tmpf
, true) < 0) {
409 lxc_error("%s\n", "lxc.start.auto");
413 /* lxc.start.delay */
414 if (set_get_compare_clear_save_load(c
, "lxc.start.delay", "5", tmpf
, true) < 0) {
415 lxc_error("%s\n", "lxc.start.delay");
419 /* lxc.start.order */
420 if (set_get_compare_clear_save_load(c
, "lxc.start.order", "1", tmpf
, true) < 0) {
421 lxc_error("%s\n", "lxc.start.order");
426 if (set_get_compare_clear_save_load(c
, "lxc.syslog", "local0", tmpf
, true) < 0) {
427 lxc_error("%s\n", "lxc.syslog");
432 if (set_get_compare_clear_save_load(c
, "lxc.utsname", "get-schwifty", tmpf
, true) <
434 lxc_error("%s\n", "lxc.utsname");
438 /* lxc.monitor.unshare */
439 if (set_get_compare_clear_save_load(c
, "lxc.monitor.unshare", "1", tmpf
, true) < 0) {
440 lxc_error("%s\n", "lxc.monitor.unshare");
445 if (set_get_compare_clear_save_load(c
, "lxc.group", "some,container,groups", tmpf
, false) < 0) {
446 lxc_error("%s\n", "lxc.group");
450 /* lxc.environment */
451 if (set_get_compare_clear_save_load(c
, "lxc.environment", "FOO=BAR", tmpf
, false) < 0) {
452 lxc_error("%s\n", "lxc.environment");
457 if (set_get_compare_clear_save_load(c
, "lxc.init_cmd", "/bin/bash", tmpf
, true) < 0) {
458 lxc_error("%s\n", "lxc.init_cmd");
463 if (set_get_compare_clear_save_load(c
, "lxc.init_uid", "1000", tmpf
, true) < 0) {
464 lxc_error("%s\n", "lxc.init_uid");
469 if (set_get_compare_clear_save_load(c
, "lxc.init_gid", "1000", tmpf
, true) < 0) {
470 lxc_error("%s\n", "lxc.init_gid");
475 if (set_get_compare_clear_save_load(c
, "lxc.ephemeral", "1", tmpf
, true) < 0) {
476 lxc_error("%s\n", "lxc.ephemeral");
480 /* lxc.no_new_privs */
481 if (set_get_compare_clear_save_load(c
, "lxc.no_new_privs", "1", tmpf
, true) < 0) {
482 lxc_error("%s\n", "lxc.no_new_privs");
486 /* lxc.limit.nofile */
487 if (set_get_compare_clear_save_load(c
, "lxc.limit.nofile", "65536", tmpf
, true) < 0) {
488 lxc_error("%s\n", "lxc.limit.nofile");
495 lxc_container_put(c
);