]> git.proxmox.com Git - mirror_lxc.git/blob - src/tests/startone.c
tests: include config.h
[mirror_lxc.git] / src / tests / startone.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
20 #include "config.h"
21
22 #include <lxc/lxccontainer.h>
23
24 #include <unistd.h>
25 #include <signal.h>
26 #include <stdio.h>
27 #include <sys/types.h>
28 #include <sys/wait.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <fcntl.h>
33
34 #define MYNAME "lxctest1"
35
36 static int destroy_container(void)
37 {
38 int status, ret;
39 pid_t pid = fork();
40
41 if (pid < 0) {
42 perror("fork");
43 return -1;
44 }
45
46 if (pid == 0) {
47 execlp("lxc-destroy", "lxc-destroy", "-f", "-n", MYNAME, NULL);
48 exit(EXIT_FAILURE);
49 }
50
51 again:
52 ret = waitpid(pid, &status, 0);
53 if (ret == -1) {
54 if (errno == EINTR)
55 goto again;
56
57 perror("waitpid");
58 return -1;
59 }
60
61 if (ret != pid)
62 goto again;
63
64 if (!WIFEXITED(status)) { // did not exit normally
65 fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__);
66 return -1;
67 }
68
69 return WEXITSTATUS(status);
70 }
71
72 static int create_container(void)
73 {
74 int status, ret;
75 pid_t pid = fork();
76
77 if (pid < 0) {
78 perror("fork");
79 return -1;
80 }
81
82 if (pid == 0) {
83 execlp("lxc-create", "lxc-create", "-t", "busybox", "-n", MYNAME, NULL);
84 exit(EXIT_FAILURE);
85 }
86
87 again:
88 ret = waitpid(pid, &status, 0);
89 if (ret == -1) {
90 if (errno == EINTR)
91 goto again;
92
93 perror("waitpid");
94 return -1;
95 }
96
97 if (ret != pid)
98 goto again;
99
100 if (!WIFEXITED(status)) { // did not exit normally
101 fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__);
102 return -1;
103 }
104
105 return WEXITSTATUS(status);
106 }
107
108 int main(int argc, char *argv[])
109 {
110 struct lxc_container *c;
111 int ret = 0;
112 const char *s;
113 bool b;
114 char buf[201];
115 int len;
116
117 ret = 1;
118
119 /* test a real container */
120 c = lxc_container_new(MYNAME, NULL);
121 if (!c) {
122 fprintf(stderr, "%d: error creating lxc_container %s\n", __LINE__, MYNAME);
123 ret = 1;
124 goto out;
125 }
126
127 if (c->is_defined(c)) {
128 fprintf(stderr, "%d: %s thought it was defined\n", __LINE__, MYNAME);
129 goto out;
130 }
131
132 ret = create_container();
133 if (ret) {
134 fprintf(stderr, "%d: failed to create a container\n", __LINE__);
135 goto out;
136 }
137
138 b = c->is_defined(c);
139 if (!b) {
140 fprintf(stderr, "%d: %s thought it was not defined\n", __LINE__, MYNAME);
141 goto out;
142 }
143
144 len = c->get_cgroup_item(c, "cpuset.cpus", buf, 200);
145 if (len >= 0) {
146 fprintf(stderr, "%d: %s not running but had cgroup settings\n", __LINE__, MYNAME);
147 goto out;
148 }
149
150 sprintf(buf, "0");
151 b = c->set_cgroup_item(c, "cpuset.cpus", buf);
152 if (b) {
153 fprintf(stderr, "%d: %s not running but could set cgroup settings\n", __LINE__, MYNAME);
154 goto out;
155 }
156
157 s = c->state(c);
158 if (!s || strcmp(s, "STOPPED")) {
159 fprintf(stderr, "%d: %s is in state %s, not in STOPPED.\n", __LINE__, c->name, s ? s : "undefined");
160 goto out;
161 }
162
163 b = c->load_config(c, NULL);
164 if (!b) {
165 fprintf(stderr, "%d: %s failed to read its config\n", __LINE__, c->name);
166 goto out;
167 }
168
169 if (!c->set_config_item(c, "lxc.uts.name", "bobo")) {
170 fprintf(stderr, "%d: failed setting lxc.uts.name\n", __LINE__);
171 goto out;
172 }
173
174 if (!lxc_container_get(c)) {
175 fprintf(stderr, "%d: failed to get extra ref to container\n", __LINE__);
176 exit(EXIT_FAILURE);
177 }
178
179 c->want_daemonize(c, true);
180 if (!c->startl(c, 0, NULL)) {
181 fprintf(stderr, "%d: %s failed to start\n", __LINE__, c->name);
182 exit(EXIT_FAILURE);
183 }
184
185 sleep(3);
186
187 s = c->state(c);
188 if (!s || strcmp(s, "RUNNING")) {
189 fprintf(stderr, "%d: %s is in state %s, not in RUNNING.\n", __LINE__, c->name, s ? s : "undefined");
190 goto out;
191 }
192
193 len = c->get_cgroup_item(c, "cpuset.cpus", buf, 0);
194 if (len <= 0) {
195 fprintf(stderr, "%d: not able to get length of cpuset.cpus (ret %d)\n", __LINE__, len);
196 goto out;
197 }
198
199 len = c->get_cgroup_item(c, "cpuset.cpus", buf, 200);
200 if (len <= 0 || strncmp(buf, "0", 1)) {
201 fprintf(stderr, "%d: not able to get cpuset.cpus (len %d buf %s)\n", __LINE__, len, buf);
202 goto out;
203 }
204
205 sprintf(buf, "FROZEN");
206 b = c->set_cgroup_item(c, "freezer.state", buf);
207 if (!b) {
208 fprintf(stderr, "%d: not able to set freezer.state.\n", __LINE__);
209 goto out;
210 }
211
212 sprintf(buf, "XXX");
213 len = c->get_cgroup_item(c, "freezer.state", buf, 200);
214 if (len <= 0 || (strcmp(buf, "FREEZING\n") && strcmp(buf, "FROZEN\n"))) {
215 fprintf(stderr, "%d: not able to get freezer.state (len %d buf %s)\n", __LINE__, len, buf);
216 goto out;
217 }
218
219 c->set_cgroup_item(c, "freezer.state", "THAWED");
220
221 c->stop(c);
222
223 /* feh - multilib has moved the lxc-init crap */
224 #if 0
225 goto ok;
226
227 ret = system("mkdir -p " LXCPATH "/lxctest1/rootfs//usr/local/libexec/lxc");
228 if (!ret)
229 ret = system("mkdir -p " LXCPATH "/lxctest1/rootfs/usr/lib/lxc/");
230 if (!ret)
231 ret = system("cp src/lxc/lxc-init " LXCPATH "/lxctest1/rootfs//usr/local/libexec/lxc");
232 if (!ret)
233 ret = system("cp src/lxc/liblxc.so " LXCPATH "/lxctest1/rootfs/usr/lib/lxc");
234 if (!ret)
235 ret = system("cp src/lxc/liblxc.so " LXCPATH "/lxctest1/rootfs/usr/lib/lxc/liblxc.so.0");
236 if (!ret)
237 ret = system("cp src/lxc/liblxc.so " LXCPATH "/lxctest1/rootfs/usr/lib");
238 if (!ret)
239 ret = system("mkdir -p " LXCPATH "/lxctest1/rootfs/dev/shm");
240 if (!ret)
241 ret = system("chroot " LXCPATH "/lxctest1/rootfs apt-get install --no-install-recommends lxc");
242 if (ret) {
243 fprintf(stderr, "%d: failed to installing lxc-init in container\n", __LINE__);
244 goto out;
245 }
246 // next write out the config file; does it match?
247 if (!c->startl(c, 1, "/bin/hostname", NULL)) {
248 fprintf(stderr, "%d: failed to lxc-execute /bin/hostname\n", __LINE__);
249 goto out;
250 }
251 // auto-check result? ('bobo' is printed on stdout)
252
253 ok:
254 #endif
255 fprintf(stderr, "all lxc_container tests passed for %s\n", c->name);
256 ret = 0;
257
258 out:
259 if (c) {
260 c->stop(c);
261 destroy_container();
262 }
263 lxc_container_put(c);
264 exit(ret);
265 }