]> git.proxmox.com Git - mirror_lxc.git/blob - src/tests/get_item.c
Merge the liblxc API work by Serge Hallyn.
[mirror_lxc.git] / src / tests / get_item.c
1 /* liblxcapi
2 *
3 * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
4 * Copyright © 2012 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 <sys/types.h>
25 #include <sys/wait.h>
26 #include <stdlib.h>
27 #include <errno.h>
28 #include <lxc/state.h>
29
30 #define MYNAME "lxctest1"
31
32 int main(int argc, char *argv[])
33 {
34 struct lxc_container *c;
35 int ret;
36 char v1[2], v2[256], v3[2048];
37
38 if ((c = lxc_container_new("testxyz")) == NULL) {
39 fprintf(stderr, "%d: error opening lxc_container %s\n", __LINE__, MYNAME);
40 ret = 1;
41 goto out;
42 }
43
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__);
46 ret = 1;
47 goto out;
48 }
49 ret = c->get_config_item(c, "lxc.hook.pre-start", v2, 255);
50 if (ret < 0) {
51 fprintf(stderr, "%d: get_config_item(lxc.hook.pre-start) returned %d\n", __LINE__, ret);
52 ret = 1;
53 goto out;
54 }
55 fprintf(stderr, "lxc.hook.pre-start returned %d %s\n", ret, v2);
56
57 ret = c->get_config_item(c, "lxc.network", v2, 255);
58 if (ret < 0) {
59 fprintf(stderr, "%d: get_config_item returned %d\n", __LINE__, ret);
60 ret = 1;
61 goto out;
62 }
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__);
66 ret = 1;
67 goto out;
68 }
69 ret = c->get_config_item(c, "lxc.tty", v2, 255);
70 if (ret < 0) {
71 fprintf(stderr, "%d: get_config_item(lxc.tty) returned %d\n", __LINE__, ret);
72 ret = 1;
73 goto out;
74 }
75 fprintf(stderr, "lxc.tty returned %d %s\n", ret, v2);
76
77 if (!c->set_config_item(c, "lxc.arch", "x86")) {
78 fprintf(stderr, "%d: failed to set arch\n", __LINE__);
79 ret = 1;
80 goto out;
81 }
82 ret = c->get_config_item(c, "lxc.arch", v2, 255);
83 if (ret < 0) {
84 fprintf(stderr, "%d: get_config_item(lxc.arch) returned %d\n", __LINE__, ret);
85 ret = 1;
86 goto out;
87 }
88 printf("lxc.arch returned %d %s\n", ret, v2);
89
90 #define HNAME "hostname1"
91 // demonstrate proper usage:
92 char *alloced;
93 if (!c->set_config_item(c, "lxc.utsname", HNAME)) {
94 fprintf(stderr, "%d: failed to set utsname\n", __LINE__);
95 ret = 1;
96 goto out;
97 }
98
99 int len;
100 len = c->get_config_item(c, "lxc.utsname", NULL, 0); // query the size of the string
101 if (len < 0) {
102 fprintf(stderr, "%d: get_config_item(lxc.utsname) returned %d\n", __LINE__, len);
103 ret = 1;
104 goto out;
105 }
106 printf("lxc.utsname returned %d\n", len);
107
108 // allocate the length of string + 1 for trailing \0
109 alloced = malloc(len+1);
110 if (!alloced) {
111 fprintf(stderr, "%d: failed to allocate %d bytes for utsname\n", __LINE__, len);
112 ret = 1;
113 goto out;
114 }
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);
118 if (ret < 0) {
119 fprintf(stderr, "%d: get_config_item(lxc.utsname) returned %d\n", __LINE__, ret);
120 ret = 1;
121 goto out;
122 }
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);
125 ret = 1;
126 goto out;
127 }
128 printf("lxc.utsname returned %d %s\n", len, alloced);
129 free(alloced);
130
131 if (!c->set_config_item(c, "lxc.mount.entry", "hi there")) {
132 fprintf(stderr, "%d: failed to set mount.entry\n", __LINE__);
133 ret = 1;
134 goto out;
135 }
136 ret = c->get_config_item(c, "lxc.mount.entry", v2, 255);
137 if (ret < 0) {
138 fprintf(stderr, "%d: get_config_item(lxc.mount.entry) returned %d\n", __LINE__, ret);
139 ret = 1;
140 goto out;
141 }
142 printf("lxc.mount.entry returned %d %s\n", ret, v2);
143
144 if (!c->set_config_item(c, "lxc.aa_profile", "unconfined")) {
145 fprintf(stderr, "%d: failed to set aa_profile\n", __LINE__);
146 ret = 1;
147 goto out;
148 }
149 ret = c->get_config_item(c, "lxc.aa_profile", v2, 255);
150 if (ret < 0) {
151 fprintf(stderr, "%d: get_config_item(lxc.aa_profile) returned %d\n", __LINE__, ret);
152 ret = 1;
153 goto out;
154 }
155 printf("lxc.aa_profile returned %d %s\n", ret, v2);
156
157 lxc_container_put(c);
158
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);
162 ret = 1;
163 goto out;
164 }
165 c->destroy(c);
166 lxc_container_put(c);
167
168 if ((c = lxc_container_new(MYNAME)) == NULL) {
169 fprintf(stderr, "%d: error opening lxc_container %s\n", __LINE__, MYNAME);
170 ret = 1;
171 goto out;
172 }
173 if (!c->createl(c, "ubuntu", "-r", "lucid", NULL)) {
174 fprintf(stderr, "%d: failed to create a lucid container\n", __LINE__);
175 ret = 1;
176 goto out;
177 }
178
179 lxc_container_put(c);
180
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);
184 ret = 1;
185 goto out;
186 }
187
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);
191 ret = 1;
192 goto out;
193 }
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);
197 ret = 1;
198 goto out;
199 }
200 ret = c->get_config_item(c, "lxc.cap.drop", v2, 255);
201 if (ret < 0) {
202 fprintf(stderr, "%d: get_config_item(lxc.cap.drop) returned %d %s\n", __LINE__, ret, v2);
203 ret = 1;
204 goto out;
205 }
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);
208 if (ret < 0) {
209 fprintf(stderr, "%d: get_config_item returned %d\n", __LINE__, ret);
210 ret = 1;
211 goto out;
212 }
213 printf("%d: get_config_item(lxc.network) returned %d %s\n", __LINE__, ret, v2);
214
215 if (!c->set_config_item(c, "lxc.network.ipv4", "10.2.3.4")) {
216 fprintf(stderr, "%d: failed to set ipv4\n", __LINE__);
217 ret = 1;
218 goto out;
219 }
220
221 ret = c->get_config_item(c, "lxc.network.0.ipv4", v2, 255);
222 if (ret <= 0) {
223 fprintf(stderr, "%d: lxc.network.0.ipv4 returned %d\n", __LINE__, ret);
224 ret = 1;
225 goto out;
226 }
227 if (!c->clear_config_item(c, "lxc.network.0.ipv4")) {
228 fprintf(stderr, "%d: failed clearing all ipv4 entries\n", __LINE__);
229 ret = 1;
230 goto out;
231 }
232 ret = c->get_config_item(c, "lxc.network.0.ipv4", v2, 255);
233 if (ret != 0) {
234 fprintf(stderr, "%d: after clearing ipv4 entries get_item(lxc.network.0.ipv4 returned %d\n", __LINE__, ret);
235 ret = 1;
236 goto out;
237 }
238
239 ret = c->get_config_item(c, "lxc.network.0.link", v2, 255);
240 if (ret < 0) {
241 fprintf(stderr, "%d: get_config_item returned %d\n", __LINE__, ret);
242 ret = 1;
243 goto out;
244 }
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);
247 if (ret < 0) {
248 fprintf(stderr, "%d: get_config_item returned %d\n", __LINE__, ret);
249 ret = 1;
250 goto out;
251 }
252 printf("%d: get_config_item (name) returned %d %s\n", __LINE__, ret, v2);
253
254 if (!c->clear_config_item(c, "lxc.network")) {
255 fprintf(stderr, "%d: clear_config_item failed\n", __LINE__);
256 ret = 1;
257 goto out;
258 }
259 ret = c->get_config_item(c, "lxc.network", v2, 255);
260 if (ret != 0) {
261 fprintf(stderr, "%d: network was not actually cleared (get_network returned %d)\n", __LINE__, ret);
262 ret = 1;
263 goto out;
264 }
265
266 ret = c->get_config_item(c, "lxc.cgroup", v3, 2047);
267 if (ret < 0) {
268 fprintf(stderr, "%d: get_config_item(cgroup.devices) returned %d\n", __LINE__, ret);
269 ret = 1;
270 goto out;
271 }
272 printf("%d: get_config_item (cgroup.devices) returned %d %s\n", __LINE__, ret, v3);
273
274 ret = c->get_config_item(c, "lxc.cgroup.devices.allow", v3, 2047);
275 if (ret < 0) {
276 fprintf(stderr, "%d: get_config_item(cgroup.devices.devices.allow) returned %d\n", __LINE__, ret);
277 ret = 1;
278 goto out;
279 }
280 printf("%d: get_config_item (cgroup.devices.devices.allow) returned %d %s\n", __LINE__, ret, v3);
281
282 if (!c->clear_config_item(c, "lxc.cgroup")) {
283 fprintf(stderr, "%d: failed clearing lxc.cgroup", __LINE__);
284 ret = 1;
285 goto out;
286 }
287 if (!c->clear_config_item(c, "lxc.cap.drop")) {
288 fprintf(stderr, "%d: failed clearing lxc.cap.drop", __LINE__);
289 ret = 1;
290 goto out;
291 }
292 if (!c->clear_config_item(c, "lxc.mount.entries")) {
293 fprintf(stderr, "%d: failed clearing lxc.mount.entries", __LINE__);
294 ret = 1;
295 goto out;
296 }
297 if (!c->clear_config_item(c, "lxc.hook")) {
298 fprintf(stderr, "%d: failed clearing lxc.hook", __LINE__);
299 ret = 1;
300 goto out;
301 }
302 c->destroy(c);
303 printf("All get_item tests passed\n");
304 ret = 0;
305 out:
306 lxc_container_put(c);
307 exit(ret);
308 };