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