]> git.proxmox.com Git - mirror_lxc.git/blob - src/tests/snapshot.c
Merge pull request #4062 from stgraber/master
[mirror_lxc.git] / src / tests / snapshot.c
1 /* liblxcapi
2 *
3 * Copyright © 2013 Serge Hallyn <serge.hallyn@ubuntu.com>.
4 * Copyright © 2013 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 <errno.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <sys/stat.h>
29 #include "lxc/lxc.h"
30
31 #define MYNAME "snapxxx1"
32 #define MYNAME2 "snapxxx3"
33 #define RESTNAME "snapxxx2"
34
35 static void try_to_remove(void)
36 {
37 struct lxc_container *c;
38
39 c = lxc_container_new(RESTNAME, NULL);
40 if (c) {
41 c->snapshot_destroy_all(c);
42 if (c->is_defined(c))
43 c->destroy(c);
44
45 lxc_container_put(c);
46 }
47
48 c = lxc_container_new(MYNAME2, NULL);
49 if (c) {
50 c->destroy_with_snapshots(c);
51 lxc_container_put(c);
52 }
53
54 c = lxc_container_new(MYNAME, NULL);
55 if (c) {
56 c->snapshot_destroy_all(c);
57 if (c->is_defined(c))
58 c->destroy(c);
59
60 lxc_container_put(c);
61 }
62 }
63
64 int main(int argc, char *argv[])
65 {
66 int i, n, ret;
67 char path[1024];
68 struct stat sb;
69 struct lxc_snapshot *s;
70 struct lxc_container *c, *c2 = NULL;
71 char *template = "busybox";
72
73 if (argc > 1)
74 template = argv[1];
75
76 try_to_remove();
77
78 c = lxc_container_new(MYNAME, NULL);
79 if (!c) {
80 fprintf(stderr, "%s: %d: failed to load first container\n", __FILE__, __LINE__);
81 exit(EXIT_FAILURE);
82 }
83
84 if (c->is_defined(c)) {
85 fprintf(stderr, "%d: %s thought it was defined\n", __LINE__, MYNAME);
86 (void) c->destroy_with_snapshots(c);
87 }
88
89 if (!c->set_config_item(c, "lxc.net.0.type", "empty")) {
90 fprintf(stderr, "%s: %d: failed to set network type\n", __FILE__, __LINE__);
91 goto err;
92 }
93
94 c->save_config(c, NULL);
95
96 if (!c->createl(c, template, NULL, NULL, 0, NULL)) {
97 fprintf(stderr, "%s: %d: failed to create %s container\n", __FILE__, __LINE__, template);
98 goto err;
99 }
100
101 c->load_config(c, NULL);
102
103 if (c->snapshot(c, NULL) != 0) {
104 fprintf(stderr, "%s: %d: failed to create snapshot\n", __FILE__, __LINE__);
105 goto err;
106 }
107
108 // rootfs should be ${lxcpath}${lxcname}/snaps/snap0/rootfs
109 ret = snprintf(path, 1024, "%s/%s/snaps/snap0/rootfs", lxc_get_global_config_item("lxc.lxcpath"), MYNAME);
110 if (ret < 0 || (size_t)ret >= 1024) {
111 fprintf(stderr, "%s: %d: failed to create string\n", __FILE__, __LINE__);
112 goto err;
113 }
114
115 ret = stat(path, &sb);
116 if (ret != 0) {
117 fprintf(stderr, "%s: %d: snapshot was not actually created\n", __FILE__, __LINE__);
118 goto err;
119 }
120
121 n = c->snapshot_list(c, &s);
122 if (n < 1) {
123 fprintf(stderr, "%s: %d: failed listing containers\n", __FILE__, __LINE__);
124 goto err;
125 }
126
127 if (strcmp(s->name, "snap0") != 0) {
128 fprintf(stderr, "%s: %d: snapshot had bad name\n", __FILE__, __LINE__);
129 goto err;
130 }
131
132 for (i=0; i<n; i++)
133 s[i].free(&s[i]);
134 free(s);
135
136 if (!c->snapshot_restore(c, "snap0", RESTNAME)) {
137 fprintf(stderr, "%s: %d: failed to restore snapshot\n", __FILE__, __LINE__);
138 goto err;
139 }
140
141 if (!c->snapshot_destroy(c, "snap0")) {
142 fprintf(stderr, "%s: %d: failed to destroy snapshot\n", __FILE__, __LINE__);
143 goto err;
144 }
145
146 c2 = lxc_container_new(RESTNAME, NULL);
147 if (!c2 || !c2->is_defined(c2)) {
148 fprintf(stderr, "%s: %d: external snapshot restore failed\n", __FILE__, __LINE__);
149 goto err;
150 }
151 lxc_container_put(c2);
152
153 c2 = c->clone(c, MYNAME2, NULL, LXC_CLONE_SNAPSHOT, "overlayfs", NULL, 0, NULL);
154 if (!c2) {
155 fprintf(stderr, "%d: %s overlayfs clone failed\n", __LINE__, MYNAME2);
156 goto good;
157 }
158
159 if (c2->snapshot(c2, NULL) != 0) {
160 fprintf(stderr, "%s: %d: failed to create snapshot\n", __FILE__, __LINE__);
161 goto err;
162 }
163
164 n = c2->snapshot_list(c2, &s);
165 if (n < 1) {
166 fprintf(stderr, "%s: %d: failed listing containers\n", __FILE__, __LINE__);
167 goto err;
168 }
169
170 if (strcmp(s->name, "snap0") != 0) {
171 fprintf(stderr, "%s: %d: snapshot had bad name\n", __FILE__, __LINE__);
172 goto err;
173 }
174
175 for (i=0; i<n; i++)
176 s[i].free(&s[i]);
177 free(s);
178
179 if (!c2->snapshot_restore(c2, "snap0", NULL)) {
180 fprintf(stderr, "%s: %d: failed to restore overlayfs snapshot\n", __FILE__, __LINE__);
181 goto err;
182 }
183
184 if (!c2->snapshot_destroy(c2, "snap0")) {
185 fprintf(stderr, "%s: %d: failed to destroy overlayfs snapshot\n", __FILE__, __LINE__);
186 goto err;
187 }
188
189 good:
190 lxc_container_put(c);
191 try_to_remove();
192
193 printf("All tests passed\n");
194 exit(EXIT_SUCCESS);
195
196 err:
197 lxc_container_put(c);
198 try_to_remove();
199
200 fprintf(stderr, "Exiting on error\n");
201 exit(EXIT_FAILURE);
202 }