]>
git.proxmox.com Git - mirror_lxc.git/blob - src/tests/get_item.c
3 * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
4 * Copyright © 2012 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>
24 #include <sys/types.h>
30 #include "lxc/state.h"
33 #define MYNAME "lxctest1"
35 int main(int argc
, char *argv
[])
37 int ret
= EXIT_FAILURE
;
38 struct lxc_container
*c
;
39 char v1
[2], v2
[256], v3
[2048];
41 if ((c
= lxc_container_new("testxyz", NULL
)) == NULL
) {
42 fprintf(stderr
, "%d: error opening lxc_container %s\n", __LINE__
, MYNAME
);
46 /* EXPECT SUCCESS: lxc.syslog with valid value. */
47 if (!c
->set_config_item(c
, "lxc.syslog", "local0")) {
48 lxc_error("%s\n", "Failed to set lxc.syslog.\n");
51 ret
= c
->get_config_item(c
, "lxc.syslog", v2
, 255);
53 lxc_error("Failed to retrieve lxc.syslog: %d.\n", ret
);
56 if (strcmp(v2
, "local0") != 0) {
57 lxc_error("Expected: local0 == %s.\n", v2
);
60 lxc_debug("Retrieving value for lxc.syslog correctly returned: %s.\n", v2
);
62 /* EXPECT FAILURE: lxc.syslog with invalid value. */
63 if (c
->set_config_item(c
, "lxc.syslog", "NONSENSE")) {
64 lxc_error("%s\n", "Succeeded int setting lxc.syslog to invalid value \"NONSENSE\".\n");
67 lxc_debug("%s\n", "Successfully failed to set lxc.syslog to invalid value.\n");
69 if (!c
->set_config_item(c
, "lxc.hook.pre-start", "hi there")) {
70 fprintf(stderr
, "%d: failed to set hook.pre-start\n", __LINE__
);
73 ret
= c
->get_config_item(c
, "lxc.hook.pre-start", v2
, 255);
75 fprintf(stderr
, "%d: get_config_item(lxc.hook.pre-start) returned %d\n", __LINE__
, ret
);
78 fprintf(stderr
, "lxc.hook.pre-start returned %d %s\n", ret
, v2
);
80 ret
= c
->get_config_item(c
, "lxc.network", v2
, 255);
82 fprintf(stderr
, "%d: get_config_item returned %d\n", __LINE__
, ret
);
85 fprintf(stderr
, "%d: get_config_item(lxc.network) returned %d %s\n", __LINE__
, ret
, v2
);
86 if (!c
->set_config_item(c
, "lxc.tty", "4")) {
87 fprintf(stderr
, "%d: failed to set tty\n", __LINE__
);
90 ret
= c
->get_config_item(c
, "lxc.tty", v2
, 255);
92 fprintf(stderr
, "%d: get_config_item(lxc.tty) returned %d\n", __LINE__
, ret
);
95 fprintf(stderr
, "lxc.tty returned %d %s\n", ret
, v2
);
97 if (!c
->set_config_item(c
, "lxc.arch", "x86")) {
98 fprintf(stderr
, "%d: failed to set arch\n", __LINE__
);
101 ret
= c
->get_config_item(c
, "lxc.arch", v2
, 255);
103 fprintf(stderr
, "%d: get_config_item(lxc.arch) returned %d\n", __LINE__
, ret
);
106 printf("lxc.arch returned %d %s\n", ret
, v2
);
108 if (!c
->set_config_item(c
, "lxc.init_uid", "100")) {
109 fprintf(stderr
, "%d: failed to set init_uid\n", __LINE__
);
112 ret
= c
->get_config_item(c
, "lxc.init_uid", v2
, 255);
114 fprintf(stderr
, "%d: get_config_item(lxc.init_uid) returned %d\n", __LINE__
, ret
);
117 printf("lxc.init_uid returned %d %s\n", ret
, v2
);
119 if (!c
->set_config_item(c
, "lxc.init_gid", "100")) {
120 fprintf(stderr
, "%d: failed to set init_gid\n", __LINE__
);
123 ret
= c
->get_config_item(c
, "lxc.init_gid", v2
, 255);
125 fprintf(stderr
, "%d: get_config_item(lxc.init_gid) returned %d\n", __LINE__
, ret
);
128 printf("lxc.init_gid returned %d %s\n", ret
, v2
);
130 #define HNAME "hostname1"
131 // demonstrate proper usage:
133 if (!c
->set_config_item(c
, "lxc.utsname", HNAME
)) {
134 fprintf(stderr
, "%d: failed to set utsname\n", __LINE__
);
139 len
= c
->get_config_item(c
, "lxc.utsname", NULL
, 0); // query the size of the string
141 fprintf(stderr
, "%d: get_config_item(lxc.utsname) returned %d\n", __LINE__
, len
);
144 printf("lxc.utsname returned %d\n", len
);
146 // allocate the length of string + 1 for trailing \0
147 alloced
= malloc(len
+1);
149 fprintf(stderr
, "%d: failed to allocate %d bytes for utsname\n", __LINE__
, len
);
152 // now pass in the malloc'd array, and pass in length of string + 1: again
153 // because we need room for the trailing \0
154 ret
= c
->get_config_item(c
, "lxc.utsname", alloced
, len
+1);
156 fprintf(stderr
, "%d: get_config_item(lxc.utsname) returned %d\n", __LINE__
, ret
);
159 if (strcmp(alloced
, HNAME
) != 0 || ret
!= len
) {
160 fprintf(stderr
, "lxc.utsname returned wrong value: %d %s not %d %s\n", ret
, alloced
, len
, HNAME
);
163 printf("lxc.utsname returned %d %s\n", len
, alloced
);
166 if (!c
->set_config_item(c
, "lxc.mount.entry", "hi there")) {
167 fprintf(stderr
, "%d: failed to set mount.entry\n", __LINE__
);
170 ret
= c
->get_config_item(c
, "lxc.mount.entry", v2
, 255);
172 fprintf(stderr
, "%d: get_config_item(lxc.mount.entry) returned %d\n", __LINE__
, ret
);
175 printf("lxc.mount.entry returned %d %s\n", ret
, v2
);
177 if (!c
->set_config_item(c
, "lxc.aa_profile", "unconfined")) {
178 fprintf(stderr
, "%d: failed to set aa_profile\n", __LINE__
);
181 ret
= c
->get_config_item(c
, "lxc.aa_profile", v2
, 255);
183 fprintf(stderr
, "%d: get_config_item(lxc.aa_profile) returned %d\n", __LINE__
, ret
);
186 printf("lxc.aa_profile returned %d %s\n", ret
, v2
);
188 lxc_container_put(c
);
190 // new test with real container
191 if ((c
= lxc_container_new(MYNAME
, NULL
)) == NULL
) {
192 fprintf(stderr
, "%d: error opening lxc_container %s\n", __LINE__
, MYNAME
);
196 lxc_container_put(c
);
198 if ((c
= lxc_container_new(MYNAME
, NULL
)) == NULL
) {
199 fprintf(stderr
, "%d: error opening lxc_container %s\n", __LINE__
, MYNAME
);
202 if (!c
->createl(c
, "busybox", NULL
, NULL
, 0, NULL
)) {
203 fprintf(stderr
, "%d: failed to create a trusty container\n", __LINE__
);
206 lxc_container_put(c
);
208 /* XXX TODO load_config needs to clear out any old config first */
209 if ((c
= lxc_container_new(MYNAME
, NULL
)) == NULL
) {
210 fprintf(stderr
, "%d: error opening lxc_container %s\n", __LINE__
, MYNAME
);
214 ret
= c
->get_config_item(c
, "lxc.cap.drop", NULL
, 300);
215 if (ret
< 5 || ret
> 255) {
216 fprintf(stderr
, "%d: get_config_item(lxc.cap.drop) with NULL returned %d\n", __LINE__
, ret
);
219 ret
= c
->get_config_item(c
, "lxc.cap.drop", v1
, 1);
220 if (ret
< 5 || ret
> 255) {
221 fprintf(stderr
, "%d: get_config_item(lxc.cap.drop) returned %d\n", __LINE__
, ret
);
224 ret
= c
->get_config_item(c
, "lxc.cap.drop", v2
, 255);
226 fprintf(stderr
, "%d: get_config_item(lxc.cap.drop) returned %d %s\n", __LINE__
, ret
, v2
);
229 printf("%d: get_config_item(lxc.cap.drop) returned %d %s\n", __LINE__
, ret
, v2
);
230 ret
= c
->get_config_item(c
, "lxc.network", v2
, 255);
232 fprintf(stderr
, "%d: get_config_item returned %d\n", __LINE__
, ret
);
235 printf("%d: get_config_item(lxc.network) returned %d %s\n", __LINE__
, ret
, v2
);
237 if (!c
->set_config_item(c
, "lxc.network.ipv4", "10.2.3.4")) {
238 fprintf(stderr
, "%d: failed to set ipv4\n", __LINE__
);
242 ret
= c
->get_config_item(c
, "lxc.network.0.ipv4", v2
, 255);
244 fprintf(stderr
, "%d: lxc.network.0.ipv4 returned %d\n", __LINE__
, ret
);
247 if (!c
->clear_config_item(c
, "lxc.network.0.ipv4")) {
248 fprintf(stderr
, "%d: failed clearing all ipv4 entries\n", __LINE__
);
251 ret
= c
->get_config_item(c
, "lxc.network.0.ipv4", v2
, 255);
253 fprintf(stderr
, "%d: after clearing ipv4 entries get_item(lxc.network.0.ipv4 returned %d\n", __LINE__
, ret
);
257 if (!c
->set_config_item(c
, "lxc.network.ipv4.gateway", "10.2.3.254")) {
258 fprintf(stderr
, "%d: failed to set ipv4.gateway\n", __LINE__
);
262 ret
= c
->get_config_item(c
, "lxc.network.0.ipv4.gateway", v2
, 255);
264 fprintf(stderr
, "%d: lxc.network.0.ipv4.gateway returned %d\n", __LINE__
, ret
);
267 if (!c
->set_config_item(c
, "lxc.network.0.ipv4.gateway", "")) {
268 fprintf(stderr
, "%d: failed clearing ipv4.gateway\n", __LINE__
);
271 ret
= c
->get_config_item(c
, "lxc.network.0.ipv4.gateway", v2
, 255);
273 fprintf(stderr
, "%d: after clearing ipv4.gateway get_item(lxc.network.0.ipv4.gateway returned %d\n", __LINE__
, ret
);
277 ret
= c
->get_config_item(c
, "lxc.network.0.link", v2
, 255);
279 fprintf(stderr
, "%d: get_config_item returned %d\n", __LINE__
, ret
);
282 printf("%d: get_config_item (link) returned %d %s\n", __LINE__
, ret
, v2
);
283 ret
= c
->get_config_item(c
, "lxc.network.0.name", v2
, 255);
285 fprintf(stderr
, "%d: get_config_item returned %d\n", __LINE__
, ret
);
288 printf("%d: get_config_item (name) returned %d %s\n", __LINE__
, ret
, v2
);
290 if (!c
->clear_config_item(c
, "lxc.network")) {
291 fprintf(stderr
, "%d: clear_config_item failed\n", __LINE__
);
294 ret
= c
->get_config_item(c
, "lxc.network", v2
, 255);
296 fprintf(stderr
, "%d: network was not actually cleared (get_network returned %d)\n", __LINE__
, ret
);
300 ret
= c
->get_config_item(c
, "lxc.cgroup", v3
, 2047);
302 fprintf(stderr
, "%d: get_config_item(cgroup.devices) returned %d\n", __LINE__
, ret
);
305 printf("%d: get_config_item (cgroup.devices) returned %d %s\n", __LINE__
, ret
, v3
);
307 ret
= c
->get_config_item(c
, "lxc.cgroup.devices.allow", v3
, 2047);
309 fprintf(stderr
, "%d: get_config_item(cgroup.devices.devices.allow) returned %d\n", __LINE__
, ret
);
312 printf("%d: get_config_item (cgroup.devices.devices.allow) returned %d %s\n", __LINE__
, ret
, v3
);
314 if (!c
->clear_config_item(c
, "lxc.cgroup")) {
315 fprintf(stderr
, "%d: failed clearing lxc.cgroup\n", __LINE__
);
318 if (!c
->clear_config_item(c
, "lxc.cap.drop")) {
319 fprintf(stderr
, "%d: failed clearing lxc.cap.drop\n", __LINE__
);
322 if (!c
->clear_config_item(c
, "lxc.mount.entry")) {
323 fprintf(stderr
, "%d: failed clearing lxc.mount.entry\n", __LINE__
);
326 if (!c
->clear_config_item(c
, "lxc.hook")) {
327 fprintf(stderr
, "%d: failed clearing lxc.hook\n", __LINE__
);
330 printf("All get_item tests passed\n");
334 lxc_container_put(c
);