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