1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138 | // Copyright (C) 2018-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Kea Hooks Basic
// Commercial End User License Agreement v2.0. See COPYING file in the premium/
// directory.
/// @file Contains tests that exercise the basic functionality of the
// class PgSqlStore. These tests are independent of the hooks framework.
#include <config.h>
#include <exceptions/exceptions.h>
#include <dhcpsrv/testutils/test_utils.h>
#include <pgsql/testutils/pgsql_schema.h>
#include <test_utils.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <testutils/gtest_utils.h>
#include <testutils/log_utils.h>
#include <testutils/multi_threading_utils.h>
#include <gtest/gtest.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <boost/lexical_cast.hpp><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <cmath><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
using namespace std;
using namespace isc;
using namespace isc::data;
using namespace isc::db;
using namespace isc::db::test;
using namespace isc::dhcp::test;
using namespace isc::legal_log;
using namespace isc::test;
using namespace boost::posix_time;
namespace ph = std::placeholders;
namespace {
/// @brief Test fixture for testing PostgreSQL database backend.
class PgSqlTest : public isc::dhcp::test::LogContentTest, public runSQL {
public:
/// @brief Store pointer
typedef boost::shared_ptr<PgSqlStore> PgSqlStorePtr;
/// @brief Constructor
PgSqlTest() {
// Ensure we have the proper schema with no transient data.
isc::db::test::createPgSQLSchema();
// Change this to true if you need extra information about logging
// checks to be printed.
logCheckVerbose(false);
}
/// @brief Destructor
virtual ~PgSqlTest() {
// If data wipe enabled, delete transient data otherwise destroy the schema
isc::db::test::destroyPgSQLSchema();
}
/// @brief Process output
///
/// Remove heading and trailing lines, trim column, etc
///
/// @param output reference to the string vector to fill with output
/// @return true if processing was successful
virtual bool getOutput(vector<string>& output) {
const vector<string>& raw = getRawOutput();
for (auto const& it : raw) {
if (it.empty()) {
continue;
}
if (*(it.crbegin()) == '\n') {
output.push_back(it.substr(0, it.length() - 1));
} else {
output.push_back(string(it));
}
}
#if 0
cerr << "OUTPUT" << endl;
for (auto const& it : output) {
cerr << it << endl;
}
#endif
return (true);
}
/// @brief Open the store
void openStore() {
// Construct the store
DatabaseConnection::ParameterMap params;
params["name"] = "keatest";
params["user"] = "keatest";
params["password"] = "keatest";
ASSERT_NO_THROW(store_.reset(new PgSqlStore(params)));
// Open the database
ASSERT_NO_THROW(store_->open());
}
/// @brief Close the store
void closeStore() {
// Close does nothing
EXPECT_NO_THROW(store_->close());
// Destructor close the database
EXPECT_NO_THROW(store_.reset());
}
/// @brief Fill some log entries
void fillStore() {
ASSERT_EQ(samples_.size(), addresses_.size());
for (size_t i = 0; i < samples_.size(); ++i) {
store_->writeln(samples_[i], addresses_[i]);
}
}
/// @brief The store
PgSqlStorePtr store_;
/// @brief Addresses samples
const vector<string> addresses_ = {
"foo",
"bar",
"",
"192.2.1.100",
"192.2.1.100",
"2001:db8:1::",
"2001:db8:1::",
"2001:0db8:0001:0002:0123:4567:89ab:cdef/128"
};
/// @brief Log entry samples
const vector<string> samples_ = {
"first log",
"another log",
"again a log",
"Address: 192.2.1.100 has been renewed",
"Address: 192.2.1.100 has been renewed "
"for 1 hrs 52 min 15 secs to a device with hardware address: "
"hwtype=1 08:00:2b:02:3f:4e, client-id: 17:34:e2:ff:09:92:54 "
"connected via relay at address: 192.2.16.33, identified by "
"circuit-id: 68:6f:77:64:79 (howdy) and remote-id: 87:f6:79:77:ef",
"Address: 2001:db8:1:: has been assigned",
"Address: 2001:db8:1:: has been assigned "
"for 0 hrs 11 mins 53 secs to a device with DUID: "
"17:34:e2:ff:09:92:54 and hardware address: hwtype=1 "
"08:00:2b:02:3f:4e (from Raw Socket) connected via relay at "
"address: fe80::abcd for client on link address: 3001::1, hop "
"count: 1, identified by remote-id: 01:02:03:04:0a:0b:0c:0d:0e:0f "
"and subscriber-id: 1a:2b:3c:4d:5e:6f",
"last log"
};
};
/// @brief Tests the PgSqlStore constructor.
TEST_F(PgSqlTest, invalidConstruction) {
// Verify that a PgSqlStore with no database name is rejected.
DatabaseConnection::ParameterMap params;
params["user"] = "keatest";
params["password"] = "keatest";
ASSERT_NO_THROW(store_.reset(new PgSqlStore(params)));
// Check params validity is done by open().
EXPECT_THROW(store_->open(), NoDatabaseName);
// Verify that a PgSqlStore with an invalid connect-timeout is rejected.
params["name"] = "keatest";
// 2^64 should be greater than INT_MAX
params["connect-timeout"] = "18446744073709551616";
ASSERT_NO_THROW(store_.reset(new PgSqlStore(params)));
EXPECT_THROW(store_->open(), DbInvalidTimeout);
}
/// @brief Tests opening and closing PgSqlStore
TEST_F(PgSqlTest, open) {
// Construct the store_
DatabaseConnection::ParameterMap params;
params["name"] = "keatest";
params["user"] = "keatest";
params["password"] = "keatest";
ASSERT_NO_THROW(store_.reset(new PgSqlStore(params)));
// Check the type is postgresql
EXPECT_EQ("postgresql", store_->getType());
// Open the database
ASSERT_NO_THROW(store_->open());
// Close does nothing
EXPECT_NO_THROW(store_->close());
// Destructor close the database
EXPECT_NO_THROW(store_.reset());
}
/// @brief Check schema version
TEST_F(PgSqlTest, version) {
// Open the store
openStore();
// Check version using the API
std::pair<uint32_t, uint32_t> version;
EXPECT_NO_THROW(version = store_->getVersion());
EXPECT_EQ(PGSQL_SCHEMA_VERSION_MAJOR, version.first);
EXPECT_EQ(PGSQL_SCHEMA_VERSION_MINOR, version.second);
// Close the store
closeStore();
// Check version using pgsql command
setenv("PGPASSWORD", "keatest", 0);
setQuery("SELECT version, minor FROM schema_version");
setCommand("psql --set ON_ERROR_STOP=1 -A -t -h localhost -q "
"-U keatest -d keatest -c ");
EXPECT_NO_THROW(execute());
EXPECT_EQ(0, getResult());
vector<string> output;
EXPECT_TRUE(getOutput(output));
ASSERT_FALSE(output.empty());
EXPECT_EQ(1, output.size());
ostringstream s;
s << version.first << "|" << version.second;
EXPECT_EQ(s.str(), output[0]);
// Check that the debug output was correct. Add the strings
// to the test vector in the class and then call checkFile
// for comparison
addString("LEGAL_LOG_PGSQL_GET_VERSION "
"obtaining schema version information");
EXPECT_TRUE(checkFile());
}
/// @brief Check addresses
TEST_F(PgSqlTest, addresses) {
// Open the store
openStore();
// Fill the store
fillStore();
// Close the store
closeStore();
setenv("PGPASSWORD", "keatest", 0);
setQuery("SELECT address FROM logs");
setCommand("psql --set ON_ERROR_STOP=1 -A -t -h localhost -q "
"-U keatest -d keatest -c ");
EXPECT_NO_THROW(execute());
EXPECT_EQ(0, getResult());
vector<string> output;
EXPECT_TRUE(getOutput(output));
ASSERT_EQ(addresses_.size(), output.size());
for (size_t i = 0; i < output.size(); ++i) {
EXPECT_EQ(addresses_[i], output[i]);
}
}
/// @brief Check log entries
TEST_F(PgSqlTest, entries) {
// Open the store
openStore();
// Fill the store
fillStore();
// Close the store
closeStore();
setenv("PGPASSWORD", "keatest", 0);
setQuery("SELECT log FROM logs");
setCommand("psql --set ON_ERROR_STOP=1 -A -t -h localhost -q "
"-U keatest -d keatest -c ");
EXPECT_NO_THROW(execute());
EXPECT_EQ(0, getResult());
vector<string> output;
EXPECT_TRUE(getOutput(output));
ASSERT_EQ(samples_.size(), output.size());
for (size_t i = 0; i < output.size(); ++i) {
EXPECT_EQ(samples_[i], output[i]);
}
}
/// @brief Check timestamps
TEST_F(PgSqlTest, timestamps) {
// Open the store
openStore();
// Get begin date
ptime begin_p = microsec_clock::universal_time();
// Fill the store
fillStore();
// Get end date
ptime end_p = microsec_clock::universal_time();
ASSERT_LE(begin_p, end_p);
// Close the store
closeStore();
setenv("PGPASSWORD", "keatest", 0);
setQuery("SELECT EXTRACT(epoch FROM timestamp) FROM logs");
setCommand("psql --set ON_ERROR_STOP=1 -A -t -h localhost -q "
"-U keatest -d keatest -c ");
EXPECT_NO_THROW(execute());
EXPECT_EQ(0, getResult());
vector<string> output;
EXPECT_TRUE(getOutput(output));
ASSERT_EQ(samples_.size(), output.size());
ptime current = begin_p;
for (size_t i = 0; i < output.size(); ++i) {
// Get the timestamp as a double from epoch
double ts_d = 0.0;
ASSERT_NO_THROW(ts_d = boost::lexical_cast<double>(output[i]));
// Extract seconds and microseconds;
double ts_s;
double ts_ms = modf(ts_d, &ts_s) * 1000000.;
// Get the timestamp in posix time format (will work after 20138?)
ptime ts_p = from_time_t(static_cast<time_t>(ts_s)) +
microseconds(static_cast<long>(ts_ms));
EXPECT_LE(current, ts_p);
current = ts_p;
}
EXPECT_LE(current, end_p);
}
/// @brief Check oversized address
TEST_F(PgSqlTest, oversized) {
// Open the store
openStore();
// Check what happen with an oversized address
string address("2001:0db8:0001:0002:0123:4567:89ab:cdef/128!");
EXPECT_THROW(store_->writeln("oversized address", address),
DbOperationError);
}
class PgSqlLegalLogDbLostCallbackTest : public LegalLogDbLostCallbackTest {
public:
/// @brief Constructor.
PgSqlLegalLogDbLostCallbackTest() { }
/// @brief Destructor.
virtual ~PgSqlLegalLogDbLostCallbackTest() { }
/// @brief Prepares the class for a test.
///
/// Invoked by gtest prior test entry, we create the
/// appropriate schema and create a basic DB manager to
/// wipe out any prior instance
virtual void SetUp() {
// Ensure we have the proper schema with no transient data.
createPgSQLSchema();
}
/// @brief Pre-text exit clean up
///
/// Invoked by gtest upon test exit, we destroy the schema
/// we created.
virtual void TearDown() {
// If data wipe enabled, delete transient data otherwise destroy the schema
destroyPgSQLSchema();
}
/// @brief Method which returns the back end specific connection
/// string
virtual std::string validConnectString() {
return (validPgSQLConnectionString());
}
/// @brief Method which returns invalid back end specific connection
/// string
virtual std::string invalidConnectString() {
return (connectionString(PGSQL_VALID_TYPE, INVALID_NAME, VALID_HOST,
VALID_USER, VALID_PASSWORD));
}
/// @brief Verifies the Backend Store behavior if DB connection can not be
/// established but succeeds on retry
///
/// This function creates a Backend Store with a back end that supports
/// connectivity lost callback. It verifies that connectivity is unavailable
/// and then recovered on retry:
/// -# The registered DbLostCallback was invoked
/// -# The registered DbRecoveredCallback was invoked
virtual void testRetryOpenDbLostAndRecoveredCallback();
/// @brief Verifies the Backend Store behavior if DB connection can not be
/// established but fails on retry
///
/// This function creates a Backend Store with a back end that supports
/// connectivity lost callback. It verifies that connectivity is unavailable
/// and then fails again on retry:
/// -# The registered DbLostCallback was invoked
/// -# The registered DbFailedCallback was invoked
virtual void testRetryOpenDbLostAndFailedCallback();
/// @brief Verifies the Backend Store behavior if DB connection can not be
/// established but succeeds on retry
///
/// This function creates a Backend Store with a back end that supports
/// connectivity lost callback. It verifies that connectivity is unavailable
/// and then recovered on retry:
/// -# The registered DbLostCallback was invoked
/// -# The registered DbRecoveredCallback was invoked after two reconnect
/// attempts (once failing and second triggered by timer)
virtual void testRetryOpenDbLostAndRecoveredAfterTimeoutCallback();
/// @brief Verifies the Backend Store behavior if DB connection can not be
/// established but fails on retry
///
/// This function creates a Backend Store with a back end that supports
/// connectivity lost callback. It verifies that connectivity is unavailable
/// and then fails again on retry:
/// -# The registered DbLostCallback was invoked
/// -# The registered DbFailedCallback was invoked after two reconnect
/// attempts (once failing and second triggered by timer)
virtual void testRetryOpenDbLostAndFailedAfterTimeoutCallback();
/// @brief Verifies open failures do NOT invoke db lost callback
///
/// The db lost callback should only be invoked after successfully
/// opening the DB and then subsequently losing it. Failing to
/// open should be handled directly by the application layer.
virtual void testNoCallbackOnOpenFailure();
/// @brief Verifies the Backend Store behavior if DB connection is lost
///
/// This function creates a Backend Store with a back end that supports
/// connectivity lost callback. It verifies connectivity by issuing a known
/// valid query. Next it simulates connectivity lost by identifying and
/// closing the socket connection to the Backend Store. It then reissues the
/// query and verifies that:
/// -# The Query throws DbOperationError (rather than exiting)
/// -# The registered DbLostCallback was invoked
/// -# The registered DbRecoveredCallback was invoked
virtual void testDbLostAndRecoveredCallback();
/// @brief Verifies the Backend Store behavior if DB connection is lost
///
/// This function creates a Backend Store with a back end that supports
/// connectivity lost callback. It verifies connectivity by issuing a known
/// valid query. Next it simulates connectivity lost by identifying and
/// closing the socket connection to the Backend Store. It then reissues the
/// query and verifies that:
/// -# The Query throws DbOperationError (rather than exiting)
/// -# The registered DbLostCallback was invoked
/// -# The registered DbFailedCallback was invoked
virtual void testDbLostAndFailedCallback();
/// @brief Verifies the Backend Store behavior if DB connection is lost
///
/// This function creates a Backend Store with a back end that supports
/// connectivity lost callback. It verifies connectivity by issuing a known
/// valid query. Next it simulates connectivity lost by identifying and
/// closing the socket connection to the Backend Store. It then reissues the
/// query and verifies that:
/// -# The Query throws DbOperationError (rather than exiting)
/// -# The registered DbLostCallback was invoked
/// -# The registered DbRecoveredCallback was invoked after two reconnect
/// attempts (once failing and second triggered by timer)
virtual void testDbLostAndRecoveredAfterTimeoutCallback();
/// @brief Verifies the Backend Store behavior if DB connection is lost
///
/// This function creates a Backend Store with a back end that supports
/// connectivity lost callback. It verifies connectivity by issuing a known
/// valid query. Next it simulates connectivity lost by identifying and
/// closing the socket connection to the Backend Store. It then reissues the
/// query and verifies that:
/// -# The Query throws DbOperationError (rather than exiting)
/// -# The registered DbLostCallback was invoked
/// -# The registered DbFailedCallback was invoked after two reconnect
/// attempts (once failing and second triggered by timer)
virtual void testDbLostAndFailedAfterTimeoutCallback();
};
void
PgSqlLegalLogDbLostCallbackTest::testRetryOpenDbLostAndRecoveredCallback() {
// Set the connectivity lost callback.
isc::db::DatabaseConnection::db_lost_callback_ =
std::bind(&LegalLogDbLostCallbackTest::db_lost_callback, this, ph::_1);
// Set the connectivity recovered callback.
isc::db::DatabaseConnection::db_recovered_callback_ =
std::bind(&LegalLogDbLostCallbackTest::db_recovered_callback, this, ph::_1);
// Set the connectivity failed callback.
isc::db::DatabaseConnection::db_failed_callback_ =
std::bind(&LegalLogDbLostCallbackTest::db_failed_callback, this, ph::_1);
// Verify that a PgSqlStore with no database name is rejected.
DatabaseConnection::ParameterMap params = db::DatabaseConnection::parse(invalidConnectString());
std::shared_ptr<DbConnectionInitWithRetry> dbr(new DbConnectionInitWithRetry());
params.emplace("retry-on-startup", "true");
ASSERT_NO_THROW(BackendStore::instance().reset(new PgSqlStore(params)));
// Check params validity is done by open().
EXPECT_THROW(BackendStore::instance()->open(), DbOpenErrorWithRetry);
// Verify there is no instance.
ASSERT_FALSE(BackendStore::instance());
dbr.reset();
params = db::DatabaseConnection::parse(validConnectString());
params.emplace("retry-on-startup", "true");
BackendStore::setParameters(params);
io_service_->poll();
// Our lost and recovered connectivity callback should have been invoked.
EXPECT_EQ(1, db_lost_callback_called_);
EXPECT_EQ(1, db_recovered_callback_called_);
EXPECT_EQ(0, db_failed_callback_called_);
ASSERT_TRUE(BackendStore::instance());
}
void
PgSqlLegalLogDbLostCallbackTest::testRetryOpenDbLostAndFailedCallback() {
// Set the connectivity lost callback.
isc::db::DatabaseConnection::db_lost_callback_ =
std::bind(&LegalLogDbLostCallbackTest::db_lost_callback, this, ph::_1);
// Set the connectivity recovered callback.
isc::db::DatabaseConnection::db_recovered_callback_ =
std::bind(&LegalLogDbLostCallbackTest::db_recovered_callback, this, ph::_1);
// Set the connectivity failed callback.
isc::db::DatabaseConnection::db_failed_callback_ =
std::bind(&LegalLogDbLostCallbackTest::db_failed_callback, this, ph::_1);
// Verify that a PgSqlStore with no database name is rejected.
DatabaseConnection::ParameterMap params = db::DatabaseConnection::parse(invalidConnectString());
std::shared_ptr<DbConnectionInitWithRetry> dbr(new DbConnectionInitWithRetry());
params.emplace("retry-on-startup", "true");
ASSERT_NO_THROW(BackendStore::instance().reset(new PgSqlStore(params)));
// Check params validity is done by open().
EXPECT_THROW(BackendStore::instance()->open(), DbOpenErrorWithRetry);
// Verify there is no instance.
ASSERT_FALSE(BackendStore::instance());
dbr.reset();
io_service_->poll();
// Our lost and recovered connectivity callback should have been invoked.
EXPECT_EQ(1, db_lost_callback_called_);
EXPECT_EQ(0, db_recovered_callback_called_);
EXPECT_EQ(1, db_failed_callback_called_);
ASSERT_FALSE(BackendStore::instance());
}
void
PgSqlLegalLogDbLostCallbackTest::testRetryOpenDbLostAndRecoveredAfterTimeoutCallback() {
// Set the connectivity lost callback.
isc::db::DatabaseConnection::db_lost_callback_ =
std::bind(&LegalLogDbLostCallbackTest::db_lost_callback, this, ph::_1);
// Set the connectivity recovered callback.
isc::db::DatabaseConnection::db_recovered_callback_ =
std::bind(&LegalLogDbLostCallbackTest::db_recovered_callback, this, ph::_1);
// Set the connectivity failed callback.
isc::db::DatabaseConnection::db_failed_callback_ =
std::bind(&LegalLogDbLostCallbackTest::db_failed_callback, this, ph::_1);
std::string access = invalidConnectString();
std::string extra = " max-reconnect-tries=3 reconnect-wait-time=1";
access += extra;
// Verify that a PgSqlStore with no database name is rejected.
DatabaseConnection::ParameterMap params = db::DatabaseConnection::parse(access);
std::shared_ptr<DbConnectionInitWithRetry> dbr(new DbConnectionInitWithRetry());
params.emplace("retry-on-startup", "true");
ASSERT_NO_THROW(BackendStore::instance().reset(new PgSqlStore(params)));
// Check params validity is done by open().
EXPECT_THROW(BackendStore::instance()->open(), DbOpenErrorWithRetry);
// Verify there is no instance.
ASSERT_FALSE(BackendStore::instance());
dbr.reset();
io_service_->poll();
// Our lost connectivity callback should have been invoked.
EXPECT_EQ(1, db_lost_callback_called_);
EXPECT_EQ(0, db_recovered_callback_called_);
EXPECT_EQ(0, db_failed_callback_called_);
ASSERT_FALSE(BackendStore::instance());
access = validConnectString();
access += extra;
params = db::DatabaseConnection::parse(access);
params.emplace("retry-on-startup", "true");
BackendStore::setParameters(params);
sleep(1);
io_service_->poll();
// Our lost and recovered connectivity callback should have been invoked.
EXPECT_EQ(2, db_lost_callback_called_);
EXPECT_EQ(1, db_recovered_callback_called_);
EXPECT_EQ(0, db_failed_callback_called_);
ASSERT_TRUE(BackendStore::instance());
sleep(1);
io_service_->poll();
// No callback should have been invoked.
EXPECT_EQ(2, db_lost_callback_called_);
EXPECT_EQ(1, db_recovered_callback_called_);
EXPECT_EQ(0, db_failed_callback_called_);
ASSERT_TRUE(BackendStore::instance());
}
void
PgSqlLegalLogDbLostCallbackTest::testRetryOpenDbLostAndFailedAfterTimeoutCallback() {
// Set the connectivity lost callback.
isc::db::DatabaseConnection::db_lost_callback_ =
std::bind(&LegalLogDbLostCallbackTest::db_lost_callback, this, ph::_1);
// Set the connectivity recovered callback.
isc::db::DatabaseConnection::db_recovered_callback_ =
std::bind(&LegalLogDbLostCallbackTest::db_recovered_callback, this, ph::_1);
// Set the connectivity failed callback.
isc::db::DatabaseConnection::db_failed_callback_ =
std::bind(&LegalLogDbLostCallbackTest::db_failed_callback, this, ph::_1);
std::string access = invalidConnectString();
std::string extra = " max-reconnect-tries=3 reconnect-wait-time=1";
access += extra;
// Verify that a PgSqlStore with no database name is rejected.
DatabaseConnection::ParameterMap params = db::DatabaseConnection::parse(access);
std::shared_ptr<DbConnectionInitWithRetry> dbr(new DbConnectionInitWithRetry());
params.emplace("retry-on-startup", "true");
ASSERT_NO_THROW(BackendStore::instance().reset(new PgSqlStore(params)));
// Check params validity is done by open().
EXPECT_THROW(BackendStore::instance()->open(), DbOpenErrorWithRetry);
// Verify there is no instance.
ASSERT_FALSE(BackendStore::instance());
dbr.reset();
io_service_->poll();
// Our lost connectivity callback should have been invoked.
EXPECT_EQ(1, db_lost_callback_called_);
EXPECT_EQ(0, db_recovered_callback_called_);
EXPECT_EQ(0, db_failed_callback_called_);
ASSERT_FALSE(BackendStore::instance());
sleep(1);
io_service_->poll();
// Our lost connectivity callback should have been invoked.
EXPECT_EQ(2, db_lost_callback_called_);
EXPECT_EQ(0, db_recovered_callback_called_);
EXPECT_EQ(0, db_failed_callback_called_);
ASSERT_FALSE(BackendStore::instance());
sleep(1);
io_service_->poll();
// Our lost and failed connectivity callback should have been invoked.
EXPECT_EQ(3, db_lost_callback_called_);
EXPECT_EQ(0, db_recovered_callback_called_);
EXPECT_EQ(1, db_failed_callback_called_);
ASSERT_FALSE(BackendStore::instance());
}
void
PgSqlLegalLogDbLostCallbackTest::testNoCallbackOnOpenFailure() {
// Set the connectivity lost callback.
isc::db::DatabaseConnection::db_lost_callback_ =
std::bind(&LegalLogDbLostCallbackTest::db_lost_callback, this, ph::_1);
// Set the connectivity recovered callback.
isc::db::DatabaseConnection::db_recovered_callback_ =
std::bind(&LegalLogDbLostCallbackTest::db_recovered_callback, this, ph::_1);
// Set the connectivity failed callback.
isc::db::DatabaseConnection::db_failed_callback_ =
std::bind(&LegalLogDbLostCallbackTest::db_failed_callback, this, ph::_1);
// Verify that a PgSqlStore with no database name is rejected.
DatabaseConnection::ParameterMap params = db::DatabaseConnection::parse(invalidConnectString());
ASSERT_NO_THROW(BackendStore::instance().reset(new PgSqlStore(params)));
// Check params validity is done by open().
EXPECT_THROW(BackendStore::instance()->open(), DbOpenError);
io_service_->poll();
EXPECT_EQ(0, db_lost_callback_called_);
EXPECT_EQ(0, db_recovered_callback_called_);
EXPECT_EQ(0, db_failed_callback_called_);
ASSERT_FALSE(BackendStore::instance());
}
void
PgSqlLegalLogDbLostCallbackTest::testDbLostAndRecoveredCallback() {
// Set the connectivity lost callback.
isc::db::DatabaseConnection::db_lost_callback_ =
std::bind(&LegalLogDbLostCallbackTest::db_lost_callback, this, ph::_1);
// Set the connectivity recovered callback.
isc::db::DatabaseConnection::db_recovered_callback_ =
std::bind(&LegalLogDbLostCallbackTest::db_recovered_callback, this, ph::_1);
// Set the connectivity failed callback.
isc::db::DatabaseConnection::db_failed_callback_ =
std::bind(&LegalLogDbLostCallbackTest::db_failed_callback, this, ph::_1);
// Find the most recently opened socket. Our SQL client's socket should
// be the next one.
int last_open_socket = findLastSocketFd();
// Fill holes.
FillFdHoles holes(last_open_socket);
// Verify that a PgSqlStore with database name is not rejected.
DatabaseConnection::ParameterMap params = db::DatabaseConnection::parse(validConnectString());
ASSERT_NO_THROW(BackendStore::instance().reset(new PgSqlStore(params)));
// Check params validity is done by open().
EXPECT_NO_THROW(BackendStore::instance()->open());
ASSERT_TRUE(BackendStore::instance());
// Find the SQL client socket.
int sql_socket = findLastSocketFd();
ASSERT_TRUE(sql_socket > last_open_socket);
// Verify we can execute a query. We don't care about the answer.
ASSERT_NO_THROW(BackendStore::instance()->writeln("test", "192.2.1.100"));
// Now close the sql socket out from under backend client
ASSERT_EQ(0, close(sql_socket));
// A query should fail with DbConnectionUnusable.
ASSERT_THROW(BackendStore::instance()->writeln("test", "192.2.1.101");,<--- There is an unknown macro here somewhere. Configuration is required. If ASSERT_THROW is a macro then please configure it.
DbConnectionUnusable);
ASSERT_TRUE(BackendStore::instance());
io_service_->poll();
// Our lost and recovered connectivity callback should have been invoked.
EXPECT_EQ(1, db_lost_callback_called_);
EXPECT_EQ(1, db_recovered_callback_called_);
EXPECT_EQ(0, db_failed_callback_called_);
ASSERT_TRUE(BackendStore::instance());
}
void
PgSqlLegalLogDbLostCallbackTest::testDbLostAndFailedCallback() {
// Set the connectivity lost callback.
isc::db::DatabaseConnection::db_lost_callback_ =
std::bind(&LegalLogDbLostCallbackTest::db_lost_callback, this, ph::_1);
// Set the connectivity recovered callback.
isc::db::DatabaseConnection::db_recovered_callback_ =
std::bind(&LegalLogDbLostCallbackTest::db_recovered_callback, this, ph::_1);
// Set the connectivity failed callback.
isc::db::DatabaseConnection::db_failed_callback_ =
std::bind(&LegalLogDbLostCallbackTest::db_failed_callback, this, ph::_1);
// Find the most recently opened socket. Our SQL client's socket should
// be the next one.
int last_open_socket = findLastSocketFd();
// Fill holes.
FillFdHoles holes(last_open_socket);
// Verify that a PgSqlStore with database name is not rejected.
DatabaseConnection::ParameterMap params = db::DatabaseConnection::parse(validConnectString());
ASSERT_NO_THROW(BackendStore::instance().reset(new PgSqlStore(params)));
// Check params validity is done by open().
EXPECT_NO_THROW(BackendStore::instance()->open());
ASSERT_TRUE(BackendStore::instance());
// Find the SQL client socket.
int sql_socket = findLastSocketFd();
ASSERT_TRUE(sql_socket > last_open_socket);
// Verify we can execute a query. We don't care about the answer.
ASSERT_NO_THROW(BackendStore::instance()->writeln("test", "192.2.1.100"));
params = db::DatabaseConnection::parse(invalidConnectString());
BackendStore::setParameters(params);
// Now close the sql socket out from under backend client
ASSERT_EQ(0, close(sql_socket));
// A query should fail with DbConnectionUnusable.
ASSERT_THROW(BackendStore::instance()->writeln("test", "192.2.1.101"),
DbConnectionUnusable);
ASSERT_TRUE(BackendStore::instance());
io_service_->poll();
// Our lost and failed connectivity callback should have been invoked.
EXPECT_EQ(1, db_lost_callback_called_);
EXPECT_EQ(0, db_recovered_callback_called_);
EXPECT_EQ(1, db_failed_callback_called_);
ASSERT_FALSE(BackendStore::instance());
}
void
PgSqlLegalLogDbLostCallbackTest::testDbLostAndRecoveredAfterTimeoutCallback() {
// Set the connectivity lost callback.
isc::db::DatabaseConnection::db_lost_callback_ =
std::bind(&LegalLogDbLostCallbackTest::db_lost_callback, this, ph::_1);
// Set the connectivity recovered callback.
isc::db::DatabaseConnection::db_recovered_callback_ =
std::bind(&LegalLogDbLostCallbackTest::db_recovered_callback, this, ph::_1);
// Set the connectivity failed callback.
isc::db::DatabaseConnection::db_failed_callback_ =
std::bind(&LegalLogDbLostCallbackTest::db_failed_callback, this, ph::_1);
std::string access = validConnectString();
std::string extra = " max-reconnect-tries=3 reconnect-wait-time=1";
access += extra;
// Find the most recently opened socket. Our SQL client's socket should
// be the next one.
int last_open_socket = findLastSocketFd();
// Fill holes.
FillFdHoles holes(last_open_socket);
// Verify that a PgSqlStore with database name is not rejected.
DatabaseConnection::ParameterMap params = db::DatabaseConnection::parse(access);
ASSERT_NO_THROW(BackendStore::instance().reset(new PgSqlStore(params)));
// Check params validity is done by open().
EXPECT_NO_THROW(BackendStore::instance()->open());
ASSERT_TRUE(BackendStore::instance());
// Find the SQL client socket.
int sql_socket = findLastSocketFd();
ASSERT_TRUE(sql_socket > last_open_socket);
// Verify we can execute a query. We don't care about the answer.
ASSERT_NO_THROW(BackendStore::instance()->writeln("test", "192.2.1.100"));
access = invalidConnectString();
access += extra;
params = db::DatabaseConnection::parse(access);
BackendStore::setParameters(params);
// Now close the sql socket out from under backend client
ASSERT_EQ(0, close(sql_socket));
// A query should fail with DbConnectionUnusable.
ASSERT_THROW(BackendStore::instance()->writeln("test", "192.2.1.101"),
DbConnectionUnusable);
ASSERT_TRUE(BackendStore::instance());
io_service_->poll();
// Our lost connectivity callback should have been invoked.
EXPECT_EQ(1, db_lost_callback_called_);
EXPECT_EQ(0, db_recovered_callback_called_);
EXPECT_EQ(0, db_failed_callback_called_);
ASSERT_FALSE(BackendStore::instance());
access = validConnectString();
access += extra;
params = db::DatabaseConnection::parse(access);
BackendStore::setParameters(params);
sleep(1);
io_service_->poll();
// Our lost and recovered connectivity callback should have been invoked.
EXPECT_EQ(2, db_lost_callback_called_);
EXPECT_EQ(1, db_recovered_callback_called_);
EXPECT_EQ(0, db_failed_callback_called_);
ASSERT_TRUE(BackendStore::instance());
sleep(1);
io_service_->poll();
// No callback should have been invoked.
EXPECT_EQ(2, db_lost_callback_called_);
EXPECT_EQ(1, db_recovered_callback_called_);
EXPECT_EQ(0, db_failed_callback_called_);
ASSERT_TRUE(BackendStore::instance());
}
void
PgSqlLegalLogDbLostCallbackTest::testDbLostAndFailedAfterTimeoutCallback() {
// Set the connectivity lost callback.
isc::db::DatabaseConnection::db_lost_callback_ =
std::bind(&LegalLogDbLostCallbackTest::db_lost_callback, this, ph::_1);
// Set the connectivity recovered callback.
isc::db::DatabaseConnection::db_recovered_callback_ =
std::bind(&LegalLogDbLostCallbackTest::db_recovered_callback, this, ph::_1);
// Set the connectivity failed callback.
isc::db::DatabaseConnection::db_failed_callback_ =
std::bind(&LegalLogDbLostCallbackTest::db_failed_callback, this, ph::_1);
std::string access = validConnectString();
std::string extra = " max-reconnect-tries=3 reconnect-wait-time=1";
access += extra;
// Find the most recently opened socket. Our SQL client's socket should
// be the next one.
int last_open_socket = findLastSocketFd();
// Fill holes.
FillFdHoles holes(last_open_socket);
// Verify that a PgSqlStore with database name is not rejected.
DatabaseConnection::ParameterMap params = db::DatabaseConnection::parse(access);
ASSERT_NO_THROW(BackendStore::instance().reset(new PgSqlStore(params)));
// Check params validity is done by open().
EXPECT_NO_THROW(BackendStore::instance()->open());
ASSERT_TRUE(BackendStore::instance());
// Find the SQL client socket.
int sql_socket = findLastSocketFd();
ASSERT_TRUE(sql_socket > last_open_socket);
// Verify we can execute a query. We don't care about the answer.
ASSERT_NO_THROW(BackendStore::instance()->writeln("test", "192.2.1.100"));
access = invalidConnectString();
access += extra;
params = db::DatabaseConnection::parse(access);
BackendStore::setParameters(params);
// Now close the sql socket out from under backend client
ASSERT_EQ(0, close(sql_socket));
// A query should fail with DbConnectionUnusable.
ASSERT_THROW(BackendStore::instance()->writeln("test", "192.2.1.101"),
DbConnectionUnusable);
ASSERT_TRUE(BackendStore::instance());
io_service_->poll();
// Our lost connectivity callback should have been invoked.
EXPECT_EQ(1, db_lost_callback_called_);
EXPECT_EQ(0, db_recovered_callback_called_);
EXPECT_EQ(0, db_failed_callback_called_);
ASSERT_FALSE(BackendStore::instance());
sleep(1);
io_service_->poll();
// Our lost connectivity callback should have been invoked.
EXPECT_EQ(2, db_lost_callback_called_);
EXPECT_EQ(0, db_recovered_callback_called_);
EXPECT_EQ(0, db_failed_callback_called_);
ASSERT_FALSE(BackendStore::instance());
sleep(1);
io_service_->poll();
// Our lost and failed connectivity callback should have been invoked.
EXPECT_EQ(3, db_lost_callback_called_);
EXPECT_EQ(0, db_recovered_callback_called_);
EXPECT_EQ(1, db_failed_callback_called_);
ASSERT_FALSE(BackendStore::instance());
}
/// @brief Verifies that loss of connectivity to PostgreSQL is handled correctly.
TEST_F(PgSqlLegalLogDbLostCallbackTest, testRetryOpenDbLostAndRecoveredCallback) {
MultiThreadingTest mt(false);
testRetryOpenDbLostAndRecoveredCallback();
}
/// @brief Verifies that loss of connectivity to PostgreSQL is handled correctly.
TEST_F(PgSqlLegalLogDbLostCallbackTest, testRetryOpenDbLostAndRecoveredCallbackMultiThreading) {
MultiThreadingTest mt(true);
testRetryOpenDbLostAndRecoveredCallback();
}
/// @brief Verifies that loss of connectivity to PostgreSQL is handled correctly.
TEST_F(PgSqlLegalLogDbLostCallbackTest, testRetryOpenDbLostAndFailedCallback) {
MultiThreadingTest mt(false);
testRetryOpenDbLostAndFailedCallback();
}
/// @brief Verifies that loss of connectivity to PostgreSQL is handled correctly.
TEST_F(PgSqlLegalLogDbLostCallbackTest, testRetryOpenDbLostAndFailedCallbackMultiThreading) {
MultiThreadingTest mt(true);
testRetryOpenDbLostAndFailedCallback();
}
/// @brief Verifies that loss of connectivity to PostgreSQL is handled correctly.
TEST_F(PgSqlLegalLogDbLostCallbackTest, testRetryOpenDbLostAndRecoveredAfterTimeoutCallback) {
MultiThreadingTest mt(false);
testRetryOpenDbLostAndRecoveredAfterTimeoutCallback();
}
/// @brief Verifies that loss of connectivity to PostgreSQL is handled correctly.
TEST_F(PgSqlLegalLogDbLostCallbackTest, testRetryOpenDbLostAndRecoveredAfterTimeoutCallbackMultiThreading) {
MultiThreadingTest mt(true);
testRetryOpenDbLostAndRecoveredAfterTimeoutCallback();
}
/// @brief Verifies that loss of connectivity to PostgreSQL is handled correctly.
TEST_F(PgSqlLegalLogDbLostCallbackTest, testRetryOpenDbLostAndFailedAfterTimeoutCallback) {
MultiThreadingTest mt(false);
testRetryOpenDbLostAndFailedAfterTimeoutCallback();
}
/// @brief Verifies that loss of connectivity to PostgreSQL is handled correctly.
TEST_F(PgSqlLegalLogDbLostCallbackTest, testRetryOpenDbLostAndFailedAfterTimeoutCallbackMultiThreading) {
MultiThreadingTest mt(true);
testRetryOpenDbLostAndFailedAfterTimeoutCallback();
}
/// @brief Verifies that db lost callback is not invoked on an open failure
TEST_F(PgSqlLegalLogDbLostCallbackTest, testNoCallbackOnOpenFailure) {
MultiThreadingTest mt(false);
testNoCallbackOnOpenFailure();
}
/// @brief Verifies that db lost callback is not invoked on an open failure
TEST_F(PgSqlLegalLogDbLostCallbackTest, testNoCallbackOnOpenFailureMultiThreading) {
MultiThreadingTest mt(true);
testNoCallbackOnOpenFailure();
}
/// @brief Verifies that loss of connectivity to PostgreSQL is handled correctly.
TEST_F(PgSqlLegalLogDbLostCallbackTest, testDbLostAndRecoveredCallback) {
MultiThreadingTest mt(false);
testDbLostAndRecoveredCallback();
}
/// @brief Verifies that loss of connectivity to PostgreSQL is handled correctly.
TEST_F(PgSqlLegalLogDbLostCallbackTest, testDbLostAndRecoveredCallbackMultiThreading) {
MultiThreadingTest mt(true);
testDbLostAndRecoveredCallback();
}
/// @brief Verifies that loss of connectivity to PostgreSQL is handled correctly.
TEST_F(PgSqlLegalLogDbLostCallbackTest, testDbLostAndFailedCallback) {
MultiThreadingTest mt(false);
testDbLostAndFailedCallback();
}
/// @brief Verifies that loss of connectivity to PostgreSQL is handled correctly.
TEST_F(PgSqlLegalLogDbLostCallbackTest, testDbLostAndFailedCallbackMultiThreading) {
MultiThreadingTest mt(true);
testDbLostAndFailedCallback();
}
/// @brief Verifies that loss of connectivity to PostgreSQL is handled correctly.
TEST_F(PgSqlLegalLogDbLostCallbackTest, testDbLostAndRecoveredAfterTimeoutCallback) {
MultiThreadingTest mt(false);
testDbLostAndRecoveredAfterTimeoutCallback();
}
/// @brief Verifies that loss of connectivity to PostgreSQL is handled correctly.
TEST_F(PgSqlLegalLogDbLostCallbackTest, testDbLostAndRecoveredAfterTimeoutCallbackMultiThreading) {
MultiThreadingTest mt(true);
testDbLostAndRecoveredAfterTimeoutCallback();
}
/// @brief Verifies that loss of connectivity to PostgreSQL is handled correctly.
TEST_F(PgSqlLegalLogDbLostCallbackTest, testDbLostAndFailedAfterTimeoutCallback) {
MultiThreadingTest mt(false);
testDbLostAndFailedAfterTimeoutCallback();
}
/// @brief Verifies that loss of connectivity to PostgreSQL is handled correctly.
TEST_F(PgSqlLegalLogDbLostCallbackTest, testDbLostAndFailedAfterTimeoutCallbackMultiThreading) {
MultiThreadingTest mt(true);
testDbLostAndFailedAfterTimeoutCallback();
}
} // end of anonymous namespace
|