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