]>
Commit | Line | Data |
---|---|---|
72d0e1cb SG |
1 | |
2 | /* liblxcapi | |
3 | * | |
4 | * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>. | |
5 | * Copyright © 2012 Canonical Ltd. | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License version 2, as | |
9 | * published by the Free Software Foundation. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License along | |
17 | * with this program; if not, write to the Free Software Foundation, Inc., | |
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
19 | */ | |
948955a2 | 20 | #include <lxc/lxccontainer.h> |
72d0e1cb SG |
21 | |
22 | #include <unistd.h> | |
23 | #include <signal.h> | |
24 | #include <stdio.h> | |
25 | #include <sys/types.h> | |
26 | #include <sys/wait.h> | |
27 | #include <stdlib.h> | |
28 | #include <errno.h> | |
29 | ||
30 | #define MYNAME "lxctest1" | |
31 | ||
32 | int main(int argc, char *argv[]) | |
33 | { | |
34 | struct lxc_container *c; | |
35 | int ret = 1; | |
36 | ||
afeecbba | 37 | if ((c = lxc_container_new(MYNAME, NULL)) == NULL) { |
72d0e1cb SG |
38 | fprintf(stderr, "%d: error opening lxc_container %s\n", __LINE__, MYNAME); |
39 | ret = 1; | |
40 | goto out; | |
41 | } | |
42 | ||
43 | if (c->is_defined(c)) { | |
44 | fprintf(stderr, "%d: %s thought it was defined\n", __LINE__, MYNAME); | |
45 | goto out; | |
46 | } | |
47 | ||
7fa3f2e9 | 48 | if (!c->set_config_item(c, "lxc.net.0.type", "veth")) { |
72d0e1cb SG |
49 | fprintf(stderr, "%d: failed to set network type\n", __LINE__); |
50 | goto out; | |
51 | } | |
70b12b4a | 52 | |
7fa3f2e9 | 53 | c->set_config_item(c, "lxc.net.0.link", "lxcbr0"); |
54 | c->set_config_item(c, "lxc.net.0.flags", "up"); | |
70b12b4a | 55 | |
787c3ebe SH |
56 | if (!c->createl(c, "busybox", NULL, NULL, 0, NULL)) { |
57 | fprintf(stderr, "%d: failed to create a container\n", __LINE__); | |
72d0e1cb SG |
58 | goto out; |
59 | } | |
60 | ||
61 | if (!c->is_defined(c)) { | |
62 | fprintf(stderr, "%d: %s thought it was not defined\n", __LINE__, MYNAME); | |
63 | goto out; | |
64 | } | |
65 | ||
cc3afb1e | 66 | c->clear_config(c); |
72d0e1cb | 67 | c->load_config(c, NULL); |
540f932a | 68 | c->want_daemonize(c, true); |
70b12b4a | 69 | |
72d0e1cb SG |
70 | if (!c->startl(c, 0, NULL)) { |
71 | fprintf(stderr, "%d: failed to start %s\n", __LINE__, MYNAME); | |
72 | goto out; | |
73 | } | |
72d0e1cb | 74 | |
0464b881 | 75 | /* Wait for init to be ready for SIGPWR */ |
cf0c72d3 | 76 | sleep(20); |
0464b881 | 77 | |
cf0c72d3 | 78 | if (!c->shutdown(c, 120)) { |
72d0e1cb | 79 | fprintf(stderr, "%d: failed to shut down %s\n", __LINE__, MYNAME); |
70b12b4a | 80 | if (!c->stop(c)) |
cf0c72d3 | 81 | fprintf(stderr, "%d: failed to kill %s\n", __LINE__, MYNAME); |
70b12b4a | 82 | |
72d0e1cb SG |
83 | goto out; |
84 | } | |
85 | ||
86 | if (!c->destroy(c)) { | |
87 | fprintf(stderr, "%d: error deleting %s\n", __LINE__, MYNAME); | |
88 | goto out; | |
89 | } | |
90 | ||
91 | if (c->is_defined(c)) { | |
92 | fprintf(stderr, "%d: %s thought it was defined\n", __LINE__, MYNAME); | |
93 | goto out; | |
94 | } | |
95 | ||
96 | fprintf(stderr, "all lxc_container tests passed for %s\n", c->name); | |
97 | ret = 0; | |
70b12b4a | 98 | |
72d0e1cb | 99 | out: |
70b12b4a | 100 | if (c && c->is_defined(c)) |
cf0c72d3 | 101 | c->destroy(c); |
cf0c72d3 | 102 | |
72d0e1cb SG |
103 | lxc_container_put(c); |
104 | exit(ret); | |
105 | } |