Skip to content

Conversation

@wxnzb
Copy link
Contributor

@wxnzb wxnzb commented Jul 15, 2025

What type of PR is this?

What this PR does / why we need it:
This PR adds unit tests for sendmsg.c, cgroup_skb.c, and cgroup_sock.c using the unitTests_BUILD_CONTEXT framework. It covers multiple test paths including map write validation and control path skipping.
Which issue(s) this PR fixes:
N/A

Special notes for your reviewer:
None
Does this PR introduce a user-facing change?:


@kmesh-bot
Copy link
Collaborator

Welcome @wxnzb! It looks like this is your first PR to kmesh-net/kmesh 🎉

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Summary of Changes

Hello @wxnzb, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request initiates the process of adding comprehensive unit tests for key eBPF programs, specifically sendmsg.c, cgroup_skb.c, and cgroup_sock.c. It introduces a detailed testing proposal, integrates new test suites into the existing Go-based eBPF testing framework, and enhances the framework's capabilities by generalizing program loading and expanding support for various eBPF program types. The changes lay the groundwork for robust testing of these critical eBPF components.

Highlights

  • Unit Test Framework Proposal: A new markdown document (docs/proposal/ut_test.md) has been added, detailing a comprehensive plan for implementing unit tests for sendmsg.c, cgroup_skb.c, and cgroup_sock.c using the unitTests_BUILD_CONTEXT framework. This proposal outlines specific testing methodologies, attachment points, and triggering mechanisms for each eBPF program.
  • Integration of New Test Suites: The workload_test.go file has been updated to include new test suites for sendmsg, cgroup_skb, and cgroup_sock, integrating them into the existing eBPF unit testing framework.
  • Generalized eBPF Program Loading: A new helper function, load_bpf_prog_to_cgroup, has been introduced in workload_test.go. This function generalizes the process of loading and attaching eBPF programs to cgroups by allowing the program name to be passed as a parameter, improving reusability and flexibility for cgroup-attached eBPF tests.
  • Expanded eBPF Program Type Support: The loadAndPrepSpec function in bpf_test.go has been modified to explicitly support ebpf.SkMsg and ebpf.CGroupSockAddr program types, ensuring that eBPF programs of these types (like those in sendmsg.c and cgroup_sock.c) are not inadvertently skipped during test setup.
  • Initial Test Implementations: Basic test cases have been added for sendmsg.c, cgroup_skb.c, and cgroup_sock.c, demonstrating the loading, attachment (for cgroup programs), and triggering mechanisms for these eBPF programs within the Go test environment.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a proposal and initial implementation for unit tests for sendmsg.c, cgroup_skb.c, and cgroup_sock.c. The changes include a design document, updates to the test framework to support new eBPF program types, and the test scaffolding itself.

My review focuses on improving the quality and reliability of the new tests. Key feedback includes:

  • Replacing time.Sleep with more robust synchronization mechanisms to avoid flaky tests.
  • Adding assertions to the tests to actually verify BPF program behavior instead of just triggering them.
  • Adhering to Go's idiomatic naming conventions for functions.
  • Improving the clarity and correctness of the proposal document and test implementation details like providing descriptive test names.

Overall, this is a good start on adding important test coverage. Addressing the feedback will make the tests more robust and maintainable.

