blob: df6dd0be0264082d9d77c29aa4369c196a7665c9 [file] [log] [blame]
Rolf Badorekef2bf512019-08-20 11:17:15 +03001/*
Petri Ovaska5bdca622022-03-10 09:59:23 +02002 Copyright (c) 2018-2022 Nokia.
Rolf Badorekef2bf512019-08-20 11:17:15 +03003
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 Tietavainena0745d22019-11-28 09:55:22 +020017/*
18 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 * platform project (RICP).
20*/
21
Rolf Badorekef2bf512019-08-20 11:17:15 +030022#include <gtest/gtest.h>
23#include <gmock/gmock.h>
24#include <arpa/inet.h>
25#include "private/databaseconfigurationimpl.hpp"
26
27using namespace shareddatalayer;
28using namespace testing;
29
30namespace
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
45TEST_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
53TEST_F(DatabaseConfigurationImplTest, CanReturnEmptyAddressListIfNoAddressesAreApplied)
54{
55 const auto retAddresses(databaseConfigurationImpl->getServerAddresses());
56 EXPECT_TRUE(retAddresses.empty());
57}
58
59TEST_F(DatabaseConfigurationImplTest, CanReturnUnknownTypeIfNoRedisDbTypeIsApplied)
60{
61 const auto retDbType(databaseConfigurationImpl->getDbType());
62 EXPECT_EQ(DatabaseConfiguration::DbType::UNKNOWN, retDbType);
63}
64
65TEST_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
72TEST_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 Badorek2dcf9402019-10-01 18:33:58 +030079TEST_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 Ovaska1c4a6052021-05-31 14:06:32 +030086TEST_F(DatabaseConfigurationImplTest, CanApplySdlStandaloneClusterDbTypeStringAndReturnType)
Petri Ovaskaece67082021-04-15 11:08:13 +030087{
Petri Ovaska1c4a6052021-05-31 14:06:32 +030088 databaseConfigurationImpl->checkAndApplyDbType("sdl-standalone-cluster");
Petri Ovaskaece67082021-04-15 11:08:13 +030089 const auto retDbType(databaseConfigurationImpl->getDbType());
Petri Ovaska1c4a6052021-05-31 14:06:32 +030090 EXPECT_EQ(DatabaseConfiguration::DbType::SDL_STANDALONE_CLUSTER, retDbType);
91}
92
93TEST_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 Ovaskaece67082021-04-15 11:08:13 +030098}
99
Rolf Badorekef2bf512019-08-20 11:17:15 +0300100TEST_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 Ovaska1c4a6052021-05-31 14:06:32 +0300112TEST_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 Badorekef2bf512019-08-20 11:17:15 +0300128TEST_F(DatabaseConfigurationImplTest, CanThrowIfIllegalDbTypeIsApplied)
129{
130 EXPECT_THROW(databaseConfigurationImpl->checkAndApplyDbType("bad_db_type"), DatabaseConfiguration::InvalidDbType);
131}
132
133TEST_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
142TEST_F(DatabaseConfigurationImplTest, IsEmptyReturnsCorrectInformation)
143{
144 EXPECT_TRUE(databaseConfigurationImpl->isEmpty());
145 databaseConfigurationImpl->checkAndApplyServerAddress("[2001::123]:12345");
146 EXPECT_FALSE(databaseConfigurationImpl->isEmpty());
147}
Rolf Badorek2dcf9402019-10-01 18:33:58 +0300148
149TEST_F(DatabaseConfigurationImplTest, DefaultSentinelAddressIsNone)
150{
Petri Ovaskaece67082021-04-15 11:08:13 +0300151 EXPECT_EQ(boost::none, databaseConfigurationImpl->getSentinelAddress(boost::none));
Rolf Badorek2dcf9402019-10-01 18:33:58 +0300152}
153
154TEST_F(DatabaseConfigurationImplTest, CanApplyAndReturnSentinelAddress)
155{
Petri Ovaska5bdca622022-03-10 09:59:23 +0200156 databaseConfigurationImpl->checkAndApplyServerAddress("dummydatabaseaddress.local:1234");
157 databaseConfigurationImpl->checkAndApplySentinelPorts("51234");
158 const auto serverAddresses(databaseConfigurationImpl->getServerAddresses());
159 const auto address = databaseConfigurationImpl->getSentinelAddress(boost::none);
Petri Ovaskaece67082021-04-15 11:08:13 +0300160 EXPECT_NE(boost::none, databaseConfigurationImpl->getSentinelAddress(boost::none));
Rolf Badorek2dcf9402019-10-01 18:33:58 +0300161 EXPECT_EQ("dummydatabaseaddress.local", address->getHost());
Petri Ovaska5bdca622022-03-10 09:59:23 +0200162 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
168TEST_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 Badorek2dcf9402019-10-01 18:33:58 +0300180}
181
182TEST_F(DatabaseConfigurationImplTest, DefaultSentinelMasterNameIsEmpty)
183{
Petri Ovaska5bdca622022-03-10 09:59:23 +0200184 EXPECT_EQ("dbaasmaster", databaseConfigurationImpl->getSentinelMasterName(boost::none));
Rolf Badorek2dcf9402019-10-01 18:33:58 +0300185}
186
187TEST_F(DatabaseConfigurationImplTest, CanApplyAndReturnSentinelMasterName)
188{
Petri Ovaska5bdca622022-03-10 09:59:23 +0200189 databaseConfigurationImpl->checkAndApplySentinelMasterNames("mymaster");
190 EXPECT_EQ("mymaster", databaseConfigurationImpl->getSentinelMasterName(boost::none));
Rolf Badorek2dcf9402019-10-01 18:33:58 +0300191}
Petri Ovaskaece67082021-04-15 11:08:13 +0300192
Petri Ovaska5bdca622022-03-10 09:59:23 +0200193TEST_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
201TEST_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
209TEST_F(DatabaseConfigurationImplTest, CanReturnSDLSentinelClusterAddresses)
Petri Ovaskaece67082021-04-15 11:08:13 +0300210{
Petri Ovaska1c4a6052021-05-31 14:06:32 +0300211 databaseConfigurationImpl->checkAndApplyDbType("sdl-sentinel-cluster");
Petri Ovaskaece67082021-04-15 11:08:13 +0300212 databaseConfigurationImpl->checkAndApplyServerAddress("cluster-0.local");
213 databaseConfigurationImpl->checkAndApplyServerAddress("cluster-1.local");
214 databaseConfigurationImpl->checkAndApplyServerAddress("cluster-2.local");
Petri Ovaska5bdca622022-03-10 09:59:23 +0200215 databaseConfigurationImpl->checkAndApplySentinelPorts("54321,54322,54323");
Petri Ovaskaece67082021-04-15 11:08:13 +0300216 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 Ovaska5bdca622022-03-10 09:59:23 +0200226 EXPECT_EQ(54322, ntohs(address1->getPort()));
227 EXPECT_EQ(54323, ntohs(address2->getPort()));
Petri Ovaskaece67082021-04-15 11:08:13 +0300228}
229
Petri Ovaska5bdca622022-03-10 09:59:23 +0200230TEST_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
251TEST_F(DatabaseConfigurationImplTest, CanReturnDefaultSentinelPortForSDLClusterAddress)
Petri Ovaskaece67082021-04-15 11:08:13 +0300252{
253 databaseConfigurationImpl->checkAndApplyServerAddress("cluster-0.local");
Petri Ovaska5bdca622022-03-10 09:59:23 +0200254 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 Ovaskaece67082021-04-15 11:08:13 +0300258}