File: clonetest.c

package info (click to toggle)
lxc 1%3A1.0.6-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 4,800 kB
  • sloc: ansic: 33,735; sh: 11,868; python: 1,223; makefile: 734
file content (178 lines) | stat: -rw-r--r-- 4,412 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* liblxcapi
 *
 * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
 * Copyright © 2012 Canonical Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
#include <lxc/lxccontainer.h>

#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <errno.h>

#define MYNAME "clonetest1"
#define MYNAME2 "clonetest2"

int main(int argc, char *argv[])
{
	struct lxc_container *c = NULL, *c2 = NULL, *c3 = NULL;
	int ret = 1;

	c = lxc_container_new(MYNAME, NULL);
	c2 = lxc_container_new(MYNAME2, NULL);
	if (c) {
		c->destroy(c);
		lxc_container_put(c);
		c = NULL;
	}
	if (c2) {
		c2->destroy(c2);
		lxc_container_put(c2);
		c2 = NULL;
	}

	if ((c = lxc_container_new(MYNAME, NULL)) == NULL) {
		fprintf(stderr, "%d: error opening lxc_container %s\n", __LINE__, MYNAME);
		ret = 1;
		goto out;
	}
	c->save_config(c, NULL);
	if (!c->createl(c, "busybox", NULL, NULL, 0, NULL)) {
		fprintf(stderr, "%d: failed to create a container\n", __LINE__);
		goto out;
	}
	c->load_config(c, NULL);

	if (!c->is_defined(c)) {
		fprintf(stderr, "%d: %s thought it was not defined\n", __LINE__, MYNAME);
		goto out;
	}

	c2 = c->clone(c, MYNAME2, NULL, 0, NULL, NULL, 0, NULL);
	if (!c2) {
		fprintf(stderr, "%d: %s clone returned NULL\n", __LINE__, MYNAME2);
		goto out;
	}

	if (!c2->is_defined(c2)) {
		fprintf(stderr, "%d: %s not defined after clone\n", __LINE__, MYNAME2);
		goto out;
	}

	fprintf(stderr, "directory backing store tests passed\n");

	// now test with lvm
	// Only do this if clonetestlvm1 exists - user has to set this up
	// in advance
	c2->destroy(c2);
	lxc_container_put(c2);
	c->destroy(c);
	lxc_container_put(c);
	c = NULL;

	c2 = lxc_container_new("clonetestlvm2", NULL);
	if (c2) {
		if (c2->is_defined(c2))
			c2->destroy(c2);
		lxc_container_put(c2);
	}
	c2 = lxc_container_new("clonetest-o1", NULL);
	if (c2) {
		if (c2->is_defined(c2))
			c2->destroy(c2);
		lxc_container_put(c2);
	}
	c2 = lxc_container_new("clonetest-o2", NULL);
	if (c2) {
		if (c2->is_defined(c2))
			c2->destroy(c2);
		lxc_container_put(c2);
	}
	c2 = NULL;

	// lvm-copied
	c = lxc_container_new("clonetestlvm1", NULL);
	if (!c) {
		fprintf(stderr, "failed loading clonetestlvm1\n");
		goto out;
	}
	if (!c->is_defined(c)) {
		fprintf(stderr, "clonetestlvm1 does not exist, skipping lvm tests\n");
		ret = 0;
		goto out;
	}

	if ((c2 = c->clone(c, "clonetestlvm2", NULL, 0, NULL, NULL, 0, NULL)) == NULL) {
		fprintf(stderr, "lvm clone failed\n");
		goto out;
	}

	lxc_container_put(c2);

	// lvm-snapshot
	c2 = lxc_container_new("clonetestlvm3", NULL);
	if (c2) {
		if (c2->is_defined(c2))
			c2->destroy(c2);
		lxc_container_put(c2);
		c2 = NULL;
	}

	if ((c2 = c->clone(c, "clonetestlvm3", NULL, LXC_CLONE_SNAPSHOT, NULL, NULL, 0, NULL)) == NULL) {
		fprintf(stderr, "lvm clone failed\n");
		goto out;
	}
	lxc_container_put(c2);
	lxc_container_put(c);
	c = c2 = NULL;

	if ((c = lxc_container_new(MYNAME, NULL)) == NULL) {
		fprintf(stderr, "error opening original container for overlay test\n");
		goto out;
	}

	// Now create an overlayfs clone of a dir-backed container
	if ((c2 = c->clone(c, "clonetest-o1", NULL, LXC_CLONE_SNAPSHOT, "overlayfs", NULL, 0, NULL)) == NULL) {
		fprintf(stderr, "overlayfs clone of dir failed\n");
		goto out;
	}

	// Now create an overlayfs clone of the overlayfs clone
	if ((c3 = c2->clone(c2, "clonetest-o2", NULL, LXC_CLONE_SNAPSHOT, "overlayfs", NULL, 0, NULL)) == NULL) {
		fprintf(stderr, "overlayfs clone of overlayfs failed\n");
		goto out;
	}

	fprintf(stderr, "all clone tests passed for %s\n", c->name);
	ret = 0;

out:
	if (c3) {
		lxc_container_put(c3);
	}
	if (c2) {
		c2->destroy(c2);
		lxc_container_put(c2);
	}
	if (c) {
		c->destroy(c);
		lxc_container_put(c);
	}
	exit(ret);
}