X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=tst%2Fconfigurationreader_test.cpp;h=d9f4f6d3dd14be646f857e579305a7cdd1f27bf5;hb=5bdca62d89d430f607310370fe1391bd1d1bd21a;hp=b1ed9f125d2661eedd48829dc90abd3b77d89032;hpb=a0745d294dcd72f7d78ea4c3accd3b477dd668a5;p=ric-plt%2Fsdl.git diff --git a/tst/configurationreader_test.cpp b/tst/configurationreader_test.cpp index b1ed9f1..d9f4f6d 100644 --- a/tst/configurationreader_test.cpp +++ b/tst/configurationreader_test.cpp @@ -1,5 +1,5 @@ /* - Copyright (c) 2018-2019 Nokia. + Copyright (c) 2018-2022 Nokia. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -62,19 +62,25 @@ namespace EXPECT_CALL(databaseConfigurationMock, checkAndApplyDbType(type)); } + void expectGetDbTypeAndWillOnceReturn(DatabaseConfiguration::DbType type) + { + EXPECT_CALL(databaseConfigurationMock, getDbType()) + .WillOnce(Return(type)); + } + void expectDBServerAddressConfigurationCheckAndApply(const std::string& address) { EXPECT_CALL(databaseConfigurationMock, checkAndApplyServerAddress(address)); } - void expectSentinelAddressConfigurationCheckAndApply(const std::string& address) + void expectCheckAndApplySentinelPorts(const std::string& portsEnvStr) { - EXPECT_CALL(databaseConfigurationMock, checkAndApplySentinelAddress(address)); + EXPECT_CALL(databaseConfigurationMock, checkAndApplySentinelPorts(portsEnvStr)); } void expectSentinelMasterNameConfigurationCheckAndApply(const std::string& address) { - EXPECT_CALL(databaseConfigurationMock, checkAndApplySentinelMasterName(address)); + EXPECT_CALL(databaseConfigurationMock, checkAndApplySentinelMasterNames(address)); } void expectDatabaseConfigurationIsEmpty_returnFalse() @@ -740,6 +746,7 @@ public: std::string dbPortEnvVariableValue; std::string sentinelPortEnvVariableValue; std::string sentinelMasterNameEnvVariableValue; + std::string dbClusterAddrListEnvVariableValue; std::istringstream is{R"JSON( { "database": @@ -762,20 +769,25 @@ public: { } - void readEnvironmentConfigurationAndExpectConfigurationErrorException() + void readEnvironmentConfigurationAndExpectConfigurationErrorException(const std::string& msg, + bool expectCall) { std::ostringstream os; - os << "Configuration error in " << someKnownInputSource << ": some error"; + os << "Configuration error in " << someKnownInputSource << ": " << msg; - EXPECT_CALL(databaseConfigurationMock, checkAndApplyDbType(_)) - .WillOnce(Throw(Exception("some error"))); + if (expectCall) + EXPECT_CALL(databaseConfigurationMock, checkAndApplyDbType(_)) + .WillOnce(Throw(Exception("some error"))); EXPECT_THROW( { try { EXPECT_CALL(systemMock, getenv(_)) - .Times(4) + .Times(5) .WillOnce(Return(dbHostEnvVariableValue.c_str())) + .WillOnce(Return(nullptr)) + .WillOnce(Return(nullptr)) + .WillOnce(Return(nullptr)) .WillOnce(Return(nullptr)); initializeReaderWithoutDirectories(); configurationReader->readDatabaseConfiguration(databaseConfigurationMock); @@ -798,9 +810,11 @@ TEST_F(ConfigurationReaderEnvironmentVariableTest, EnvironmentConfigurationCanOv expectGetEnvironmentString(dbPortEnvVariableValue.c_str()); expectGetEnvironmentString(nullptr); //SENTINEL_PORT_ENV_VAR_NAME expectGetEnvironmentString(nullptr); //SENTINEL_MASTER_NAME_ENV_VAR_NAME + expectGetEnvironmentString(nullptr); //DB_CLUSTER_ENV_VAR_NAME expectDbTypeConfigurationCheckAndApply("redis-standalone"); expectDBServerAddressConfigurationCheckAndApply("unknownAddress.local:12345"); + expectGetDbTypeAndWillOnceReturn(DatabaseConfiguration::DbType::REDIS_STANDALONE); initializeReaderWithSDLconfigFileDirectory(); configurationReader->readConfigurationFromInputStream(is); configurationReader->readDatabaseConfiguration(databaseConfigurationMock); @@ -814,9 +828,11 @@ TEST_F(ConfigurationReaderEnvironmentVariableTest, EnvironmentConfigurationWitho expectGetEnvironmentString(nullptr); //DB_PORT_ENV_VAR_NAME expectGetEnvironmentString(nullptr); //SENTINEL_PORT_ENV_VAR_NAME expectGetEnvironmentString(nullptr); //SENTINEL_MASTER_NAME_ENV_VAR_NAME + expectGetEnvironmentString(nullptr); //DB_CLUSTER_ENV_VAR_NAME expectDbTypeConfigurationCheckAndApply("redis-standalone"); expectDBServerAddressConfigurationCheckAndApply("server.local"); + expectGetDbTypeAndWillOnceReturn(DatabaseConfiguration::DbType::REDIS_STANDALONE); initializeReaderWithoutDirectories(); configurationReader->readDatabaseConfiguration(databaseConfigurationMock); } @@ -824,13 +840,14 @@ TEST_F(ConfigurationReaderEnvironmentVariableTest, EnvironmentConfigurationWitho TEST_F(ConfigurationReaderEnvironmentVariableTest, EmptyEnvironmentVariableThrows) { dbHostEnvVariableValue = ""; - readEnvironmentConfigurationAndExpectConfigurationErrorException(); + readEnvironmentConfigurationAndExpectConfigurationErrorException("Missing environment variable configuration!", + false); } TEST_F(ConfigurationReaderEnvironmentVariableTest, IllegalCharacterInEnvironmentVariableThrows) { dbHostEnvVariableValue = "@"; - readEnvironmentConfigurationAndExpectConfigurationErrorException(); + readEnvironmentConfigurationAndExpectConfigurationErrorException("some error", true); } TEST_F(ConfigurationReaderEnvironmentVariableTest, EnvironmentConfigurationAcceptIPv6Address) @@ -841,9 +858,11 @@ TEST_F(ConfigurationReaderEnvironmentVariableTest, EnvironmentConfigurationAccep expectGetEnvironmentString(nullptr); //DB_PORT_ENV_VAR_NAME expectGetEnvironmentString(nullptr); //SENTINEL_PORT_ENV_VAR_NAME expectGetEnvironmentString(nullptr); //SENTINEL_MASTER_NAME_ENV_VAR_NAME + expectGetEnvironmentString(nullptr); //DB_CLUSTER_ENV_VAR_NAME expectDbTypeConfigurationCheckAndApply("redis-standalone"); expectDBServerAddressConfigurationCheckAndApply("[2001::123]:12345"); + expectGetDbTypeAndWillOnceReturn(DatabaseConfiguration::DbType::REDIS_STANDALONE); initializeReaderWithoutDirectories(); configurationReader->readDatabaseConfiguration(databaseConfigurationMock); } @@ -859,10 +878,78 @@ TEST_F(ConfigurationReaderEnvironmentVariableTest, EnvironmentConfigurationWithS expectGetEnvironmentString(sentinelPortEnvVariableValue.c_str()); sentinelMasterNameEnvVariableValue = "mymaster"; expectGetEnvironmentString(sentinelMasterNameEnvVariableValue.c_str()); + expectGetEnvironmentString(nullptr); //DB_CLUSTER_ENV_VAR_NAME expectDbTypeConfigurationCheckAndApply("redis-sentinel"); - expectSentinelAddressConfigurationCheckAndApply("sentinelAddress.local:2222"); + expectDBServerAddressConfigurationCheckAndApply("sentinelAddress.local:1111"); + expectGetDbTypeAndWillOnceReturn(DatabaseConfiguration::DbType::REDIS_SENTINEL); + expectCheckAndApplySentinelPorts(sentinelPortEnvVariableValue); expectSentinelMasterNameConfigurationCheckAndApply(sentinelMasterNameEnvVariableValue); initializeReaderWithoutDirectories(); configurationReader->readDatabaseConfiguration(databaseConfigurationMock); } + +TEST_F(ConfigurationReaderEnvironmentVariableTest, EnvironmentConfigurationWithSentinelAndClusterConfiguration) +{ + InSequence dummy; + dbHostEnvVariableValue = "address-0.local"; + expectGetEnvironmentString(dbHostEnvVariableValue.c_str()); + expectGetEnvironmentString(nullptr); //DB_PORT_ENV_VAR_NAME + sentinelPortEnvVariableValue = "2222,2223,2224"; + expectGetEnvironmentString(sentinelPortEnvVariableValue.c_str()); + sentinelMasterNameEnvVariableValue = "mymaster-0,mymaster-1,mymaster-2"; + expectGetEnvironmentString(sentinelMasterNameEnvVariableValue.c_str()); + dbClusterAddrListEnvVariableValue = "address-0.local,address-1.local,address-2.local"; + expectGetEnvironmentString(dbClusterAddrListEnvVariableValue.c_str()); + + expectDbTypeConfigurationCheckAndApply("sdl-sentinel-cluster"); + expectDBServerAddressConfigurationCheckAndApply("address-0.local"); + expectDBServerAddressConfigurationCheckAndApply("address-1.local"); + expectDBServerAddressConfigurationCheckAndApply("address-2.local"); + expectGetDbTypeAndWillOnceReturn(DatabaseConfiguration::DbType::SDL_SENTINEL_CLUSTER); + expectCheckAndApplySentinelPorts(sentinelPortEnvVariableValue); + expectSentinelMasterNameConfigurationCheckAndApply(sentinelMasterNameEnvVariableValue); + initializeReaderWithoutDirectories(); + configurationReader->readDatabaseConfiguration(databaseConfigurationMock); +} + +TEST_F(ConfigurationReaderEnvironmentVariableTest, EnvironmentConfigurationWithoutSentinelAndWithClusterConfiguration) +{ + InSequence dummy; + dbHostEnvVariableValue = "address-0.local"; + expectGetEnvironmentString(dbHostEnvVariableValue.c_str()); + expectGetEnvironmentString(nullptr); //DB_PORT_ENV_VAR_NAME + expectGetEnvironmentString(nullptr); //SENTINEL_PORT_ENV_VAR_NAME + expectGetEnvironmentString(nullptr); //SENTINEL_MASTER_NAME_ENV_VAR_NAME + dbClusterAddrListEnvVariableValue = "address-0.local,address-1.local,address-2.local"; + expectGetEnvironmentString(dbClusterAddrListEnvVariableValue.c_str()); + + expectDbTypeConfigurationCheckAndApply("sdl-standalone-cluster"); + expectDBServerAddressConfigurationCheckAndApply("address-0.local"); + expectDBServerAddressConfigurationCheckAndApply("address-1.local"); + expectDBServerAddressConfigurationCheckAndApply("address-2.local"); + expectGetDbTypeAndWillOnceReturn(DatabaseConfiguration::DbType::SDL_STANDALONE_CLUSTER); + initializeReaderWithoutDirectories(); + configurationReader->readDatabaseConfiguration(databaseConfigurationMock); +} + +TEST_F(ConfigurationReaderEnvironmentVariableTest, EnvironmentConfigurationWithoutSentinelAndWithClusterConfigurationAndDbPort) +{ + InSequence dummy; + dbHostEnvVariableValue = "address-0.local"; + expectGetEnvironmentString(dbHostEnvVariableValue.c_str()); + dbPortEnvVariableValue = "1111"; + expectGetEnvironmentString(dbPortEnvVariableValue.c_str()); + expectGetEnvironmentString(nullptr); //SENTINEL_PORT_ENV_VAR_NAME + expectGetEnvironmentString(nullptr); //SENTINEL_MASTER_NAME_ENV_VAR_NAME + dbClusterAddrListEnvVariableValue = "address-0.local,address-1.local,address-2.local"; + expectGetEnvironmentString(dbClusterAddrListEnvVariableValue.c_str()); + + expectDbTypeConfigurationCheckAndApply("sdl-standalone-cluster"); + expectDBServerAddressConfigurationCheckAndApply("address-0.local:1111"); + expectDBServerAddressConfigurationCheckAndApply("address-1.local:1111"); + expectDBServerAddressConfigurationCheckAndApply("address-2.local:1111"); + expectGetDbTypeAndWillOnceReturn(DatabaseConfiguration::DbType::SDL_STANDALONE_CLUSTER); + initializeReaderWithoutDirectories(); + configurationReader->readDatabaseConfiguration(databaseConfigurationMock); +}