From 838d9b45c03ff9f04522f92bd88982089345fe06 Mon Sep 17 00:00:00 2001 From: Dong Zhang <41927498+dzhangalibaba@users.noreply.github.com> Date: Thu, 12 Dec 2019 17:38:55 -0800 Subject: [PATCH] [MultiDB]:swsscommon replace old API with new APIs including tests (#324) * [MultiDB]:swsscommon replace old API with new APIs including tests * update code format * move db setup into global setup --- common/logger.cpp | 4 +- common/loglevel.cpp | 2 +- common/warm_restart.cpp | 21 ++------- common/warm_restart.h | 3 +- tests/Makefile.am | 3 +- tests/json_ut.cpp | 3 +- tests/main.cpp | 46 +++++++++++++++++++ tests/ntf_ut.cpp | 6 +-- tests/redis_multi_db_ut.cpp | 30 ++---------- .../database_config.json | 30 ++++++++---- tests/redis_piped_state_ut.cpp | 18 ++++---- tests/redis_piped_ut.cpp | 17 ++++--- tests/redis_state_ut.cpp | 30 ++++++------ tests/redis_subscriber_state_ut.cpp | 19 ++++---- tests/redis_ut.cpp | 27 ++++++----- tests/selectable_priority.cpp | 5 +- tests/test_redis_ut.py | 10 ++-- tests/warm_restart_ut.cpp | 16 +++---- 18 files changed, 154 insertions(+), 136 deletions(-) create mode 100644 tests/main.cpp diff --git a/common/logger.cpp b/common/logger.cpp index fc0b7ec61..c7e412532 100644 --- a/common/logger.cpp +++ b/common/logger.cpp @@ -91,7 +91,7 @@ void Logger::linkToDbWithOutput(const std::string &dbName, const PriorityChangeN // Initialize internal DB with observer logger.m_settingChangeObservers.insert(std::make_pair(dbName, std::make_pair(prioNotify, outputNotify))); - DBConnector db(LOGLEVEL_DB, DBConnector::DEFAULT_UNIXSOCKET, 0); + DBConnector db("LOGLEVEL_DB", 0); RedisClient redisClient(&db); auto keys = redisClient.keys("*"); @@ -169,7 +169,7 @@ Logger::Priority Logger::getMinPrio() [[ noreturn ]] void Logger::settingThread() { Select select; - DBConnector db(LOGLEVEL_DB, DBConnector::DEFAULT_UNIXSOCKET, 0); + DBConnector db("LOGLEVEL_DB", 0); std::vector> selectables(m_settingChangeObservers.size()); for (const auto& i : m_settingChangeObservers) diff --git a/common/loglevel.cpp b/common/loglevel.cpp index 022be0ad7..d56bf3313 100644 --- a/common/loglevel.cpp +++ b/common/loglevel.cpp @@ -107,7 +107,7 @@ int main(int argc, char **argv) } } - DBConnector db(LOGLEVEL_DB, DBConnector::DEFAULT_UNIXSOCKET, 0); + DBConnector db("LOGLEVEL_DB", 0); RedisClient redisClient(&db); auto keys = redisClient.keys("*"); for (auto& key : keys) diff --git a/common/warm_restart.cpp b/common/warm_restart.cpp index 3b6109858..62aa846c1 100644 --- a/common/warm_restart.cpp +++ b/common/warm_restart.cpp @@ -32,8 +32,7 @@ WarmStart &WarmStart::getInstance(void) void WarmStart::initialize(const std::string &app_name, const std::string &docker_name, unsigned int db_timeout, - const std::string &db_hostname, - int db_port) + bool isTcpConn) { auto& warmStart = getInstance(); @@ -43,20 +42,10 @@ void WarmStart::initialize(const std::string &app_name, } /* Use unix socket for db connection by default */ - if(db_hostname.empty()) - { - warmStart.m_stateDb = - std::make_shared(STATE_DB, swss::DBConnector::DEFAULT_UNIXSOCKET, db_timeout); - warmStart.m_cfgDb = - std::make_shared(CONFIG_DB, swss::DBConnector::DEFAULT_UNIXSOCKET, db_timeout); - } - else - { - warmStart.m_stateDb = - std::make_shared(STATE_DB, db_hostname, db_port, db_timeout); - warmStart.m_cfgDb = - std::make_shared(CONFIG_DB, db_hostname, db_port, db_timeout); - } + warmStart.m_stateDb = + std::make_shared("STATE_DB", db_timeout, isTcpConn); + warmStart.m_cfgDb = + std::make_shared("CONFIG_DB", db_timeout, isTcpConn); warmStart.m_stateWarmRestartEnableTable = std::unique_ptr(new Table(warmStart.m_stateDb.get(), STATE_WARM_RESTART_ENABLE_TABLE_NAME)); diff --git a/common/warm_restart.h b/common/warm_restart.h index c620b2487..dd6bf7c42 100644 --- a/common/warm_restart.h +++ b/common/warm_restart.h @@ -43,8 +43,7 @@ class WarmStart static void initialize(const std::string &app_name, const std::string &docker_name, unsigned int db_timeout = 0, - const std::string &db_hostname = "", - int db_port = 6379); + bool isTcpConn = false); static bool checkWarmStart(const std::string &app_name, const std::string &docker_name, diff --git a/tests/Makefile.am b/tests/Makefile.am index 877ed59dd..9fd0a8a7f 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -26,7 +26,8 @@ tests_SOURCES = redis_ut.cpp \ redis_subscriber_state_ut.cpp \ selectable_priority.cpp \ warm_restart_ut.cpp \ - redis_multi_db_ut.cpp + redis_multi_db_ut.cpp \ + main.cpp tests_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST) $(LIBNL_CFLAGS) tests_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST) $(LIBNL_CFLAGS) diff --git a/tests/json_ut.cpp b/tests/json_ut.cpp index 18398c609..a2374f759 100644 --- a/tests/json_ut.cpp +++ b/tests/json_ut.cpp @@ -6,13 +6,12 @@ using namespace std; using namespace swss; using json = nlohmann::json; -#define TEST_DB (15) // Default Redis config supports 16 databases, max DB ID is 15 #define TEST_DUMP_FILE "ut_dump_file.txt" TEST(JSON, test) { /* Construct the file */ - DBConnector db(TEST_DB, "localhost", 6379, 0); + DBConnector db("TEST_DB", 0, true); ProducerTable *p; p = new ProducerTable(&db, "UT_REDIS", TEST_DUMP_FILE); string separator = p->getTableNameSeparator(); diff --git a/tests/main.cpp b/tests/main.cpp new file mode 100644 index 000000000..89c4bdb29 --- /dev/null +++ b/tests/main.cpp @@ -0,0 +1,46 @@ +#include "gtest/gtest.h" +#include "common/dbconnector.h" +#include + +using namespace std; +using namespace swss; + +string existing_file = "./tests/redis_multi_db_ut_config/database_config.json"; +string nonexisting_file = "./tests/redis_multi_db_ut_config/database_config_nonexisting.json"; + +class SwsscommonEnvironment : public ::testing::Environment { +public: + // Override this to define how to set up the environment. + void SetUp() override { + // by default , init should be false + cout<<"Default : isInit = "<(std::thread(ntf_thread, std::ref(nc))); @@ -81,7 +81,7 @@ TEST(Notifications, pops) { SWSS_LOG_ENTER(); - swss::DBConnector dbNtf(ASIC_DB, "localhost", 6379, 0); + swss::DBConnector dbNtf("ASIC_DB", 0, true); swss::NotificationConsumer nc(&dbNtf, "NOTIFICATIONS", 100, (size_t)messages); swss::NotificationProducer notifications(&dbNtf, "NOTIFICATIONS"); @@ -125,7 +125,7 @@ TEST(Notifications, peek) { SWSS_LOG_ENTER(); - swss::DBConnector dbNtf(ASIC_DB, "localhost", 6379, 0); + swss::DBConnector dbNtf("ASIC_DB", 0, true); swss::NotificationConsumer nc(&dbNtf, "NOTIFICATIONS", 100, (size_t)10); swss::NotificationProducer notifications(&dbNtf, "NOTIFICATIONS"); diff --git a/tests/redis_multi_db_ut.cpp b/tests/redis_multi_db_ut.cpp index 9698526ee..902b88b2b 100644 --- a/tests/redis_multi_db_ut.cpp +++ b/tests/redis_multi_db_ut.cpp @@ -9,39 +9,15 @@ using namespace std; using namespace swss; using json = nlohmann::json; +extern string existing_file; TEST(DBConnector, multi_db_test) { - string file = "./tests/redis_multi_db_ut_config/database_config.json"; - string nonexisting_file = "./tests/redis_multi_db_ut_config/database_config_nonexisting.json"; - - // by default , init should be false - cout<<"Default : isInit = "<& f) static void producerWorker(int index) { string tableName = "UT_REDIS_THREAD_" + to_string(index); - DBConnector db(TEST_DB, "localhost", 6379, 0); + DBConnector db("TEST_DB", 0, true); RedisPipeline pipeline(&db); ProducerTable p(&pipeline, tableName, true); @@ -102,7 +101,7 @@ static void producerWorker(int index) static void consumerWorker(int index) { string tableName = "UT_REDIS_THREAD_" + to_string(index); - DBConnector db(TEST_DB, "localhost", 6379, 0); + DBConnector db("TEST_DB", 0, true); ConsumerTable c(&db, tableName); Select cs; Selectable *selectcs; @@ -137,7 +136,7 @@ static void consumerWorker(int index) static void clearDB() { - DBConnector db(TEST_DB, "localhost", 6379, 0); + DBConnector db("TEST_DB", 0, true); RedisReply r(&db, "FLUSHALL", REDIS_REPLY_STATUS); r.checkStatusOK(); } @@ -171,7 +170,7 @@ TEST(DBConnector, piped_test) TEST(DBConnector, piped_multitable) { - DBConnector db(TEST_DB, "localhost", 6379, 0); + DBConnector db("TEST_DB", 0, true); ConsumerTable *consumers[NUMBER_OF_THREADS]; thread *producerThreads[NUMBER_OF_THREADS]; KeyOpFieldsValuesTuple kco; @@ -235,7 +234,7 @@ static void notificationProducer() { sleep(1); - DBConnector db(TEST_DB, "localhost", 6379, 0); + DBConnector db("TEST_DB", 0, true); NotificationProducer np(&db, "UT_REDIS_CHANNEL"); vector values; @@ -248,7 +247,7 @@ static void notificationProducer() TEST(DBConnector, piped_notifications) { - DBConnector db(TEST_DB, "localhost", 6379, 0); + DBConnector db("TEST_DB", 0, true); NotificationConsumer nc(&db, "UT_REDIS_CHANNEL"); Select s; s.addSelectable(&nc); @@ -322,7 +321,7 @@ TEST(DBConnector, piped_selectableevent) TEST(Table, piped_test) { string tableName = "TABLE_UT_TEST"; - DBConnector db(TEST_DB, "localhost", 6379, 0); + DBConnector db("TEST_DB", 0, true); RedisPipeline pipeline(&db); Table t(&pipeline, tableName, true); @@ -415,7 +414,7 @@ TEST(ProducerConsumer, piped_Prefix) { string tableName = "tableName"; - DBConnector db(TEST_DB, "localhost", 6379, 0); + DBConnector db("TEST_DB", 0, true); RedisPipeline pipeline(&db); ProducerTable p(&pipeline, tableName, true); diff --git a/tests/redis_state_ut.cpp b/tests/redis_state_ut.cpp index d62746f69..9e7c6b40c 100644 --- a/tests/redis_state_ut.cpp +++ b/tests/redis_state_ut.cpp @@ -16,7 +16,7 @@ using namespace std; using namespace swss; -#define TEST_DB APPL_DB // Need to test against a DB which uses a colon table name separator due to hardcoding in consumer_table_pops.lua +#define TEST_DB "APPL_DB" // Need to test against a DB which uses a colon table name separator due to hardcoding in consumer_table_pops.lua #define NUMBER_OF_THREADS (64) // Spawning more than 256 threads causes libc++ to except #define NUMBER_OF_OPS (1000) #define MAX_FIELDS_DIV (30) // Testing up to 30 fields objects @@ -76,7 +76,7 @@ static inline void validateFields(const string& key, const vector tablenames(NUMBER_OF_THREADS); thread *producerThreads[NUMBER_OF_THREADS]; diff --git a/tests/redis_subscriber_state_ut.cpp b/tests/redis_subscriber_state_ut.cpp index 8c2eb65fd..c754a7e4e 100644 --- a/tests/redis_subscriber_state_ut.cpp +++ b/tests/redis_subscriber_state_ut.cpp @@ -12,14 +12,11 @@ using namespace std; using namespace swss; -#define TEST_DB (15) // Default Redis config supports 16 databases, max DB ID is 15 #define NUMBER_OF_THREADS (64) // Spawning more than 256 threads causes libc++ to except #define NUMBER_OF_OPS (1000) #define MAX_FIELDS_DIV (30) // Testing up to 30 fields objects #define PRINT_SKIP (10) // Print + for Producer and - for Subscriber for every 100 ops -static const string dbhost = "localhost"; -static const int dbport = 6379; static const string testTableName = "UT_REDIS_TABLE"; static const string testTableName2 = "UT_REDIS_TABLE2"; @@ -88,14 +85,14 @@ static inline void validateFields(const string& key, const vector& f) void producerWorker(int index) { string tableName = "UT_REDIS_THREAD_" + to_string(index); - DBConnector db(TEST_DB, "localhost", 6379, 0); + DBConnector db("TEST_DB", 0, true); ProducerTable p(&db, tableName); for (int i = 0; i < NUMBER_OF_OPS; i++) @@ -103,7 +102,7 @@ void producerWorker(int index) void consumerWorker(int index) { string tableName = "UT_REDIS_THREAD_" + to_string(index); - DBConnector db(TEST_DB, "localhost", 6379, 0); + DBConnector db("TEST_DB", 0, true); ConsumerTable c(&db, tableName); Select cs; Selectable *selectcs; @@ -138,14 +137,14 @@ void consumerWorker(int index) void clearDB() { - DBConnector db(TEST_DB, "localhost", 6379, 0); + DBConnector db("TEST_DB", 0, true); RedisReply r(&db, "FLUSHALL", REDIS_REPLY_STATUS); r.checkStatusOK(); } void TableBasicTest(string tableName) { - DBConnector db(TEST_DB, "localhost", 6379, 0); + DBConnector db("TEST_DB", 0, true); Table t(&db, tableName); @@ -272,7 +271,7 @@ void TableBasicTest(string tableName) TEST(DBConnector, RedisClient) { - DBConnector db(TEST_DB, "localhost", 6379, 0); + DBConnector db("TEST_DB", 0, true); RedisClient redic(&db); @@ -441,7 +440,7 @@ TEST(DBConnector, test) TEST(DBConnector, multitable) { - DBConnector db(TEST_DB, "localhost", 6379, 0); + DBConnector db("TEST_DB", 0, true); ConsumerTable *consumers[NUMBER_OF_THREADS]; thread *producerThreads[NUMBER_OF_THREADS]; KeyOpFieldsValuesTuple kco; @@ -505,7 +504,7 @@ void notificationProducer() { sleep(1); - DBConnector db(TEST_DB, "localhost", 6379, 0); + DBConnector db("TEST_DB", 0, true); NotificationProducer np(&db, "UT_REDIS_CHANNEL"); vector values; @@ -518,7 +517,7 @@ void notificationProducer() TEST(DBConnector, notifications) { - DBConnector db(TEST_DB, "localhost", 6379, 0); + DBConnector db("TEST_DB", 0, true); NotificationConsumer nc(&db, "UT_REDIS_CHANNEL"); Select s; s.addSelectable(&nc); @@ -646,7 +645,7 @@ TEST(ProducerConsumer, Prefix) { std::string tableName = "tableName"; - DBConnector db(TEST_DB, "localhost", 6379, 0); + DBConnector db("TEST_DB", 0, true); ProducerTable p(&db, tableName); std::vector values; @@ -675,7 +674,7 @@ TEST(ProducerConsumer, Pop) { std::string tableName = "tableName"; - DBConnector db(TEST_DB, "localhost", 6379, 0); + DBConnector db("TEST_DB", 0, true); ProducerTable p(&db, tableName); std::vector values; @@ -703,7 +702,7 @@ TEST(ProducerConsumer, Pop2) { std::string tableName = "tableName"; - DBConnector db(TEST_DB, "localhost", 6379, 0); + DBConnector db("TEST_DB", 0, true); ProducerTable p(&db, tableName); std::vector values; @@ -742,7 +741,7 @@ TEST(ProducerConsumer, PopEmpty) { std::string tableName = "tableName"; - DBConnector db(TEST_DB, "localhost", 6379, 0); + DBConnector db("TEST_DB", 0, true); ConsumerTable c(&db, tableName); @@ -762,7 +761,7 @@ TEST(ProducerConsumer, ConsumerSelectWithInitData) clearDB(); string tableName = "tableName"; - DBConnector db(TEST_DB, "localhost", 6379, 0); + DBConnector db("TEST_DB", 0, true); ProducerTable p(&db, tableName); for (int i = 0; i < NUMBER_OF_OPS; i++) diff --git a/tests/selectable_priority.cpp b/tests/selectable_priority.cpp index 1c33e087d..24fbd3760 100644 --- a/tests/selectable_priority.cpp +++ b/tests/selectable_priority.cpp @@ -14,7 +14,6 @@ using namespace std; using namespace swss; -#define TEST_VIEW (7) #define DEFAULT_POP_BATCH_SIZE (10) @@ -22,7 +21,7 @@ TEST(Priority, default_pri_values) { std::string tableName = "tableName"; - DBConnector db(TEST_VIEW, "localhost", 6379, 0); + DBConnector db("TEST_DB", 0, true); timespec interval = { .tv_sec = 1, .tv_nsec = 0 }; @@ -49,7 +48,7 @@ TEST(Priority, set_pri_values) { std::string tableName = "tableName"; - DBConnector db(TEST_VIEW, "localhost", 6379, 0); + DBConnector db("TEST_DB", 0, true); timespec interval = { .tv_sec = 1, .tv_nsec = 0 }; diff --git a/tests/test_redis_ut.py b/tests/test_redis_ut.py index 32281a743..3356beee1 100644 --- a/tests/test_redis_ut.py +++ b/tests/test_redis_ut.py @@ -1,7 +1,7 @@ from swsscommon import swsscommon def test_ProducerTable(): - db = swsscommon.DBConnector(0, "localhost", 6379, 0) + db = swsscommon.DBConnector("APPL_DB", 0, True) ps = swsscommon.ProducerTable(db, "abc") cs = swsscommon.ConsumerTable(db, "abc") fvs = swsscommon.FieldValuePairs([('a','b')]) @@ -13,7 +13,7 @@ def test_ProducerTable(): assert cfvs[0] == ('a', 'b') def test_ProducerStateTable(): - db = swsscommon.DBConnector(0, "localhost", 6379, 0) + db = swsscommon.DBConnector("APPL_DB", 0, True) ps = swsscommon.ProducerStateTable(db, "abc") cs = swsscommon.ConsumerStateTable(db, "abc") fvs = swsscommon.FieldValuePairs([('a','b')]) @@ -25,7 +25,7 @@ def test_ProducerStateTable(): assert cfvs[0] == ('a', 'b') def test_Table(): - db = swsscommon.DBConnector(0, "localhost", 6379, 0) + db = swsscommon.DBConnector("APPL_DB", 0, True) tbl = swsscommon.Table(db, "test_TABLE") fvs = swsscommon.FieldValuePairs([('a','b'), ('c', 'd')]) tbl.set("aaa", fvs) @@ -39,7 +39,7 @@ def test_Table(): assert fvs[1] == ('c', 'd') def test_SubscriberStateTable(): - db = swsscommon.DBConnector(0, "localhost", 6379, 0) + db = swsscommon.DBConnector("APPL_DB", 0, True) t = swsscommon.Table(db, "testsst") sel = swsscommon.Select() cst = swsscommon.SubscriberStateTable(db, "testsst") @@ -55,7 +55,7 @@ def test_SubscriberStateTable(): assert cfvs[0] == ('a', 'b') def test_Notification(): - db = swsscommon.DBConnector(0, "localhost", 6379, 0) + db = swsscommon.DBConnector("APPL_DB", 0, True) ntfc = swsscommon.NotificationConsumer(db, "testntf") sel = swsscommon.Select() sel.addSelectable(ntfc) diff --git a/tests/warm_restart_ut.cpp b/tests/warm_restart_ut.cpp index 1babd9a9e..2cdaa7a2c 100644 --- a/tests/warm_restart_ut.cpp +++ b/tests/warm_restart_ut.cpp @@ -13,11 +13,11 @@ static const string testDockerName = "TestDocker"; TEST(WarmRestart, checkWarmStart_and_State) { - DBConnector stateDb(STATE_DB, "localhost", 6379, 0); + DBConnector stateDb("STATE_DB", 0, true); Table stateWarmRestartTable(&stateDb, STATE_WARM_RESTART_TABLE_NAME); Table stateWarmRestartEnableTable(&stateDb, STATE_WARM_RESTART_ENABLE_TABLE_NAME); - DBConnector configDb(CONFIG_DB, "localhost", 6379, 0); + DBConnector configDb("CONFIG_DB", 0, true); Table cfgWarmRestartTable(&configDb, CFG_WARM_RESTART_TABLE_NAME); //Clean up warm restart state for testAppName and warm restart config for testDockerName @@ -26,7 +26,7 @@ TEST(WarmRestart, checkWarmStart_and_State) //Initialize WarmStart class for TestApp - WarmStart::initialize(testAppName, testDockerName, 0, "localhost", 6379); + WarmStart::initialize(testAppName, testDockerName, 0, true); // Application is supposed to set the warm start state explicitly based on // its progress of state restore. @@ -140,11 +140,11 @@ TEST(WarmRestart, checkWarmStart_and_State) TEST(WarmRestart, getWarmStartTimer) { - DBConnector configDb(CONFIG_DB, "localhost", 6379, 0); + DBConnector configDb("CONFIG_DB", 0, true); Table cfgWarmRestartTable(&configDb, CFG_WARM_RESTART_TABLE_NAME); //Initialize WarmStart class for TestApp - WarmStart::initialize(testAppName, testDockerName, 0, "localhost", 6379); + WarmStart::initialize(testAppName, testDockerName, 0, true); uint32_t timer; // By default, no default warm start timer exists in configDB for any application @@ -162,14 +162,14 @@ TEST(WarmRestart, getWarmStartTimer) TEST(WarmRestart, set_get_DataCheckState) { - DBConnector stateDb(STATE_DB, "localhost", 6379, 0); + DBConnector stateDb("STATE_DB", 0, true); Table stateWarmRestartTable(&stateDb, STATE_WARM_RESTART_TABLE_NAME); //Clean up warm restart state for testAppName stateWarmRestartTable.del(testAppName); //Initialize WarmStart class for TestApp - WarmStart::initialize(testAppName, testDockerName, 0, "localhost", 6379); + WarmStart::initialize(testAppName, testDockerName, 0, true); WarmStart::DataCheckState state; // basic state set check for shutdown stage @@ -234,4 +234,4 @@ TEST(WarmRestart, set_get_DataCheckState) EXPECT_EQ(value, "failed"); state = WarmStart::getDataCheckState(testAppName, WarmStart::STAGE_RESTORE); EXPECT_EQ(state, WarmStart::CHECK_FAILED); -} \ No newline at end of file +}