Skip to content

Commit 2dee8e2

Browse files
committed
[doc]build and complier
Signed-off-by: Cyber-SiKu <[email protected]>
1 parent 3c37098 commit 2dee8e2

File tree

1 file changed

+275
-0
lines changed

1 file changed

+275
-0
lines changed

docs/05-Develop/03-build-and-test.md

Lines changed: 275 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,275 @@
1+
# 编译和测试
2+
3+
- [编译和测试](#编译和测试)
4+
- [编译](#编译)
5+
- [使用Docker进行编译(推荐方式)](#使用docker进行编译推荐方式)
6+
- [获取或者构建docker镜像](#获取或者构建docker镜像)
7+
- [在docker镜像中编译](#在docker镜像中编译)
8+
- [在物理机上编译(不推荐)](#在物理机上编译不推荐)
9+
- [安装依赖](#安装依赖)
10+
- [一键编译](#一键编译)
11+
- [制作镜像](#制作镜像)
12+
- [上传镜像](#上传镜像)
13+
- [测试](#测试)
14+
- [docker 容器中测试](#docker-容器中测试)
15+
- [在物理机上测试](#在物理机上测试)
16+
- [测试用例编译及执行](#测试用例编译及执行)
17+
- [编译全部模块](#编译全部模块)
18+
- [列出所有测试模块](#列出所有测试模块)
19+
- [编译对应模块的代码](#编译对应模块的代码)
20+
- [执行测试](#执行测试)
21+
- [执行单个测试模块](#执行单个测试模块)
22+
- [运行单元/集成测试](#运行单元集成测试)
23+
24+
25+
## 编译
26+
27+
**请注意:**
28+
29+
1. 如您只是想体验Curve的部署流程和基本功能,**则不需要编译Curve**,请参考 [部署](https://github.com/opencurve/curveadm/wiki)
30+
2. 本文档仅用来帮助你搭建Curve代码编译环境,便于您参与Curve的开发调试
31+
3. 以下镜像和编译过程目前仅支持 x86 系统
32+
4. 如要编译[arm分支](https://github.com/opencurve/curve/pull/2408),请根据 [Dockerfile](https://github.com/opencurve/curve/blob/master/docker/debian11/compile/Dockerfile)打包编译镜像
33+
5. 目前master分支不支持在arm系统上编译运行
34+
6. 推荐 debian 10及以上版本的操作系统,其他操作系统未经过全面测试
35+
36+
### 使用Docker进行编译(推荐方式)
37+
38+
#### 获取或者构建docker镜像
39+
40+
方法一:从docker hub镜像库中拉取docker镜像(推荐方式)
41+
42+
```bash
43+
docker pull opencurvedocker/curve-base:build-debian11
44+
```
45+
46+
方法二:手动构建docker镜像
47+
48+
使用工程目录下的 docker/debian11/compile/Dockerfile 进行构建,命令如下:
49+
50+
```bash
51+
docker build -t opencurvedocker/curve-base:build-debian11
52+
```
53+
54+
**注意:** 上述操作不建议在Curve工程目录执行,否则构建镜像时会把当前目录的文件都复制到docker镜像中,建议把Dockerfile拷贝到新建的干净目录下进行docker镜像的构建。
55+
56+
### 在docker镜像中编译
57+
58+
**注意:** 以下命令中的 `make docker``make build` 会拉起一个临时的容器,该容器会在退出后自动删除(`--rm`)。命令会映射一些目录到容器中,如果你对此有疑虑或需要一些个性化的设置,可以阅读并自行修改 `util/docker.sh`
59+
60+
```bash
61+
git clone https://github.com/opencurve/curve.git 或者 git clone https://gitee.com/mirrors/curve.git
62+
cd curve
63+
# (中国大陆可选)将外部依赖替换为国内下载点或镜像仓库,可以加快编译速度: bash replace-curve-repo.sh
64+
65+
# curve v2.0 之前
66+
bash mk-tar.sh (编译 curvebs 并打tar包)
67+
bash mk-deb.sh (编译 curvebs 并打debian包)
68+
69+
# (当前)curve v2.0 及之后
70+
# 编译 curvebs:
71+
make build stor=bs dep=1
72+
# or
73+
make dep stor=bs && make build stor=bs
74+
# 编译 curvefs:
75+
make build stor=fs dep=1
76+
# or
77+
make dep stor=fs && make build stor=fs
78+
```
79+
80+
也可以通过命令 `make docker` 进入到容器中, 然后在容器中执行编译相关的命令,执行手动编译。
81+
命令 `make ci-build` 是直接进行编译,不进入容器。
82+
83+
```bash
84+
make docker
85+
86+
# bs
87+
make ci-build stor=bs dep=1
88+
# or
89+
make ci-dep stor=bs && make ci-build stor=bs
90+
# fs
91+
make ci-build stor=fs dep=1
92+
# or
93+
make ci-dep stor=fs && make ci-build stor=fs
94+
```
95+
96+
**注意:** `mk-tar.sh``mk-deb.sh` 用于 curve v2.0 之前版本的编译打包,v2.0 版本之后不再维护。
97+
98+
## 在物理机上编译(不推荐)
99+
100+
Curve编译依赖的包括:
101+
102+
| 依赖 | 版本 |
103+
|:-- |:-- |
104+
| bazel | 4.2.2 |
105+
| gcc | 支持c++11的兼容版本 |
106+
107+
Curve的其他依赖项,均由bazel去管理,不可单独安装。
108+
109+
**注意:** 4.* 版本的 bazel 均可以成功编译 Curve 项目,其他版本不兼容。
110+
4.2.2 为推荐版本。
111+
112+
### 安装依赖
113+
114+
编译相关的软件依赖可以参考 [dockerfile](https://github.com/opencurve/curve/blob/master/docker/debian11/compile/Dockerfile) 中的安装步骤。
115+
116+
### 一键编译
117+
118+
```bash
119+
git clone https://github.com/opencurve/curve.git 或者 git clone https://gitee.com/mirrors/curve.git
120+
# (中国大陆可选)将外部依赖替换为国内下载点或镜像仓库,可以加快下载速度: bash replace-curve-repo.sh
121+
# curve v2.0 之前
122+
bash mk-tar.sh (编译 curvebs 并打tar包)
123+
bash mk-deb.sh (编译 curvebs 并打debian包)
124+
125+
# (当前)curve v2.0 及之后
126+
# 编译 tar dep 包
127+
make tar dep=1 (编译 curvebs 并打tar包)
128+
make deb dep=1 (编译 curvebs 并打debian包)
129+
# 编译 curvebs:
130+
make ci-build stor=bs dep=1
131+
# or
132+
make ci-dep stor=bs && make ci-build stor=bs
133+
# 编译 curvefs:
134+
make ci-build stor=fs dep=1
135+
# or
136+
make ci-dep stor=fs && make ci-build stor=fs
137+
```
138+
## 制作镜像
139+
140+
该步骤可以在容器内执行也可以在物理机上执行。
141+
注意若是在容器内执行,需要在执行 `docker run` 命令时添加 `-v /var/run/docker.sock:/var/run/docker.sock -v /root/.docker:/root/.docker` 参数。
142+
143+
```bash
144+
# 编译 curvebs:
145+
# 后面的tag参数可以自定义,用于上传到镜像仓库
146+
make image stor=bs tag=test
147+
# 编译 curvefs:
148+
make image stor=fs tag=test
149+
```
150+
151+
## 上传镜像
152+
153+
```bash
154+
# test 为上一步中的tag参数
155+
docker push test
156+
```
157+
158+
# 测试
159+
160+
## docker 容器中测试
161+
162+
docker 镜像 `opencurvedocker/curve-base:build-debian11` 中已经安装了测试中所有的依赖,可以直接在容器中进行测试。
163+
164+
运行以下命令会拉起一个容器并在容器中执行全部 curvebs 相关的 ci 测试:
165+
```bash
166+
bash ut.sh curvebs
167+
```
168+
169+
同样的,你也可以使用命令 `make docker` 进入到容器中,然后在容器中执行测试相关的命令(比如你对某些测试用例单独进行测试时)。
170+
171+
172+
与编译一样你也可以使用 `make docker` 命令进入到容器中,然后在容器中执行测试命令:
173+
174+
```bash
175+
make docker
176+
bash util/ut_in_image.sh curvebs
177+
```
178+
179+
**注意** 脚本 util/ut_in_image.sh 会运行 minio,如果中途失败,下次运行时需要手动终止 minio 的运行。
180+
181+
## 在物理机上测试
182+
183+
相关的软件可以参考 [dockerfile](https://github.com/opencurve/curve/blob/master/docker/debian11/compile/Dockerfile) 来安装依赖。
184+
使用以下命令在物理机上运行全部 curvebs 相关的 ci 测试:
185+
186+
```bash
187+
bash util/ut_in_image.sh curvebs
188+
```
189+
190+
## 测试用例编译及执行
191+
192+
### 编译全部模块
193+
194+
仅编译全部模块,不进行打包
195+
```bash
196+
bash ./build.sh
197+
```
198+
199+
### 列出所有测试模块
200+
201+
```bash
202+
# curvebs
203+
bazel query '//test/...'
204+
# curvefs
205+
bazel query '//curvefs/test/...'
206+
```
207+
208+
### 编译对应模块的代码
209+
210+
编译对应模块,例如test/common目录下的common-test测试:
211+
212+
```bash
213+
bazel build test/common:common-test --copt -DHAVE_ZLIB=1 --define=with_glog=true --compilation_mode=dbg --define=libunwind=true
214+
```
215+
216+
### 执行测试
217+
218+
执行测试前需要先准备好测试用例运行所需的依赖:
219+
220+
运行单元测试:
221+
- 构建对应的模块测试:
222+
```bash
223+
$ bazel build xxx/...//:xxx_test
224+
```
225+
- 运行对应的模块测试:
226+
```bash
227+
$ bazel run xxx/...//:xxx_test
228+
# 或者
229+
$ ./bazel-bin/xxx/.../xxx_test
230+
```
231+
- 编译全部测试及文件
232+
```bash
233+
$ bazel build "..."
234+
```
235+
- bazel 默认自带缓存编译, 但有时可能会失效.
236+
237+
清除项目构建缓存:
238+
```bash
239+
$ bazel clean
240+
```
241+
242+
清除项目依赖缓存(bazel 会将WORKSPACE 文件中的指定依赖项自行编译, 这部分同样也会缓存):
243+
```bash
244+
$ bazel clean --expunge
245+
```
246+
- debug 模式编译(-c 指定向bazel 传递参数), 该模式会在默认构建文件中加入调试符号, 及减少优化等级.
247+
```bash
248+
$ bazel build xxx//:xxx_test -c dbg
249+
```
250+
- 优化模式编译
251+
```bash
252+
$ bazel build xxx//:xxx_test -c opt
253+
# 优化模式下加入调试符号
254+
$ bazel build xxx//:xxx_test -c opt --copt -g
255+
```
256+
- 更多文档, 详见 [bazel docs](https://bazel.build/docs).
257+
258+
#### 执行单个测试模块
259+
260+
```bash
261+
./bazel-bin/test/common/common-test
262+
```
263+
264+
#### 运行单元/集成测试
265+
266+
bazel 编译后的可执行程序都在 `./bazel-bin` 目录下,例如 test/common 目录下的测试代码对应的测试程序为 `./bazel-bin/test/common/common-test`,可以直接运行程序进行测试。
267+
- CurveBS相关单元测试程序目录在 ./bazel-bin/test 目录下
268+
- CurveFS相关单元测试程序目录在 ./bazel-bin/curvefs/test 目录下
269+
- 集成测试在 ./bazel-bin/test/integration 目录下
270+
- NEBD相关单元测试程序在 ./bazel-bin/nebd/test 目录下
271+
- NBD相关单元测试程序在 ./bazel-bin/nbd/test 目录下
272+
273+
如果想运行所有的单元测试和集成测试,可以参考 [docker 容器中测试](#docker-容器中测试)[在物理机上测试](#在物理机上测试)
274+
275+

0 commit comments

Comments
 (0)