diff options
author | Damian Hobson-Garcia <dhobsong@igel.co.jp> | 2022-04-05 12:11:38 +0900 |
---|---|---|
committer | Damian Hobson-Garcia <dhobsong@igel.co.jp> | 2022-04-20 10:58:39 +0900 |
commit | e9e5cf1f67a45e4d409dc9e1caa6ce8151579c88 (patch) | |
tree | ce471b907ba5c5bc553e57aef20dda52d00c514c /drm-lease-manager/test/lease-config-test.c | |
parent | 69acc5f3928f0578438cefdd69b39bc05bb0769b (diff) |
Add configuration file loading and parsing
Parse the lease configuration information from a configuration file.
Each lease configuration takes a name and list of connectors to
add to the lease. As long as one connector is found for a given
configuration, the lease will be created.
Bug-AGL: SPEC-3815
Change-Id: Iec4eaf37fba5db17a22e4945de10a06ac94063e4
Signed-off-by: Damian Hobson-Garcia <dhobsong@igel.co.jp>
Diffstat (limited to 'drm-lease-manager/test/lease-config-test.c')
-rw-r--r-- | drm-lease-manager/test/lease-config-test.c | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/drm-lease-manager/test/lease-config-test.c b/drm-lease-manager/test/lease-config-test.c new file mode 100644 index 0000000..fa5edcd --- /dev/null +++ b/drm-lease-manager/test/lease-config-test.c @@ -0,0 +1,102 @@ +/* Copyright 2022 IGEL Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "lease-config.h" +#include <check.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +#define CONFIG_FILE_TEMPLATE "/tmp/dlmconfig_tmpXXXXXX" +int config_fd; +char config_file[] = CONFIG_FILE_TEMPLATE; + +static void test_setup(void) +{ + strcpy(config_file, CONFIG_FILE_TEMPLATE); + config_fd = mkstemp(config_file); + ck_assert_int_ge(config_fd, 0); +} + +static void test_shutdown(void) +{ + close(config_fd); + unlink(config_file); +} + +/* parse config file test */ +/* Test details: Parse a config file + * Expected results: a config with the expected results + */ + +START_TEST(parse_leases) +{ + ck_assert_ptr_ne(config_file, NULL); + + char test_data[] = "[[lease]]\n" + "name = \"lease 1\"\n" + "connectors = [\"1\", \"b\",\"gamma\" ]\n" + "[[lease]]\n" + "name = \"lease 2\"\n" + "connectors = [\"connector 3\"]\n"; + + write(config_fd, test_data, sizeof(test_data)); + + struct lease_config *config = NULL; + int nconfigs = parse_config(config_file, &config); + + ck_assert_int_eq(nconfigs, 2); + ck_assert_ptr_ne(config, NULL); + + ck_assert_str_eq(config[0].lease_name, "lease 1"); + ck_assert_int_eq(config[0].cnames, 3); + ck_assert_str_eq(config[0].connector_names[0], "1"); + ck_assert_str_eq(config[0].connector_names[1], "b"); + ck_assert_str_eq(config[0].connector_names[2], "gamma"); + + ck_assert_str_eq(config[1].lease_name, "lease 2"); + ck_assert_int_eq(config[1].cnames, 1); + ck_assert_str_eq(config[1].connector_names[0], "connector 3"); + + release_config(nconfigs, config); +} +END_TEST + +static void add_parse_tests(Suite *s) +{ + TCase *tc = tcase_create("Config file parsing tests"); + + tcase_add_checked_fixture(tc, test_setup, test_shutdown); + + tcase_add_test(tc, parse_leases); + suite_add_tcase(s, tc); +} + +int main(void) +{ + int number_failed; + Suite *s; + SRunner *sr; + + s = suite_create("DLM lease manager tests"); + + add_parse_tests(s); + + sr = srunner_create(s); + srunner_run_all(sr, CK_NORMAL); + number_failed = srunner_ntests_failed(sr); + srunner_free(sr); + return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; +} |