From 47f9742b82d27d1c77c50ff37e260f527a52758f Mon Sep 17 00:00:00 2001 From: abhinav-nexthop Date: Thu, 15 Oct 2026 14:22:57 +0530 Subject: [PATCH 1/2] Add integration test for LoadBalancer.id --- .../ConfigLoadBalancingTest.cpp | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/fboss/cli/fboss2/test/integration_test/ConfigLoadBalancingTest.cpp b/fboss/cli/fboss2/test/integration_test/ConfigLoadBalancingTest.cpp index f99ff7491b5..c257b2aa497 100644 --- a/fboss/cli/fboss2/test/integration_test/ConfigLoadBalancingTest.cpp +++ b/fboss/cli/fboss2/test/integration_test/ConfigLoadBalancingTest.cpp @@ -18,6 +18,7 @@ */ #include +#include #include #include #include @@ -213,6 +214,94 @@ TEST_F(ConfigLoadBalancingTest, EcmpHashSeed) { testSeed("ecmp", kEcmpIdValue, 314159); } +// LoadBalancer.id is the key `config load-balancing ` finds the entry +// by (CmdConfigLoadBalancing.cpp findOrCreateLoadBalancer). Changing an ecmp +// attribute must update the one ECMP entry -- not append a second one -- and +// leave the LAG entry untouched, both in the running config and in the +// agent's switch state, which keys loadBalancerMaps by the id. The create half +// (an absent id gets a new entry) is covered only by the unit test +// CmdConfigLoadBalancingTestFixture.applyCreatesNewLoadBalancer. +TEST_F(ConfigLoadBalancingTest, LoadBalancerIdSelectsEntry) { + const int newSeed = 271829; + + // Running-config entries with the given id. + auto configEntries = [this](int idValue) { + std::vector out; + auto config = getRunningConfig(); + for (const auto& lb : config["sw"]["loadBalancers"]) { + if (lb.count("id") && lb["id"].asInt() == idValue) { + out.push_back(lb); + } + } + return out; + }; + // Switch-state entries with the given id. loadBalancerMaps: + // SwitchIdList -> LoadBalancerID -> LoadBalancerFields. + auto stateEntries = [this](int idValue) { + std::vector out; + auto maps = getSwitchState("loadBalancerMaps"); + for (const auto& [switchIds, lbs] : maps.items()) { + for (const auto& [key, lb] : lbs.items()) { + if (lb.count("id") && lb["id"].asInt() == idValue) { + out.push_back(lb); + } + } + } + return out; + }; + + ASSERT_EQ(configEntries(kEcmpIdValue).size(), 1) + << "expected exactly one ECMP load balancer before the test"; + const auto lagConfigBefore = configEntries(kLagIdValue); + const auto lagStateBefore = stateEntries(kLagIdValue); + const auto originalSeed = getSeed(kEcmpIdValue); + ASSERT_TRUE(!originalSeed.has_value() || *originalSeed != newSeed) + << "test seed matches the original -- pick a different one"; + + // Puts the original ECMP seed back. Armed as a guard so a failed check + // still leaves the DUT as found; runs explicitly in Step 3. + auto restore = [&]() { + runAndCommit( + {"config", + "load-balancing", + "ecmp", + "hash-seed", + originalSeed.has_value() ? std::to_string(*originalSeed) : "default"}); + }; + auto restoreGuard = folly::makeGuard(restore); + + XLOG(INFO) << "[Step 1] config load-balancing ecmp hash-seed " << newSeed; + runAndCommit( + {"config", + "load-balancing", + "ecmp", + "hash-seed", + std::to_string(newSeed)}); + + XLOG(INFO) << "[Step 2] Verifying only the ECMP entry changed..."; + auto ecmpConfig = configEntries(kEcmpIdValue); + ASSERT_EQ(ecmpConfig.size(), 1) + << "the command appended an ECMP entry instead of reusing it"; + ASSERT_TRUE(ecmpConfig[0].count("seed")); + EXPECT_EQ(ecmpConfig[0]["seed"].asInt(), newSeed); + EXPECT_EQ(configEntries(kLagIdValue), lagConfigBefore) + << "LAG load balancer changed in the running config"; + + auto ecmpState = stateEntries(kEcmpIdValue); + ASSERT_FALSE(ecmpState.empty()) << "no ECMP load balancer in switch state"; + for (const auto& lb : ecmpState) { + EXPECT_EQ(lb["seed"].asInt(), newSeed); + } + EXPECT_EQ(stateEntries(kLagIdValue), lagStateBefore) + << "LAG load balancer changed in switch state"; + + XLOG(INFO) << "[Step 3] Restoring the original ECMP seed..."; + restoreGuard.dismiss(); + restore(); + EXPECT_EQ(getSeed(kEcmpIdValue), originalSeed); + EXPECT_EQ(configEntries(kEcmpIdValue).size(), 1); +} + TEST_F(ConfigLoadBalancingTest, EcmpHashFieldsIpv4) { testFields( "ecmp", From 22e4c7a63b41996f9eae4503da8048a3cbe0e5ce Mon Sep 17 00:00:00 2001 From: abhinav-nexthop Date: Fri, 13 Nov 2026 19:34:38 +0530 Subject: [PATCH 2/2] Fold LoadBalancer.id checks into the hash-seed test Address review on the upstream PR: LoadBalancerIdSelectsEntry re-ran the same command as EcmpHashSeed, hardcoded a seed that fails on a switch already using it, and matched switch-state entries on the value's id rather than the loadBalancerMaps key. Drop the separate test and have testSeed check, for the load balancer it changes, that the running config still has exactly one entry with that id, that the switch-state entry stored under that key carries the new seed and its own id equals the key, and that the other load balancer is untouched in both. Derive the test seed from the current one so it always differs, and restore it from a scope guard so a failed check leaves the switch as found. --- .../ConfigLoadBalancingTest.cpp | 193 +++++++----------- 1 file changed, 79 insertions(+), 114 deletions(-) diff --git a/fboss/cli/fboss2/test/integration_test/ConfigLoadBalancingTest.cpp b/fboss/cli/fboss2/test/integration_test/ConfigLoadBalancingTest.cpp index c257b2aa497..b0b355bb32a 100644 --- a/fboss/cli/fboss2/test/integration_test/ConfigLoadBalancingTest.cpp +++ b/fboss/cli/fboss2/test/integration_test/ConfigLoadBalancingTest.cpp @@ -143,14 +143,64 @@ class ConfigLoadBalancingTest : public Fboss2IntegrationTest { EXPECT_EQ(getAlgorithm(idValue), original); } - // Drive `config load-balancing hash-seed `, verify running - // config matches, then restore. Uses the *numeric* seed verification - // unconditionally; the restore path handles both "had a seed" and "seed - // unset" original states. - void testSeed(const std::string& area, int idValue, int newSeed) { + // Running-config load balancers with the given id. + int countConfigLoadBalancers(int idValue) const { + auto config = getRunningConfig(); + int count = 0; + for (const auto& lb : config["sw"]["loadBalancers"]) { + if (lb.count("id") && lb["id"].asInt() == idValue) { + ++count; + } + } + return count; + } + + // Switch-state load balancers stored under map key `idValue`, one per + // switch. loadBalancerMaps: SwitchIdList -> LoadBalancerID -> + // LoadBalancerFields. + std::vector getStateLoadBalancers(int idValue) const { + std::vector out; + auto maps = getSwitchState("loadBalancerMaps"); + for (const auto& [switchIds, lbs] : maps.items()) { + for (const auto& [key, lb] : lbs.items()) { + if (key.asInt() == idValue) { + out.push_back(lb); + } + } + } + return out; + } + + // Drive `config load-balancing hash-seed `, verify the change + // in the running config and the agent's switch state, then restore. + // + // This is also the LoadBalancer.id coverage: the CLI finds the entry to + // update by id (CmdConfigLoadBalancing.cpp findOrCreateLoadBalancer) and the + // agent keys loadBalancerMaps by it. So the command must update the one + // config entry with `idValue` rather than append a second, the switch-state + // entry under that key must carry the new seed, and the load balancer with + // `otherIdValue` must be left untouched. The create half (an absent id gets + // a new entry) is covered by the unit test + // CmdConfigLoadBalancingTestFixture.applyCreatesNewLoadBalancer. + void testSeed(const std::string& area, int idValue, int otherIdValue) { + const auto original = getSeed(idValue); + // Flip the low bit of the current seed, so the test value always differs + // from whatever the DUT already uses. + const int32_t newSeed = original.has_value() ? (*original ^ 1) : 1; XLOG(INFO) << "--- " << area << " hash-seed " << newSeed << " ---"; - auto original = getSeed(idValue); - ASSERT_TRUE(!original.has_value() || *original != newSeed); + + ASSERT_EQ(countConfigLoadBalancers(idValue), 1) + << "expected exactly one load balancer with id " << idValue; + const auto otherConfigBefore = getLoadBalancer(otherIdValue); + const auto otherStateBefore = getStateLoadBalancers(otherIdValue); + + // Put the original seed back even if a check below fails. + const std::string restoreValue = + original.has_value() ? std::to_string(*original) : "default"; + auto restoreGuard = folly::makeGuard([&] { + runAndCommit( + {"config", "load-balancing", area, "hash-seed", restoreValue}); + }); runAndCommit( {"config", @@ -158,25 +208,28 @@ class ConfigLoadBalancingTest : public Fboss2IntegrationTest { area, "hash-seed", std::to_string(newSeed)}); - auto observed = getSeed(idValue); - ASSERT_TRUE(observed.has_value()); - EXPECT_EQ(*observed, newSeed); - // Restore: either original had a value or was unset (use "default"). - if (original.has_value()) { - runAndCommit( - {"config", - "load-balancing", - area, - "hash-seed", - std::to_string(*original)}); - auto restored = getSeed(idValue); - ASSERT_TRUE(restored.has_value()); - EXPECT_EQ(*restored, *original); - } else { - runAndCommit({"config", "load-balancing", area, "hash-seed", "default"}); - EXPECT_FALSE(getSeed(idValue).has_value()); + EXPECT_EQ(countConfigLoadBalancers(idValue), 1) + << "the command appended a load balancer instead of updating id " + << idValue; + EXPECT_EQ(getSeed(idValue), newSeed); + EXPECT_EQ(getLoadBalancer(otherIdValue), otherConfigBefore) + << "load balancer " << otherIdValue << " changed in the running config"; + + const auto stateLbs = getStateLoadBalancers(idValue); + EXPECT_FALSE(stateLbs.empty()) + << "no load balancer under key " << idValue << " in switch state"; + for (const auto& lb : stateLbs) { + EXPECT_EQ(lb["id"].asInt(), idValue) + << "loadBalancerMaps key does not match the entry's id"; + EXPECT_EQ(lb["seed"].asInt(), newSeed); } + EXPECT_EQ(getStateLoadBalancers(otherIdValue), otherStateBefore) + << "load balancer " << otherIdValue << " changed in switch state"; + + restoreGuard.dismiss(); + runAndCommit({"config", "load-balancing", area, "hash-seed", restoreValue}); + EXPECT_EQ(getSeed(idValue), original); } // Generic hash-fields-* test. Sets `newTokens`, verifies the running @@ -211,95 +264,7 @@ TEST_F(ConfigLoadBalancingTest, EcmpHashAlgorithm) { } TEST_F(ConfigLoadBalancingTest, EcmpHashSeed) { - testSeed("ecmp", kEcmpIdValue, 314159); -} - -// LoadBalancer.id is the key `config load-balancing ` finds the entry -// by (CmdConfigLoadBalancing.cpp findOrCreateLoadBalancer). Changing an ecmp -// attribute must update the one ECMP entry -- not append a second one -- and -// leave the LAG entry untouched, both in the running config and in the -// agent's switch state, which keys loadBalancerMaps by the id. The create half -// (an absent id gets a new entry) is covered only by the unit test -// CmdConfigLoadBalancingTestFixture.applyCreatesNewLoadBalancer. -TEST_F(ConfigLoadBalancingTest, LoadBalancerIdSelectsEntry) { - const int newSeed = 271829; - - // Running-config entries with the given id. - auto configEntries = [this](int idValue) { - std::vector out; - auto config = getRunningConfig(); - for (const auto& lb : config["sw"]["loadBalancers"]) { - if (lb.count("id") && lb["id"].asInt() == idValue) { - out.push_back(lb); - } - } - return out; - }; - // Switch-state entries with the given id. loadBalancerMaps: - // SwitchIdList -> LoadBalancerID -> LoadBalancerFields. - auto stateEntries = [this](int idValue) { - std::vector out; - auto maps = getSwitchState("loadBalancerMaps"); - for (const auto& [switchIds, lbs] : maps.items()) { - for (const auto& [key, lb] : lbs.items()) { - if (lb.count("id") && lb["id"].asInt() == idValue) { - out.push_back(lb); - } - } - } - return out; - }; - - ASSERT_EQ(configEntries(kEcmpIdValue).size(), 1) - << "expected exactly one ECMP load balancer before the test"; - const auto lagConfigBefore = configEntries(kLagIdValue); - const auto lagStateBefore = stateEntries(kLagIdValue); - const auto originalSeed = getSeed(kEcmpIdValue); - ASSERT_TRUE(!originalSeed.has_value() || *originalSeed != newSeed) - << "test seed matches the original -- pick a different one"; - - // Puts the original ECMP seed back. Armed as a guard so a failed check - // still leaves the DUT as found; runs explicitly in Step 3. - auto restore = [&]() { - runAndCommit( - {"config", - "load-balancing", - "ecmp", - "hash-seed", - originalSeed.has_value() ? std::to_string(*originalSeed) : "default"}); - }; - auto restoreGuard = folly::makeGuard(restore); - - XLOG(INFO) << "[Step 1] config load-balancing ecmp hash-seed " << newSeed; - runAndCommit( - {"config", - "load-balancing", - "ecmp", - "hash-seed", - std::to_string(newSeed)}); - - XLOG(INFO) << "[Step 2] Verifying only the ECMP entry changed..."; - auto ecmpConfig = configEntries(kEcmpIdValue); - ASSERT_EQ(ecmpConfig.size(), 1) - << "the command appended an ECMP entry instead of reusing it"; - ASSERT_TRUE(ecmpConfig[0].count("seed")); - EXPECT_EQ(ecmpConfig[0]["seed"].asInt(), newSeed); - EXPECT_EQ(configEntries(kLagIdValue), lagConfigBefore) - << "LAG load balancer changed in the running config"; - - auto ecmpState = stateEntries(kEcmpIdValue); - ASSERT_FALSE(ecmpState.empty()) << "no ECMP load balancer in switch state"; - for (const auto& lb : ecmpState) { - EXPECT_EQ(lb["seed"].asInt(), newSeed); - } - EXPECT_EQ(stateEntries(kLagIdValue), lagStateBefore) - << "LAG load balancer changed in switch state"; - - XLOG(INFO) << "[Step 3] Restoring the original ECMP seed..."; - restoreGuard.dismiss(); - restore(); - EXPECT_EQ(getSeed(kEcmpIdValue), originalSeed); - EXPECT_EQ(configEntries(kEcmpIdValue).size(), 1); + testSeed("ecmp", kEcmpIdValue, kLagIdValue); } TEST_F(ConfigLoadBalancingTest, EcmpHashFieldsIpv4) { @@ -359,7 +324,7 @@ TEST_F(ConfigLoadBalancingTest, DISABLED_LagHashAlgorithm) { } TEST_F(ConfigLoadBalancingTest, DISABLED_LagHashSeed) { - testSeed("lag", kLagIdValue, 271828); + testSeed("lag", kLagIdValue, kEcmpIdValue); } TEST_F(ConfigLoadBalancingTest, DISABLED_LagHashFieldsIpv4) {