From 7ede3434a3dd74a3f257ffb3f80f22764193d201 Mon Sep 17 00:00:00 2001 From: Becivells <732903873@qq.com> Date: Sat, 19 Jan 2019 22:03:07 +0800 Subject: [PATCH] polish bats test cases (#192) * report check * add soar -help;change 6. * add dsn root:password@host:port/db * check has password * check has password from stdout or log --- .../test_Check_soar_report_for_html.golden | 635 ++++++++++++++++++ .../test_Check_soar_report_for_json.golden | 20 + ...test_Check_soar_report_for_markdown.golden | 28 + ...k_the_default_config_of_the_changes.golden | 2 +- ...eck_the_default_config_of_the_changes.yaml | 126 ---- test/main.bats | 78 ++- test/query.bats | 3 +- 7 files changed, 760 insertions(+), 132 deletions(-) create mode 100644 test/fixture/test_Check_soar_report_for_html.golden create mode 100644 test/fixture/test_Check_soar_report_for_json.golden create mode 100644 test/fixture/test_Check_soar_report_for_markdown.golden delete mode 100644 test/fixture/test_Check_the_default_config_of_the_changes.yaml diff --git a/test/fixture/test_Check_soar_report_for_html.golden b/test/fixture/test_Check_soar_report_for_html.golden new file mode 100644 index 0000000..05e5b4d --- /dev/null +++ b/test/fixture/test_Check_soar_report_for_html.golden @@ -0,0 +1,635 @@ + + +soar report check + + + + + +

Query: 687D590364E29465

+ +

★ ★ ★ ☆ ☆ 75分

+ +
select * from film
+
+ +

最外层 SELECT 未指定 WHERE 条件

+ + + +

不建议使用 SELECT * 类型查询

+ + + diff --git a/test/fixture/test_Check_soar_report_for_json.golden b/test/fixture/test_Check_soar_report_for_json.golden new file mode 100644 index 0000000..c030390 --- /dev/null +++ b/test/fixture/test_Check_soar_report_for_json.golden @@ -0,0 +1,20 @@ +{ + "687D590364E29465": { + "CLA.001": { + "Item": "CLA.001", + "Severity": "L4", + "Summary": "最外层 SELECT 未指定 WHERE 条件", + "Content": "SELECT 语句没有 WHERE 子句,可能检查比预期更多的行(全表扫描)。对于 SELECT COUNT(*) 类型的请求如果不要求精度,建议使用 SHOW TABLE STATUS 或 EXPLAIN 替代。", + "Case": "select id from tbl", + "Position": 0 + }, + "COL.001": { + "Item": "COL.001", + "Severity": "L1", + "Summary": "不建议使用 SELECT * 类型查询", + "Content": "当表结构变更时,使用 * 通配符选择所有列将导致查询的含义和行为会发生更改,可能导致查询返回更多的数据。", + "Case": "select * from tbl where id=1", + "Position": 0 + } + } +} diff --git a/test/fixture/test_Check_soar_report_for_markdown.golden b/test/fixture/test_Check_soar_report_for_markdown.golden new file mode 100644 index 0000000..c47aefc --- /dev/null +++ b/test/fixture/test_Check_soar_report_for_markdown.golden @@ -0,0 +1,28 @@ +# Query: 687D590364E29465 + +★ ★ ★ ☆ ☆ 75分 + +```sql + +SELECT + * +FROM + film +``` + +## 最外层 SELECT 未指定 WHERE 条件 + +* **Item:** CLA.001 + +* **Severity:** L4 + +* **Content:** SELECT 语句没有 WHERE 子句,可能检查比预期更多的行(全表扫描)。对于 SELECT COUNT(\*) 类型的请求如果不要求精度,建议使用 SHOW TABLE STATUS 或 EXPLAIN 替代。 + +## 不建议使用 SELECT * 类型查询 + +* **Item:** COL.001 + +* **Severity:** L1 + +* **Content:** 当表结构变更时,使用 \* 通配符选择所有列将导致查询的含义和行为会发生更改,可能导致查询返回更多的数据。 + diff --git a/test/fixture/test_Check_the_default_config_of_the_changes.golden b/test/fixture/test_Check_the_default_config_of_the_changes.golden index 8b7a9a2..7b23075 100644 --- a/test/fixture/test_Check_the_default_config_of_the_changes.golden +++ b/test/fixture/test_Check_the_default_config_of_the_changes.golden @@ -49,7 +49,7 @@ profiling: true trace: true explain: false delimiter: ; -log-level: 5 +log-level: 3 log-output: /dev/null report-type: html report-css: sdfs diff --git a/test/fixture/test_Check_the_default_config_of_the_changes.yaml b/test/fixture/test_Check_the_default_config_of_the_changes.yaml deleted file mode 100644 index 8b7a9a2..0000000 --- a/test/fixture/test_Check_the_default_config_of_the_changes.yaml +++ /dev/null @@ -1,126 +0,0 @@ -online-dsn: - user: online-test - password: '********' - net: tcp - addr: 192.168.12.200:3307 - schema: information_schema - charset: utf8mb4 - collation: utf8mb4_general_ci - loc: GMT - tls: dsfsfdsf - server-public-key: AAAAB3NzaC1yc2EAAAADAQABAAABAQC0JFhoEgrl5/51UHlIIlfWwhiJLR/EEeF8enGnY0PnAldLQ8STdWD8Um2BUtVjgE9COl1X3mN4vMvnSm8A6aPn66enHn0hKzwh1GvcuZNTPgeaZyGKWG0kcvbziUjAPsxxvRvvoaUspSkRYAP/9vpq3SImJKuIHCPfjnGMurKV1n7W/QfpmNjUEwYOswDjL1Ik6Jy6Lrzf8T0hQEy+dYoV4zNM0HcROCXFuu1LyG+WTch3FW660BecNT8+c4sVTHuUMXgGot8OUtwgfXrt5ZL5as7cuyKiWsLVrtrtvL3T0ZHlV8qxQ3DT1gqjSw6jBOzyDOx3jwthAbdsWjyK4Oqp - maxallowedpacket: 419437 - params: - charset: utf8mb4 - timeout: 60 - read-timeout: 70 - write-timeout: 80 - allow-native-passwords: false - allow-old-passwords: true - disable: false -test-dsn: - user: test-user - password: '********' - net: tcp - addr: 192.168.12.34:3309 - schema: information_schema - charset: utf8mb4 - collation: utf8mb4_general_ci - loc: GMT - tls: aabbbaa - server-public-key: this is a tset serverpublic - maxallowedpacket: 4194309 - params: - charset: utf8mb4 - timeout: 50 - read-timeout: 40 - write-timeout: 30 - allow-native-passwords: false - allow-old-passwords: true - disable: false -allow-online-as-test: true -drop-test-temporary: false -cleanup-test-database: true -only-syntax-check: true -sampling-statistic-target: 110 -sampling: true -sampling-condition: aaa -profiling: true -trace: true -explain: false -delimiter: ; -log-level: 5 -log-output: /dev/null -report-type: html -report-css: sdfs -report-javascript: sdfsd -report-title: SQL优化分析报告-test -markdown-extensions: 92 -markdown-html-flags: 10 -ignore-rules: -- COL.012 -rewrite-rules: -- delimiter -- orderbynull -- groupbyconst -- dmlorderby -- having -- star2columns -- insertcolumns -- distinctstar -blacklist: /tmp/blacklist -max-join-table-count: 12 -max-group-by-cols-count: 15 -max-distinct-count: 7 -max-index-cols-count: 2 -max-text-cols-count: 3 -max-total-rows: 9999991 -max-query-cost: 9992 -spaghetti-query-length: 2041 -allow-drop-index: true -max-in-count: 101 -max-index-bytes-percolumn: 762 -max-index-bytes: 3073 -allow-charsets: -- utf8 -- utf8mb4 -allow-collates: [] -allow-engines: -- innodb -- tokudb -max-index-count: 12 -max-column-count: 41 -max-value-count: 102 -index-prefix: idx_ -unique-key-prefix: uk_ -max-subquery-depth: 6 -max-varchar-length: 1022 -column-not-allow-type: -- boolean -min-cardinality: 2 -explain-sql-report-type: pretty -explain-type: extended -explain-format: traditional -explain-warn-select-type: -- "" -explain-warn-access-type: -- ALL -explain-max-keys: 31 -explain-min-keys: 10 -explain-max-rows: 10002 -explain-warn-extra: -- Using temporary -- Using filesort -explain-max-filtered: 120 -explain-warn-scalability: -- O(log(n)) -show-warnings: true -show-last-query-cost: true -query: "" -list-heuristic-rules: true -list-rewrite-rules: true -list-test-sqls: true -list-report-types: true -verbose: true -dry-run: false -max-pretty-sql-length: 1022 diff --git a/test/main.bats b/test/main.bats index 5ca83c5..74377db 100644 --- a/test/main.bats +++ b/test/main.bats @@ -28,6 +28,8 @@ load test_helper } # 5. soar 使用 config 配置文件路径是否正确 +# 13. soar -check-config 数据库连接配置检查 * +# soar 数据库测试(线上、线下、-allow-online-as-test) @test "Check config cases" { run ${SOAR_BIN_ENV} -check-config [ $status -eq 0 ] @@ -37,7 +39,7 @@ load test_helper # 6. soar 使用配置文件修改默认参数是否正确 # 注意:不启用的配置为默认配置项目 @test "Check the default config of the changes" { - ${SOAR_BIN} -config ${BATS_FIXTURE_DIRNAME}/${BATS_TEST_NAME}.yaml -print-config -log-output=/dev/null > ${BATS_TMP_DIRNAME}/${BATS_TEST_NAME}.golden + ${SOAR_BIN} -config ${BATS_FIXTURE_DIRNAME}/${BATS_TEST_NAME}.golden -print-config -log-output=/dev/null > ${BATS_TMP_DIRNAME}/${BATS_TEST_NAME}.golden run golden_diff [ $status -eq 0 ] } @@ -55,16 +57,86 @@ load test_helper run golden_diff [ $status -eq 0 ] } +# 10. report 为 json 格式是否正常 +@test "Check soar report for json" { + ${SOAR_BIN} -query "select * from film" \ + -report-type json > ${BATS_TMP_DIRNAME}/${BATS_TEST_NAME}.golden + run golden_diff + [ $status -eq 0 ] +} + +# 10. report 为 markdown 格式是否正常 +@test "Check soar report for markdown" { + ${SOAR_BIN} -query "select * from film" \ + -report-type markdown > ${BATS_TMP_DIRNAME}/${BATS_TEST_NAME}.golden + run golden_diff + [ $status -eq 0 ] +} + +# 11. report 格式 html 检查 +@test "Check soar report for html" { + ${SOAR_BIN} -query "select * from film" \ + -report-title "soar report check" \ + -report-javascript "https://cdn.bootcss.com/twitter-bootstrap/3.4.0/js/npm.js" \ + -report-css "https://cdn.bootcss.com/twitter-bootstrap/3.4.0/css/bootstrap-theme.css" \ + -report-type html > ${BATS_TMP_DIRNAME}/${BATS_TEST_NAME}.golden + run golden_diff + [ $status -eq 0 ] +} + +# 12. 黑名单功能是否正常 +# soar 的日志和黑名单的相对路径都相对于 soar 的二进制文件路径说的 +@test "Check soar blacklist" { + run ${SOAR_BIN} -blacklist ../etc/soar.blacklist -query "show processlist;" + [ $status -eq 0 ] + [ -z ${output} ] +} + +# 13. soar -check-config 数据库连接配置检查 * +# 参见 5 + +# 14. soar -help 检查 +@test "Check soar help" { + run ${SOAR_BIN} -help + [ $status -eq 2 ] + [ "${#lines[@]}" -gt 30 ] +} -# 17. 语法检查(正确) +# 15. soar 数据库测试(线上、线下、-allow-online-as-test) +# 参见 5 + +# 16. 语法检查(正确) @test "Syntax Check OK" { run ${SOAR_BIN} -query "select * from film" -only-syntax-check [ $status -eq 0 ] [ -z $ouput ] } -# 17. 语法检查(错误) +# 16. 语法检查(错误) @test "Syntax Check Error" { run ${SOAR_BIN} -query "select * frm film" -only-syntax-check [ $status -eq 1 ] [ -n $ouput ] } + +# 17. dsn 检查 +@test "Check soar test dsn root:passwd@host:port/db" { +run ${SOAR_BIN} -online-dsn="root:pase@D@192.168.12.11:3306/testDB" -print-config + [ $(expr "$output" : ".*user: root") -ne 0 ] + [ $(expr "$output" : ".*addr: 192.168.12.11:3306") -ne 0 ] + [ $(expr "$output" : ".*schema: testDB") -ne 0 ] + [ $(expr "$output" : ".*charset: utf8") -ne 0 ] +} + +# 18. 日志中是否含有密码 +@test "Check log has password" { + ${SOAR_BIN_ENV} -query "select * from film" -log-level=7 + run grep "1tIsB1g3rt" ${SOAR_BIN}.log + [ ${status} -eq 1 ] +} + +# 18. 输出中是否含有密码 +@test "Check stdout has password" { + run ${SOAR_BIN_ENV} -query "select * from film" -log-level=7 + [ $(expr "$output" : ".*1tIsB1g3rt.*") -eq 0 ] + [ ${status} -eq 0 ] +} \ No newline at end of file diff --git a/test/query.bats b/test/query.bats index debb935..33d872c 100644 --- a/test/query.bats +++ b/test/query.bats @@ -5,5 +5,4 @@ load test_helper @test "Check Query Optimizer" { run ${SOAR_BIN} -query "select * from film where length > 120" [ $status -eq 0 ] -} - +} \ No newline at end of file -- GitLab