|
| 1 | +# Kmesh Developer Testing Guide |
| 2 | + |
| 3 | +This document provides detailed instructions for running unit tests in the Kmesh project. It outlines how and where tests are executed, how to prepare your development environment, how to troubleshoot common problems, and how to adapt based on platform. |
| 4 | + |
| 5 | +## Who Is This For |
| 6 | + |
| 7 | +- New contributors preparing to submit a pull request with test coverage |
| 8 | +- Developers verifying changes involving eBPF or Go modules |
| 9 | +- Users on Apple Silicon, UTM, Raspberry Pi, or similar ARM-based setups |
| 10 | + |
| 11 | +## Types of Tests in Kmesh |
| 12 | + |
| 13 | +| Type | Location | Command | |
| 14 | +|--------------|-------------------------------------|---------------------------------------| |
| 15 | +| Go Unit Test | All Go modules (e.g. pkg/, cmd/) | `go test ./...` | |
| 16 | +| BPF UT | test/bpf_ut/ | `make -C test/bpf_ut test` | |
| 17 | +| Shell-based | test/runtest.sh | `bash test/runtest.sh` | |
| 18 | +| Integration | test/e2e/ | `go test ./test/e2e/...` | |
| 19 | + |
| 20 | +Integration tests require a configured cluster with Kmesh deployed. |
| 21 | + |
| 22 | +## Recommended Approach |
| 23 | + |
| 24 | +| Platform | Suggested Method | |
| 25 | +|-----------------------------------|--------------------------------------| |
| 26 | +| Linux with supported kernel (5.10+) | Local testing with `make test` | |
| 27 | +| macOS on Apple Silicon / UTM / ARM | Use Dockerized testing scripts | |
| 28 | + |
| 29 | +## Running Unit Tests |
| 30 | + |
| 31 | +### Option 1: Run Inside Docker |
| 32 | + |
| 33 | +Use Docker for isolated and consistent builds across systems. |
| 34 | + |
| 35 | +```bash |
| 36 | +./hack/run-ut.sh --docker |
| 37 | +``` |
| 38 | + |
| 39 | +This script: |
| 40 | +-Builds the project inside a Docker container |
| 41 | +-Runs tests with required environment and flags |
| 42 | + |
| 43 | +If Docker permission fails, refer to the troubleshooting section. |
| 44 | + |
| 45 | +### Option 2: Run Locally (Linux only) |
| 46 | + |
| 47 | +Run tests directly on a supported kernel version. |
| 48 | + |
| 49 | +```bash |
| 50 | +./hack/run-ut.sh --local |
| 51 | +``` |
| 52 | + |
| 53 | +Or specify the Make variable directly: |
| 54 | + |
| 55 | +```bash |
| 56 | +make test RUN_IN_CONTAINER=0 |
| 57 | +``` |
| 58 | + |
| 59 | +Ensure your system includes: |
| 60 | + |
| 61 | +- clang |
| 62 | +- llvm |
| 63 | +- libelf-dev |
| 64 | +- protobuf and protobuf-c |
| 65 | +- Go version 1.22 or newer |
| 66 | + |
| 67 | +## Manual Testing |
| 68 | + |
| 69 | +Manually invoke Go unit tests using the appropriate environment configuration: |
| 70 | + |
| 71 | +```bash |
| 72 | +export ROOT_DIR=$(pwd) |
| 73 | +export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib:$ROOT_DIR/api/v2-c:$ROOT_DIR/bpf/deserialization_to_bpf_map |
| 74 | +export PKG_CONFIG_PATH=$ROOT_DIR/mk |
| 75 | +export C_INCLUDE_PATH=$ROOT_DIR/bpf/include:$C_INCLUDE_PATH |
| 76 | + |
| 77 | +go generate ./bpf/kmesh/bpf2go/... |
| 78 | +go test ./... -gcflags="all=-N -l" |
| 79 | +``` |
| 80 | + |
| 81 | +Use the gcflags option to prevent the Go compiler from inlining functions. This is required when using patching libraries like gomonkey. |
| 82 | + |
| 83 | +## Troubleshooting and Known Issues |
| 84 | + |
| 85 | +### Missing BPF Object Files |
| 86 | + |
| 87 | +Error: |
| 88 | + |
| 89 | +pattern kmeshcgroupskb_bpfel.o: no matching files found |
| 90 | + |
| 91 | +Fix: |
| 92 | + |
| 93 | +```bash |
| 94 | +cd bpf/kmesh/bpf2go |
| 95 | +go generate |
| 96 | +``` |
| 97 | + |
| 98 | +Also run: |
| 99 | + |
| 100 | +```bash |
| 101 | +cd bpf/kmesh/bpf2go/kernelnative/normal |
| 102 | +go generate |
| 103 | + |
| 104 | +cd ../dualengine |
| 105 | +go generate |
| 106 | + |
| 107 | +cd ../general |
| 108 | +go generate |
| 109 | +``` |
| 110 | + |
| 111 | +If a folder like kernelnative/enhanced is referenced but does not exist, verify its status in the upstream repository. |
| 112 | + |
| 113 | +### go.mod Version Error |
| 114 | + |
| 115 | +If you see: |
| 116 | + |
| 117 | +invalid go version '1.23.0' |
| 118 | + |
| 119 | +Edit go.mod and update: |
| 120 | + |
| 121 | +go 1.23 |
| 122 | + |
| 123 | +Do not append .0 to the version number. |
| 124 | + |
| 125 | +### Docker Permission Denied |
| 126 | + |
| 127 | +Error: |
| 128 | + |
| 129 | +docker: permission denied while connecting to /var/run/docker.sock |
| 130 | + |
| 131 | +Fix: |
| 132 | + |
| 133 | +```bash |
| 134 | +sudo usermod -aG docker $USER |
| 135 | +``` |
| 136 | + |
| 137 | +Then log out and back in, or reboot. |
| 138 | + |
| 139 | +To verify Docker access: |
| 140 | + |
| 141 | +```bash |
| 142 | +docker run hello-world |
| 143 | +``` |
| 144 | + |
| 145 | +### Missing securec Headers or libboundscheck |
| 146 | + |
| 147 | +When using Docker, the build may fail due to missing native libraries such as securec. |
| 148 | + |
| 149 | +There are two options: |
| 150 | +-Enter the Docker container and install needed packages manually |
| 151 | +-Ask maintainers to include securec in the build image or offer an official workaround |
| 152 | + |
| 153 | +## Verifying BPF Unit Tests |
| 154 | + |
| 155 | +To verify BPF-specific tests: |
| 156 | + |
| 157 | +```bash |
| 158 | +make -C test/bpf_ut test |
| 159 | +``` |
| 160 | + |
| 161 | +Install required dependencies if not already installed: |
| 162 | + |
| 163 | +```bash |
| 164 | +sudo apt install clang llvm libelf-dev |
| 165 | +``` |
| 166 | + |
| 167 | +## Verifying Shell Tests |
| 168 | + |
| 169 | +Run high-level shell-based tests using: |
| 170 | + |
| 171 | +```bash |
| 172 | +bash test/runtest.sh |
| 173 | +``` |
| 174 | + |
| 175 | +These tests expect a clean Kmesh deployment and a functioning Kubernetes with Istio and Kmesh configured. |
| 176 | + |
| 177 | +## Summary |
| 178 | + |
| 179 | +| Feature | Run Method | |
| 180 | +|--------------------------|----------------------------------------| |
| 181 | +| Run all tests (Docker) | `./hack/run-ut.sh --docker` | |
| 182 | +| Run all tests (local) | `./hack/run-ut.sh --local` | |
| 183 | +| Manual test | `go test ./...` with environment set | |
| 184 | +| Regenerate .o files | `go generate` in bpf2go directories | |
| 185 | +| Fix Docker error | Add user to docker group | |
| 186 | +| Go version requirement | Go 1.22 or higher | |
| 187 | + |
| 188 | +## Still Stuck |
| 189 | + |
| 190 | +If problems persist, file an issue on GitHub or ask questions in the Kmesh community. Some test workflows may be improved in future if official support for ARM/UTM or Docker-based BPF builds stabilizes. |
| 191 | + |
| 192 | +Contributed by: AkarshSahlot |
| 193 | +Tested on: Ubuntu (ARM64) via UTM |
| 194 | +Updated: August 2025 |
0 commit comments