Skip to content

Commit

Permalink
Add unittest
Browse files Browse the repository at this point in the history
Signed-off-by: Ze Gan <[email protected]>
  • Loading branch information
Pterosaur committed Dec 29, 2023
1 parent f74928d commit f4fa7ce
Show file tree
Hide file tree
Showing 7 changed files with 361 additions and 22 deletions.
1 change: 1 addition & 0 deletions tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ tests_tests_SOURCES = tests/redis_ut.cpp \
common/loglevel.cpp \
tests/loglevel_ut.cpp \
tests/redis_multi_ns_ut.cpp \
tests/redis_smartswitch_ut.cpp \
tests/fdb_flush.cpp \
tests/stringutility_ut.cpp \
tests/redisutility_ut.cpp \
Expand Down
113 changes: 113 additions & 0 deletions tests/redis_multi_db_ut_config/database_config4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
{
"INSTANCES": {
"redis":{
"hostname" : "127.0.0.1",
"port": 6379,
"unix_socket_path": "/var/run/redis/redis.sock"
}
},
"DATABASES" : {
"APPL_DB" : {
"id" : 0,
"separator": ":",
"instance" : "redis"
},
"ASIC_DB" : {
"id" : 1,
"separator": ":",
"instance" : "redis"
},
"COUNTERS_DB" : {
"id" : 2,
"separator": ":",
"instance" : "redis"
},
"LOGLEVEL_DB" : {
"id" : 3,
"separator": ":",
"instance" : "redis"
},
"CONFIG_DB" : {
"id" : 4,
"separator": "|",
"instance" : "redis"
},
"PFC_WD_DB" : {
"id" : 5,
"separator": ":",
"instance" : "redis"
},
"FLEX_COUNTER_DB" : {
"id" : 5,
"separator": ":",
"instance" : "redis"
},
"STATE_DB" : {
"id" : 6,
"separator": "|",
"instance" : "redis"
},
"SNMP_OVERLAY_DB" : {
"id" : 7,
"separator": "|",
"instance" : "redis"
},
"RESTAPI_DB": {
"id": 8,
"separator": "|",
"instance": "redis"
},
"GB_ASIC_DB": {
"id": 9,
"separator": ":",
"instance": "redis"
},
"GB_COUNTERS_DB": {
"id": 10,
"separator": ":",
"instance": "redis"
},
"GB_FLEX_COUNTER_DB": {
"id": 11,
"separator": ":",
"instance": "redis"
},
"PROFILE_DB" : {
"id" : 12,
"separator": "|",
"instance" : "redis"
},
"STATE_DB2" : {
"id" : 13,
"separator": "|",
"instance" : "redis"
},
"APPL_STATE_DB" : {
"id" : 14,
"separator": ":",
"instance" : "redis"
},
"DPU_APPL_DB" : {
"id" : 15,
"separator": ":",
"instance" : "redis",
"format": "proto"
},
"DPU_APPL_STATE_DB" : {
"id" : 16,
"separator": "|",
"instance" : "redis"
},
"DPU_STATE_DB" : {
"id" : 17,
"separator": "|",
"instance" : "redis"
},
"DPU_COUNTERS_DB" : {
"id" : 18,
"separator": ":",
"instance" : "redis"
}
},
"VERSION" : "1.0"
}
118 changes: 118 additions & 0 deletions tests/redis_multi_db_ut_config/database_config5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
{
"INSTANCES": {
"redis":{
"hostname" : "127.0.0.1",
"port": 6379,
"unix_socket_path": "/var/run/redis/redis.sock"
},
"remote-redis":{
"hostname" : "127.0.0.1",
"port": 6379,
"unix_socket_path": ""
}
},
"DATABASES" : {
"APPL_DB" : {
"id" : 0,
"separator": ":",
"instance" : "redis"
},
"ASIC_DB" : {
"id" : 1,
"separator": ":",
"instance" : "redis"
},
"COUNTERS_DB" : {
"id" : 2,
"separator": ":",
"instance" : "redis"
},
"LOGLEVEL_DB" : {
"id" : 3,
"separator": ":",
"instance" : "redis"
},
"CONFIG_DB" : {
"id" : 4,
"separator": "|",
"instance" : "redis"
},
"PFC_WD_DB" : {
"id" : 5,
"separator": ":",
"instance" : "redis"
},
"FLEX_COUNTER_DB" : {
"id" : 5,
"separator": ":",
"instance" : "redis"
},
"STATE_DB" : {
"id" : 6,
"separator": "|",
"instance" : "redis"
},
"SNMP_OVERLAY_DB" : {
"id" : 7,
"separator": "|",
"instance" : "redis"
},
"RESTAPI_DB": {
"id": 8,
"separator": "|",
"instance": "redis"
},
"GB_ASIC_DB": {
"id": 9,
"separator": ":",
"instance": "redis"
},
"GB_COUNTERS_DB": {
"id": 10,
"separator": ":",
"instance": "redis"
},
"GB_FLEX_COUNTER_DB": {
"id": 11,
"separator": ":",
"instance": "redis"
},
"PROFILE_DB" : {
"id" : 12,
"separator": "|",
"instance" : "redis"
},
"STATE_DB2" : {
"id" : 13,
"separator": "|",
"instance" : "redis"
},
"APPL_STATE_DB" : {
"id" : 14,
"separator": ":",
"instance" : "redis"
},
"DPU_APPL_DB" : {
"id" : 15,
"separator": ":",
"instance" : "remote-redis",
"format": "proto"
},
"DPU_APPL_STATE_DB" : {
"id" : 16,
"separator": "|",
"instance" : "remote-redis"
},
"DPU_STATE_DB" : {
"id" : 17,
"separator": "|",
"instance" : "remote-redis"
},
"DPU_COUNTERS_DB" : {
"id" : 18,
"separator": ":",
"instance" : "remote-redis"
}
},
"VERSION" : "1.0"
}
8 changes: 8 additions & 0 deletions tests/redis_multi_db_ut_config/database_global.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
{
"namespace" : "asic3",
"include" : "../redis_multi_db_ut_config/database_config3.json"
},
{
"database_name" : "dpu0",
"include" : "../redis_multi_db_ut_config/database_config4.json"
},
{
"database_name" : "dpu1",
"include" : "../redis_multi_db_ut_config/database_config5.json"
}
],
"VERSION" : "1.0"
Expand Down
60 changes: 40 additions & 20 deletions tests/redis_multi_ns_ut.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <iostream>
#include <fstream>
#include <map>
#include <set>
#include <algorithm>
#include "gtest/gtest.h"
#include "common/dbconnector.h"
#include <nlohmann/json.hpp>
Expand All @@ -14,8 +16,8 @@ extern string global_existing_file;

