MySQL Test 初探

愛可生測試團隊負責人,專注於 MySQL 相關的測試工作。


什麼是 MySQL Test?

MySQL Test 是 MySQL 發行版本中集成 all-in-one 測試框架,用於做 mysql 服務的單元,迴歸和一致性測試,並提供了運行單元測試和創建新單元測試的工具。

框架包括一組測試用例和用於運行它們的程序:perl 腳本(mysql-test-run.pl)和 c++ 二進制(mysqltest)。

  • perl 腳本:負責控制流程,包括啟停、識別執行哪些用例、創建文件夾、收集結果等操作。
  • mysqltest:負責執行測試用例,包括讀文件,解析特定語法,執行用例。


安裝環境

OS:Ubuntu 18.04.1 LTS

1. 下載 MySQL 源碼包

本文采用的 MySQL 版本是 5.7.26,可根據需要自行選擇版本。

<code>wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.26.tar.gz/<code>

2. 安裝編譯 MySQL 源碼所需依賴包

<code>apt install make cmake gcc g++ perl \\    bison libaio-dev libncurses5 \\    libncurses5-dev libnuma-dev/<code>

如上步操作失敗或速度過慢,可修改 /etc/apt/sources.list 換成國內的源。

<code>deb https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiversedeb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiversedeb https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiversedeb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiversedeb https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiversedeb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiversedeb https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiversedeb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiversedeb https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiversedeb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse/<code>

3. 安裝 boost 1.59

需要安裝 boost 1.59 版本,系統默認的 1.65 版本不可用。

<code>wget https://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz./bootstrap.sh./b2 install/<code>

4. 配置編譯安裝

<code>cmake . -DBUILD_CONFIG=mysql_release -DCPACK_MONOLITHIC_INSTALL=ON -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DMYSQLX_TCP_PORT=33060 -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DMYSQL_TCP_PORT=3306 -DMYSQLX_UNIX_ADDR=/usr/local/mysql/mysqlx.sock -DMYSQL_DATADIR=/usr/local/mysql/data -DSYSCONFDIR=/usr/local/mysql/etc -DENABLE_DOWNLOADS=ON -DWITH_BOOST=systemmake -j4make install/<code>

編譯完成後,在 MySQL 安裝目錄生成如下目錄結構。

<code>drwxr-xr-x  2 root root   4096 Feb 12 04:24 collections/drwxr-xr-x  4 root root   4096 Feb 12 04:24 extra/drwxr-xr-x  2 root root  40960 Feb 12 04:24 include/drwxr-xr-x  4 root root   4096 Feb 12 04:24 lib/-rw-r--r--  1 root root    836 Apr 13  2019 lsan.supplrwxrwxrwx  1 root root     19 Feb 12 04:24 mtr -> ./mysql-test-run.pl*-rwxr-xr-x  1 root root  36862 Apr 13  2019 mysql-stress-test.pl*lrwxrwxrwx  1 root root     19 Feb 12 04:24 mysql-test-run -> ./mysql-test-run.pl*-rwxr-xr-x  1 root root 220158 Apr 13  2019 mysql-test-run.pl*drwxr-xr-x  2 root root  65536 Feb 16 10:22 r/drwxr-xr-x  7 root root  12288 Feb 12 04:24 std_data/drwxr-xr-x 46 root root   4096 Feb 12 04:24 suite/drwxr-xr-x  2 root root  77824 Feb 16 10:22 t/-rw-r--r--  1 root root  29730 Apr 13  2019 valgrind.suppdrwxr-xr-x  9 root root   4096 Mar  5 08:40 var//<code>


測試示例

我們通過一個最簡單的例子來說明這個框架是怎麼使用的。

1. 創建測試用例

在 mysql-test/t 目錄下創建一個文件名為 action_1st.test 的文件,

<code>root@ubuntu:/usr/local/mysql/mysql-test# vim t/action_1st.test--disable_warningsDROP TABLE IF EXISTS t1;SET @@sql_mode='NO_ENGINE_SUBSTITUTION';--enable_warningsSET SQL_WARNINGS=1;CREATE TABLE t1 (a INT);INSERT INTO t1 VALUES (1);INSERT INTO t1 VALUES (2);DROP TABLE t1;/<code>

在 /mysql-test/r 目錄下創建一個 action_1st.resul 的文件,

<code>root@ubuntu:/usr/local/mysql/mysql-test# vim r/action_1st.resultDROP TABLE IF EXISTS t1;SET @@sql_mode='NO_ENGINE_SUBSTITUTION';SET SQL_WARNINGS=1;CREATE TABLE t1 (a INT);INSERT INTO t1 VALUES (1);INSERT INTO t1 VALUES (2);DROP TABLE t1;/<code>

2. 執行並查看運行效果

執行測試用例,

<code>root@ubuntu:/usr/local/mysql/mysql-test# ./mtr action_1st.testLogging: ./mtr  action_1st.testMySQL Version 5.7.26Checking supported features... - SSL connections supportedCollecting tests...Checking leftover processes...Removing old var directory...Creating var directory '/usr/local/mysql/mysql-test/var'...Installing system database...Using parallel: 1==============================================================================TEST                                      RESULT   TIME (ms) or COMMENT--------------------------------------------------------------------------worker[1] Using MTR_BUILD_THREAD 300, with reserved ports 13000..13009worker[1] mysql-test-run: WARNING: running this>

測試用例運行時,mysqltest 會將 mysql-test/t/action_1st.test 的執行結果與 mysql-test/r/action_1st.result 作差異對比 diff。如果預期結果與實際結果不同,測試用例失敗,如上圖所示,測試用例的執行結果與預期結果一致。


分享到:


相關文章: