Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 1 | /* |
Petri Ovaska | 5bdca62 | 2022-03-10 09:59:23 +0200 | [diff] [blame] | 2 | Copyright (c) 2018-2022 Nokia. |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
Timo Tietavainen | a0745d2 | 2019-11-28 09:55:22 +0200 | [diff] [blame] | 17 | /* |
| 18 | * This source code is part of the near-RT RIC (RAN Intelligent Controller) |
| 19 | * platform project (RICP). |
| 20 | */ |
| 21 | |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 22 | #include <gtest/gtest.h> |
| 23 | #include <gmock/gmock.h> |
| 24 | #include <arpa/inet.h> |
| 25 | #include "private/databaseconfigurationimpl.hpp" |
| 26 | |
| 27 | using namespace shareddatalayer; |
| 28 | using namespace testing; |
| 29 | |
| 30 | namespace |
| 31 | { |
| 32 | class DatabaseConfigurationImplTest: public testing::Test |
| 33 | { |
| 34 | public: |
| 35 | std::unique_ptr<DatabaseConfigurationImpl> databaseConfigurationImpl; |
| 36 | |
| 37 | DatabaseConfigurationImplTest() |
| 38 | { |
| 39 | InSequence dummy; |
| 40 | databaseConfigurationImpl.reset(new DatabaseConfigurationImpl()); |
| 41 | } |
| 42 | }; |
| 43 | } |
| 44 | |
| 45 | TEST_F(DatabaseConfigurationImplTest, CanReturnDefaultAddress) |
| 46 | { |
| 47 | const auto retAddresses(databaseConfigurationImpl->getDefaultServerAddresses()); |
| 48 | EXPECT_EQ(1U, retAddresses.size()); |
| 49 | EXPECT_EQ("localhost", retAddresses.back().getHost()); |
| 50 | EXPECT_EQ(6379U, ntohs(retAddresses.back().getPort())); |
| 51 | } |
| 52 | |
| 53 | TEST_F(DatabaseConfigurationImplTest, CanReturnEmptyAddressListIfNoAddressesAreApplied) |
| 54 | { |
| 55 | const auto retAddresses(databaseConfigurationImpl->getServerAddresses()); |
| 56 | EXPECT_TRUE(retAddresses.empty()); |
| 57 | } |
| 58 | |
| 59 | TEST_F(DatabaseConfigurationImplTest, CanReturnUnknownTypeIfNoRedisDbTypeIsApplied) |
| 60 | { |
| 61 | const auto retDbType(databaseConfigurationImpl->getDbType()); |
| 62 | EXPECT_EQ(DatabaseConfiguration::DbType::UNKNOWN, retDbType); |
| 63 | } |
| 64 | |
| 65 | TEST_F(DatabaseConfigurationImplTest, CanApplyRedisDbTypeStringAndReturnType) |
| 66 | { |
| 67 | databaseConfigurationImpl->checkAndApplyDbType("redis-standalone"); |
| 68 | const auto retDbType(databaseConfigurationImpl->getDbType()); |
| 69 | EXPECT_EQ(DatabaseConfiguration::DbType::REDIS_STANDALONE, retDbType); |
| 70 | } |
| 71 | |
| 72 | TEST_F(DatabaseConfigurationImplTest, CanApplyRedisClusterDbTypeStringAndReturnType) |
| 73 | { |
| 74 | databaseConfigurationImpl->checkAndApplyDbType("redis-cluster"); |
| 75 | const auto retDbType(databaseConfigurationImpl->getDbType()); |
| 76 | EXPECT_EQ(DatabaseConfiguration::DbType::REDIS_CLUSTER, retDbType); |
| 77 | } |
| 78 | |
Rolf Badorek | 2dcf940 | 2019-10-01 18:33:58 +0300 | [diff] [blame] | 79 | TEST_F(DatabaseConfigurationImplTest, CanApplyRedisSentinelDbTypeStringAndReturnType) |
| 80 | { |
| 81 | databaseConfigurationImpl->checkAndApplyDbType("redis-sentinel"); |
| 82 | const auto retDbType(databaseConfigurationImpl->getDbType()); |
| 83 | EXPECT_EQ(DatabaseConfiguration::DbType::REDIS_SENTINEL, retDbType); |
| 84 | } |
| 85 | |
Petri Ovaska | 1c4a605 | 2021-05-31 14:06:32 +0300 | [diff] [blame] | 86 | TEST_F(DatabaseConfigurationImplTest, CanApplySdlStandaloneClusterDbTypeStringAndReturnType) |
Petri Ovaska | ece6708 | 2021-04-15 11:08:13 +0300 | [diff] [blame] | 87 | { |
Petri Ovaska | 1c4a605 | 2021-05-31 14:06:32 +0300 | [diff] [blame] | 88 | databaseConfigurationImpl->checkAndApplyDbType("sdl-standalone-cluster"); |
Petri Ovaska | ece6708 | 2021-04-15 11:08:13 +0300 | [diff] [blame] | 89 | const auto retDbType(databaseConfigurationImpl->getDbType()); |
Petri Ovaska | 1c4a605 | 2021-05-31 14:06:32 +0300 | [diff] [blame] | 90 | EXPECT_EQ(DatabaseConfiguration::DbType::SDL_STANDALONE_CLUSTER, retDbType); |
| 91 | } |
| 92 | |
| 93 | TEST_F(DatabaseConfigurationImplTest, CanApplySdlSentinelClusterDbTypeStringAndReturnType) |
| 94 | { |
| 95 | databaseConfigurationImpl->checkAndApplyDbType("sdl-sentinel-cluster"); |
| 96 | const auto retDbType(databaseConfigurationImpl->getDbType()); |
| 97 | EXPECT_EQ(DatabaseConfiguration::DbType::SDL_SENTINEL_CLUSTER, retDbType); |
Petri Ovaska | ece6708 | 2021-04-15 11:08:13 +0300 | [diff] [blame] | 98 | } |
| 99 | |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 100 | TEST_F(DatabaseConfigurationImplTest, CanApplyNewAddressesOneByOneAndReturnAllAddresses) |
| 101 | { |
| 102 | databaseConfigurationImpl->checkAndApplyServerAddress("dummydatabaseaddress.local"); |
| 103 | databaseConfigurationImpl->checkAndApplyServerAddress("10.20.30.40:65535"); |
| 104 | const auto retAddresses(databaseConfigurationImpl->getServerAddresses()); |
| 105 | EXPECT_EQ(2U, retAddresses.size()); |
| 106 | EXPECT_EQ("dummydatabaseaddress.local", retAddresses.at(0).getHost()); |
| 107 | EXPECT_EQ(6379U, ntohs(retAddresses.at(0).getPort())); |
| 108 | EXPECT_EQ("10.20.30.40", retAddresses.at(1).getHost()); |
| 109 | EXPECT_EQ(65535U, ntohs(retAddresses.at(1).getPort())); |
| 110 | } |
| 111 | |
Petri Ovaska | 1c4a605 | 2021-05-31 14:06:32 +0300 | [diff] [blame] | 112 | TEST_F(DatabaseConfigurationImplTest, CanGetAddressesOneByOneWithAddressIndex) |
| 113 | { |
| 114 | databaseConfigurationImpl->checkAndApplyServerAddress("server0.local"); |
| 115 | databaseConfigurationImpl->checkAndApplyServerAddress("10.20.30.40:65535"); |
| 116 | const auto addresses(databaseConfigurationImpl->getServerAddresses(boost::none)); |
| 117 | const auto addresses0(databaseConfigurationImpl->getServerAddresses(0)); |
| 118 | const auto addresses1(databaseConfigurationImpl->getServerAddresses(1)); |
| 119 | EXPECT_EQ(2U, addresses.size()); |
| 120 | EXPECT_EQ(1U, addresses0.size()); |
| 121 | EXPECT_EQ(1U, addresses1.size()); |
| 122 | EXPECT_EQ("server0.local", addresses0.at(0).getHost()); |
| 123 | EXPECT_EQ(6379U, ntohs(addresses0.at(0).getPort())); |
| 124 | EXPECT_EQ("10.20.30.40", addresses1.at(0).getHost()); |
| 125 | EXPECT_EQ(65535U, ntohs(addresses1.at(0).getPort())); |
| 126 | } |
| 127 | |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 128 | TEST_F(DatabaseConfigurationImplTest, CanThrowIfIllegalDbTypeIsApplied) |
| 129 | { |
| 130 | EXPECT_THROW(databaseConfigurationImpl->checkAndApplyDbType("bad_db_type"), DatabaseConfiguration::InvalidDbType); |
| 131 | } |
| 132 | |
| 133 | TEST_F(DatabaseConfigurationImplTest, CanApplyIPv6AddressAndReturnIt) |
| 134 | { |
| 135 | databaseConfigurationImpl->checkAndApplyServerAddress("[2001::123]:12345"); |
| 136 | const auto retAddresses(databaseConfigurationImpl->getServerAddresses()); |
| 137 | EXPECT_EQ(1U, retAddresses.size()); |
| 138 | EXPECT_EQ("2001::123", retAddresses.at(0).getHost()); |
| 139 | EXPECT_EQ(12345U, ntohs(retAddresses.at(0).getPort())); |
| 140 | } |
| 141 | |
| 142 | TEST_F(DatabaseConfigurationImplTest, IsEmptyReturnsCorrectInformation) |
| 143 | { |
| 144 | EXPECT_TRUE(databaseConfigurationImpl->isEmpty()); |
| 145 | databaseConfigurationImpl->checkAndApplyServerAddress("[2001::123]:12345"); |
| 146 | EXPECT_FALSE(databaseConfigurationImpl->isEmpty()); |
| 147 | } |
Rolf Badorek | 2dcf940 | 2019-10-01 18:33:58 +0300 | [diff] [blame] | 148 | |
| 149 | TEST_F(DatabaseConfigurationImplTest, DefaultSentinelAddressIsNone) |
| 150 | { |
Petri Ovaska | ece6708 | 2021-04-15 11:08:13 +0300 | [diff] [blame] | 151 | EXPECT_EQ(boost::none, databaseConfigurationImpl->getSentinelAddress(boost::none)); |
Rolf Badorek | 2dcf940 | 2019-10-01 18:33:58 +0300 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | TEST_F(DatabaseConfigurationImplTest, CanApplyAndReturnSentinelAddress) |
| 155 | { |
Petri Ovaska | 5bdca62 | 2022-03-10 09:59:23 +0200 | [diff] [blame] | 156 | databaseConfigurationImpl->checkAndApplyServerAddress("dummydatabaseaddress.local:1234"); |
| 157 | databaseConfigurationImpl->checkAndApplySentinelPorts("51234"); |
| 158 | const auto serverAddresses(databaseConfigurationImpl->getServerAddresses()); |
| 159 | const auto address = databaseConfigurationImpl->getSentinelAddress(boost::none); |
Petri Ovaska | ece6708 | 2021-04-15 11:08:13 +0300 | [diff] [blame] | 160 | EXPECT_NE(boost::none, databaseConfigurationImpl->getSentinelAddress(boost::none)); |
Rolf Badorek | 2dcf940 | 2019-10-01 18:33:58 +0300 | [diff] [blame] | 161 | EXPECT_EQ("dummydatabaseaddress.local", address->getHost()); |
Petri Ovaska | 5bdca62 | 2022-03-10 09:59:23 +0200 | [diff] [blame] | 162 | EXPECT_EQ(51234, ntohs(address->getPort())); |
| 163 | EXPECT_EQ(1U, serverAddresses.size()); |
| 164 | EXPECT_EQ("dummydatabaseaddress.local", serverAddresses.at(0).getHost()); |
| 165 | EXPECT_EQ(1234U, ntohs(serverAddresses.at(0).getPort())); |
| 166 | } |
| 167 | |
| 168 | TEST_F(DatabaseConfigurationImplTest, CanApplyAndReturnSentinelAddressDefaultSentinelPort) |
| 169 | { |
| 170 | databaseConfigurationImpl->checkAndApplyServerAddress("dummydatabaseaddress.local:1234"); |
| 171 | databaseConfigurationImpl->checkAndApplySentinelPorts(""); |
| 172 | const auto serverAddresses(databaseConfigurationImpl->getServerAddresses()); |
| 173 | const auto address = databaseConfigurationImpl->getSentinelAddress(boost::none); |
| 174 | EXPECT_NE(boost::none, databaseConfigurationImpl->getSentinelAddress(boost::none)); |
| 175 | EXPECT_EQ("dummydatabaseaddress.local", address->getHost()); |
| 176 | EXPECT_EQ(26379, ntohs(address->getPort())); |
| 177 | EXPECT_EQ(1U, serverAddresses.size()); |
| 178 | EXPECT_EQ("dummydatabaseaddress.local", serverAddresses.at(0).getHost()); |
| 179 | EXPECT_EQ(1234U, ntohs(serverAddresses.at(0).getPort())); |
Rolf Badorek | 2dcf940 | 2019-10-01 18:33:58 +0300 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | TEST_F(DatabaseConfigurationImplTest, DefaultSentinelMasterNameIsEmpty) |
| 183 | { |
Petri Ovaska | 5bdca62 | 2022-03-10 09:59:23 +0200 | [diff] [blame] | 184 | EXPECT_EQ("dbaasmaster", databaseConfigurationImpl->getSentinelMasterName(boost::none)); |
Rolf Badorek | 2dcf940 | 2019-10-01 18:33:58 +0300 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | TEST_F(DatabaseConfigurationImplTest, CanApplyAndReturnSentinelMasterName) |
| 188 | { |
Petri Ovaska | 5bdca62 | 2022-03-10 09:59:23 +0200 | [diff] [blame] | 189 | databaseConfigurationImpl->checkAndApplySentinelMasterNames("mymaster"); |
| 190 | EXPECT_EQ("mymaster", databaseConfigurationImpl->getSentinelMasterName(boost::none)); |
Rolf Badorek | 2dcf940 | 2019-10-01 18:33:58 +0300 | [diff] [blame] | 191 | } |
Petri Ovaska | ece6708 | 2021-04-15 11:08:13 +0300 | [diff] [blame] | 192 | |
Petri Ovaska | 5bdca62 | 2022-03-10 09:59:23 +0200 | [diff] [blame] | 193 | TEST_F(DatabaseConfigurationImplTest, CanApplyAndReturnSentinelMasterNames) |
| 194 | { |
| 195 | databaseConfigurationImpl->checkAndApplySentinelMasterNames("mymaster-0,mymaster-1,mymaster-2"); |
| 196 | EXPECT_EQ("mymaster-0", databaseConfigurationImpl->getSentinelMasterName(0)); |
| 197 | EXPECT_EQ("mymaster-1", databaseConfigurationImpl->getSentinelMasterName(1)); |
| 198 | EXPECT_EQ("mymaster-2", databaseConfigurationImpl->getSentinelMasterName(2)); |
| 199 | } |
| 200 | |
| 201 | TEST_F(DatabaseConfigurationImplTest, CanApplyAndReturnDefaultSentinelMasterNamesWhenNotAllMasterNamesAreSet) |
| 202 | { |
| 203 | databaseConfigurationImpl->checkAndApplySentinelMasterNames("mymaster-0"); |
| 204 | EXPECT_EQ("mymaster-0", databaseConfigurationImpl->getSentinelMasterName(0)); |
| 205 | EXPECT_EQ("dbaasmaster", databaseConfigurationImpl->getSentinelMasterName(1)); |
| 206 | EXPECT_EQ("dbaasmaster", databaseConfigurationImpl->getSentinelMasterName(2)); |
| 207 | } |
| 208 | |
| 209 | TEST_F(DatabaseConfigurationImplTest, CanReturnSDLSentinelClusterAddresses) |
Petri Ovaska | ece6708 | 2021-04-15 11:08:13 +0300 | [diff] [blame] | 210 | { |
Petri Ovaska | 1c4a605 | 2021-05-31 14:06:32 +0300 | [diff] [blame] | 211 | databaseConfigurationImpl->checkAndApplyDbType("sdl-sentinel-cluster"); |
Petri Ovaska | ece6708 | 2021-04-15 11:08:13 +0300 | [diff] [blame] | 212 | databaseConfigurationImpl->checkAndApplyServerAddress("cluster-0.local"); |
| 213 | databaseConfigurationImpl->checkAndApplyServerAddress("cluster-1.local"); |
| 214 | databaseConfigurationImpl->checkAndApplyServerAddress("cluster-2.local"); |
Petri Ovaska | 5bdca62 | 2022-03-10 09:59:23 +0200 | [diff] [blame] | 215 | databaseConfigurationImpl->checkAndApplySentinelPorts("54321,54322,54323"); |
Petri Ovaska | ece6708 | 2021-04-15 11:08:13 +0300 | [diff] [blame] | 216 | auto address0 = databaseConfigurationImpl->getSentinelAddress(0); |
| 217 | auto address1 = databaseConfigurationImpl->getSentinelAddress(1); |
| 218 | auto address2 = databaseConfigurationImpl->getSentinelAddress(2); |
| 219 | EXPECT_NE(boost::none, databaseConfigurationImpl->getSentinelAddress(0)); |
| 220 | EXPECT_NE(boost::none, databaseConfigurationImpl->getSentinelAddress(1)); |
| 221 | EXPECT_NE(boost::none, databaseConfigurationImpl->getSentinelAddress(2)); |
| 222 | EXPECT_EQ("cluster-0.local", address0->getHost()); |
| 223 | EXPECT_EQ("cluster-1.local", address1->getHost()); |
| 224 | EXPECT_EQ("cluster-2.local", address2->getHost()); |
| 225 | EXPECT_EQ(54321, ntohs(address0->getPort())); |
Petri Ovaska | 5bdca62 | 2022-03-10 09:59:23 +0200 | [diff] [blame] | 226 | EXPECT_EQ(54322, ntohs(address1->getPort())); |
| 227 | EXPECT_EQ(54323, ntohs(address2->getPort())); |
Petri Ovaska | ece6708 | 2021-04-15 11:08:13 +0300 | [diff] [blame] | 228 | } |
| 229 | |
Petri Ovaska | 5bdca62 | 2022-03-10 09:59:23 +0200 | [diff] [blame] | 230 | TEST_F(DatabaseConfigurationImplTest, CanReturnSDLSentinelPorts) |
| 231 | { |
| 232 | databaseConfigurationImpl->checkAndApplyDbType("sdl-sentinel-cluster"); |
| 233 | databaseConfigurationImpl->checkAndApplyServerAddress("cluster-0.local"); |
| 234 | databaseConfigurationImpl->checkAndApplyServerAddress("cluster-1.local"); |
| 235 | databaseConfigurationImpl->checkAndApplyServerAddress("cluster-2.local"); |
| 236 | databaseConfigurationImpl->checkAndApplySentinelPorts("54321"); |
| 237 | auto address0 = databaseConfigurationImpl->getSentinelAddress(0); |
| 238 | auto address1 = databaseConfigurationImpl->getSentinelAddress(1); |
| 239 | auto address2 = databaseConfigurationImpl->getSentinelAddress(2); |
| 240 | EXPECT_NE(boost::none, databaseConfigurationImpl->getSentinelAddress(0)); |
| 241 | EXPECT_NE(boost::none, databaseConfigurationImpl->getSentinelAddress(1)); |
| 242 | EXPECT_NE(boost::none, databaseConfigurationImpl->getSentinelAddress(2)); |
| 243 | EXPECT_EQ("cluster-0.local", address0->getHost()); |
| 244 | EXPECT_EQ("cluster-1.local", address1->getHost()); |
| 245 | EXPECT_EQ("cluster-2.local", address2->getHost()); |
| 246 | EXPECT_EQ(54321, ntohs(address0->getPort())); |
| 247 | EXPECT_EQ(26379, ntohs(address1->getPort())); |
| 248 | EXPECT_EQ(26379, ntohs(address2->getPort())); |
| 249 | } |
| 250 | |
| 251 | TEST_F(DatabaseConfigurationImplTest, CanReturnDefaultSentinelPortForSDLClusterAddress) |
Petri Ovaska | ece6708 | 2021-04-15 11:08:13 +0300 | [diff] [blame] | 252 | { |
| 253 | databaseConfigurationImpl->checkAndApplyServerAddress("cluster-0.local"); |
Petri Ovaska | 5bdca62 | 2022-03-10 09:59:23 +0200 | [diff] [blame] | 254 | auto address = databaseConfigurationImpl->getSentinelAddress(boost::none); |
| 255 | EXPECT_NE(boost::none, databaseConfigurationImpl->getSentinelAddress(boost::none)); |
| 256 | EXPECT_EQ("cluster-0.local", address->getHost()); |
| 257 | EXPECT_EQ(26379, ntohs(address->getPort())); |
Petri Ovaska | ece6708 | 2021-04-15 11:08:13 +0300 | [diff] [blame] | 258 | } |