TEST(DBConnector, multi_ns_test)
{
std::string local_file, dir_name, ns_name;
vector<string> namespaces;
std::string local_file, dir_name;
set<string> namespaces;
vector<string> ns_names;

// load global config file again, should throw exception with init already done
Expand Down Expand Up @@ -57,18 +59,20 @@ TEST(DBConnector, multi_ns_test)

for (auto& element : g["INCLUDES"])
{
swss::SonicDBKey key;
local_file.append(dir_name);
local_file.append(element["include"]);

if(element["namespace"].empty())
if(!element["namespace"].empty())
{
ns_name = EMPTY_NAMESPACE;
key.netns = element["namespace"];
}
else
namespaces.insert(key.netns);

if (!element["database_name"].empty())
{
ns_name = element["namespace"];
key.containerName = element["database_name"];
}
namespaces.push_back(ns_name);

// parse config file
ifstream i(local_file);
Expand All @@ -93,17 +97,34 @@ TEST(DBConnector, multi_ns_test)
string instName = it.value().at("instance");
int dbId = it.value().at("id");
cout<<"testing "<<dbName<<endl;
cout<<ns_name<<"#"<<dbId<<dbName<<"#"<<m_inst_info[instName].unixSocketPath<<"#"<<m_inst_info[instName].hostname<<"#"<<m_inst_info[instName].port<<endl;
// dbInst info matches between get api and context in json file
EXPECT_EQ(instName, SonicDBConfig::getDbInst(dbName, ns_name));
// dbId info matches between get api and context in json file
EXPECT_EQ(dbId, SonicDBConfig::getDbId(dbName, ns_name));
// socket info matches between get api and context in json file
EXPECT_EQ(m_inst_info[instName].unixSocketPath, SonicDBConfig::getDbSock(dbName, ns_name));
// hostname info matches between get api and context in json file
EXPECT_EQ(m_inst_info[instName].hostname, SonicDBConfig::getDbHostname(dbName, ns_name));
// port info matches between get api and context in json file
EXPECT_EQ(m_inst_info[instName].port, SonicDBConfig::getDbPort(dbName, ns_name));
cout<<key.netns<<"#"<<dbId<<dbName<<"#"<<m_inst_info[instName].unixSocketPath<<"#"<<m_inst_info[instName].hostname<<"#"<<m_inst_info[instName].port<<endl;
if (key.containerName.empty())
{
// Test for original namespace only API
// dbInst info matches between get api and context in json file
EXPECT_EQ(instName, SonicDBConfig::getDbInst(dbName, key.netns));
// dbId info matches between get api and context in json file
EXPECT_EQ(dbId, SonicDBConfig::getDbId(dbName, key.netns));
// socket info matches between get api and context in json file
EXPECT_EQ(m_inst_info[instName].unixSocketPath, SonicDBConfig::getDbSock(dbName, key.netns));
// hostname info matches between get api and context in json file
EXPECT_EQ(m_inst_info[instName].hostname, SonicDBConfig::getDbHostname(dbName, key.netns));
// port info matches between get api and context in json file
EXPECT_EQ(m_inst_info[instName].port, SonicDBConfig::getDbPort(dbName, key.netns));
}
else
{
// dbInst info matches between get api and context in json file
EXPECT_EQ(instName, SonicDBConfig::getDbInst(dbName, key));
// dbId info matches between get api and context in json file
EXPECT_EQ(dbId, SonicDBConfig::getDbId(dbName, key));
// socket info matches between get api and context in json file
EXPECT_EQ(m_inst_info[instName].unixSocketPath, SonicDBConfig::getDbSock(dbName, key));
// hostname info matches between get api and context in json file
EXPECT_EQ(m_inst_info[instName].hostname, SonicDBConfig::getDbHostname(dbName, key));
// port info matches between get api and context in json file
EXPECT_EQ(m_inst_info[instName].port, SonicDBConfig::getDbPort(dbName, key));
}
}
}
local_file.clear();
Expand All @@ -112,10 +133,9 @@ TEST(DBConnector, multi_ns_test)

// Get the namespaces from the database_global.json file and compare with the list we created here.
ns_names = SonicDBConfig::getNamespaces();
sort (namespaces.begin(), namespaces.end());
sort (ns_names.begin(), ns_names.end());
EXPECT_EQ(ns_names.size(), namespaces.size());
EXPECT_TRUE(namespaces == ns_names);
EXPECT_TRUE(std::equal(namespaces.begin(), namespaces.end(), ns_names.begin()));
}

void clearNetnsDB()
Expand Down
Loading

0 comments on commit f4fa7ce

Please sign in to comment.