]>
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>
28 #include <lxc/state.h>
30 #define MYNAME "lxctest1"
32 int main(int argc
, char *argv
[])
34 struct lxc_container
*c
;
36 char v1
[2], v2
[256], v3
[2048];
38 if ((c
= lxc_container_new("testxyz")) == NULL
) {
39 fprintf(stderr
, "%d: error opening lxc_container %s\n", __LINE__
, MYNAME
);
44 if (!c
->set_config_item(c
, "lxc.hook.pre-start", "hi there")) {
45 fprintf(stderr
, "%d: failed to set hook.pre-start\n", __LINE__
);
49 ret
= c
->get_config_item(c
, "lxc.hook.pre-start", v2
, 255);
51 fprintf(stderr
, "%d: get_config_item(lxc.hook.pre-start) returned %d\n", __LINE__
, ret
);
55 fprintf(stderr
, "lxc.hook.pre-start returned %d %s\n", ret
, v2
);
57 ret
= c
->get_config_item(c
, "lxc.network", v2
, 255);
59 fprintf(stderr
, "%d: get_config_item returned %d\n", __LINE__
, ret
);
63 fprintf(stderr
, "%d: get_config_item(lxc.network) returned %d %s\n", __LINE__
, ret
, v2
);
64 if (!c
->set_config_item(c
, "lxc.tty", "4")) {
65 fprintf(stderr
, "%d: failed to set tty\n", __LINE__
);
69 ret
= c
->get_config_item(c
, "lxc.tty", v2
, 255);
71 fprintf(stderr
, "%d: get_config_item(lxc.tty) returned %d\n", __LINE__
, ret
);
75 fprintf(stderr
, "lxc.tty returned %d %s\n", ret
, v2
);
77 if (!c
->set_config_item(c
, "lxc.arch", "x86")) {
78 fprintf(stderr
, "%d: failed to set arch\n", __LINE__
);
82 ret
= c
->get_config_item(c
, "lxc.arch", v2
, 255);
84 fprintf(stderr
, "%d: get_config_item(lxc.arch) returned %d\n", __LINE__
, ret
);
88 printf("lxc.arch returned %d %s\n", ret
, v2
);
90 #define HNAME "hostname1"
91 // demonstrate proper usage:
93 if (!c
->set_config_item(c
, "lxc.utsname", HNAME
)) {
94 fprintf(stderr
, "%d: failed to set utsname\n", __LINE__
);
100 len
= c
->get_config_item(c
, "lxc.utsname", NULL
, 0); // query the size of the string
102 fprintf(stderr
, "%d: get_config_item(lxc.utsname) returned %d\n", __LINE__
, len
);
106 printf("lxc.utsname returned %d\n", len
);
108 // allocate the length of string + 1 for trailing \0
109 alloced
= malloc(len
+1);
111 fprintf(stderr
, "%d: failed to allocate %d bytes for utsname\n", __LINE__
, len
);
115 // now pass in the malloc'd array, and pass in length of string + 1: again
116 // because we need room for the trailing \0
117 ret
= c
->get_config_item(c
, "lxc.utsname", alloced
, len
+1);
119 fprintf(stderr
, "%d: get_config_item(lxc.utsname) returned %d\n", __LINE__
, ret
);
123 if (strcmp(alloced
, HNAME
) != 0 || ret
!= len
) {
124 fprintf(stderr
, "lxc.utsname returned wrong value: %d %s not %d %s\n", ret
, alloced
, len
, HNAME
);
128 printf("lxc.utsname returned %d %s\n", len
, alloced
);
131 if (!c
->set_config_item(c
, "lxc.mount.entry", "hi there")) {
132 fprintf(stderr
, "%d: failed to set mount.entry\n", __LINE__
);
136 ret
= c
->get_config_item(c
, "lxc.mount.entry", v2
, 255);
138 fprintf(stderr
, "%d: get_config_item(lxc.mount.entry) returned %d\n", __LINE__
, ret
);
142 printf("lxc.mount.entry returned %d %s\n", ret
, v2
);
144 if (!c
->set_config_item(c
, "lxc.aa_profile", "unconfined")) {
145 fprintf(stderr
, "%d: failed to set aa_profile\n", __LINE__
);
149 ret
= c
->get_config_item(c
, "lxc.aa_profile", v2
, 255);
151 fprintf(stderr
, "%d: get_config_item(lxc.aa_profile) returned %d\n", __LINE__
, ret
);
155 printf("lxc.aa_profile returned %d %s\n", ret
, v2
);
157 lxc_container_put(c
);
159 // new test with real container
160 if ((c
= lxc_container_new(MYNAME
)) == NULL
) {
161 fprintf(stderr
, "%d: error opening lxc_container %s\n", __LINE__
, MYNAME
);
166 lxc_container_put(c
);
168 if ((c
= lxc_container_new(MYNAME
)) == NULL
) {
169 fprintf(stderr
, "%d: error opening lxc_container %s\n", __LINE__
, MYNAME
);
173 if (!c
->createl(c
, "ubuntu", "-r", "lucid", NULL
)) {
174 fprintf(stderr
, "%d: failed to create a lucid container\n", __LINE__
);
179 lxc_container_put(c
);
181 /* XXX TODO load_config needs to clear out any old config first */
182 if ((c
= lxc_container_new(MYNAME
)) == NULL
) {
183 fprintf(stderr
, "%d: error opening lxc_container %s\n", __LINE__
, MYNAME
);
188 ret
= c
->get_config_item(c
, "lxc.cap.drop", NULL
, 300);
189 if (ret
< 5 || ret
> 255) {
190 fprintf(stderr
, "%d: get_config_item(lxc.cap.drop) with NULL returned %d\n", __LINE__
, ret
);
194 ret
= c
->get_config_item(c
, "lxc.cap.drop", v1
, 1);
195 if (ret
< 5 || ret
> 255) {
196 fprintf(stderr
, "%d: get_config_item(lxc.cap.drop) returned %d\n", __LINE__
, ret
);
200 ret
= c
->get_config_item(c
, "lxc.cap.drop", v2
, 255);
202 fprintf(stderr
, "%d: get_config_item(lxc.cap.drop) returned %d %s\n", __LINE__
, ret
, v2
);
206 printf("%d: get_config_item(lxc.cap.drop) returned %d %s\n", __LINE__
, ret
, v2
);
207 ret
= c
->get_config_item(c
, "lxc.network", v2
, 255);
209 fprintf(stderr
, "%d: get_config_item returned %d\n", __LINE__
, ret
);
213 printf("%d: get_config_item(lxc.network) returned %d %s\n", __LINE__
, ret
, v2
);
215 if (!c
->set_config_item(c
, "lxc.network.ipv4", "10.2.3.4")) {
216 fprintf(stderr
, "%d: failed to set ipv4\n", __LINE__
);
221 ret
= c
->get_config_item(c
, "lxc.network.0.ipv4", v2
, 255);
223 fprintf(stderr
, "%d: lxc.network.0.ipv4 returned %d\n", __LINE__
, ret
);
227 if (!c
->clear_config_item(c
, "lxc.network.0.ipv4")) {
228 fprintf(stderr
, "%d: failed clearing all ipv4 entries\n", __LINE__
);
232 ret
= c
->get_config_item(c
, "lxc.network.0.ipv4", v2
, 255);
234 fprintf(stderr
, "%d: after clearing ipv4 entries get_item(lxc.network.0.ipv4 returned %d\n", __LINE__
, ret
);
239 ret
= c
->get_config_item(c
, "lxc.network.0.link", v2
, 255);
241 fprintf(stderr
, "%d: get_config_item returned %d\n", __LINE__
, ret
);
245 printf("%d: get_config_item (link) returned %d %s\n", __LINE__
, ret
, v2
);
246 ret
= c
->get_config_item(c
, "lxc.network.0.name", v2
, 255);
248 fprintf(stderr
, "%d: get_config_item returned %d\n", __LINE__
, ret
);
252 printf("%d: get_config_item (name) returned %d %s\n", __LINE__
, ret
, v2
);
254 if (!c
->clear_config_item(c
, "lxc.network")) {
255 fprintf(stderr
, "%d: clear_config_item failed\n", __LINE__
);
259 ret
= c
->get_config_item(c
, "lxc.network", v2
, 255);
261 fprintf(stderr
, "%d: network was not actually cleared (get_network returned %d)\n", __LINE__
, ret
);
266 ret
= c
->get_config_item(c
, "lxc.cgroup", v3
, 2047);
268 fprintf(stderr
, "%d: get_config_item(cgroup.devices) returned %d\n", __LINE__
, ret
);
272 printf("%d: get_config_item (cgroup.devices) returned %d %s\n", __LINE__
, ret
, v3
);
274 ret
= c
->get_config_item(c
, "lxc.cgroup.devices.allow", v3
, 2047);
276 fprintf(stderr
, "%d: get_config_item(cgroup.devices.devices.allow) returned %d\n", __LINE__
, ret
);
280 printf("%d: get_config_item (cgroup.devices.devices.allow) returned %d %s\n", __LINE__
, ret
, v3
);
282 if (!c
->clear_config_item(c
, "lxc.cgroup")) {
283 fprintf(stderr
, "%d: failed clearing lxc.cgroup", __LINE__
);
287 if (!c
->clear_config_item(c
, "lxc.cap.drop")) {
288 fprintf(stderr
, "%d: failed clearing lxc.cap.drop", __LINE__
);
292 if (!c
->clear_config_item(c
, "lxc.mount.entries")) {
293 fprintf(stderr
, "%d: failed clearing lxc.mount.entries", __LINE__
);
297 if (!c
->clear_config_item(c
, "lxc.hook")) {
298 fprintf(stderr
, "%d: failed clearing lxc.hook", __LINE__
);
303 printf("All get_item tests passed\n");
306 lxc_container_put(c
);