]> git.proxmox.com Git - mirror_lxc.git/blob - src/tests/clonetest.c
spelling: timeout
[mirror_lxc.git] / src / tests / clonetest.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 "clonetest1"
30 #define MYNAME2 "clonetest2"
31
32 int main(int argc, char *argv[])
33 {
34 struct lxc_container *c = NULL, *c2 = NULL, *c3 = NULL;
35 int ret = 1;
36
37 c = lxc_container_new(MYNAME, NULL);
38 c2 = lxc_container_new(MYNAME2, NULL);
39 if (c) {
40 c->destroy(c);
41 lxc_container_put(c);
42 c = NULL;
43 }
44
45 if (c2) {
46 c2->destroy(c2);
47 lxc_container_put(c2);
48 c2 = NULL;
49 }
50
51 if ((c = lxc_container_new(MYNAME, NULL)) == NULL) {
52 fprintf(stderr, "%d: error opening lxc_container %s\n", __LINE__, MYNAME);
53 ret = 1;
54 goto out;
55 }
56
57 c->save_config(c, NULL);
58
59 if (!c->createl(c, "busybox", NULL, NULL, 0, NULL)) {
60 fprintf(stderr, "%d: failed to create a container\n", __LINE__);
61 goto out;
62 }
63
64 c->load_config(c, NULL);
65
66 if (!c->is_defined(c)) {
67 fprintf(stderr, "%d: %s thought it was not defined\n", __LINE__, MYNAME);
68 goto out;
69 }
70
71 c2 = c->clone(c, MYNAME2, NULL, 0, NULL, NULL, 0, NULL);
72 if (!c2) {
73 fprintf(stderr, "%d: %s clone returned NULL\n", __LINE__, MYNAME2);
74 goto out;
75 }
76
77 if (!c2->is_defined(c2)) {
78 fprintf(stderr, "%d: %s not defined after clone\n", __LINE__, MYNAME2);
79 goto out;
80 }
81
82 fprintf(stderr, "directory backing store tests passed\n");
83
84 // now test with lvm
85 // Only do this if clonetestlvm1 exists - user has to set this up
86 // in advance
87 c2->destroy(c2);
88 lxc_container_put(c2);
89 c->destroy(c);
90 lxc_container_put(c);
91 c = NULL;
92
93 c2 = lxc_container_new("clonetestlvm2", NULL);
94 if (c2) {
95 if (c2->is_defined(c2))
96 c2->destroy(c2);
97 lxc_container_put(c2);
98 }
99
100 c2 = lxc_container_new("clonetest-o1", NULL);
101 if (c2) {
102 if (c2->is_defined(c2))
103 c2->destroy(c2);
104 lxc_container_put(c2);
105 }
106
107 c2 = lxc_container_new("clonetest-o2", NULL);
108 if (c2) {
109 if (c2->is_defined(c2))
110 c2->destroy(c2);
111 lxc_container_put(c2);
112 }
113 c2 = NULL;
114
115 // lvm-copied
116 c = lxc_container_new("clonetestlvm1", NULL);
117 if (!c) {
118 fprintf(stderr, "failed loading clonetestlvm1\n");
119 goto out;
120 }
121
122 if (!c->is_defined(c)) {
123 fprintf(stderr, "clonetestlvm1 does not exist, skipping lvm tests\n");
124 ret = 0;
125 goto out;
126 }
127
128 if ((c2 = c->clone(c, "clonetestlvm2", NULL, 0, NULL, NULL, 0, NULL)) == NULL) {
129 fprintf(stderr, "lvm clone failed\n");
130 goto out;
131 }
132
133 lxc_container_put(c2);
134
135 // lvm-snapshot
136 c2 = lxc_container_new("clonetestlvm3", NULL);
137 if (c2) {
138 if (c2->is_defined(c2))
139 c2->destroy(c2);
140 lxc_container_put(c2);
141 c2 = NULL;
142 }
143
144 if ((c2 = c->clone(c, "clonetestlvm3", NULL, LXC_CLONE_SNAPSHOT, NULL, NULL, 0, NULL)) == NULL) {
145 fprintf(stderr, "lvm clone failed\n");
146 goto out;
147 }
148
149 lxc_container_put(c2);
150 lxc_container_put(c);
151 c = c2 = NULL;
152
153 if ((c = lxc_container_new(MYNAME, NULL)) == NULL) {
154 fprintf(stderr, "error opening original container for overlay test\n");
155 goto out;
156 }
157
158 // Now create an overlayfs clone of a dir-backed container
159 if ((c2 = c->clone(c, "clonetest-o1", NULL, LXC_CLONE_SNAPSHOT, "overlayfs", NULL, 0, NULL)) == NULL) {
160 fprintf(stderr, "overlayfs clone of dir failed\n");
161 goto out;
162 }
163
164 // Now create an overlayfs clone of the overlayfs clone
165 if ((c3 = c2->clone(c2, "clonetest-o2", NULL, LXC_CLONE_SNAPSHOT, "overlayfs", NULL, 0, NULL)) == NULL) {
166 fprintf(stderr, "overlayfs clone of overlayfs failed\n");
167 goto out;
168 }
169
170 fprintf(stderr, "all clone tests passed for %s\n", c->name);
171 ret = 0;
172
173 out:
174 if (c3) {
175 lxc_container_put(c3);
176 }
177
178 if (c2) {
179 c2->destroy(c2);
180 lxc_container_put(c2);
181 }
182
183 if (c) {
184 c->destroy(c);
185 lxc_container_put(c);
186 }
187
188 exit(ret);
189 }