]> git.proxmox.com Git - mirror_lxc.git/blob - src/tests/list.c
tests: include config.h
[mirror_lxc.git] / src / tests / list.c
1 /* list.c
2 *
3 * Copyright © 2013 Canonical, Inc
4 * Author: Serge Hallyn <serge.hallyn@ubuntu.com>
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
20 #include "config.h"
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <lxc/lxccontainer.h>
26
27 static void test_list_func(const char *lxcpath, const char *type,
28 int (*func)(const char *path, char ***names,
29 struct lxc_container ***cret))
30 {
31 int i, n, n2;
32 struct lxc_container **clist;
33 char **names;
34
35 printf("%-10s Counting containers\n", type);
36
37 n = func(lxcpath, NULL, NULL);
38 printf("%-10s Counted %d containers\n", type, n);
39 printf("%-10s Get container struct only\n", type);
40
41 n2 = func(lxcpath, NULL, &clist);
42 if (n2 != n)
43 printf("Warning: first call returned %d, second %d\n", n, n2);
44
45 for (i = 0; i < n2; i++) {
46 struct lxc_container *c = clist[i];
47 printf("%-10s Got container struct %s\n", type, c->name);
48 lxc_container_put(c);
49 }
50
51 if (n2 > 0) {
52 free(clist);
53 clist = NULL;
54 }
55
56 printf("%-10s Get names only\n", type);
57 n2 = func(lxcpath, &names, NULL);
58 if (n2 != n)
59 printf("Warning: first call returned %d, second %d\n", n, n2);
60
61 for (i = 0; i < n2; i++) {
62 printf("%-10s Got container name %s\n", type, names[i]);
63 free(names[i]);
64 }
65
66 if (n2 > 0) {
67 free(names);
68 names = NULL;
69 }
70
71 printf("%-10s Get names and containers\n", type);
72 n2 = func(lxcpath, &names, &clist);
73 if (n2 != n)
74 printf("Warning: first call returned %d, second %d\n", n, n2);
75
76 for (i = 0; i < n2; i++) {
77 struct lxc_container *c = clist[i];
78 printf("%-10s Got container struct %s, name %s\n", type, c->name, names[i]);
79
80 if (strcmp(c->name, names[i]))
81 fprintf(stderr, "ERROR: name mismatch!\n");
82
83 free(names[i]);
84 lxc_container_put(c);
85 }
86
87 if (n2 > 0) {
88 free(names);
89 free(clist);
90 }
91 }
92
93 int main(int argc, char *argv[])
94 {
95 const char *lxcpath = NULL;
96
97 if (argc > 1)
98 lxcpath = argv[1];
99
100 test_list_func(lxcpath, "Defined:", list_defined_containers);
101 test_list_func(lxcpath, "Active:", list_active_containers);
102 test_list_func(lxcpath, "All:", list_all_containers);
103
104 exit(EXIT_SUCCESS);
105 }