1 /* Copyright 2022 IGEL Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include "lease-config.h"
22 #define CONFIG_FILE_TEMPLATE "/tmp/dlmconfig_tmpXXXXXX"
24 char config_file[] = CONFIG_FILE_TEMPLATE;
26 static void test_setup(void)
28 strcpy(config_file, CONFIG_FILE_TEMPLATE);
29 config_fd = mkstemp(config_file);
30 ck_assert_int_ge(config_fd, 0);
33 static void test_shutdown(void)
39 /* parse config file test */
40 /* Test details: Parse a config file
41 * Expected results: a config with the expected results
44 START_TEST(parse_leases)
46 ck_assert_ptr_ne(config_file, NULL);
48 char test_data[] = "[[lease]]\n"
49 "name = \"lease 1\"\n"
50 "connectors = [\"1\", \"b\",\"gamma\" ]\n"
52 "name = \"lease 2\"\n"
53 "connectors = [\"connector 3\"]\n";
55 write(config_fd, test_data, sizeof(test_data));
57 struct lease_config *config = NULL;
58 int nconfigs = parse_config(config_file, &config);
60 ck_assert_int_eq(nconfigs, 2);
61 ck_assert_ptr_ne(config, NULL);
63 ck_assert_str_eq(config[0].lease_name, "lease 1");
64 ck_assert_int_eq(config[0].cnames, 3);
65 ck_assert_str_eq(config[0].connector_names[0], "1");
66 ck_assert_str_eq(config[0].connector_names[1], "b");
67 ck_assert_str_eq(config[0].connector_names[2], "gamma");
69 ck_assert_str_eq(config[1].lease_name, "lease 2");
70 ck_assert_int_eq(config[1].cnames, 1);
71 ck_assert_str_eq(config[1].connector_names[0], "connector 3");
73 release_config(nconfigs, config);
77 static void add_parse_tests(Suite *s)
79 TCase *tc = tcase_create("Config file parsing tests");
81 tcase_add_checked_fixture(tc, test_setup, test_shutdown);
83 tcase_add_test(tc, parse_leases);
84 suite_add_tcase(s, tc);
93 s = suite_create("DLM lease manager tests");
97 sr = srunner_create(s);
98 srunner_run_all(sr, CK_NORMAL);
99 number_failed = srunner_ntests_failed(sr);
101 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;