]> git.proxmox.com Git - mirror_lxc.git/blame - src/tests/get_item.c
make failure to connect to cgmanager DEBUG instead of ERROR
[mirror_lxc.git] / src / tests / get_item.c
CommitLineData
72d0e1cb
SG
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 */
948955a2 19#include <lxc/lxccontainer.h>
72d0e1cb
SG
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>
95ee490b 28#include <string.h>
f2363e38 29#include "lxc/state.h"
72d0e1cb
SG
30
31#define MYNAME "lxctest1"
32
33int main(int argc, char *argv[])
34{
35 struct lxc_container *c;
36 int ret;
37 char v1[2], v2[256], v3[2048];
38
afeecbba 39 if ((c = lxc_container_new("testxyz", NULL)) == NULL) {
72d0e1cb
SG
40 fprintf(stderr, "%d: error opening lxc_container %s\n", __LINE__, MYNAME);
41 ret = 1;
42 goto out;
43 }
44
45 if (!c->set_config_item(c, "lxc.hook.pre-start", "hi there")) {
46 fprintf(stderr, "%d: failed to set hook.pre-start\n", __LINE__);
47 ret = 1;
48 goto out;
49 }
50 ret = c->get_config_item(c, "lxc.hook.pre-start", v2, 255);
51 if (ret < 0) {
52 fprintf(stderr, "%d: get_config_item(lxc.hook.pre-start) returned %d\n", __LINE__, ret);
53 ret = 1;
54 goto out;
55 }
56 fprintf(stderr, "lxc.hook.pre-start returned %d %s\n", ret, v2);
57
58 ret = c->get_config_item(c, "lxc.network", v2, 255);
59 if (ret < 0) {
60 fprintf(stderr, "%d: get_config_item returned %d\n", __LINE__, ret);
61 ret = 1;
62 goto out;
63 }
64 fprintf(stderr, "%d: get_config_item(lxc.network) returned %d %s\n", __LINE__, ret, v2);
65 if (!c->set_config_item(c, "lxc.tty", "4")) {
66 fprintf(stderr, "%d: failed to set tty\n", __LINE__);
67 ret = 1;
68 goto out;
69 }
70 ret = c->get_config_item(c, "lxc.tty", v2, 255);
71 if (ret < 0) {
72 fprintf(stderr, "%d: get_config_item(lxc.tty) returned %d\n", __LINE__, ret);
73 ret = 1;
74 goto out;
75 }
76 fprintf(stderr, "lxc.tty returned %d %s\n", ret, v2);
77
78 if (!c->set_config_item(c, "lxc.arch", "x86")) {
79 fprintf(stderr, "%d: failed to set arch\n", __LINE__);
80 ret = 1;
81 goto out;
82 }
83 ret = c->get_config_item(c, "lxc.arch", v2, 255);
84 if (ret < 0) {
85 fprintf(stderr, "%d: get_config_item(lxc.arch) returned %d\n", __LINE__, ret);
86 ret = 1;
87 goto out;
88 }
89 printf("lxc.arch returned %d %s\n", ret, v2);
90
91#define HNAME "hostname1"
92 // demonstrate proper usage:
93 char *alloced;
94 if (!c->set_config_item(c, "lxc.utsname", HNAME)) {
95 fprintf(stderr, "%d: failed to set utsname\n", __LINE__);
96 ret = 1;
97 goto out;
98 }
99
100 int len;
101 len = c->get_config_item(c, "lxc.utsname", NULL, 0); // query the size of the string
102 if (len < 0) {
103 fprintf(stderr, "%d: get_config_item(lxc.utsname) returned %d\n", __LINE__, len);
104 ret = 1;
105 goto out;
106 }
107 printf("lxc.utsname returned %d\n", len);
108
109 // allocate the length of string + 1 for trailing \0
110 alloced = malloc(len+1);
111 if (!alloced) {
112 fprintf(stderr, "%d: failed to allocate %d bytes for utsname\n", __LINE__, len);
113 ret = 1;
114 goto out;
115 }
116 // now pass in the malloc'd array, and pass in length of string + 1: again
117 // because we need room for the trailing \0
118 ret = c->get_config_item(c, "lxc.utsname", alloced, len+1);
119 if (ret < 0) {
120 fprintf(stderr, "%d: get_config_item(lxc.utsname) returned %d\n", __LINE__, ret);
121 ret = 1;
122 goto out;
123 }
124 if (strcmp(alloced, HNAME) != 0 || ret != len) {
125 fprintf(stderr, "lxc.utsname returned wrong value: %d %s not %d %s\n", ret, alloced, len, HNAME);
126 ret = 1;
127 goto out;
128 }
129 printf("lxc.utsname returned %d %s\n", len, alloced);
130 free(alloced);
131
132 if (!c->set_config_item(c, "lxc.mount.entry", "hi there")) {
133 fprintf(stderr, "%d: failed to set mount.entry\n", __LINE__);
134 ret = 1;
135 goto out;
136 }
137 ret = c->get_config_item(c, "lxc.mount.entry", v2, 255);
138 if (ret < 0) {
139 fprintf(stderr, "%d: get_config_item(lxc.mount.entry) returned %d\n", __LINE__, ret);
140 ret = 1;
141 goto out;
142 }
143 printf("lxc.mount.entry returned %d %s\n", ret, v2);
144
145 if (!c->set_config_item(c, "lxc.aa_profile", "unconfined")) {
146 fprintf(stderr, "%d: failed to set aa_profile\n", __LINE__);
147 ret = 1;
148 goto out;
149 }
150 ret = c->get_config_item(c, "lxc.aa_profile", v2, 255);
151 if (ret < 0) {
152 fprintf(stderr, "%d: get_config_item(lxc.aa_profile) returned %d\n", __LINE__, ret);
153 ret = 1;
154 goto out;
155 }
156 printf("lxc.aa_profile returned %d %s\n", ret, v2);
157
158 lxc_container_put(c);
159
160 // new test with real container
afeecbba 161 if ((c = lxc_container_new(MYNAME, NULL)) == NULL) {
72d0e1cb
SG
162 fprintf(stderr, "%d: error opening lxc_container %s\n", __LINE__, MYNAME);
163 ret = 1;
164 goto out;
165 }
166 c->destroy(c);
167 lxc_container_put(c);
168
afeecbba 169 if ((c = lxc_container_new(MYNAME, NULL)) == NULL) {
72d0e1cb
SG
170 fprintf(stderr, "%d: error opening lxc_container %s\n", __LINE__, MYNAME);
171 ret = 1;
172 goto out;
173 }
e403a064
SG
174 if (!c->createl(c, "ubuntu", NULL, NULL, 0, "-r", "trusty", NULL)) {
175 fprintf(stderr, "%d: failed to create a trusty container\n", __LINE__);
72d0e1cb
SG
176 ret = 1;
177 goto out;
178 }
179
180 lxc_container_put(c);
181
182 /* XXX TODO load_config needs to clear out any old config first */
afeecbba 183 if ((c = lxc_container_new(MYNAME, NULL)) == NULL) {
72d0e1cb
SG
184 fprintf(stderr, "%d: error opening lxc_container %s\n", __LINE__, MYNAME);
185 ret = 1;
186 goto out;
187 }
188
189 ret = c->get_config_item(c, "lxc.cap.drop", NULL, 300);
190 if (ret < 5 || ret > 255) {
191 fprintf(stderr, "%d: get_config_item(lxc.cap.drop) with NULL returned %d\n", __LINE__, ret);
192 ret = 1;
193 goto out;
194 }
195 ret = c->get_config_item(c, "lxc.cap.drop", v1, 1);
196 if (ret < 5 || ret > 255) {
197 fprintf(stderr, "%d: get_config_item(lxc.cap.drop) returned %d\n", __LINE__, ret);
198 ret = 1;
199 goto out;
200 }
201 ret = c->get_config_item(c, "lxc.cap.drop", v2, 255);
202 if (ret < 0) {
203 fprintf(stderr, "%d: get_config_item(lxc.cap.drop) returned %d %s\n", __LINE__, ret, v2);
204 ret = 1;
205 goto out;
206 }
207 printf("%d: get_config_item(lxc.cap.drop) returned %d %s\n", __LINE__, ret, v2);
208 ret = c->get_config_item(c, "lxc.network", v2, 255);
209 if (ret < 0) {
210 fprintf(stderr, "%d: get_config_item returned %d\n", __LINE__, ret);
211 ret = 1;
212 goto out;
213 }
214 printf("%d: get_config_item(lxc.network) returned %d %s\n", __LINE__, ret, v2);
215
216 if (!c->set_config_item(c, "lxc.network.ipv4", "10.2.3.4")) {
217 fprintf(stderr, "%d: failed to set ipv4\n", __LINE__);
218 ret = 1;
219 goto out;
220 }
221
222 ret = c->get_config_item(c, "lxc.network.0.ipv4", v2, 255);
223 if (ret <= 0) {
224 fprintf(stderr, "%d: lxc.network.0.ipv4 returned %d\n", __LINE__, ret);
225 ret = 1;
226 goto out;
227 }
228 if (!c->clear_config_item(c, "lxc.network.0.ipv4")) {
229 fprintf(stderr, "%d: failed clearing all ipv4 entries\n", __LINE__);
230 ret = 1;
231 goto out;
232 }
233 ret = c->get_config_item(c, "lxc.network.0.ipv4", v2, 255);
234 if (ret != 0) {
235 fprintf(stderr, "%d: after clearing ipv4 entries get_item(lxc.network.0.ipv4 returned %d\n", __LINE__, ret);
236 ret = 1;
237 goto out;
238 }
239
240 ret = c->get_config_item(c, "lxc.network.0.link", v2, 255);
241 if (ret < 0) {
242 fprintf(stderr, "%d: get_config_item returned %d\n", __LINE__, ret);
243 ret = 1;
244 goto out;
245 }
246 printf("%d: get_config_item (link) returned %d %s\n", __LINE__, ret, v2);
247 ret = c->get_config_item(c, "lxc.network.0.name", v2, 255);
248 if (ret < 0) {
249 fprintf(stderr, "%d: get_config_item returned %d\n", __LINE__, ret);
250 ret = 1;
251 goto out;
252 }
253 printf("%d: get_config_item (name) returned %d %s\n", __LINE__, ret, v2);
254
255 if (!c->clear_config_item(c, "lxc.network")) {
256 fprintf(stderr, "%d: clear_config_item failed\n", __LINE__);
257 ret = 1;
258 goto out;
259 }
260 ret = c->get_config_item(c, "lxc.network", v2, 255);
261 if (ret != 0) {
262 fprintf(stderr, "%d: network was not actually cleared (get_network returned %d)\n", __LINE__, ret);
263 ret = 1;
264 goto out;
265 }
266
267 ret = c->get_config_item(c, "lxc.cgroup", v3, 2047);
268 if (ret < 0) {
269 fprintf(stderr, "%d: get_config_item(cgroup.devices) returned %d\n", __LINE__, ret);
270 ret = 1;
271 goto out;
272 }
273 printf("%d: get_config_item (cgroup.devices) returned %d %s\n", __LINE__, ret, v3);
274
275 ret = c->get_config_item(c, "lxc.cgroup.devices.allow", v3, 2047);
276 if (ret < 0) {
277 fprintf(stderr, "%d: get_config_item(cgroup.devices.devices.allow) returned %d\n", __LINE__, ret);
278 ret = 1;
279 goto out;
280 }
281 printf("%d: get_config_item (cgroup.devices.devices.allow) returned %d %s\n", __LINE__, ret, v3);
282
283 if (!c->clear_config_item(c, "lxc.cgroup")) {
03fadd16 284 fprintf(stderr, "%d: failed clearing lxc.cgroup\n", __LINE__);
72d0e1cb
SG
285 ret = 1;
286 goto out;
287 }
288 if (!c->clear_config_item(c, "lxc.cap.drop")) {
03fadd16 289 fprintf(stderr, "%d: failed clearing lxc.cap.drop\n", __LINE__);
72d0e1cb
SG
290 ret = 1;
291 goto out;
292 }
293 if (!c->clear_config_item(c, "lxc.mount.entries")) {
03fadd16 294 fprintf(stderr, "%d: failed clearing lxc.mount.entries\n", __LINE__);
72d0e1cb
SG
295 ret = 1;
296 goto out;
297 }
298 if (!c->clear_config_item(c, "lxc.hook")) {
03fadd16 299 fprintf(stderr, "%d: failed clearing lxc.hook\n", __LINE__);
72d0e1cb
SG
300 ret = 1;
301 goto out;
302 }
303 c->destroy(c);
304 printf("All get_item tests passed\n");
305 ret = 0;
306out:
307 lxc_container_put(c);
308 exit(ret);
309};