t.Run(tt.objFilename, tt.run())
}
}
func testCgroupSkb(t *testing.T) {

Choose a reason for hiding this comment

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

high

This test function, and testCgroupSock, currently only trigger the BPF programs but lack assertions to verify the behavior. Relying on manual log inspection via startLogReader is not sufficient for automated testing. Please add assertions that check BPF map states or other program outputs to validate correctness.

Additionally, the use of time.Sleep (line 847) can lead to flaky tests. It's better to use synchronization mechanisms like channels or wait groups to coordinate between the test's main thread and goroutines.

t.Run(tt.objFilename, tt.run())
}
}
func testCgroupSock(t *testing.T) {

Choose a reason for hiding this comment

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

high

This test function currently only triggers the BPF program but lacks assertions to verify the behavior. Relying on manual log inspection via startLogReader is not sufficient for automated testing. Please add assertions that check BPF map states or other program outputs to validate correctness.

Additionally, the use of time.Sleep (line 890) can lead to flaky tests. It's better to use more reliable synchronization mechanisms.

- 1
- To achieve generality for mounting to cgroup, a proName parameter can be added to this function to make it more generic.
```
func load_bpf_2_cgroup(t *testing.T, objFilename string, cgroupPath string) (*ebpf.Collection, link.Link) {

Choose a reason for hiding this comment

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

medium

The function name load_bpf_2_cgroup uses snake_case. The idiomatic way to name functions in Go is to use camelCase (e.g., loadBpfToCgroup). The same applies to the proposed new function load_bpf_prog_to_cgroup on line 157.

- Controls skipping of simulated connections.
- 4:is_managed_by_kmesh_skb(skb) == false
- Packets not managed by Kmesh should be skipped directly.
- 5:Packets not managed by Kmesh should be skipped directly.

Choose a reason for hiding this comment

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

medium

This point appears to be a duplicate of the one on the preceding line. Please remove it to improve clarity.

t.Fatal("loading collection:", err)
}
}
spec.Programs["sockops_prog"].AttachType)

Choose a reason for hiding this comment

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

medium

There appears to be a syntax error on this line with a misplaced closing parenthesis. Please correct the code snippet.

objFilename: "workload_cgroup_sock_test.o",
uts: []unitTest_BUILD_CONTEXT{
{
name: "",

Choose a reason for hiding this comment

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

medium

The test case name is empty. Please provide a descriptive name that explains what this test case is supposed to verify.

Suggested change
name: "",
name: "load_and_trigger_cgroup_connect4_prog_success",

t.Run(tt.objFilename, tt.run())
}
}
func load_bpf_prog_to_cgroup(t *testing.T, objFilename string, progName string, cgroupPath string) (*ebpf.Collection, link.Link) {

Choose a reason for hiding this comment

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

medium

In Go, function names are idiomatically written in camelCase (e.g., loadBpfProgToCgroup) rather than snake_case. Please rename this function to follow Go conventions.

Also, consider refactoring the existing load_bpf_2_cgroup function to use this more generic one, and then remove load_bpf_2_cgroup to avoid code duplication.

Suggested change
func load_bpf_prog_to_cgroup(t *testing.T, objFilename string, progName string, cgroupPath string) (*ebpf.Collection, link.Link) {
func loadBpfProgToCgroup(t *testing.T, objFilename string, progName string, cgroupPath string) (*ebpf.Collection, link.Link) {

@@ -0,0 +1 @@
#include "workload/cgroup_skb.c" No newline at end of file

Choose a reason for hiding this comment

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

medium

This file is missing a newline character at the end. It's a common convention and good practice to end files with a newline.

Suggested change
#include "workload/cgroup_skb.c"
#include "workload/cgroup_skb.c"

@@ -0,0 +1 @@
#include "workload/cgroup_sock.c" No newline at end of file

Choose a reason for hiding this comment

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

medium

This file is missing a newline character at the end. It's a common convention and good practice to end files with a newline.

Suggested change
#include "workload/cgroup_sock.c"
#include "workload/cgroup_sock.c"

@@ -0,0 +1 @@
#include "workload/sendmsg.c" No newline at end of file

Choose a reason for hiding this comment

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

medium

This file is missing a newline character at the end. It's a common convention and good practice to end files with a newline.

Suggested change
#include "workload/sendmsg.c"
#include "workload/sendmsg.c"

@wxnzb wxnzb changed the title Proposal:UT_Init Proposal UT_Init Jul 15, 2025
Copy link
Contributor

@LiZhenCheng9527 LiZhenCheng9527 left a comment

Choose a reason for hiding this comment

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

I am puzzled by your code implementation. Don't ebpf unit tests require mocking the eBPF
prog? I suggest you first submit a proposal to clarify how it will be implemented. Then, you can submit the code in subsequent PRs.

@@ -0,0 +1,220 @@
### Proposed Implementation Plan
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, I will adjust this accordingly.

- Currently, we are still considering whether it is necessary to implement triggering only in one of these cases.
#### Triggering cgroup_sock.c
```
conn, err := net.Dial("tcp", "1.1.1.1:80") // 目标地址不重要
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you not adjust after copying and pasting?

}
```
- 2
- Does this function need to be modified
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this your question?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes,it is.

@codecov
Copy link

codecov bot commented Jul 15, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 38.29%. Comparing base (f951d17) to head (716c0d3).
⚠️ Report is 166 commits behind head on main.
see 11 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d878050...716c0d3. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@LiZhenCheng9527
Copy link
Contributor

You can refer to https://github.com/kmesh-net/kmesh/blob/main/docs/proposal/ebpf_unit_test.md

@kmesh-bot kmesh-bot added size/XL and removed size/L labels Jul 22, 2025
@LiZhenCheng9527
Copy link
Contributor

Please update this PR and fix the errors. Then we can merge it

wxnzb added 9 commits October 18, 2025 22:48
Signed-off-by: sweeywu <sweetwx6@gmail.com>
Signed-off-by: sweeywu <sweetwx6@gmail.com>
Signed-off-by: sweeywu <sweetwx6@gmail.com>
Signed-off-by: sweeywu <sweetwx6@gmail.com>
Signed-off-by: sweeywu <sweetwx6@gmail.com>
Signed-off-by: sweeywu <sweetwx6@gmail.com>
Signed-off-by: sweeywu <sweetwx6@gmail.com>
Signed-off-by: sweeywu <sweetwx6@gmail.com>
Signed-off-by: sweeywu <sweetwx6@gmail.com>
@LiZhenCheng9527
Copy link
Contributor

/lgtm
/approve

@kmesh-bot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: LiZhenCheng9527

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants