ClickHouse功能测试初探
ClickHouse功能测试初探
Functional Tests
功能测试是最常用的测试手段,许多clickhouse的特性都可以用功能测试来覆盖
功能测试入口位于ClickHouse源码目录的tests子目录下
测试用例在queres目录下,包含stateless, stateful和bug子目录,其中stateless用例无需预加载数据,执行stateful用例前, 需要预加载测试数据集hits和visits. tests/queries/bugs目录的测试用例是已知bug导致未通过的用例, 如果bug已修复, 将用例移动到tests/queries/0_stateless目录下
用例通常是一个.sql脚本或者一个.sh脚本, 用例的正确输出结果在.reference文件中,clickhouse-tests执行这些脚本得到结果后,与.reference文件内容做对比,判断用例是否执行成功
用例执行
- 执行单个用例
./clickhouse-test -c "clickhouse-client -h localhost" 01428_hash_set_nan_key - 执行所有用例
./clickhouse-test -c "clickhouse-client -h localhost"
-c参数表示启动clickhouse客户端的命令
clickhouse_test开始执行测试用例
Using queries from ‘queries’ directory
Connecting to ClickHouse server… OK
Running 2848 stateless tests.
01776_decrypt_aead_size_check: [ OK ]
01763_filter_push_down_bugs:: [ OK ]
01761_alter_decimal_zookeeper:: [ SKIPPED ] - no zookeeper
01759_optimize_skip_unused_shards_zero_shards:: [ FAIL ] - return code 170
- 不执行stateless用例
./clickhouse-test --no-stateless - 不执行stateful用例
./clickhouse-test --no-stateful
部分用例名称有带有zookeeper,shard,long关键字. zookeeper表示测试用例依赖zookeeper. shard,disktributed, global
关键字表示测试用例需要连接其他CH节点.带有long关键字的用例运行时间会超过1s
建议单机执行的用例放在代码合入门禁中,作为合入代码前必须执行的用例
带有zookeeper,shard,long关键字的用例可以放在每日构建的环境中执行,保证每日版本质量
编写用例
在0_stateless目录下添加一个新的.sql或者.sh脚本, sql测试用例执行
clickhouse-client -n --testmode < 00000_test.sql > 00000_test.reference, sh测试用例./00000_test.sh > ./00000_test.reference.得到.reference文件作为校验结果
检查语句返回的错误码
如果被测的sql语句执行时会返回一个错误码, 那么可以在sql语句末尾中添加如下判断:
SELECT decrypt('aes-128-gcm', 'text', 'key', 'IV'); -- { serverError 36 }
如果执行用例时没有错误码返回, 或者返回了其它错误码, 那么这条用例就会执行失败
测试环境搭建
测试用例需要构造的集群从programs/server/config.xml配置文件中读取:
| cluster | shard_num | shard_weight | replica_num | host_name | host_address | port |
|---|---|---|---|---|---|---|
| single_remote_shard_at_port_9001 | 1 | 1 | 1 | localhost | ::1 | 9001 |
| test_cluster_two_shards | 1 | 1 | 1 | 127.0.0.1 | 127.0.0.1 | 9000 |
| test_cluster_two_shards | 2 | 1 | 1 | 127.0.0.2 | 127.0.0.2 | 9001 |
| test_cluster_two_shards_internal_replication | 1 | 1 | 1 | 127.0.0.1 | 127.0.0.1 | 9000 |
| test_cluster_two_shards_internal_replication | 2 | 1 | 1 | 127.0.0.2 | 127.0.0.2 | 9001 |
| test_cluster_two_shards_localhost | 1 | 1 | 1 | localhost | ::1 | 9000 |
| test_cluster_two_shards_localhost | 2 | 1 | 1 | localhost | ::1 | 9000 |
| test_cluster_with_incorrect_pw | 1 | 1 | 1 | 127.0.0.1 | 127.0.0.1 | 9000 |
| test_cluster_with_incorrect_pw | 1 | 1 | 2 | 127.0.0.1 | 127.0.0.1 | 9000 |
| test_shard_localhost | 2 | 1 | 1 | localhost | ::1 | 9000 |
| test_shard_localhost_secure | 1 | 1 | 1 | localhost | ::1 | 9440 |
| test_unavailable_shard | 1 | 1 | 1 | localhost | ::1 | 9000 |
| test_unavailable_shard | 2 | 1 | 1 | localhost | ::1 | 1 |
| two_remote_shards_at_port_9001_9002 | 1 | 1 | 1 | localhost | ::1 | 9001 |
| two_remote_shards_at_port_9001_9002 | 2 | 1 | 1 | localhost | ::1 | 9002 |
| two_shards_one_local_one_remote_at_port_9001 | 1 | 1 | 1 | localhost | ::1 | 9000 |
| two_shards_one_local_one_remote_at_port_9001 | 2 | 1 | 1 | localhost | ::1 | 9001 |
从表格中的信息来看,至少需要两个clickhouse实例才能保证所有用例能够正常执行
构建门禁先考虑单机环境够支持的用例,执行时间过长的用例也不放在构建门禁中
Unit Tests
单元测试需要在构建clickhouse时开启ENABLE_TESTS编译选项,此选项默认开启. 会在源码目录下的tests子目录生成可执行文件
只需在build目录下执行ninja test或者make test命令即可执行单元测试.
单元测试分为6个可执行文件,分别覆盖对应的模块, 也可以到指定目录下直接手动执行这些可执行文件
| Test Name | Test Case Location | gtest or not |
|---|---|---|
| test_local_date_time_comparison | build/base/common/tests/local_date_time_comparison | no |
| unit_tests_libcommon | build/base/common/tests/unit_tests_libcommon | yes |
| unit_tests_dbms | build/src/unit_tests_dbms | yes |
| hashing_write_buffer | build/src/IO/tests/hashing_write_buffer | no |
| hashing_read_buffer | build/src/IO/tests/hashing_read_buffer | no |
| in_join_subqueries_preprocessor | build/src/Interpreters/tests | no |
其中unit_tests_libcommon和unit_tests_dbms使用了gtest单元测试框架, 其他测试文件是一个单纯的可执行文件.
通常情况下, 执行了功能测试后,就不必再执行单元测试, 因为功能测试更简单易用, 更容易发现功能缺陷
Integration Tests
集成测试可以测试已经配置好的clickhouse集群的场景,以及Clickhouse与MySQL, Postgres, MongoDB,等模块交互的场景。 集成测试用例运行在容器或者多个容器上
- 点赞
- 收藏
- 关注作者
评论(0)