]>
Commit | Line | Data |
---|---|---|
9be53773 SH |
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> |
9be53773 SH |
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 | if (c2) { | |
45 | c2->destroy(c2); | |
46 | lxc_container_put(c2); | |
47 | c2 = NULL; | |
48 | } | |
49 | ||
50 | if ((c = lxc_container_new(MYNAME, NULL)) == NULL) { | |
51 | fprintf(stderr, "%d: error opening lxc_container %s\n", __LINE__, MYNAME); | |
52 | ret = 1; | |
53 | goto out; | |
54 | } | |
55 | c->save_config(c, NULL); | |
787c3ebe | 56 | if (!c->createl(c, "busybox", NULL, NULL, 0, NULL)) { |
9be53773 SH |
57 | fprintf(stderr, "%d: failed to create a container\n", __LINE__); |
58 | goto out; | |
59 | } | |
60 | c->load_config(c, NULL); | |
61 | ||
62 | if (!c->is_defined(c)) { | |
63 | fprintf(stderr, "%d: %s thought it was not defined\n", __LINE__, MYNAME); | |
64 | goto out; | |
65 | } | |
66 | ||
481624b3 | 67 | c2 = c->clone(c, MYNAME2, NULL, 0, NULL, NULL, 0, NULL); |
9be53773 SH |
68 | if (!c2) { |
69 | fprintf(stderr, "%d: %s clone returned NULL\n", __LINE__, MYNAME2); | |
70 | goto out; | |
71 | } | |
72 | ||
3856bc9f | 73 | if (!c2->is_defined(c2)) { |
9be53773 SH |
74 | fprintf(stderr, "%d: %s not defined after clone\n", __LINE__, MYNAME2); |
75 | goto out; | |
76 | } | |
77 | ||
78 | fprintf(stderr, "directory backing store tests passed\n"); | |
79 | ||
80 | // now test with lvm | |
81 | // Only do this if clonetestlvm1 exists - user has to set this up | |
82 | // in advance | |
0898897a | 83 | c2->destroy(c2); |
9be53773 | 84 | lxc_container_put(c2); |
0898897a | 85 | c->destroy(c); |
9be53773 SH |
86 | lxc_container_put(c); |
87 | c = NULL; | |
88 | ||
89 | c2 = lxc_container_new("clonetestlvm2", NULL); | |
90 | if (c2) { | |
91 | if (c2->is_defined(c2)) | |
92 | c2->destroy(c2); | |
93 | lxc_container_put(c2); | |
94 | } | |
95 | c2 = lxc_container_new("clonetest-o1", NULL); | |
96 | if (c2) { | |
97 | if (c2->is_defined(c2)) | |
98 | c2->destroy(c2); | |
99 | lxc_container_put(c2); | |
100 | } | |
101 | c2 = lxc_container_new("clonetest-o2", NULL); | |
102 | if (c2) { | |
103 | if (c2->is_defined(c2)) | |
104 | c2->destroy(c2); | |
105 | lxc_container_put(c2); | |
106 | } | |
107 | c2 = NULL; | |
108 | ||
109 | // lvm-copied | |
110 | c = lxc_container_new("clonetestlvm1", NULL); | |
111 | if (!c) { | |
112 | fprintf(stderr, "failed loading clonetestlvm1\n"); | |
113 | goto out; | |
114 | } | |
115 | if (!c->is_defined(c)) { | |
116 | fprintf(stderr, "clonetestlvm1 does not exist, skipping lvm tests\n"); | |
117 | ret = 0; | |
118 | goto out; | |
119 | } | |
120 | ||
481624b3 | 121 | if ((c2 = c->clone(c, "clonetestlvm2", NULL, 0, NULL, NULL, 0, NULL)) == NULL) { |
9be53773 SH |
122 | fprintf(stderr, "lvm clone failed\n"); |
123 | goto out; | |
124 | } | |
125 | ||
126 | lxc_container_put(c2); | |
127 | ||
128 | // lvm-snapshot | |
129 | c2 = lxc_container_new("clonetestlvm3", NULL); | |
130 | if (c2) { | |
131 | if (c2->is_defined(c2)) | |
132 | c2->destroy(c2); | |
133 | lxc_container_put(c2); | |
134 | c2 = NULL; | |
135 | } | |
136 | ||
481624b3 | 137 | if ((c2 = c->clone(c, "clonetestlvm3", NULL, LXC_CLONE_SNAPSHOT, NULL, NULL, 0, NULL)) == NULL) { |
9be53773 SH |
138 | fprintf(stderr, "lvm clone failed\n"); |
139 | goto out; | |
140 | } | |
141 | lxc_container_put(c2); | |
142 | lxc_container_put(c); | |
143 | c = c2 = NULL; | |
144 | ||
145 | if ((c = lxc_container_new(MYNAME, NULL)) == NULL) { | |
146 | fprintf(stderr, "error opening original container for overlay test\n"); | |
147 | goto out; | |
148 | } | |
149 | ||
150 | // Now create an overlayfs clone of a dir-backed container | |
481624b3 | 151 | if ((c2 = c->clone(c, "clonetest-o1", NULL, LXC_CLONE_SNAPSHOT, "overlayfs", NULL, 0, NULL)) == NULL) { |
9be53773 SH |
152 | fprintf(stderr, "overlayfs clone of dir failed\n"); |
153 | goto out; | |
154 | } | |
155 | ||
156 | // Now create an overlayfs clone of the overlayfs clone | |
481624b3 | 157 | if ((c3 = c2->clone(c2, "clonetest-o2", NULL, LXC_CLONE_SNAPSHOT, "overlayfs", NULL, 0, NULL)) == NULL) { |
9be53773 SH |
158 | fprintf(stderr, "overlayfs clone of overlayfs failed\n"); |
159 | goto out; | |
160 | } | |
161 | ||
162 | fprintf(stderr, "all clone tests passed for %s\n", c->name); | |
163 | ret = 0; | |
164 | ||
165 | out: | |
166 | if (c3) { | |
167 | lxc_container_put(c3); | |
168 | } | |
169 | if (c2) { | |
0898897a | 170 | c2->destroy(c2); |
9be53773 SH |
171 | lxc_container_put(c2); |
172 | } | |
173 | if (c) { | |
0898897a | 174 | c->destroy(c); |
9be53773 SH |
175 | lxc_container_put(c); |
176 | } | |
177 | exit(ret); | |
178 | } |