]> git.proxmox.com Git - mirror_lxc.git/blob - src/tests/snapshot.c
API support for container snapshots (v2)
[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 "../lxc/lxc.h"
24
25 #define MYNAME "snapxxx1"
26 #define RESTNAME "snapxxx2"
27
28 void try_to_remove()
29 {
30 struct lxc_container *c;
31 char snappath[1024];
32 c = lxc_container_new(RESTNAME, NULL);
33 if (c) {
34 if (c->is_defined(c))
35 c->destroy(c);
36 lxc_container_put(c);
37 }
38 snprintf(snappath, 1024, "%ssnaps/%s", lxc_get_default_config_path(), MYNAME);
39 c = lxc_container_new("snap0", snappath);
40 if (c) {
41 if (c->is_defined(c))
42 c->destroy(c);
43 lxc_container_put(c);
44 }
45 c = lxc_container_new(MYNAME, NULL);
46 if (c) {
47 if (c->is_defined(c))
48 c->destroy(c);
49 lxc_container_put(c);
50 }
51 }
52
53 int main(int argc, char *argv[])
54 {
55 struct lxc_container *c;
56 char *template = "busybox";
57
58 if (argc > 1)
59 template = argv[1];
60
61 try_to_remove();
62 c = lxc_container_new(MYNAME, NULL);
63 if (!c) {
64 fprintf(stderr, "%s: %d: failed to load first container\n", __FILE__, __LINE__);
65 exit(1);
66 }
67
68 if (c->is_defined(c)) {
69 fprintf(stderr, "%d: %s thought it was defined\n", __LINE__, MYNAME);
70 (void) c->destroy(c);
71 }
72 if (!c->set_config_item(c, "lxc.network.type", "empty")) {
73 fprintf(stderr, "%s: %d: failed to set network type\n", __FILE__, __LINE__);
74 goto err;
75 }
76 c->save_config(c, NULL);
77 if (!c->createl(c, template, NULL, NULL, 0, NULL)) {
78 fprintf(stderr, "%s: %d: failed to create %s container\n", __FILE__, __LINE__, template);
79 goto err;
80 }
81 c->load_config(c, NULL);
82
83 if (c->snapshot(c, NULL) != 0) {
84 fprintf(stderr, "%s: %d: failed to create snapsot\n", __FILE__, __LINE__);
85 goto err;
86 }
87
88 // rootfs should be ${lxcpath}snaps/${lxcname}/snap0/rootfs
89 struct stat sb;
90 int ret;
91 char path[1024];
92 snprintf(path, 1024, "%ssnaps/%s/snap0/rootfs", lxc_get_default_config_path(), MYNAME);
93 ret = stat(path, &sb);
94 if (ret != 0) {
95 fprintf(stderr, "%s: %d: snapshot was not actually created\n", __FILE__, __LINE__);
96 goto err;
97 }
98
99 struct lxc_snapshot *s;
100 int i, n;
101
102 n = c->snapshot_list(c, &s);
103 if (n < 1) {
104 fprintf(stderr, "%s: %d: failed listing containers\n", __FILE__, __LINE__);
105 goto err;
106 }
107 if (strcmp(s->name, "snap0") != 0) {
108 fprintf(stderr, "%s: %d: snapshot had bad name\n", __FILE__, __LINE__);
109 goto err;
110 }
111 for (i=0; i<n; i++) {
112 s[i].free(&s[i]);
113 }
114 free(s);
115
116 if (!c->snapshot_restore(c, "snap0", RESTNAME)) {
117 fprintf(stderr, "%s: %d: failed to restore snapshot\n", __FILE__, __LINE__);
118 goto err;
119 }
120
121 printf("All tests passed\n");
122 lxc_container_put(c);
123 exit(0);
124
125 err:
126 lxc_container_put(c);
127 fprintf(stderr, "Exiting on error\n");
128 try_to_remove();
129 exit(1);
130 }