Skip to content

Commit

Permalink
Improve coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Sun <stephens@nvidia.com>
  • Loading branch information
stephenxs committed Apr 3, 2024
1 parent 782af6d commit 9a7400d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 7 deletions.
40 changes: 33 additions & 7 deletions tests/mock_tests/flexcounter_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,25 @@ namespace flexcounter_test
bool _checkFlexCounterTableContent(std::shared_ptr<swss::Table> table, const std::string key, std::vector<swss::FieldValueTuple> entries)
{
vector<FieldValueTuple> fieldValues;
bool result = false;
if (table->get(key, fieldValues))
{
set<FieldValueTuple> fvSet(fieldValues.begin(), fieldValues.end());
set<FieldValueTuple> expectedSet(entries.begin(), entries.end());

return (fvSet == expectedSet);
result = (fvSet == expectedSet);
if (!result && gTraditionalFlexCounter)
{
// We can not mock database operation when counter model is traditional.
// As a result, the plugin field will not be inserted into the database.
// We add it into the entries fetched from database manually and redo comparing
// The plugin field must be the last one in entries vector
fvSet.insert(entries.back());
return (fvSet == expectedSet);
}
}

return false;
return result;
}

bool checkFlexCounterGroup(const std::string group, std::vector<swss::FieldValueTuple> entries)
Expand Down Expand Up @@ -195,7 +205,7 @@ namespace flexcounter_test
sai_switch_api = pold_sai_switch_api;
}

struct FlexCounterTest : public ::testing::Test
struct FlexCounterTest : public ::testing::TestWithParam<bool>
{
shared_ptr<swss::DBConnector> m_app_db;
shared_ptr<swss::DBConnector> m_config_db;
Expand Down Expand Up @@ -228,7 +238,11 @@ namespace flexcounter_test
{
::testing_db::reset();

gTraditionalFlexCounter = false;
gTraditionalFlexCounter = GetParam();
if (gTraditionalFlexCounter)
{
initFlexCounterTables();
}

_hook_sai_switch_api();

Expand Down Expand Up @@ -385,7 +399,7 @@ namespace flexcounter_test

};

TEST_F(FlexCounterTest, CounterTest)
TEST_P(FlexCounterTest, CounterTest)
{
// Check flex counter database after system initialization
ASSERT_TRUE(checkFlexCounterGroup(QUEUE_WATERMARK_STAT_COUNTER_FLEX_COUNTER_GROUP,
Expand All @@ -404,8 +418,8 @@ namespace flexcounter_test
{
{STATS_MODE_FIELD, STATS_MODE_READ},
{POLL_INTERVAL_FIELD, PORT_RATE_FLEX_COUNTER_POLLING_INTERVAL_MS},
{PORT_PLUGIN_FIELD, ""},
{FLEX_COUNTER_STATUS_FIELD, "disable"}
{FLEX_COUNTER_STATUS_FIELD, "disable"},
{PORT_PLUGIN_FIELD, ""}
}));
ASSERT_TRUE(checkFlexCounterGroup(PG_DROP_STAT_COUNTER_FLEX_COUNTER_GROUP,
{
Expand Down Expand Up @@ -606,6 +620,12 @@ namespace flexcounter_test

// Check flex counter database
auto rifOid = gIntfsOrch->m_rifsToAdd[0].m_rif_id;
Table vid2rid = Table(m_asic_db.get(), "VIDTORID");
if (gTraditionalFlexCounter)
{
const auto id = sai_serialize_object_id(rifOid);
vid2rid.set("", { {id, ""} });
}
(gIntfsOrch)->doTask(*gIntfsOrch->m_updateMapsTimer);
ASSERT_TRUE(checkFlexCounter(RIF_STAT_COUNTER_FLEX_COUNTER_GROUP, rifOid,
{
Expand Down Expand Up @@ -737,4 +757,10 @@ namespace flexcounter_test
{QUEUE_ATTR_ID_LIST, "SAI_QUEUE_ATTR_PAUSE_STATUS"}
}));
}

INSTANTIATE_TEST_CASE_P(
FlexCounterTests,
FlexCounterTest,
::testing::Values(true, false)
);
}
26 changes: 26 additions & 0 deletions tests/mock_tests/mock_table.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "table.h"
#include "producerstatetable.h"
#include "producertable.h"
#include <set>
#include <memory>

Expand Down Expand Up @@ -154,4 +155,29 @@ namespace swss
return std::shared_ptr<std::string>(NULL);
}
}

void ProducerTable::set(const std::string &key,
const std::vector<FieldValueTuple> &values,
const std::string &op,
const std::string &prefix)
{
auto &table = gDB[m_pipe->getDbId()][getTableName()];
auto iter = table.find(key);
if (iter == table.end())
{
table[key] = values;
}
else
{
merge_values(iter->second, values);
}
}

void ProducerTable::del(const std::string &key,
const std::string &op,
const std::string &prefix)
{
auto &table = gDB[m_pipe->getDbId()][getTableName()];
table.erase(key);
}
}

0 comments on commit 9a7400d

Please sign in to comment.