开发者笔记
关于 JSON 调试、测试和文档的说明。
开发 Redis JSON 涉及设置开发环境(可以是基于 Linux 的或基于 macOS 的)、构建 RedisJSON(提供 JSON 的 Redis 模块)、运行测试和基准测试,以及调试 JSON 模块及其测试。
克隆 git 存储库
要克隆 RedisJSON 模块及其子模块,请运行
git clone --recursive https://github.com/RedisJSON/RedisJSON.git
在隔离环境中工作
在开发中使用隔离环境有几个原因,例如保持工作站干净以及为不同的 Linux 发行版进行开发。
您可以使用虚拟机作为隔离的开发环境。要设置一个,您可以使用 Vagrant 或 Docker。
要使用 Docker 设置虚拟机
rejson=$(docker run -d -it -v $PWD:/build debian:bullseye bash)
docker exec -it $rejson bash
然后从容器中运行 cd /build
。
在这种模式下,所有安装都保留在 Docker 容器的范围内。退出容器后,您可以使用先前的 docker exec
命令重新启动它,或者将容器的状态保存到镜像并在以后恢复它
docker commit $rejson redisjson1
docker stop $rejson
rejson=$(docker run -d -it -v $PWD:/build redisjson1 bash)
docker exec -it $rejson bash
您可以将 debian:bullseye
替换为您选择的 OS。如果您使用与主机相同的 OS,您可以在构建后在主机上运行 RedisJSON 二进制文件。
安装先决条件
要构建和测试 RedisJSON,需要安装几个软件包,具体取决于底层操作系统。目前,我们支持 Ubuntu/Debian、CentOS、Fedora 和 macOS。
进入 RedisJSON
目录并运行
$ ./sbin/setup
这将在您的系统上安装各种软件包,使用本机软件包管理器和 pip。它将自行调用 sudo
,提示您进行权限确认。
如果您希望避免这种情况,您可以
- 查看
system-setup.py
并手动安装软件包, - 使用
system-setup.py --nop
显示安装命令而不执行它们, - 使用如上所述的隔离环境,
- 使用 Python 虚拟环境,因为 Python 安装在非隔离环境中使用时已知会很敏感:
python -m virtualenv venv; . ./venv/bin/activate
安装 Redis
通常,最好运行最新的 Redis 版本。
如果您的操作系统有 Redis 6.x 软件包,您可以使用操作系统软件包管理器安装它。
否则,您可以调用
$ ./deps/readies/bin/getredis
获取帮助
make help
提供了开发功能的快速摘要
make setup # install prerequisites
make build
DEBUG=1 # build debug variant
SAN=type # build with LLVM sanitizer (type=address|memory|leak|thread)
VALGRIND|VG=1 # build for testing with Valgrind
make clean # remove binary files
ALL=1 # remove binary directories
make all # build all libraries and packages
make test # run both cargo and python tests
make cargo_test # run inbuilt rust unit tests
make pytest # run flow tests using RLTest
TEST=file:name # run test matching `name` from `file`
TEST_ARGS="..." # RLTest arguments
QUICK=1 # run only general tests
GEN=1 # run general tests on a standalone Redis topology
AOF=1 # run AOF persistency tests on a standalone Redis topology
SLAVES=1 # run replication tests on standalone Redis topology
CLUSTER=1 # run general tests on a Redis Community Edition Cluster topology
VALGRIND|VG=1 # run specified tests with Valgrind
VERBOSE=1 # display more RLTest-related information
make pack # build package (RAMP file)
make upload-artifacts # copy snapshot packages to S3
OSNICK=nick # copy snapshots for specific OSNICK
make upload-release # copy release packages to S3
common options for upload operations:
STAGING=1 # copy to staging lab area (for validation)
FORCE=1 # allow operation outside CI environment
VERBOSE=1 # show more details
NOP=1 # do not copy, just print commands
make coverage # perform coverage analysis
make show-cov # show coverage analysis results (implies COV=1)
make upload-cov # upload coverage analysis results to codecov.io (implies COV=1)
make docker # build for specific Linux distribution
OSNICK=nick # Linux distribution to build for
REDIS_VER=ver # use Redis version `ver`
TEST=1 # test after build
PACK=1 # create packages
ARTIFACTS=1 # copy artifacts from docker image
PUBLISH=1 # publish (i.e. docker push) after build
make sanbox # create container for CLang Sanitizer tests
从源代码构建
运行 make build
以构建 RedisJSON。
注意
-
二进制文件放置在
target/release/
下,根据平台和构建变体。 -
RedisJSON 使用 Cargo 作为其构建系统。
make build
将同时调用 Cargo 和后续的make
命令,这些命令是完成构建所需的。
使用 make clean
删除构建的工件。make clean ALL=1
将删除整个 bin 子目录。
运行测试
有几组单元测试
- Rust 测试,集成在源代码中,由
make cargo_test
运行。 - Python 测试(由 RLTest 启用),位于
tests/pytests
中,由make pytest
运行。
您可以使用 make test
运行所有测试。要仅运行特定测试,请使用 TEST
参数。例如,运行 make test TEST=regex
。
您可以针对“嵌入式”一次性 Redis 实例或您提供的实例运行模块的测试。要使用“嵌入式”模式,您必须将 redis-server
可执行文件包含在您的 PATH
中。
您可以通过使用 REDIS_PORT
环境变量指定 Redis 端口来覆盖嵌入式服务器的生成,例如
$ # use an existing local Redis instance for testing the module
$ REDIS_PORT=6379 make test
调试
要包含调试信息,您需要在编译 RedisJSON 之前设置 DEBUG
环境变量。例如,运行 export DEBUG=1
。
您可以在单测试模式下在 Python 测试中添加断点。要设置断点,请在测试中调用 BB()
函数。