diff --git a/configure.ac b/configure.ac index b95367ef5..0d25c024b 100644 --- a/configure.ac +++ b/configure.ac @@ -249,6 +249,7 @@ AC_OUTPUT(Makefile unittest/lib/Makefile unittest/vslib/Makefile unittest/syncd/Makefile + unittest/saidump/Makefile pyext/Makefile pyext/py2/Makefile pyext/py3/Makefile) diff --git a/saidump/Makefile.am b/saidump/Makefile.am old mode 100644 new mode 100755 index 46c51d0fc..c4515cc99 --- a/saidump/Makefile.am +++ b/saidump/Makefile.am @@ -2,8 +2,11 @@ AM_CXXFLAGS = $(SAIINC) -I$(top_srcdir)/lib bin_PROGRAMS = saidump -saidump_SOURCES = saidump.cpp +saidump_SOURCES = main.cpp saidump.cpp saidump_CPPFLAGS = $(CODE_COVERAGE_CPPFLAGS) saidump_CXXFLAGS = $(DBGFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS_COMMON) $(CODE_COVERAGE_CXXFLAGS) saidump_LDADD = -lhiredis -lswsscommon -lpthread -L$(top_srcdir)/meta/.libs -lsaimetadata -lsaimeta \ -L$(top_srcdir)/lib/.libs -lsairedis -lzmq $(CODE_COVERAGE_LIBS) + +noinst_LIBRARIES = libsaidump.a +libsaidump_a_SOURCES = saidump.cpp diff --git a/saidump/main.cpp b/saidump/main.cpp new file mode 100755 index 000000000..1c6c70ef9 --- /dev/null +++ b/saidump/main.cpp @@ -0,0 +1,19 @@ +#include "saidump.h" + +using namespace syncd; + +int main(int argc, char **argv) +{ + SWSS_LOG_ENTER(); + swss::Logger::getInstance().setMinPrio(swss::Logger::SWSS_INFO); + + SaiDump m_saiDump; + + if(SAI_STATUS_SUCCESS != m_saiDump.handleCmdLine(argc, argv)) + { + return EXIT_FAILURE; + } + + m_saiDump.dumpFromRedisDb(); + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/saidump/saidump.cpp b/saidump/saidump.cpp old mode 100644 new mode 100755 index 6a3321e58..2f5f63e1b --- a/saidump/saidump.cpp +++ b/saidump/saidump.cpp @@ -1,55 +1,35 @@ -#include -#include -#include -#include +#include "saidump.h" +using namespace syncd; -extern "C" { -#include -} - -#include "swss/table.h" -#include "meta/sai_serialize.h" -#include "sairediscommon.h" - -#include - -// TODO split to multiple cpp - -using namespace swss; - -struct CmdOptions -{ - bool skipAttributes; - bool dumpTempView; - bool dumpGraph; -}; -CmdOptions g_cmdOptions; -std::map g_oid_map; - -void printUsage() +void SaiDump::printUsage() { SWSS_LOG_ENTER(); - std::cout << "Usage: saidump [-t] [-g] [-h]" << std::endl; + std::cout << "Usage: saidump [-t] [-g] [-r] [-m] [-h]" << std::endl; std::cout << " -t --tempView:" << std::endl; std::cout << " Dump temp view" << std::endl; std::cout << " -g --dumpGraph:" << std::endl; std::cout << " Dump current graph" << std::endl; + std::cout << " -r --rdb:" << std::endl; + std::cout << " Dump by parsing the RDB JSON file, which is created by rdbtools based on Redis dump.rdb that is generated by Redis SAVE command" << std::endl; + std::cout << " -m --max:" << std::endl; + std::cout << " Config the the RDB JSON file's max size in MB, which is optional with default value 100MB" << std::endl; std::cout << " -h --help:" << std::endl; std::cout << " Print out this message" << std::endl; } -CmdOptions handleCmdLine(int argc, char **argv) +sai_status_t SaiDump::handleCmdLine(int argc, char **argv) { SWSS_LOG_ENTER(); - CmdOptions options; - - options.dumpTempView = false; - options.dumpGraph = false; + sai_status_t status = SAI_STATUS_SUCCESS; + dumpTempView = false; + dumpGraph = false; + rdbJSonSizeLimit = RDB_JSON_MAX_SIZE; - const char* const optstring = "gth"; + const char* const optstring = "gtr:m:h"; + uint64_t result = 0; while (true) { @@ -57,6 +37,8 @@ CmdOptions handleCmdLine(int argc, char **argv) { { "dumpGraph", no_argument, 0, 'g' }, { "tempView", no_argument, 0, 't' }, + { "rdb", required_argument, 0, 'r' }, + { "max", required_argument, 0, 'm' }, { "help", no_argument, 0, 'h' }, { 0, 0, 0, 0 } }; @@ -74,12 +56,42 @@ CmdOptions handleCmdLine(int argc, char **argv) { case 'g': SWSS_LOG_NOTICE("Dumping graph"); - options.dumpGraph = true; + dumpGraph = true; + status = SAI_STATUS_SUCCESS; break; case 't': SWSS_LOG_NOTICE("Dumping temp view"); - options.dumpTempView = true; + dumpTempView = true; + status = SAI_STATUS_SUCCESS; + break; + + case 'r': + SWSS_LOG_NOTICE("Dumping from %s", optarg); + rdbJsonFile = std::string(optarg); + status = SAI_STATUS_SUCCESS; + break; + + case 'm': + if(!regex_match(optarg, std::regex(R"([+]?\d+)"))) //only positive numeric chars are valid, such as 3984, +3232, etc. + { + SWSS_LOG_WARN("invalid option -m %s", optarg); + printUsage(); + exit(EXIT_SUCCESS); + } + + result = strtoull(optarg, NULL, 0); + + if((errno == ERANGE && result == ULLONG_MAX) || result == 0 || result >= INT_MAX) + { + SWSS_LOG_WARN("invalid option -m %s", optarg); + printUsage(); + exit(EXIT_SUCCESS); + } + + rdbJSonSizeLimit = result * 1024 * 1024; + SWSS_LOG_NOTICE("Configure the RDB JSON MAX size to %llu MB", rdbJSonSizeLimit / 1024 / 1024); + status = SAI_STATUS_SUCCESS; break; case 'h': @@ -96,11 +108,10 @@ CmdOptions handleCmdLine(int argc, char **argv) exit(EXIT_FAILURE); } } - - return options; + return status; } -size_t get_max_attr_len(const TableMap& map) +size_t SaiDump::get_max_attr_len(const TableMap& map) { SWSS_LOG_ENTER(); @@ -114,7 +125,7 @@ size_t get_max_attr_len(const TableMap& map) return max; } -std::string pad_string(std::string s, size_t pad) +std::string SaiDump::pad_string(std::string s, size_t pad) { SWSS_LOG_ENTER(); @@ -128,7 +139,7 @@ std::string pad_string(std::string s, size_t pad) return s; } -const TableMap* get_table_map(sai_object_id_t object_id) +const TableMap* SaiDump::get_table_map(sai_object_id_t object_id) { SWSS_LOG_ENTER(); @@ -143,7 +154,7 @@ const TableMap* get_table_map(sai_object_id_t object_id) return it->second; } -void print_attributes(size_t indent, const TableMap& map) +void SaiDump::print_attributes(size_t indent, const TableMap& map) { SWSS_LOG_ENTER(); @@ -171,7 +182,7 @@ void print_attributes(size_t indent, const TableMap& map) #define GV_ROOT_COLOR "0.650 0.200 1.000" #define GV_NODE_COLOR "0.650 0.500 1.000" -void dumpGraph(const TableDump& td) +void SaiDump::dumpGraphFun(const TableDump& td) { SWSS_LOG_ENTER(); @@ -399,23 +410,171 @@ void dumpGraph(const TableDump& td) std::cout << "}" << std::endl; } -int main(int argc, char ** argv) +#define SWSS_LOG_ERROR_AND_STDERR(format, ...) { fprintf(stderr, format"\n", ##__VA_ARGS__); SWSS_LOG_ERROR(format, ##__VA_ARGS__); } + +/** + * @brief Process the input JSON file to make sure it's a valid JSON file for the JSON library. + */ +sai_status_t SaiDump::preProcessFile(const std::string file_name) { - swss::Logger::getInstance().setMinPrio(swss::Logger::SWSS_DEBUG); + SWSS_LOG_ENTER(); + + std::ifstream input_file(file_name); + + if (!input_file.is_open()) + { + SWSS_LOG_ERROR_AND_STDERR("Failed to open the input file %s.", file_name.c_str()); + return SAI_STATUS_FAILURE; + } + input_file.seekg(0, std::ios::end); // Move to the end of the file + uint64_t file_size = input_file.tellg(); // Get the current position + SWSS_LOG_NOTICE("Get %s's size %" PRIu64 " Bytes, limit: %" PRIu64 " MB.", file_name.c_str(), file_size, rdbJSonSizeLimit / 1024 / 1024); + + if (file_size >= rdbJSonSizeLimit) + { + SWSS_LOG_ERROR_AND_STDERR("Get %s's size failure or its size %" PRIu64 " >= %" PRIu64 " MB.", file_name.c_str(), file_size, rdbJSonSizeLimit / 1024 / 1024); + return SAI_STATUS_FAILURE; + } + + input_file.seekg(0); // Move to the begin of the file + + // Read the content of the input file into a string + std::string content((std::istreambuf_iterator(input_file)), + std::istreambuf_iterator()); + + content = regex_replace(content, std::regex("\\},\\{\\r"), ","); + + std::ofstream outputFile(file_name); + + if (!outputFile.is_open()) + { + SWSS_LOG_ERROR_AND_STDERR("Failed to open the output file %s.", file_name.c_str()); + return SAI_STATUS_FAILURE; + } + + //Remove the 1st and last char to make sure its format is same as previous output + if (content.size() >= 2 && content[0] == '[' && content[content.length()-1] == ']') + { + outputFile << content.substr(1, content.size()-2); + } + else + { + outputFile << content; + } + + return SAI_STATUS_SUCCESS; +} + +sai_status_t SaiDump::dumpFromRedisRdbJson() +{ SWSS_LOG_ENTER(); - swss::Logger::getInstance().setMinPrio(swss::Logger::SWSS_NOTICE); + if (SAI_STATUS_FAILURE == preProcessFile(rdbJsonFile)) + { + return SAI_STATUS_FAILURE; + } + + std::ifstream input_file(rdbJsonFile); + + if (!input_file.is_open()) + { + SWSS_LOG_ERROR_AND_STDERR("The file %s does not exist for dumping from Redis RDB JSON file.", rdbJsonFile.c_str()); + return SAI_STATUS_FAILURE; + } - swss::Logger::getInstance().setMinPrio(swss::Logger::SWSS_INFO); + try + { + // Parse the JSON data from the file (validation) + nlohmann::json jsonData; + input_file >> jsonData; - g_cmdOptions = handleCmdLine(argc, argv); + SWSS_LOG_DEBUG("JSON file is valid."); + + for (json::iterator it = jsonData.begin(); it != jsonData.end(); ++it) + { + json jj_key = it.key(); + + std::string keystr = jj_key; + std::string item_name = keystr; + size_t pos = keystr.find_first_of(":"); + + if (pos != std::string::npos) + { + if(ASIC_STATE_TABLE != keystr.substr(0, pos)) // filter out non ASIC_STATE + { + continue; + } + + item_name = keystr.substr(pos + 1); + + if (item_name.find(":") != std::string::npos) + { + item_name.replace(item_name.find_first_of(":"), 1, " "); + } + } + else + { + continue; + } + + std::cout << item_name << " " << std::endl; + + json jj = it.value(); + + if (!it->is_object()) + { + continue; + } + + TableMap map; + + for (json::iterator itt = jj.begin(); itt != jj.end(); ++itt) + { + if (itt.key() != "NULL") + { + map[itt.key()] = itt.value(); + } + } + + constexpr size_t LINE_IDENT = 4; + size_t max_len = get_max_attr_len(map); + std::string str_indent = pad_string("", LINE_IDENT); + + for (const auto&field: map) + { + std::cout << str_indent << pad_string(field.first, max_len) << " : "; + std::cout << field.second << std::endl; + } + + std::cout << std::endl; + } + + return SAI_STATUS_SUCCESS; + } + catch (std::exception &ex) + { + SWSS_LOG_ERROR_AND_STDERR("JSON file %s is invalid.", rdbJsonFile.c_str()); + SWSS_LOG_ERROR_AND_STDERR("JSON parsing error: %s.", ex.what()); + } + + return SAI_STATUS_FAILURE; +} + +void SaiDump::dumpFromRedisDb() +{ + SWSS_LOG_ENTER(); + if (rdbJsonFile.size() > 0) + { + dumpFromRedisRdbJson(); + return; + } swss::DBConnector db("ASIC_DB", 0); std::string table = ASIC_STATE_TABLE; - if (g_cmdOptions.dumpTempView) + if (dumpTempView) { table = TEMP_PREFIX + table; } @@ -423,7 +582,6 @@ int main(int argc, char ** argv) swss::Table t(&db, table); TableDump dump; - t.dump(dump); for (const auto&key: dump) @@ -441,16 +599,14 @@ int main(int argc, char ** argv) { sai_object_id_t object_id; sai_deserialize_object_id(str_object_id, object_id); - g_oid_map[object_id] = &key.second; } } - if (g_cmdOptions.dumpGraph) + if (dumpGraph) { - dumpGraph(dump); - - return EXIT_SUCCESS; + dumpGraphFun(dump); + return; } for (const auto&key: dump) @@ -458,15 +614,10 @@ int main(int argc, char ** argv) auto start = key.first.find_first_of(":"); auto str_object_type = key.first.substr(0, start); auto str_object_id = key.first.substr(start + 1); - std::cout << str_object_type << " " << str_object_id << " " << std::endl; size_t indent = 4; - print_attributes(indent, key.second); - std::cout << std::endl; } - - return EXIT_SUCCESS; } diff --git a/saidump/saidump.h b/saidump/saidump.h new file mode 100755 index 000000000..5bb5f2869 --- /dev/null +++ b/saidump/saidump.h @@ -0,0 +1,52 @@ +#pragma once + +extern "C" { +#include +} +#include +#include +#include +#include +#include +#include "swss/table.h" +#include "meta/sai_serialize.h" +#include "sairediscommon.h" +#include "swss/json.hpp" + +using namespace swss; +using json = nlohmann::json; + +// Default value: 100MB +static constexpr int64_t RDB_JSON_MAX_SIZE = 1024 * 1024 * 100; + +namespace syncd +{ + class SaiDump + { + public: + SaiDump() = default; + ~SaiDump() = default; + sai_status_t handleCmdLine(int argc, char **argv); + void dumpFromRedisDb(); + void printUsage(); + sai_status_t dumpFromRedisRdbJson(); + sai_status_t preProcessFile(const std::string file_name); + + public: + std::string rdbJsonFile; + uint64_t rdbJSonSizeLimit; + + private: + bool skipAttributes; + bool dumpTempView; + bool dumpGraph; + std::map g_oid_map; + + private: + size_t get_max_attr_len(const TableMap& map); + std::string pad_string(std::string s, size_t pad); + const TableMap* get_table_map(sai_object_id_t object_id); + void dumpGraphFun(const TableDump& td); + void print_attributes(size_t indent, const TableMap& map); + }; +} diff --git a/unittest/Makefile.am b/unittest/Makefile.am index 9be94ee0a..175555bed 100644 --- a/unittest/Makefile.am +++ b/unittest/Makefile.am @@ -1 +1 @@ -SUBDIRS = meta lib vslib syncd +SUBDIRS = meta lib vslib syncd saidump diff --git a/unittest/saidump/Makefile.am b/unittest/saidump/Makefile.am new file mode 100755 index 000000000..0519d4ca6 --- /dev/null +++ b/unittest/saidump/Makefile.am @@ -0,0 +1,13 @@ +AM_CXXFLAGS = $(SAIINC) -I$(top_srcdir)/saidump -I$(top_srcdir)/lib -I$(top_srcdir)/vslib -I$(top_srcdir)/meta + +bin_PROGRAMS = tests + +LDADD_GTEST = -L/usr/src/gtest -lgtest -lgtest_main + +tests_SOURCES = main.cpp + +tests_CXXFLAGS = $(DBGFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS_COMMON) +tests_LDFLAGS = -D_UNITTEST_ +tests_LDADD = $(LDADD_GTEST) $(top_srcdir)/saidump/libsaidump.a -lhiredis -lswsscommon -lnl-genl-3 -lnl-nf-3 -lnl-route-3 -lnl-3 -lpthread -L$(top_srcdir)/lib/.libs -lsairedis -L$(top_srcdir)/meta/.libs -lsaimetadata -lsaimeta -lzmq $(CODE_COVERAGE_LIBS) + +TESTS = tests \ No newline at end of file diff --git a/unittest/saidump/dump.json b/unittest/saidump/dump.json new file mode 100755 index 000000000..6ab042953 --- /dev/null +++ b/unittest/saidump/dump.json @@ -0,0 +1,73 @@ +[{ +"ROUTE_TABLE:10.1.0.2":{"nexthop":"3.3.3.18,3.3.3.20","ifname":"Ethernet-IB0,Ethernet-IB0","weight":"1,1"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board41:asic0:Ethernet136:5-6":{"profile":"egress_lossy_profile"}, +"SYSTEM_PORT_TABLE:ixre-egl-board41|asic0|Ethernet112":{"core_index":"0","core_port_index":"15","num_voq":"8","speed":"400000","switch_id":"18","system_port_id":"57"}, +"SYSTEM_PORT_TABLE:ixre-egl-board40|asic1|Ethernet280":{"core_index":"0","core_port_index":"18","num_voq":"8","speed":"400000","switch_id":"2","system_port_id":"39"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board41:asic1:Ethernet176:3-4":{"profile":"egress_lossless_profile"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board41:asic0:Ethernet64:5-6":{"profile":"egress_lossy_profile"}, +"SYSTEM_PORT_TABLE:ixre-egl-board40|asic0|Ethernet-IB0":{"core_index":"0","core_port_index":"19","num_voq":"8","speed":"10000","switch_id":"0","system_port_id":"19"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board40:asic1:Ethernet176:3-4":{"profile":"egress_lossless_profile"}, +"SYSTEM_PORT_TABLE:ixre-egl-board40|asic1|Ethernet208":{"core_index":"1","core_port_index":"9","num_voq":"8","speed":"400000","switch_id":"2","system_port_id":"30"}, +"INTF_TABLE:Ethernet136:10.0.0.4/31":{"scope":"global","family":"IPv4"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board40:asic1:Ethernet224:3-4":{"profile":"egress_lossless_profile"}, +"BUFFER_PROFILE_TABLE:egress_lossy_profile":{"dynamic_th":"-1","pool":"ingress_lossless_pool","size":"0"}, +"ROUTE_TABLE:10.0.0.10/31":{"nexthop":"3.3.3.2","ifname":"Ethernet-IB0"}, +"SYSTEM_PORT_TABLE:ixre-egl-board41|asic0|Ethernet-IB0":{"core_index":"0","core_port_index":"19","num_voq":"8","speed":"10000","switch_id":"18","system_port_id":"61"}, +"SYSTEM_PORT_TABLE:ixre-egl-board40|asic0|Ethernet64":{"core_index":"1","core_port_index":"9","num_voq":"8","speed":"400000","switch_id":"0","system_port_id":"9"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board41:asic0:Ethernet0:5-6":{"profile":"egress_lossy_profile"}, +"BUFFER_PG_TABLE:Ethernet80:0":{"profile":"ingress_lossy_profile"}, +"SYSTEM_PORT_TABLE:ixre-egl-board41|asic1|Ethernet-Rec1":{"core_index":"1","core_port_index":"20","num_voq":"8","speed":"10000","switch_id":"20","system_port_id":"83"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board41:asic1:Ethernet224:0-2":{"profile":"egress_lossy_profile"}, +"ROUTE_TABLE:10.0.0.4/31":{"nexthop":"0.0.0.0","ifname":"Ethernet136"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board41:asic0:Ethernet120:3-4":{"profile":"egress_lossless_profile"}, +"PORT_TABLE:Ethernet104":{"alias":"Ethernet14/1","asic_port_name":"Eth104","coreid":"0","coreportid":"14","description":"Ethernet14/1","index":"14","lanes":"32,33,34,35,36,37,38,39","mtu":"9100","numvoq":"8","pfc_asym":"off","role":"Ext","speed":"400000","tpid":"0x8100","admin_status":"down","oper_status":"down"}, +"ROUTE_TABLE:3333::3:1":{"nexthop":"::","ifname":"Ethernet-IB0"}, +"INTF_TABLE:Ethernet-IB0:3.3.3.1/32":{"scope":"global","family":"IPv4"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board41:asic1:Ethernet272:0-2":{"profile":"egress_lossy_profile"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board41:asic0:Ethernet8:5-6":{"profile":"egress_lossy_profile"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board40:asic0:Ethernet104:0-2":{"profile":"egress_lossy_profile"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board41:asic0:Ethernet96:0-2":{"profile":"egress_lossy_profile"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board41:asic0:Ethernet8:3-4":{"profile":"egress_lossless_profile"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board41:asic0:Ethernet72:0-2":{"profile":"egress_lossy_profile"}, +"LAG_MEMBER_TABLE:PortChannel102:Ethernet80":{"status":"disabled"}, +"SYSTEM_PORT_TABLE:ixre-egl-board41|asic1|Ethernet192":{"core_index":"1","core_port_index":"7","num_voq":"8","speed":"400000","switch_id":"20","system_port_id":"70"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board40:asic1:Ethernet160:3-4":{"profile":"egress_lossless_profile"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board40:asic0:Ethernet96:5-6":{"profile":"egress_lossy_profile"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board40:asic1:Ethernet264:3-4":{"profile":"egress_lossless_profile"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board40:asic1:Ethernet272:5-6":{"profile":"egress_lossy_profile"}, +"BUFFER_POOL_TABLE:ingress_lossless_pool":{"mode":"dynamic","size":"6441610000","type":"both","xoff":"11354112"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board41:asic1:Ethernet192:5-6":{"profile":"egress_lossy_profile"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board40:asic1:Ethernet272:3-4":{"profile":"egress_lossless_profile"}, +"LAG_MEMBER_TABLE:PortChannel102:Ethernet32":{"status":"disabled"}, +"SYSTEM_PORT_TABLE:ixre-egl-board41|asic1|Ethernet184":{"core_index":"1","core_port_index":"6","num_voq":"8","speed":"400000","switch_id":"20","system_port_id":"69"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board41:asic1:Ethernet208:0-2":{"profile":"egress_lossy_profile"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board40:asic1:Ethernet248:0-2":{"profile":"egress_lossy_profile"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board40:asic0:Ethernet72:0-2":{"profile":"egress_lossy_profile"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board40:asic0:Ethernet32:3-4":{"profile":"egress_lossless_profile"}, +"INTF_TABLE:PortChannel102:10.0.0.0/31":{"scope":"global","family":"IPv4"}, +"PORT_TABLE:Ethernet32":{"mtu":"9100","admin_status":"up","alias":"Ethernet5/1","asic_port_name":"Eth32","coreid":"1","coreportid":"5","description":"ARISTA01T3:Ethernet1","index":"5","lanes":"104,105,106,107,108,109,110,111","numvoq":"8","pfc_asym":"off","role":"Ext","speed":"400000","tpid":"0x8100","oper_status":"up"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board40:asic1:Ethernet168:5-6":{"profile":"egress_lossy_profile"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board41:asic1:Ethernet176:0-2":{"profile":"egress_lossy_profile"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board41:asic0:Ethernet56:5-6":{"profile":"egress_lossy_profile"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board41:asic0:Ethernet96:3-4":{"profile":"egress_lossless_profile"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board40:asic0:Ethernet120:5-6":{"profile":"egress_lossy_profile"}, +"BUFFER_PROFILE_TABLE:ingress_lossy_profile":{"dynamic_th":"0","pool":"ingress_lossless_pool","size":"1280","xon_offset":"2560"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board41:asic0:Ethernet48:0-2":{"profile":"egress_lossy_profile"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board41:asic1:Ethernet200:0-2":{"profile":"egress_lossy_profile"}, +"COPP_TABLE:queue1_group1":{"cbs":"6000","cir":"6000","meter_type":"packets","mode":"sr_tcm","queue":"1","red_action":"drop","trap_action":"trap","trap_priority":"1","trap_ids":"ip2me"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board40:asic1:Ethernet184:3-4":{"profile":"egress_lossless_profile"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board41:asic1:Ethernet248:5-6":{"profile":"egress_lossy_profile"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board41:asic1:Ethernet256:3-4":{"profile":"egress_lossless_profile"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board40:asic0:Ethernet88:5-6":{"profile":"egress_lossy_profile"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board40:asic0:Ethernet48:3-4":{"profile":"egress_lossless_profile"}, +"SYSTEM_PORT_TABLE:ixre-egl-board40|asic1|Ethernet224":{"core_index":"0","core_port_index":"11","num_voq":"8","speed":"400000","switch_id":"2","system_port_id":"32"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board41:asic0:Ethernet24:0-2":{"profile":"egress_lossy_profile"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board40:asic1:Ethernet168:0-2":{"profile":"egress_lossy_profile"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board41:asic0:Ethernet56:3-4":{"profile":"egress_lossless_profile"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board41:asic1:Ethernet272:5-6":{"profile":"egress_lossy_profile"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board40:asic1:Ethernet208:5-6":{"profile":"egress_lossy_profile"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board41:asic0:Ethernet96:5-6":{"profile":"egress_lossy_profile"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board41:asic1:Ethernet152:3-4":{"profile":"egress_lossless_profile"}, +"BUFFER_QUEUE_TABLE:ixre-egl-board41:asic0:Ethernet40:5-6":{"profile":"egress_lossy_profile"}, +"INTF_TABLE:Loopback0":{"NULL":"NULL","mac_addr":"00:00:00:00:00:00"} +}] \ No newline at end of file diff --git a/unittest/saidump/main.cpp b/unittest/saidump/main.cpp new file mode 100755 index 000000000..72d095683 --- /dev/null +++ b/unittest/saidump/main.cpp @@ -0,0 +1,63 @@ +#include +#include "meta/sai_serialize.h" +#include "saidump.h" +#define ARGVN 5 + +TEST(SaiDump, printUsage) +{ + SWSS_LOG_ENTER(); + testing::internal::CaptureStdout(); + syncd::SaiDump m_saiDump; + m_saiDump.printUsage(); + std::string output = testing::internal::GetCapturedStdout(); + EXPECT_EQ(true, output.find("Usage: saidump [-t] [-g] [-r] [-m] [-h]") != std::string::npos); +} + +TEST(SaiDump, handleCmdLine) +{ + SWSS_LOG_ENTER(); + const char *arr[] = {"saidump", "-r", "./dump.json", "-m", "100"}; + char **argv = new char *[ARGVN]; + + for (int i = 0; i < ARGVN; ++i) + { + argv[i] = const_cast(arr[i]); + } + + syncd::SaiDump m_saiDump; + m_saiDump.handleCmdLine(ARGVN, argv); + delete[] argv; + EXPECT_EQ(m_saiDump.rdbJsonFile, "./dump.json"); + EXPECT_EQ(m_saiDump.rdbJSonSizeLimit, RDB_JSON_MAX_SIZE); +} + + +TEST(SaiDump, dumpFromRedisRdbJson) +{ + SWSS_LOG_ENTER(); + syncd::SaiDump m_saiDump; + m_saiDump.rdbJsonFile = ""; + EXPECT_EQ(SAI_STATUS_FAILURE, m_saiDump.dumpFromRedisRdbJson()); + m_saiDump.rdbJsonFile = "./dump.json"; + EXPECT_EQ(SAI_STATUS_SUCCESS, m_saiDump.dumpFromRedisRdbJson()); +} + +TEST(SaiDump, preProcessFile) +{ + SWSS_LOG_ENTER(); + syncd::SaiDump m_saiDump; + EXPECT_EQ(SAI_STATUS_FAILURE, m_saiDump.preProcessFile("")); + m_saiDump.rdbJSonSizeLimit = 0; + EXPECT_EQ(SAI_STATUS_FAILURE, m_saiDump.preProcessFile("./dump.json")); + m_saiDump.rdbJSonSizeLimit = RDB_JSON_MAX_SIZE; + EXPECT_EQ(SAI_STATUS_SUCCESS, m_saiDump.preProcessFile("./dump.json")); + m_saiDump.rdbJsonFile = "./dump.json"; + EXPECT_EQ(SAI_STATUS_SUCCESS, m_saiDump.dumpFromRedisRdbJson()); +} + +int main(int argc, char *argv[]) +{ + SWSS_LOG_ENTER(); + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file