summaryrefslogtreecommitdiff
path: root/test/unit/config_t.c
blob: 342667ecd36c7ece251f3c707233ca6fd09873d1 (plain)
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
/*
 * Copyright (C) 2010 Red Hat, Inc. All rights reserved.
 *
 * This file is part of LVM2.
 *
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
 * of the GNU General Public License v.2.
 *
 * 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 "units.h"

static struct dm_pool *mem;

int config_init(void) {
	mem = dm_pool_create("config test", 1024);
	return mem == NULL;
}

int config_fini(void) {
	dm_pool_destroy(mem);
	return 0;
}

static const char *conf =
	"id = \"yada-yada\"\n"
	"seqno = 15\n"
	"status = [\"READ\", \"WRITE\"]\n"
	"flags = []\n"
	"extent_size = 8192\n"
	"physical_volumes {\n"
	"    pv0 {\n"
	"        id = \"abcd-efgh\"\n"
	"    }\n"
	"    pv1 {\n"
	"        id = \"bbcd-efgh\"\n"
	"    }\n"
	"    pv2 {\n"
	"        id = \"cbcd-efgh\"\n"
	"    }\n"
	"}\n";

static const char *overlay =
	"id = \"yoda-soda\"\n"
	"flags = [\"FOO\"]\n"
	"physical_volumes {\n"
	"    pv1 {\n"
	"        id = \"hgfe-dcba\"\n"
	"    }\n"
	"    pv3 {\n"
	"        id = \"dbcd-efgh\"\n"
	"    }\n"
	"}\n";

static void test_parse(void)
{
	struct dm_config_tree *tree = dm_config_from_string(conf);
	const struct dm_config_value *value;

	CU_ASSERT((long) tree);
	CU_ASSERT(dm_config_has_node(tree->root, "id"));
	CU_ASSERT(dm_config_has_node(tree->root, "physical_volumes"));
	CU_ASSERT(dm_config_has_node(tree->root, "physical_volumes/pv0"));
	CU_ASSERT(dm_config_has_node(tree->root, "physical_volumes/pv0/id"));

	CU_ASSERT(!strcmp(dm_config_find_str(tree->root, "id", "foo"), "yada-yada"));
	CU_ASSERT(!strcmp(dm_config_find_str(tree->root, "idt", "foo"), "foo"));

	CU_ASSERT(!strcmp(dm_config_find_str(tree->root, "physical_volumes/pv0/bb", "foo"), "foo"));
	CU_ASSERT(!strcmp(dm_config_find_str(tree->root, "physical_volumes/pv0/id", "foo"), "abcd-efgh"));

	CU_ASSERT(!dm_config_get_uint32(tree->root, "id", NULL));
	CU_ASSERT(dm_config_get_uint32(tree->root, "extent_size", NULL));

	/* FIXME: Currently everything parses as a list, even if it's not */
	// CU_ASSERT(!dm_config_get_list(tree->root, "id", NULL));
	// CU_ASSERT(!dm_config_get_list(tree->root, "extent_size", NULL));

	CU_ASSERT(dm_config_get_list(tree->root, "flags", &value));
	CU_ASSERT(value->next == NULL); /* an empty list */
	CU_ASSERT(dm_config_get_list(tree->root, "status", &value));
	CU_ASSERT(value->next != NULL); /* a non-empty list */

	dm_config_destroy(tree);
}

static void test_clone(void)
{
	struct dm_config_tree *tree = dm_config_from_string(conf);
	struct dm_config_node *n = dm_config_clone_node(tree, tree->root, 1);
	const struct dm_config_value *value;

	/* Check that the nodes are actually distinct. */
	CU_ASSERT(n != tree->root);
	CU_ASSERT(n->sib != tree->root->sib);
	CU_ASSERT(dm_config_find_node(n, "physical_volumes") != NULL);
	CU_ASSERT(dm_config_find_node(tree->root, "physical_volumes") != NULL);
	CU_ASSERT(dm_config_find_node(n, "physical_volumes") != dm_config_find_node(tree->root, "physical_volumes"));

	CU_ASSERT(dm_config_has_node(n, "id"));
	CU_ASSERT(dm_config_has_node(n, "physical_volumes"));
	CU_ASSERT(dm_config_has_node(n, "physical_volumes/pv0"));
	CU_ASSERT(dm_config_has_node(n, "physical_volumes/pv0/id"));

	CU_ASSERT(!strcmp(dm_config_find_str(n, "id", "foo"), "yada-yada"));
	CU_ASSERT(!strcmp(dm_config_find_str(n, "idt", "foo"), "foo"));

	CU_ASSERT(!strcmp(dm_config_find_str(n, "physical_volumes/pv0/bb", "foo"), "foo"));
	CU_ASSERT(!strcmp(dm_config_find_str(n, "physical_volumes/pv0/id", "foo"), "abcd-efgh"));

	CU_ASSERT(!dm_config_get_uint32(n, "id", NULL));
	CU_ASSERT(dm_config_get_uint32(n, "extent_size", NULL));

	/* FIXME: Currently everything parses as a list, even if it's not */
	// CU_ASSERT(!dm_config_get_list(tree->root, "id", NULL));
	// CU_ASSERT(!dm_config_get_list(tree->root, "extent_size", NULL));

	CU_ASSERT(dm_config_get_list(n, "flags", &value));
	CU_ASSERT(value->next == NULL); /* an empty list */
	CU_ASSERT(dm_config_get_list(n, "status", &value));
	CU_ASSERT(value->next != NULL); /* a non-empty list */

	dm_config_destroy(tree);
}

static void test_cascade(void)
{
	struct dm_config_tree *t1 = dm_config_from_string(conf),
		              *t2 = dm_config_from_string(overlay),
		              *tree = dm_config_insert_cascaded_tree(t2, t1);

	CU_ASSERT(!strcmp(dm_config_tree_find_str(tree, "id", "foo"), "yoda-soda"));
	CU_ASSERT(!strcmp(dm_config_tree_find_str(tree, "idt", "foo"), "foo"));

	CU_ASSERT(!strcmp(dm_config_tree_find_str(tree, "physical_volumes/pv0/bb", "foo"), "foo"));
	CU_ASSERT(!strcmp(dm_config_tree_find_str(tree, "physical_volumes/pv1/id", "foo"), "hgfe-dcba"));
	CU_ASSERT(!strcmp(dm_config_tree_find_str(tree, "physical_volumes/pv3/id", "foo"), "dbcd-efgh"));

	dm_config_destroy(t1);
	dm_config_destroy(t2);
}

CU_TestInfo config_list[] = {
	{ (char*)"parse", test_parse },
	{ (char*)"clone", test_clone },
	{ (char*)"cascade", test_cascade },
	CU_TEST_INFO_NULL
};