From a8c024043ca7a762c29e3ed97b791df541b219d9 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 25 Sep 2019 20:13:18 +0800 Subject: [PATCH] Improve integration tests (#8276) * improve integration tests * fix Makefile * improve makefile and integration docs --- Makefile | 128 +++++++++++++++++++++---------- integrations/README.md | 34 +++++--- integrations/README_ZH.md | 35 ++++++--- integrations/integration_test.go | 6 +- integrations/mssql.ini.tmpl | 2 +- integrations/mysql.ini.tmpl | 2 +- integrations/mysql8.ini.tmpl | 2 +- integrations/pgsql.ini.tmpl | 2 +- 8 files changed, 143 insertions(+), 68 deletions(-) diff --git a/Makefile b/Makefile index 84fb7e1c9..b881bc955 100644 --- a/Makefile +++ b/Makefile @@ -204,59 +204,84 @@ test-sqlite\#%: integrations.sqlite.test test-sqlite-migration: migrations.sqlite.test GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/sqlite.ini ./migrations.sqlite.test -generate-ini: +generate-ini-mysql: sed -e 's|{{TEST_MYSQL_HOST}}|${TEST_MYSQL_HOST}|g' \ -e 's|{{TEST_MYSQL_DBNAME}}|${TEST_MYSQL_DBNAME}|g' \ -e 's|{{TEST_MYSQL_USERNAME}}|${TEST_MYSQL_USERNAME}|g' \ -e 's|{{TEST_MYSQL_PASSWORD}}|${TEST_MYSQL_PASSWORD}|g' \ integrations/mysql.ini.tmpl > integrations/mysql.ini + +.PHONY: test-mysql +test-mysql: integrations.mysql.test generate-ini-mysql + GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/mysql.ini ./integrations.mysql.test + +.PHONY: test-mysql\#% +test-mysql\#%: integrations.mysql.test generate-ini-mysql + GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/mysql.ini ./integrations.mysql.test -test.run $* + +.PHONY: test-mysql-migration +test-mysql-migration: migrations.mysql.test generate-ini-mysql + GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/mysql.ini ./migrations.mysql.test + + +generate-ini-mysql8: sed -e 's|{{TEST_MYSQL8_HOST}}|${TEST_MYSQL8_HOST}|g' \ -e 's|{{TEST_MYSQL8_DBNAME}}|${TEST_MYSQL8_DBNAME}|g' \ -e 's|{{TEST_MYSQL8_USERNAME}}|${TEST_MYSQL8_USERNAME}|g' \ -e 's|{{TEST_MYSQL8_PASSWORD}}|${TEST_MYSQL8_PASSWORD}|g' \ integrations/mysql8.ini.tmpl > integrations/mysql8.ini + +.PHONY: test-mysql8 +test-mysql8: integrations.mysql8.test generate-ini-mysql8 + GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/mysql8.ini ./integrations.mysql8.test + +.PHONY: test-mysql8\#% +test-mysql8\#%: integrations.mysql8.test generate-ini-mysql8 + GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/mysql8.ini ./integrations.mysql8.test -test.run $* + +.PHONY: test-mysql8-migration +test-mysql8-migration: migrations.mysql8.test generate-ini-mysql8 + GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/mysql8.ini ./migrations.mysql8.test + + +generate-ini-pgsql: sed -e 's|{{TEST_PGSQL_HOST}}|${TEST_PGSQL_HOST}|g' \ -e 's|{{TEST_PGSQL_DBNAME}}|${TEST_PGSQL_DBNAME}|g' \ -e 's|{{TEST_PGSQL_USERNAME}}|${TEST_PGSQL_USERNAME}|g' \ -e 's|{{TEST_PGSQL_PASSWORD}}|${TEST_PGSQL_PASSWORD}|g' \ integrations/pgsql.ini.tmpl > integrations/pgsql.ini + +.PHONY: test-pgsql +test-pgsql: integrations.pgsql.test generate-ini-pgsql + GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/pgsql.ini ./integrations.pgsql.test + +.PHONY: test-pgsql\#% +test-pgsql\#%: integrations.pgsql.test generate-ini-pgsql + GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/pgsql.ini ./integrations.pgsql.test -test.run $* + +.PHONY: test-pgsql-migration +test-pgsql-migration: migrations.pgsql.test generate-ini-pgsql + GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/pgsql.ini ./migrations.pgsql.test + + +generate-ini-mssql: sed -e 's|{{TEST_MSSQL_HOST}}|${TEST_MSSQL_HOST}|g' \ -e 's|{{TEST_MSSQL_DBNAME}}|${TEST_MSSQL_DBNAME}|g' \ -e 's|{{TEST_MSSQL_USERNAME}}|${TEST_MSSQL_USERNAME}|g' \ -e 's|{{TEST_MSSQL_PASSWORD}}|${TEST_MSSQL_PASSWORD}|g' \ integrations/mssql.ini.tmpl > integrations/mssql.ini -.PHONY: test-mysql -test-mysql: integrations.test generate-ini - GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/mysql.ini ./integrations.test - -.PHONY: test-mysql-migration -test-mysql-migration: migrations.test generate-ini - GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/mysql.ini ./migrations.test - -.PHONY: test-mysql8 -test-mysql8: integrations.test generate-ini - GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/mysql8.ini ./integrations.test - -.PHONY: test-mysql8-migration -test-mysql8-migration: migrations.test generate-ini - GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/mysql8.ini ./migrations.test - -.PHONY: test-pgsql -test-pgsql: integrations.test generate-ini - GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/pgsql.ini ./integrations.test - -.PHONY: test-pgsql-migration -test-pgsql-migration: migrations.test generate-ini - GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/pgsql.ini ./migrations.test - .PHONY: test-mssql -test-mssql: integrations.test generate-ini - GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/mssql.ini ./integrations.test +test-mssql: integrations.mssql.test generate-ini-mssql + GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/mssql.ini ./integrations.mssql.test + +.PHONY: test-mssql\#% +test-mssql\#%: integrations.mssql.test generate-ini-mssql + GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/mssql.ini ./integrations.mssql.test -test.run $* .PHONY: test-mssql-migration -test-mssql-migration: migrations.test generate-ini - GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/mssql.ini ./migrations.test +test-mssql-migration: migrations.mssql.test generate-ini-mssql + GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/mssql.ini ./migrations.mssql.test .PHONY: bench-sqlite @@ -264,24 +289,33 @@ bench-sqlite: integrations.sqlite.test GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/sqlite.ini ./integrations.sqlite.test -test.cpuprofile=cpu.out -test.run DontRunTests -test.bench . .PHONY: bench-mysql -bench-mysql: integrations.test generate-ini - GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/mysql.ini ./integrations.test -test.cpuprofile=cpu.out -test.run DontRunTests -test.bench . +bench-mysql: integrations.mysql.test generate-ini-mysql + GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/mysql.ini ./integrations.mysql.test -test.cpuprofile=cpu.out -test.run DontRunTests -test.bench . .PHONY: bench-mssql -bench-mssql: integrations.test generate-ini - GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/mssql.ini ./integrations.test -test.cpuprofile=cpu.out -test.run DontRunTests -test.bench . +bench-mssql: integrations.mssql.test generate-ini-mssql + GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/mssql.ini ./integrations.mssql.test -test.cpuprofile=cpu.out -test.run DontRunTests -test.bench . .PHONY: bench-pgsql -bench-pgsql: integrations.test generate-ini - GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/pgsql.ini ./integrations.test -test.cpuprofile=cpu.out -test.run DontRunTests -test.bench . +bench-pgsql: integrations.pgsql.test generate-ini-pgsql + GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/pgsql.ini ./integrations.pgsql.test -test.cpuprofile=cpu.out -test.run DontRunTests -test.bench . .PHONY: integration-test-coverage -integration-test-coverage: integrations.cover.test generate-ini +integration-test-coverage: integrations.cover.test generate-ini-mysql GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/mysql.ini ./integrations.cover.test -test.coverprofile=integration.coverage.out -integrations.test: $(SOURCES) - GO111MODULE=on $(GO) test -mod=vendor -c code.gitea.io/gitea/integrations -o integrations.test +integrations.mysql.test: $(SOURCES) + GO111MODULE=on $(GO) test -mod=vendor -c code.gitea.io/gitea/integrations -o integrations.mysql.test + +integrations.mysql8.test: $(SOURCES) + GO111MODULE=on $(GO) test -mod=vendor -c code.gitea.io/gitea/integrations -o integrations.mysql8.test + +integrations.pgsql.test: $(SOURCES) + GO111MODULE=on $(GO) test -mod=vendor -c code.gitea.io/gitea/integrations -o integrations.pgsql.test + +integrations.mssql.test: $(SOURCES) + GO111MODULE=on $(GO) test -mod=vendor -c code.gitea.io/gitea/integrations -o integrations.mssql.test integrations.sqlite.test: $(SOURCES) GO111MODULE=on $(GO) test -mod=vendor -c code.gitea.io/gitea/integrations -o integrations.sqlite.test -tags 'sqlite sqlite_unlock_notify' @@ -289,9 +323,21 @@ integrations.sqlite.test: $(SOURCES) integrations.cover.test: $(SOURCES) GO111MODULE=on $(GO) test -mod=vendor -c code.gitea.io/gitea/integrations -coverpkg $(shell echo $(PACKAGES) | tr ' ' ',') -o integrations.cover.test -.PHONY: migrations.test -migrations.test: $(SOURCES) - $(GO) test -c code.gitea.io/gitea/integrations/migration-test -o migrations.test +.PHONY: migrations.mysql.test +migrations.mysql.test: $(SOURCES) + $(GO) test -c code.gitea.io/gitea/integrations/migration-test -o migrations.mysql.test + +.PHONY: migrations.mysql8.test +migrations.mysql8.test: $(SOURCES) + $(GO) test -c code.gitea.io/gitea/integrations/migration-test -o migrations.mysql8.test + +.PHONY: migrations.pgsql.test +migrations.pgsql.test: $(SOURCES) + $(GO) test -c code.gitea.io/gitea/integrations/migration-test -o migrations.pgsql.test + +.PHONY: migrations.mssql.test +migrations.mssql.test: $(SOURCES) + $(GO) test -c code.gitea.io/gitea/integrations/migration-test -o migrations.mssql.test .PHONY: migrations.sqlite.test migrations.sqlite.test: $(SOURCES) diff --git a/integrations/README.md b/integrations/README.md index a88cec6e4..6ffd76815 100644 --- a/integrations/README.md +++ b/integrations/README.md @@ -27,31 +27,45 @@ make test-sqlite ## Run mysql integrations tests Setup a mysql database inside docker ``` -docker run -e "MYSQL_DATABASE=test" -e "MYSQL_ALLOW_EMPTY_PASSWORD=yes" --rm --name mysql mysql:5.7 #(just ctrl-c to stop db and clean the container) +docker run -e "MYSQL_DATABASE=test" -e "MYSQL_ALLOW_EMPTY_PASSWORD=yes" -p 3306:3306 --rm --name mysql mysql:5.7 #(just ctrl-c to stop db and clean the container) ``` Start tests based on the database container ``` -TEST_MYSQL_HOST="$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mysql):3306" TEST_MYSQL_DBNAME=test TEST_MYSQL_USERNAME=root TEST_MYSQL_PASSWORD='' make test-mysql +TEST_MYSQL_HOST=localhost:3306 TEST_MYSQL_DBNAME=test TEST_MYSQL_USERNAME=root TEST_MYSQL_PASSWORD='' make test-mysql ``` ## Run pgsql integrations tests Setup a pgsql database inside docker ``` -docker run -e "POSTGRES_DB=test" --rm --name pgsql postgres:9.5 #(just ctrl-c to stop db and clean the container) +docker run -e "POSTGRES_DB=test" -p 5432:5432 --rm --name pgsql postgres:9.5 #(just ctrl-c to stop db and clean the container) ``` Start tests based on the database container ``` -TEST_PGSQL_HOST=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' pgsql) TEST_PGSQL_DBNAME=test TEST_PGSQL_USERNAME=postgres TEST_PGSQL_PASSWORD=postgres make test-pgsql +TEST_PGSQL_HOST=localhost:5432 TEST_PGSQL_DBNAME=test TEST_PGSQL_USERNAME=postgres TEST_PGSQL_PASSWORD=postgres make test-pgsql +``` + +## Run mssql integrations tests +Setup a mssql database inside docker +``` +docker run -e "ACCEPT_EULA=Y" -e "MSSQL_PID=Standard" -e "SA_PASSWORD=MwantsaSecurePassword1" -p 1433:1433 --rm --name mssql microsoft/mssql-server-linux:latest #(just ctrl-c to stop db and clean the container) +``` +Start tests based on the database container +``` +TEST_MSSQL_HOST=localhost:1433 TEST_MSSQL_DBNAME=gitea_test TEST_MSSQL_USERNAME=sa TEST_MSSQL_PASSWORD=MwantsaSecurePassword1 make test-mssql ``` ## Running individual tests -Example command to run GPG test with sqlite backend: +Example command to run GPG test: + +For sqlite: + ``` -go test -c code.gitea.io/gitea/integrations \ - -o integrations.sqlite.test -tags 'sqlite' && - GITEA_ROOT="$GOPATH/src/code.gitea.io/gitea" \ - GITEA_CONF=integrations/sqlite.ini ./integrations.sqlite.test \ - -test.v -test.run GPG +make test-sqlite#GPG ``` +For other databases(replace MSSQL to MYSQL, MYSQL8, PGSQL): + +``` +TEST_MSSQL_HOST=localhost:1433 TEST_MSSQL_DBNAME=test TEST_MSSQL_USERNAME=sa TEST_MSSQL_PASSWORD=MwantsaSecurePassword1 make test-mssql#GPG +``` \ No newline at end of file diff --git a/integrations/README_ZH.md b/integrations/README_ZH.md index 62d9cccfc..fd3176456 100644 --- a/integrations/README_ZH.md +++ b/integrations/README_ZH.md @@ -26,31 +26,46 @@ make test-sqlite ## 如何使用 mysql 数据库进行集成测试 首先在docker容器里部署一个 mysql 数据库 ``` -docker run -e "MYSQL_DATABASE=test" -e "MYSQL_ALLOW_EMPTY_PASSWORD=yes" --rm --name mysql mysql:5.7 #(just ctrl-c to stop db and clean the container) +docker run -e "MYSQL_DATABASE=test" -e "MYSQL_ALLOW_EMPTY_PASSWORD=yes" -p 3306:3306 --rm --name mysql mysql:5.7 #(just ctrl-c to stop db and clean the container) ``` 之后便可以基于这个数据库进行集成测试 ``` -TEST_MYSQL_HOST="$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mysql):3306" TEST_MYSQL_DBNAME=test TEST_MYSQL_USERNAME=root TEST_MYSQL_PASSWORD='' make test-mysql +TEST_MYSQL_HOST=localhost:3306 TEST_MYSQL_DBNAME=test TEST_MYSQL_USERNAME=root TEST_MYSQL_PASSWORD='' make test-mysql ``` ## 如何使用 pgsql 数据库进行集成测试 同上,首先在 docker 容器里部署一个 pgsql 数据库 ``` -docker run -e "POSTGRES_DB=test" --rm --name pgsql postgres:9.5 #(just ctrl-c to stop db and clean the container) +docker run -e "POSTGRES_DB=test" -p 5432:5432 --rm --name pgsql postgres:9.5 #(just ctrl-c to stop db and clean the container) ``` 之后便可以基于这个数据库进行集成测试 ``` -TEST_PGSQL_HOST=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' pgsql) TEST_PGSQL_DBNAME=test TEST_PGSQL_USERNAME=postgres TEST_PGSQL_PASSWORD=postgres make test-pgsql +TEST_PGSQL_HOST=localhost:5432 TEST_PGSQL_DBNAME=test TEST_PGSQL_USERNAME=postgres TEST_PGSQL_PASSWORD=postgres make test-pgsql +``` + +## Run mssql integrations tests +同上,首先在 docker 容器里部署一个 mssql 数据库 +``` +docker run -e "ACCEPT_EULA=Y" -e "MSSQL_PID=Standard" -e "SA_PASSWORD=MwantsaSecurePassword1" -p 1433:1433 --rm --name mssql microsoft/mssql-server-linux:latest #(just ctrl-c to stop db and clean the container) +``` +之后便可以基于这个数据库进行集成测试 +``` +TEST_MSSQL_HOST=localhost:1433 TEST_MSSQL_DBNAME=gitea_test TEST_MSSQL_USERNAME=sa TEST_MSSQL_PASSWORD=MwantsaSecurePassword1 make test-mssql ``` ## 如何进行自定义的集成测试 -下面的示例展示了怎样基于 sqlite 数据库进行 GPG 测试: +下面的示例展示了怎样在集成测试中只进行 GPG 测试: + +sqlite 数据库: + ``` -go test -c code.gitea.io/gitea/integrations \ - -o integrations.sqlite.test -tags 'sqlite' && - GITEA_ROOT="$GOPATH/src/code.gitea.io/gitea" \ - GITEA_CONF=integrations/sqlite.ini ./integrations.sqlite.test \ - -test.v -test.run GPG +make test-sqlite#GPG +``` + +其它数据库(把 MSSQL 替换为 MYSQL, MYSQL8, PGSQL): + +``` +TEST_MSSQL_HOST=localhost:1433 TEST_MSSQL_DBNAME=test TEST_MSSQL_USERNAME=sa TEST_MSSQL_PASSWORD=MwantsaSecurePassword1 make test-mssql#GPG ``` diff --git a/integrations/integration_test.go b/integrations/integration_test.go index e4d506ee7..897315f2a 100644 --- a/integrations/integration_test.go +++ b/integrations/integration_test.go @@ -131,7 +131,7 @@ func initIntegrationTest() { if err != nil { log.Fatalf("sql.Open: %v", err) } - if _, err = db.Exec("CREATE DATABASE IF NOT EXISTS testgitea"); err != nil { + if _, err = db.Exec(fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s", setting.Database.Name)); err != nil { log.Fatalf("db.Exec: %v", err) } case setting.Database.UsePostgreSQL: @@ -150,7 +150,7 @@ func initIntegrationTest() { if rows.Next() { break } - if _, err = db.Exec("CREATE DATABASE testgitea"); err != nil { + if _, err = db.Exec(fmt.Sprintf("CREATE DATABASE %s", setting.Database.Name)); err != nil { log.Fatalf("db.Exec: %v", err) } case setting.Database.UseMSSQL: @@ -160,7 +160,7 @@ func initIntegrationTest() { if err != nil { log.Fatalf("sql.Open: %v", err) } - if _, err := db.Exec("If(db_id(N'gitea') IS NULL) BEGIN CREATE DATABASE gitea; END;"); err != nil { + if _, err := db.Exec(fmt.Sprintf("If(db_id(N'%s') IS NULL) BEGIN CREATE DATABASE %s; END;", setting.Database.Name, setting.Database.Name)); err != nil { log.Fatalf("db.Exec: %v", err) } defer db.Close() diff --git a/integrations/mssql.ini.tmpl b/integrations/mssql.ini.tmpl index 06d9de610..d38d038a4 100644 --- a/integrations/mssql.ini.tmpl +++ b/integrations/mssql.ini.tmpl @@ -62,7 +62,7 @@ PROVIDER_CONFIG = data/sessions-mssql [log] MODE = test,file -ROOT_PATH = sqlite-log +ROOT_PATH = mssql-log REDIRECT_MACARON_LOG = true ROUTER = , MACARON = , diff --git a/integrations/mysql.ini.tmpl b/integrations/mysql.ini.tmpl index 44ca51ff8..6eed7e157 100644 --- a/integrations/mysql.ini.tmpl +++ b/integrations/mysql.ini.tmpl @@ -62,7 +62,7 @@ PROVIDER_CONFIG = data/sessions-mysql [log] MODE = test,file -ROOT_PATH = sqlite-log +ROOT_PATH = mysql-log REDIRECT_MACARON_LOG = true ROUTER = , MACARON = , diff --git a/integrations/mysql8.ini.tmpl b/integrations/mysql8.ini.tmpl index 6db3c880f..1e14bc135 100644 --- a/integrations/mysql8.ini.tmpl +++ b/integrations/mysql8.ini.tmpl @@ -59,7 +59,7 @@ PROVIDER_CONFIG = data/sessions-mysql8 [log] MODE = test,file -ROOT_PATH = sqlite-log +ROOT_PATH = mysql8-log REDIRECT_MACARON_LOG = true ROUTER = , MACARON = , diff --git a/integrations/pgsql.ini.tmpl b/integrations/pgsql.ini.tmpl index e69198c86..cd5dc44ea 100644 --- a/integrations/pgsql.ini.tmpl +++ b/integrations/pgsql.ini.tmpl @@ -62,7 +62,7 @@ PROVIDER_CONFIG = data/sessions-pgsql [log] MODE = test,file -ROOT_PATH = sqlite-log +ROOT_PATH = pgsql-log REDIRECT_MACARON_LOG = true ROUTER = , MACARON = ,