Add configuration file loading and parsing
[src/drm-lease-manager.git] / drm-lease-manager / test / lease-config-test.c
1 /* Copyright 2021 IGEL Co., Ltd.
2  *
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
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include "lease-config.h"
17 #include <check.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20
21 static void test_setup(void)
22 {
23 }
24
25 static void test_shutdown(void)
26 {
27 }
28
29 /* parse config file test */
30 /* Test details: Parse a config file
31  * Expected results: a config with the expected results
32  */
33
34 START_TEST(parse_leases)
35 {
36         char tmp_file[] = "/tmp/dlmconfig_tmpXXXXXX";
37         int fd = mkstemp(tmp_file);
38
39         ck_assert_int_ge(fd, 0);
40
41         FILE *config_file = fdopen(fd, "w");
42         ck_assert_ptr_ne(config_file, NULL);
43
44         char test_data[] = "[[lease]]\n"
45                            "name = \"lease 1\"\n"
46                            "connectors = [\"1\", \"b\",\"gamma\" ]\n"
47                            "[[lease]]\n"
48                            "name = \"lease 2\"\n"
49                            "connectors = [\"connector 3\"]\n";
50
51         fwrite(test_data, 1, sizeof(test_data), config_file);
52         fclose(config_file);
53
54         struct lease_config *config = NULL;
55         int nconfigs = parse_config(tmp_file, &config);
56
57         ck_assert_int_eq(nconfigs, 2);
58         ck_assert_ptr_ne(config, NULL);
59
60         ck_assert_str_eq(config[0].lease_name, "lease 1");
61         ck_assert_int_eq(config[0].cnames, 3);
62         ck_assert_str_eq(config[0].connector_names[0], "1");
63         ck_assert_str_eq(config[0].connector_names[1], "b");
64         ck_assert_str_eq(config[0].connector_names[2], "gamma");
65
66         ck_assert_str_eq(config[1].lease_name, "lease 2");
67         ck_assert_int_eq(config[1].cnames, 1);
68         ck_assert_str_eq(config[1].connector_names[0], "connector 3");
69
70         release_config(nconfigs, config);
71 }
72 END_TEST
73
74 static void add_parse_tests(Suite *s)
75 {
76         TCase *tc = tcase_create("Config file parsing tests");
77
78         tcase_add_checked_fixture(tc, test_setup, test_shutdown);
79
80         tcase_add_test(tc, parse_leases);
81         suite_add_tcase(s, tc);
82 }
83
84 int main(void)
85 {
86         int number_failed;
87         Suite *s;
88         SRunner *sr;
89
90         s = suite_create("DLM lease manager tests");
91
92         add_parse_tests(s);
93
94         sr = srunner_create(s);
95         srunner_run_all(sr, CK_NORMAL);
96         number_failed = srunner_ntests_failed(sr);
97         srunner_free(sr);
98         return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
99 }