Skip to content

Commit b70c48c

Browse files
committed
fix ut_test_md
Signed-off-by: sweeywu <sweetwx6@gmail.com>
1 parent 41d9c5c commit b70c48c

File tree

1 file changed

+47
-54
lines changed

1 file changed

+47
-54
lines changed

docs/proposal/ut_test.md

Lines changed: 47 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
---
22
title: Add Unit Testing Support for eBPF Programs in Kmesh
33
authors:
4-
- "@wxnzb"
4+
- "@wxnzb"
55
reviewers:
6-
- "@lizhencheng"
6+
- "@lizhencheng"
77

88
approvers:
9-
- "@lizhencheng"
9+
- "@lizhencheng"
1010

1111
creation-date: 2025-07-15
12-
1312
---
1413

15-
1614
### Summary
1715

1816
<!--
@@ -64,10 +62,9 @@ nitty-gritty.
6462

6563
- Test Framework: sendmsg.c and cgroup_sock.c will use the unitTests_BUILD_CONTEXT framework,cgroup_skb.c use the unitTests_BPF_PROG_TEST_RUN framework
6664

67-
- Mock Functions: For each _test.c file, include the necessary mocked BPF helper functions required during testing.
65+
- Mock Functions: For each \_test.c file, include the necessary mocked BPF helper functions required during testing.
6866

6967
- Testing Methods:
70-
7168
- For branches that write to BPF maps, use coll.Maps["..."] on the Go testing side to verify whether the map contents are correct.
7269

7370
### Design Details
@@ -103,14 +100,14 @@ struct {
103100
104101
```go
105102
//load the eBPF program
106-
spec := loadAndPrepSpec(t, path.Join(*testPath, objFilePath))
107-
var (
108-
coll *ebpf.Collection
109-
err error
110-
)
111-
t.Log(path.Join(*testPath, objFilePath))
112-
// Load the eBPF collection into the kernel
113-
coll, err = ebpf.NewCollection(spec)
103+
spec := loadAndPrepSpec(t, path.Join(*testPath, objFilePath))
104+
var (
105+
coll *ebpf.Collection
106+
err error
107+
)
108+
t.Log(path.Join(*testPath, objFilePath))
109+
// Load the eBPF collection into the kernel
110+
coll, err = ebpf.NewCollection(spec)
114111
```
115112

116113
- Lookup the sockhash map and attach the sk_msg eBPF program to the map
@@ -121,15 +118,16 @@ struct {
121118
t.Log(ebpf.SockHash)
122119
prog := coll.Programs["sendmsg_prog"]
123120
err = link.RawAttachProgram(link.RawAttachProgramOptions{
124-
Attach: ebpf.AttachSkMsgVerdict,
125-
Target: sockMap.FD(),
126-
Program: prog,
127-
})
121+
Attach: ebpf.AttachSkMsgVerdict,
122+
Target: sockMap.FD(),
123+
Program: prog,
124+
})
128125
```
129126

130127
#### set up
131128

132129
- Establish a network connection, obtain the file descriptor (fd), insert the fd into the map, and sending messages through this fd will trigger the eBPF program
130+
133131
```go
134132
localIP := get_local_ipv4(t)
135133
clientPort := 12345
@@ -141,7 +139,7 @@ struct {
141139
}
142140
defer listener.Close()
143141

