]> git.proxmox.com Git - mirror_lxc.git/blob - src/tests/destroytest.c
eaf3c84d07c06e7eae527cc9081be8d1f1a97946
[mirror_lxc.git] / src / tests / destroytest.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
29 #define MYNAME "lxctest1"
30
31 static int create_container(void)
32 {
33 int status, ret;
34 pid_t pid = fork();
35
36 if (pid < 0) {
37 perror("fork");
38 return -1;
39 }
40 if (pid == 0) {
41 ret = execlp("lxc-create", "lxc-create", "-t", "busybox", "-n", MYNAME, NULL);
42 // Should not return
43 perror("execl");
44 exit(1);
45 }
46 again:
47 ret = waitpid(pid, &status, 0);
48 if (ret == -1) {
49 if (errno == EINTR)
50 goto again;
51 perror("waitpid");
52 return -1;
53 }
54 if (ret != pid)
55 goto again;
56 if (!WIFEXITED(status)) { // did not exit normally
57 fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__);
58 return -1;
59 }
60 return WEXITSTATUS(status);
61 }
62
63 int main(int argc, char *argv[])
64 {
65 struct lxc_container *c;
66 int ret = 1;
67
68 if ((c = lxc_container_new(MYNAME, NULL)) == NULL) {
69 fprintf(stderr, "%d: error opening lxc_container %s\n", __LINE__, MYNAME);
70 ret = 1;
71 goto out;
72 }
73
74 if (c->is_defined(c)) {
75 fprintf(stderr, "%d: %s thought it was defined\n", __LINE__, MYNAME);
76 goto out;
77 }
78
79 if (create_container()) {
80 fprintf(stderr, "%d: failed to create a container\n", __LINE__);
81 goto out;
82 }
83
84 if (!c->is_defined(c)) {
85 fprintf(stderr, "%d: %s thought it was not defined\n", __LINE__, MYNAME);
86 goto out;
87 }
88
89 if (!c->destroy(c)) {
90 fprintf(stderr, "%d: error deleting %s\n", __LINE__, MYNAME);
91 goto out;
92 }
93
94 if (c->is_defined(c)) {
95 fprintf(stderr, "%d: %s thought it was defined\n", __LINE__, MYNAME);
96 goto out;
97 }
98
99 fprintf(stderr, "all lxc_container tests passed for %s\n", c->name);
100 ret = 0;
101 out:
102 lxc_container_put(c);
103 exit(ret);
104 }