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