144-
// try to connect
142+
// try to connect
145143
conn, err := (&net.Dialer{
146144
LocalAddr: &net.TCPAddr{
147145
IP: net.ParseIP(localIP),
@@ -219,7 +217,7 @@ struct {
219217
#### set up
220218

221219
```go
222-
conn, err := net.Dial("tcp4", "...")
220+
conn, err := net.Dial("tcp4", "...")
223221
if err != nil {
224222
t.Fatalf("Dial failed: %v", err)
225223
}
@@ -229,13 +227,13 @@ defer conn.Close()
229227
### test
230228

231229
- Currently, connect4 and connect6 each have 5 test points.
232-
230+
233231
- 1
234-
232+
235233
- handle_kmesh_manage_process(&kmesh_ctx) internally calls bpf_map_update_elem(&map_of_manager, &key, &value, BPF_ANY); or err = bpf_map_delete_elem(&map_of_manager, &key); for verification.
236-
234+
237235
- When the destination address is CONTROL_CMD_IP: ENABLE_KMESH_PORT, it adds its netns_cookie to the map; when the destination address is CONTROL_CMD_IP: DISABLE_KMESH_PORT, it deletes its netns_cookie from the map.
238-
236+
239237
- Validation method:
240238

241239
- Verify the addition when inserting.
@@ -285,13 +283,13 @@ defer conn.Close()
285283
```
286284

287285
- Notes
288-
286+
289287
- Here it may be necessary to mock storage = bpf_sk_storage_get(&map_of_sock_storage, sk, 0, BPF_LOCAL_STORAGE_GET_F_CREATE); inside workload_cgroup_sock_test.c.
290288

291289
- 2
292-
290+
293291
- The function sock_traffic_control(&kmesh_ctx) is critical and internally includes
294-
292+
295293
- frontend_v = map_lookup_frontend(&frontend_k); Consider how to return frontend_v; this must return a value.
296294

297295
- By constructing a key-value pair so that the map contains this k-v, it can be found; construct the frontend map.
@@ -316,7 +314,7 @@ defer conn.Close()
316314
if ip4 == nil {
317315
t.Fatalf("invalid IPv4 address")
318316
}
319-
copy(f_key.Addr.Raw[0:4], ip4)
317+
copy(f_key.Addr.Raw[0:4], ip4)
320318
// Build the value
321319
f_val := frontend_value{
322320
UpstreamID: 1,
@@ -344,8 +342,8 @@ type service_value struct {
344342
wpIP := net.ParseIP("localIP").To4()
345343
// Build the value
346344
s_val := service_value{
347-
WpAddr: ip_addr{Raw: [16]byte{}},
348-
WaypointPort: 55555, //Build
345+
WpAddr: ip_addr{Raw: [16]byte{}},
346+
WaypointPort: 55555, //Build
349347
}
350348
```
351349

@@ -356,14 +354,14 @@ s_val := service_value{
356354
```go
357355
test:=localIp+":"+strconv.Itoa(htons(55555))
358356
- _, err = net.Listen("tcp4", "10.30.0.124:985")
359-
```
357+
```
360358

361359
- 2.2.2:
362-
360+
363361
- 2.2: If not found, perform kmesh_map_lookup_elem(&map_of_backend, key); this must return a value
364-
362+
365363
- 2.2.1:
366-
364+
367365
- Test point: waypoint == true in backend_value
368366

369367
- Construction:
@@ -379,11 +377,11 @@ type backend_value struct {
379377
wpIP := net.ParseIP(localIP).To4()
380378
// Build the value
381379
b_val := backend_value{
382-
Addr: ip_addr{Raw: [16]byte{}},
383-
ServiceCount: 0,
384-
Service: [MAX_SERVICE_COUNT]uint32{},
385-
WpAddr: ip_addr{Raw: [16]byte{}},
386-
WaypointPort: uint32(testPort),
380+
Addr: ip_addr{Raw: [16]byte{}},
381+
ServiceCount: 0,
382+
Service: [MAX_SERVICE_COUNT]uint32{},
383+
WpAddr: ip_addr{Raw: [16]byte{}},
384+
WaypointPort: uint32(testPort),
387385
}
388386
// map WpAddr
389387
copy(b_val.WpAddr.Raw[0:4], wpIP)
@@ -400,7 +398,7 @@ _, err = net.Listen("tcp4", testIpPort)
400398
```
401399

402400
- 2.2.2
403-
401+
404402
- Test point: waypoint == false in backend_value
405403

406404
- Construction:
@@ -414,11 +412,11 @@ type backend_value struct {
414412
WaypointPort uint32
415413
}
416414
b_val := backend_value{
417-
Addr: ip_addr{Raw: [16]byte{}},
418-
ServiceCount: 0,
419-
Service: [MAX_SERVICE_COUNT]uint32{},
420-
WpAddr: ip_addr{Raw: [16]byte{}},
421-
WaypointPort: 0,
415+
Addr: ip_addr{Raw: [16]byte{}},
416+
ServiceCount: 0,
417+
Service: [MAX_SERVICE_COUNT]uint32{},
418+
WpAddr: ip_addr{Raw: [16]byte{}},
419+
WaypointPort: 0,
422420
}
423421
if err := BackendMap.Update(&b_key, &b_val, ebpf.UpdateAny); err != nil {
424422
log.Fatalf("Update failed: %v", err)
@@ -459,15 +457,15 @@ expectedIP := localIP
459457
### cgroup_skb.c
460458

461459
- It is a type supported by bpf_prog_run and can be tested using the first framework.
462-
- Since it uses the same __sk_buff parameters as tc triggers, it can be written by following that example.
460+
- Since it uses the same \_\_sk_buff parameters as tc triggers, it can be written by following that example.
463461

464462
```c
465463
#include "ut_common.h"
466464
#include <linux/bpf.h>
467465
#include <linux/if_ether.h>
468466
#include <linux/in.h>
469467
#include <netinet/tcp.h>
470-
#include "workload/cgroup_skb.c"
468+
#include "workload/cgroup_skb.c"
471469
struct tcp_probe_info mock_info;
472470
bool mock_ringbuf_called = false;
473471
static __always_inline void mock_clear()
@@ -495,7 +493,7 @@ struct {
495493
};
496494

497495
// 模拟 IP + TCP 头,构造简单 TCP SYN 报文
498-
#define SRC_IP 0x0F00000A
496+
#define SRC_IP 0x0F00000A
499497
#define SRC_PORT 23445
500498
#define DEST_IP 0x0F00010A
501499
#define DEST_PORT 80
@@ -576,8 +574,3 @@ information to express the idea and why it was not acceptable.
576574
Note: This is a simplified version of kubernetes enhancement proposal template.
577575
https://github.com/kubernetes/enhancements/tree/3317d4cb548c396a430d1c1ac6625226018adf6a/keps/NNNN-kep-template
578576
-->
579-
580-
581-
582-
583-

0 commit comments

Comments
 (0)