From 1ddd9dc19cc1df1a1ab8c6123283999f9dea6760 Mon Sep 17 00:00:00 2001 From: Anton Popovichenko Date: Thu, 19 Aug 2021 22:26:16 +0200 Subject: Core/Config: Implement config override with env vars (#26811) * Core/Config: Implement config override with env vars Implement overriding of configuration from the .conf file with environment variables. Environment variables keys are autogenerated based on the keys defined in .conf file. Usage example: $ export TC_DATA_DIR=/usr $ TC_WORLD_SERVER_PORT=8080 ./worldserver * Core/Config Fix typo in logs Co-authored-by: Giacomo Pozzoni * Core/Config Fix code style in EnvVarForIniKey Co-authored-by: Shauren * Update tests/common/Config.cpp * Apply suggestions from code review Co-authored-by: Peter Keresztes Schmidt * Apply suggestions from code review Co-authored-by: Peter Keresztes Schmidt Co-authored-by: Anton Popovichenko Co-authored-by: Giacomo Pozzoni Co-authored-by: Shauren Co-authored-by: Peter Keresztes Schmidt --- tests/common/Config.cpp | 115 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 tests/common/Config.cpp (limited to 'tests') diff --git a/tests/common/Config.cpp b/tests/common/Config.cpp new file mode 100644 index 00000000000..fb657c358fd --- /dev/null +++ b/tests/common/Config.cpp @@ -0,0 +1,115 @@ +/* + * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#define CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER +#include "tc_catch2.h" + +#include "Config.h" +#include +#include +#include + +std::string CreateConfigWithMap(std::map const& map) +{ + auto mTempFileRel = boost::filesystem::unique_path("deleteme.ini"); + auto mTempFileAbs = boost::filesystem::temp_directory_path() / mTempFileRel; + std::ofstream iniStream; + iniStream.open(mTempFileAbs.c_str()); + + iniStream << "[test]\n"; + for (auto const& itr : map) + iniStream << itr.first << " = " << itr.second << "\n"; + + iniStream.close(); + + return mTempFileAbs.native(); +} + +TEST_CASE("Envariable variables", "[Config]") +{ + std::map config; + config["Int.Nested"] = "4242"; + config["lower"] = "simpleString"; + config["UPPER"] = "simpleString"; + config["SomeLong.NestedNameWithNumber.Like1"] = "1"; + + auto filePath = CreateConfigWithMap(config); + + std::string err; + REQUIRE(sConfigMgr->LoadInitial(filePath, std::vector(), err)); + REQUIRE(err.empty()); + + SECTION("Nested int") + { + REQUIRE(sConfigMgr->GetIntDefault("Int.Nested", 10) == 4242); + + setenv("TC_INT_NESTED", "8080", 1); + REQUIRE(!sConfigMgr->OverrideWithEnvVariablesIfAny().empty()); + REQUIRE(sConfigMgr->GetIntDefault("Int.Nested", 10) == 8080); + } + + SECTION("Simple lower string") + { + REQUIRE(sConfigMgr->GetStringDefault("lower", "") == "simpleString"); + + setenv("TC_LOWER", "envstring", 1); + REQUIRE(!sConfigMgr->OverrideWithEnvVariablesIfAny().empty()); + REQUIRE(sConfigMgr->GetStringDefault("lower", "") == "envstring"); + } + + SECTION("Simple upper string") + { + REQUIRE(sConfigMgr->GetStringDefault("UPPER", "") == "simpleString"); + + setenv("TC_UPPER", "envupperstring", 1); + REQUIRE(!sConfigMgr->OverrideWithEnvVariablesIfAny().empty()); + REQUIRE(sConfigMgr->GetStringDefault("UPPER", "") == "envupperstring"); + } + + SECTION("Long nested name with number") + { + REQUIRE(sConfigMgr->GetFloatDefault("SomeLong.NestedNameWithNumber.Like1", 0) == 1); + + setenv("TC_SOME_LONG_NESTED_NAME_WITH_NUMBER_LIKE_1", "42", 1); + REQUIRE(!sConfigMgr->OverrideWithEnvVariablesIfAny().empty()); + REQUIRE(sConfigMgr->GetFloatDefault("SomeLong.NestedNameWithNumber.Like1", 0) == 42); + } + + SECTION("String that not exist in config") + { + setenv("TC_UNIQUE_STRING", "somevalue", 1); + REQUIRE(sConfigMgr->GetStringDefault("Unique.String", "") == "somevalue"); + } + + SECTION("Int that not exist in config") + { + setenv("TC_UNIQUE_INT", "100", 1); + REQUIRE(sConfigMgr->GetIntDefault("Unique.Int", 1) == 100); + } + + SECTION("Not existing string") + { + REQUIRE(sConfigMgr->GetStringDefault("NotFound.String", "none") == "none"); + } + + SECTION("Not existing int") + { + REQUIRE(sConfigMgr->GetIntDefault("NotFound.Int", 1) == 1); + } + + std::remove(filePath.c_str()); +} -- cgit v1.2.3