]>
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>
26 #include <sys/types.h>
30 #include "confile_utils.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
);
91 static int set_and_clear_complete_netdev(struct lxc_container
*c
)
93 if (!c
->set_config_item(c
, "lxc.net.1.type", "veth")) {
94 lxc_error("%s\n", "lxc.net.1.type");
98 if (!c
->set_config_item(c
, "lxc.net.1.ipv4", "10.0.2.3/24")) {
99 lxc_error("%s\n", "lxc.net.1.ipv4");
103 if (!c
->set_config_item(c
, "lxc.net.1.ipv4_gateway", "10.0.2.2")) {
104 lxc_error("%s\n", "lxc.net.1.ipv4");
108 if (!c
->set_config_item(c
, "lxc.net.1.ipv6",
109 "2003:db8:1:0:214:1234:fe0b:3596/64")) {
110 lxc_error("%s\n", "lxc.net.1.ipv6");
114 if (!c
->set_config_item(c
, "lxc.net.1.ipv6_gateway",
115 "2003:db8:1:0::1")) {
116 lxc_error("%s\n", "lxc.net.1.ipv6");
120 if (!c
->set_config_item(c
, "lxc.net.1.flags", "up")) {
121 lxc_error("%s\n", "lxc.net.1.flags");
125 if (!c
->set_config_item(c
, "lxc.net.1.link", "br0")) {
126 lxc_error("%s\n", "lxc.net.1.link");
130 if (!c
->set_config_item(c
, "lxc.net.1.veth.pair", "bla")) {
131 lxc_error("%s\n", "lxc.net.1.veth.pair");
135 if (!c
->set_config_item(c
, "lxc.net.1.hwaddr",
136 "52:54:00:80:7a:5d")) {
137 lxc_error("%s\n", "lxc.net.1.hwaddr");
141 if (!c
->set_config_item(c
, "lxc.net.1.mtu", "2000")) {
142 lxc_error("%s\n", "lxc.net.1.mtu");
146 if (!c
->clear_config_item(c
, "lxc.net.1")) {
147 lxc_error("%s", "failed to clear \"lxc.net.1\"\n");
157 int test_idmap_parser(void)
164 static struct idmap_check idmaps
[] = {
168 { true, "u 1 100001 999999999" },
169 { true, "g 1 100001 999999999" },
172 { true, "u 1000 165536 65536" },
173 { true, "g 999 999 1" },
174 { true, "u 0 5000 100000" },
175 { true, "g 577 789 5" },
176 { true, "u 65536 65536 1 " },
178 { false, "1u 0 0 0" },
179 { false, "1g 0 0 0a" },
180 { false, "1 u 0 0 0" },
181 { false, "1g 0 0 0 1" },
182 { false, "1u a0 b0 c0 d1" },
183 { false, "1g 0 b0 0 d1" },
184 { false, "1u a0 0 c0 1" },
185 { false, "g -1 0 -10" },
186 { false, "a 1 0 10" },
187 { false, "u 1 1 0 10" },
188 { false, "g 1 0 10 z " },
191 for (i
= 0; i
< sizeof(idmaps
) / sizeof(struct idmap_check
); i
++) {
192 unsigned long hostid
, nsid
, range
;
195 ret
= parse_idmaps(idmaps
[i
].idmap
, &type
, &nsid
, &hostid
,
197 if ((ret
< 0 && idmaps
[i
].is_valid
) ||
198 (ret
== 0 && !idmaps
[i
].is_valid
)) {
199 lxc_error("failed to parse idmap \"%s\"\n",
208 static int set_get_compare_clear_save_load_network(
209 struct lxc_container
*c
, const char *key
, const char *value
,
210 const char *config_file
, bool compare
, const char *network_type
)
212 char retval
[4096] = {0};
215 if (!c
->set_config_item(c
, "lxc.net.0.type", network_type
)) {
216 lxc_error("%s\n", "lxc.net.0.type");
220 if (!c
->set_config_item(c
, key
, value
)) {
221 lxc_error("failed to set config item \"%s\" to \"%s\"\n", key
,
226 ret
= c
->get_config_item(c
, key
, retval
, sizeof(retval
));
228 lxc_error("failed to get config item \"%s\"\n", key
);
233 ret
= strcmp(retval
, value
);
236 "expected value \"%s\" and retrieved value \"%s\" "
237 "for config key \"%s\" do not match\n",
244 if (!c
->save_config(c
, config_file
)) {
245 lxc_error("%s\n", "failed to save config file");
252 if (!c
->load_config(c
, config_file
)) {
253 lxc_error("%s\n", "failed to load config file");
258 if (!c
->clear_config_item(c
, key
)) {
259 lxc_error("failed to clear config item \"%s\"\n", key
);
263 if (!c
->clear_config_item(c
, "lxc.net.0.type")) {
264 lxc_error("%s\n", "lxc.net.0.type");
274 int main(int argc
, char *argv
[])
276 struct lxc_container
*c
;
278 int ret
= EXIT_FAILURE
;
279 char tmpf
[] = "lxc-parse-config-file-XXXXXX";
280 char retval
[4096] = {0};
282 c
= lxc_container_new(tmpf
, NULL
);
284 lxc_error("%s\n", "failed to create new container");
290 lxc_error("%s\n", "Could not create temporary file");
297 if (set_get_compare_clear_save_load(c
, "lxc.arch", "x86_64", tmpf
,
299 lxc_error("%s\n", "lxc.arch");
304 if (set_get_compare_clear_save_load(c
, "lxc.pts", "1000", tmpf
, true) <
306 lxc_error("%s\n", "lxc.pts");
311 if (set_get_compare_clear_save_load(c
, "lxc.tty", "4", tmpf
, true) <
313 lxc_error("%s\n", "lxc.tty");
318 if (set_get_compare_clear_save_load(c
, "lxc.devttydir", "not-dev", tmpf
,
320 lxc_error("%s\n", "lxc.devttydir");
327 if (set_get_compare_clear_save_load(c
, "lxc.aa_profile", "unconfined",
329 lxc_error("%s\n", "lxc.aa_profile");
336 if (set_get_compare_clear_save_load(c
, "lxc.aa_allow_incomplete", "1",
338 lxc_error("%s\n", "lxc.aa_allow_incomplete");
345 if (set_get_compare_clear_save_load(c
, "lxc.se_context", "system_u:system_r:lxc_t:s0:c22",
347 lxc_error("%s\n", "lxc.apparmor.se_context");
351 /* lxc.apparmor.profile */
352 if (set_get_compare_clear_save_load(c
, "lxc.apparmor.profile", "unconfined",
354 lxc_error("%s\n", "lxc.apparmor.profile");
358 /* lxc.apparmor.allow_incomplete */
359 if (set_get_compare_clear_save_load(c
, "lxc.apparmor.allow_incomplete", "1",
361 lxc_error("%s\n", "lxc.apparmor.allow_incomplete");
365 /* lxc.selinux.context */
366 if (set_get_compare_clear_save_load(c
, "lxc.selinux.context", "system_u:system_r:lxc_t:s0:c22",
368 lxc_error("%s\n", "lxc.apparmor.selinux.context");
372 /* lxc.cgroup.cpuset.cpus */
373 if (set_get_compare_clear_save_load(c
, "lxc.cgroup.cpuset.cpus",
374 "1-100", tmpf
, false) < 0) {
375 lxc_error("%s\n", "lxc.cgroup.cpuset.cpus");
380 if (!c
->set_config_item(c
, "lxc.cgroup.cpuset.cpus", "1-100")) {
381 lxc_error("%s\n", "failed to set config item "
382 "\"lxc.cgroup.cpuset.cpus\" to \"1-100\"");
386 if (!c
->set_config_item(c
, "lxc.cgroup.memory.limit_in_bytes",
390 "failed to set config item "
391 "\"lxc.cgroup.memory.limit_in_bytes\" to \"123456789\"");
395 if (!c
->get_config_item(c
, "lxc.cgroup", retval
, sizeof(retval
))) {
396 lxc_error("%s\n", "failed to get config item \"lxc.cgroup\"");
404 * We can't really save the config here since save_config() wants to
405 * chown the container's directory but we haven't created an on-disk
406 * container. So let's test set-get-clear.
408 if (set_get_compare_clear_save_load(
409 c
, "lxc.id_map", "u 0 100000 1000000000", NULL
, false) < 0) {
410 lxc_error("%s\n", "lxc.id_map");
414 if (!c
->set_config_item(c
, "lxc.id_map", "u 1 100000 10000000")) {
415 lxc_error("%s\n", "failed to set config item "
416 "\"lxc.id_map\" to \"u 1 100000 10000000\"");
420 if (!c
->set_config_item(c
, "lxc.id_map", "g 1 100000 10000000")) {
421 lxc_error("%s\n", "failed to set config item "
422 "\"lxc.id_map\" to \"g 1 100000 10000000\"");
426 if (!c
->get_config_item(c
, "lxc.id_map", retval
, sizeof(retval
))) {
427 lxc_error("%s\n", "failed to get config item \"lxc.cgroup\"");
435 if (set_get_compare_clear_save_load(c
, "lxc.loglevel", "DEBUG", tmpf
,
437 lxc_error("%s\n", "lxc.loglevel");
442 if (set_get_compare_clear_save_load(c
, "lxc.logfile", "/some/path",
444 lxc_error("%s\n", "lxc.logfile");
449 if (set_get_compare_clear_save_load(c
, "lxc.mount", "/some/path", NULL
,
451 lxc_error("%s\n", "lxc.mount");
456 * Note that we cannot compare the values since the getter for
457 * lxc.mount.auto does not preserve ordering.
459 if (set_get_compare_clear_save_load(c
, "lxc.mount.auto",
460 "proc:rw sys:rw cgroup-full:rw",
462 lxc_error("%s\n", "lxc.mount.auto");
467 * Note that we cannot compare the values since the getter for
468 * lxc.mount.entry appends newlines.
470 if (set_get_compare_clear_save_load(
471 c
, "lxc.mount.entry",
472 "/dev/dri dev/dri none bind,optional,create=dir", tmpf
,
474 lxc_error("%s\n", "lxc.mount.entry");
479 if (set_get_compare_clear_save_load(c
, "lxc.rootfs", "/some/path", tmpf
,
481 lxc_error("%s\n", "lxc.rootfs");
485 /* lxc.rootfs.mount */
486 if (set_get_compare_clear_save_load(c
, "lxc.rootfs.mount", "/some/path",
488 lxc_error("%s\n", "lxc.rootfs.mount");
492 /* lxc.rootfs.options */
493 if (set_get_compare_clear_save_load(c
, "lxc.rootfs.options",
494 "ext4,discard", tmpf
, true) < 0) {
495 lxc_error("%s\n", "lxc.rootfs.options");
499 /* lxc.rootfs.backend */
500 if (set_get_compare_clear_save_load(c
, "lxc.rootfs.backend", "btrfs",
502 lxc_error("%s\n", "lxc.rootfs.backend");
507 if (set_get_compare_clear_save_load(c
, "lxc.utsname", "the-shire", tmpf
,
509 lxc_error("%s\n", "lxc.utsname");
513 /* lxc.hook.pre-start */
514 if (set_get_compare_clear_save_load(
515 c
, "lxc.hook.pre-start", "/some/pre-start", tmpf
, false) < 0) {
516 lxc_error("%s\n", "lxc.hook.pre-start");
520 /* lxc.hook.pre-mount */
521 if (set_get_compare_clear_save_load(
522 c
, "lxc.hook.pre-mount", "/some/pre-mount", tmpf
, false) < 0) {
523 lxc_error("%s\n", "lxc.hook.pre-mount");
528 if (set_get_compare_clear_save_load(c
, "lxc.hook.mount", "/some/mount",
530 lxc_error("%s\n", "lxc.hook.mount");
534 /* lxc.hook.autodev */
535 if (set_get_compare_clear_save_load(c
, "lxc.hook.autodev",
536 "/some/autodev", tmpf
, false) < 0) {
537 lxc_error("%s\n", "lxc.hook.autodev");
542 if (set_get_compare_clear_save_load(c
, "lxc.hook.start", "/some/start",
544 lxc_error("%s\n", "lxc.hook.start");
549 if (set_get_compare_clear_save_load(c
, "lxc.hook.stop", "/some/stop",
551 lxc_error("%s\n", "lxc.hook.stop");
555 /* lxc.hook.post-stop */
556 if (set_get_compare_clear_save_load(
557 c
, "lxc.hook.post-stop", "/some/post-stop", tmpf
, false) < 0) {
558 lxc_error("%s\n", "lxc.hook.post-stop");
563 if (set_get_compare_clear_save_load(c
, "lxc.hook.clone", "/some/clone",
565 lxc_error("%s\n", "lxc.hook.clone");
569 /* lxc.hook.destroy */
570 if (set_get_compare_clear_save_load(c
, "lxc.hook.destroy",
571 "/some/destroy", tmpf
, false) < 0) {
572 lxc_error("%s\n", "lxc.hook.destroy");
577 if (set_get_compare_clear_save_load(c
, "lxc.cap.drop",
578 "sys_module mknod setuid net_raw",
580 lxc_error("%s\n", "lxc.cap.drop");
585 if (set_get_compare_clear_save_load(c
, "lxc.cap.keep",
586 "sys_module mknod setuid net_raw",
588 lxc_error("%s\n", "lxc.cap.keep");
593 if (set_get_compare_clear_save_load(c
, "lxc.console", "none", tmpf
,
595 lxc_error("%s\n", "lxc.console");
599 /* lxc.console.logfile */
600 if (set_get_compare_clear_save_load(c
, "lxc.console.logfile",
601 "/some/logfile", tmpf
, true) < 0) {
602 lxc_error("%s\n", "lxc.console.logfile");
607 if (set_get_compare_clear_save_load(
608 c
, "lxc.seccomp", "/some/seccomp/file", tmpf
, true) < 0) {
609 lxc_error("%s\n", "lxc.seccomp");
614 if (set_get_compare_clear_save_load(c
, "lxc.autodev", "1", tmpf
, true) <
616 lxc_error("%s\n", "lxc.autodev");
621 if (set_get_compare_clear_save_load(c
, "lxc.haltsignal", "1", tmpf
,
623 lxc_error("%s\n", "lxc.haltsignal");
627 /* lxc.rebootsignal */
628 if (set_get_compare_clear_save_load(c
, "lxc.rebootsignal", "1", tmpf
,
630 lxc_error("%s\n", "lxc.rebootsignal");
635 if (set_get_compare_clear_save_load(c
, "lxc.stopsignal", "1", tmpf
,
637 lxc_error("%s\n", "lxc.stopsignal");
642 if (set_get_compare_clear_save_load(c
, "lxc.start.auto", "1", tmpf
,
644 lxc_error("%s\n", "lxc.start.auto");
648 /* lxc.start.delay */
649 if (set_get_compare_clear_save_load(c
, "lxc.start.delay", "5", tmpf
,
651 lxc_error("%s\n", "lxc.start.delay");
655 /* lxc.start.order */
656 if (set_get_compare_clear_save_load(c
, "lxc.start.order", "1", tmpf
,
658 lxc_error("%s\n", "lxc.start.order");
663 if (set_get_compare_clear_save_load(c
, "lxc.syslog", "local0", tmpf
,
665 lxc_error("%s\n", "lxc.syslog");
670 if (set_get_compare_clear_save_load(c
, "lxc.utsname", "get-schwifty",
672 lxc_error("%s\n", "lxc.utsname");
676 /* lxc.monitor.unshare */
677 if (set_get_compare_clear_save_load(c
, "lxc.monitor.unshare", "1", tmpf
,
679 lxc_error("%s\n", "lxc.monitor.unshare");
684 if (set_get_compare_clear_save_load(
685 c
, "lxc.group", "some,container,groups", tmpf
, false) < 0) {
686 lxc_error("%s\n", "lxc.group");
690 /* lxc.environment */
691 if (set_get_compare_clear_save_load(c
, "lxc.environment", "FOO=BAR",
693 lxc_error("%s\n", "lxc.environment");
698 if (set_get_compare_clear_save_load(c
, "lxc.init_cmd", "/bin/bash",
700 lxc_error("%s\n", "lxc.init_cmd");
705 if (set_get_compare_clear_save_load(c
, "lxc.init_uid", "1000", tmpf
,
707 lxc_error("%s\n", "lxc.init_uid");
712 if (set_get_compare_clear_save_load(c
, "lxc.init_gid", "1000", tmpf
,
714 lxc_error("%s\n", "lxc.init_gid");
719 if (set_get_compare_clear_save_load(c
, "lxc.ephemeral", "1", tmpf
,
721 lxc_error("%s\n", "lxc.ephemeral");
725 /* lxc.no_new_privs */
726 if (set_get_compare_clear_save_load(c
, "lxc.no_new_privs", "1", tmpf
,
728 lxc_error("%s\n", "lxc.no_new_privs");
732 /* lxc.limit.nofile */
733 if (set_get_compare_clear_save_load(c
, "lxc.limit.nofile", "65536",
735 lxc_error("%s\n", "lxc.limit.nofile");
739 if (test_idmap_parser() < 0) {
740 lxc_error("%s\n", "failed to test parser for \"lxc.id_map\"");
744 if (set_get_compare_clear_save_load(c
, "lxc.net.0.type", "veth",
746 lxc_error("%s\n", "lxc.net.0.type");
750 if (set_get_compare_clear_save_load(c
, "lxc.net.2.type", "none",
752 lxc_error("%s\n", "lxc.net.2.type");
756 if (set_get_compare_clear_save_load(c
, "lxc.net.3.type", "empty",
758 lxc_error("%s\n", "lxc.net.3.type");
762 if (set_get_compare_clear_save_load(c
, "lxc.net.4.type", "vlan",
764 lxc_error("%s\n", "lxc.net.4.type");
768 if (set_get_compare_clear_save_load(c
, "lxc.net.0.type", "macvlan",
770 lxc_error("%s\n", "lxc.net.0.type");
774 if (set_get_compare_clear_save_load(c
, "lxc.net.1000.type", "phys",
776 lxc_error("%s\n", "lxc.net.1000.type");
780 if (set_get_compare_clear_save_load(c
, "lxc.net.0.flags", "up",
782 lxc_error("%s\n", "lxc.net.0.flags");
786 if (set_get_compare_clear_save_load(c
, "lxc.net.0.name", "eth0",
788 lxc_error("%s\n", "lxc.net.0.name");
792 if (set_get_compare_clear_save_load(c
, "lxc.net.0.link", "bla",
794 lxc_error("%s\n", "lxc.net.0.link");
798 if (set_get_compare_clear_save_load_network(
799 c
, "lxc.net.0.macvlan.mode", "private", tmpf
, true,
801 lxc_error("%s\n", "lxc.net.0.macvlan.mode");
805 if (set_get_compare_clear_save_load_network(
806 c
, "lxc.net.0.macvlan.mode", "vepa", tmpf
, true,
808 lxc_error("%s\n", "lxc.net.0.macvlan.mode");
812 if (set_get_compare_clear_save_load_network(
813 c
, "lxc.net.0.macvlan.mode", "bridge", tmpf
, true,
815 lxc_error("%s\n", "lxc.net.0.macvlan.mode");
819 if (set_get_compare_clear_save_load_network(
820 c
, "lxc.net.0.veth.pair", "clusterfuck", tmpf
, true,
822 lxc_error("%s\n", "lxc.net.0.veth.pair");
826 if (set_get_compare_clear_save_load(c
, "lxc.net.0.script.up",
827 "/some/up/path", tmpf
, true)) {
828 lxc_error("%s\n", "lxc.net.0.script.up");
832 if (set_get_compare_clear_save_load(c
, "lxc.net.0.script.down",
833 "/some/down/path", tmpf
, true)) {
834 lxc_error("%s\n", "lxc.net.0.script.down");
838 if (set_get_compare_clear_save_load(c
, "lxc.net.0.hwaddr",
839 "52:54:00:80:7a:5d", tmpf
, true)) {
840 lxc_error("%s\n", "lxc.net.0.hwaddr");
844 if (set_get_compare_clear_save_load(c
, "lxc.net.0.mtu", "2000",
846 lxc_error("%s\n", "lxc.net.0.mtu");
850 if (set_get_compare_clear_save_load_network(c
, "lxc.net.0.vlan.id",
851 "2", tmpf
, true, "vlan")) {
852 lxc_error("%s\n", "lxc.net.0.vlan.id");
856 if (set_get_compare_clear_save_load(c
, "lxc.net.0.ipv4.gateway",
857 "10.0.2.2", tmpf
, true)) {
858 lxc_error("%s\n", "lxc.net.0.ipv4.gateway");
862 if (set_get_compare_clear_save_load(c
, "lxc.net.0.ipv6.gateway",
863 "2003:db8:1::1", tmpf
, true)) {
864 lxc_error("%s\n", "lxc.net.0.ipv6.gateway");
868 if (set_get_compare_clear_save_load(c
, "lxc.net.0.ipv4",
869 "10.0.2.3/24", tmpf
, true)) {
870 lxc_error("%s\n", "lxc.net.0.ipv4");
874 if (set_get_compare_clear_save_load(
875 c
, "lxc.net.0.ipv6", "2003:db8:1:0:214:1234:fe0b:3596/64",
877 lxc_error("%s\n", "lxc.net.0.ipv6");
881 if (set_and_clear_complete_netdev(c
) < 0) {
882 lxc_error("%s\n", "failed to clear whole network");
890 lxc_container_put(c
);