Skip to content

NDCO-6047: Add tests for pfctl#29

Open
TuxPowered42 wants to merge 2 commits intomasterfrom
NDCO_6047
Open

NDCO-6047: Add tests for pfctl#29
TuxPowered42 wants to merge 2 commits intomasterfrom
NDCO_6047

Conversation

@TuxPowered42
Copy link
Copy Markdown
Contributor

No description provided.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an isolated GoogleTest suite for src/pfctl.cpp by introducing a Python-based pfctl mock and a small production change to allow overriding the pfctl executable path at runtime.

Changes:

  • Introduce tests/mock_pfctl.py and new PfctlTest fixture + test suite covering table ops, kill commands, and pf_sync_table() behavior.
  • Add pfctl_command override in src/pfctl.cpp/src/pfctl.h (defaulting to /sbin/pfctl) so tests can run against the mock.
  • Split the test CMake target into testtool_test_pool and testtool_test_pfctl, and update check target dependencies accordingly.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/pfctl_test.h Adds a GTest fixture that sets up temp state and points pfctl_command at the mock script.
tests/pfctl_test.cpp Adds extensive unit tests for pfctl helper functions using the mock’s JSON state.
tests/mock_pfctl.py Implements a minimal pfctl simulator for table and kill operations.
src/pfctl.h Exposes pfctl_command and changes pf_get_table signature to match pointer-style usage.
src/pfctl.cpp Uses pfctl_command instead of hardcoding /sbin/pfctl.
CMakeLists.txt Creates separate executables for pool vs pfctl tests and updates check target.
CLAUDE.md Adds developer documentation on build/test workflow and architecture.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +49 to +51
virtual void TearDown() override {
unlink(state_file.c_str());
}
Comment on lines +297 to +309
// Helper to build SyncedLbNode array
static void InitSyncedNodes(SyncedLbNode *nodes) {
memset(nodes, 0, sizeof(SyncedLbNode) * MAX_NODES);
}

static void SetSyncedNode(SyncedLbNode *nodes, int index,
const char *address, LbNodeState wanted,
LbNodeAdminState admin) {
if (address)
strncpy(nodes[index].ip_address[1], address, ADDR_LEN);
nodes[index].wanted_state = wanted;
nodes[index].admin_state = admin;
}
Add pfctl tests using a mock pfctl Python script

Make the pfctl binary path configurable so tests can substitute a
mock implementation. The mock script (tests/mock_pfctl.py) uses
argparse to simulate pfctl table and kill operations, storing state
in a temporary JSON file. This tests real process spawning via
popen() without requiring FreeBSD.

32 new tests cover all functions in pfctl.cpp: pfctl_run_command,
pf_table_add, pf_table_del, pf_kill_src_nodes_to,
pf_kill_states_to_rdr, pf_get_table, pf_is_in_table,
pf_table_rebalance, and pf_sync_table.

Also renames the test binary from testtool_test to
testtool_test_pool and fixes the pf_get_table declaration in
pfctl.h to match the implementation signature.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants