Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: test all supported engines on their oldest supported releases #1050

Merged
merged 3 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
#!/bin/zsh -e

golint -set_exit_status ./...
go mod tidy -v
docker compose up -d mysql mariadb postgres

export SHIORI_TEST_PG_URL="postgres://shiori:shiori@localhost:5432/shiori?sslmode=disable"
export SHIORI_TEST_MYSQL_URL="shiori:shiori@(localhost:3306)/shiori"
export SHIORI_TEST_MARIADB_URL="shiori:shiori@(localhost:3307)/shiori"

make lint

make unittest GOTESTFMT_FLAGS="-hide all"
19 changes: 17 additions & 2 deletions .github/workflows/_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:15
image: postgres:13.18
env:
POSTGRES_PASSWORD: shiori
POSTGRES_USER: shiori
Expand All @@ -23,7 +23,7 @@ jobs:
ports:
- 5432:5432
mariadb:
image: mariadb:11
image: mariadb:10.5.27
env:
MYSQL_USER: shiori
MYSQL_PASSWORD: shiori
Expand All @@ -33,6 +33,20 @@ jobs:
--health-cmd="/usr/local/bin/healthcheck.sh --connect" --health-interval 10s --health-timeout 5s --health-retries 5
ports:
- 3306:3306
mysql:
image: mysql:8.0.40
env:
MYSQL_USER: shiori
MYSQL_PASSWORD: shiori
MYSQL_DATABASE: shiori
MYSQL_ROOT_PASSWORD: shiori
options: >-
--health-cmd="mysqladmin ping"
--health-interval=10s
--health-timeout=5s
--health-retries=3
ports:
- 3307:3306

name: Go unit tests (ubuntu-latest)
steps:
Expand Down Expand Up @@ -60,6 +74,7 @@ jobs:
env:
SHIORI_TEST_PG_URL: "postgres://shiori:shiori@localhost:5432/shiori?sslmode=disable"
SHIORI_TEST_MYSQL_URL: "shiori:shiori@(localhost:3306)/shiori"
SHIORI_TEST_MARIADB_URL: "shiori:shiori@(localhost:3307)/shiori"
CGO_ENABLED: 1 # go test -race requires cgo

- run: go build -tags osusergo,netgo -ldflags="-s -w -X main.version=$(git describe --tags) -X main.date=$(date --iso-8601=seconds)"
Expand Down
14 changes: 12 additions & 2 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ services:
- shiori

postgres:
image: postgres:15
image: postgres:13.18
environment:
POSTGRES_PASSWORD: shiori
POSTGRES_USER: shiori
ports:
- "5432:5432"

mariadb:
image: mariadb:11
image: mariadb:10.5.27
environment:
MYSQL_ROOT_PASSWORD: toor
MYSQL_DATABASE: shiori
Expand All @@ -53,5 +53,15 @@ services:
ports:
- "3306:3306"

mysql:
image: mysql:8.0.40
environment:
MYSQL_ROOT_PASSWORD: toor
MYSQL_DATABASE: shiori
MYSQL_USER: shiori
MYSQL_PASSWORD: shiori
ports:
- "3307:3306"

volumes:
go-mod-cache:
9 changes: 7 additions & 2 deletions docs/Contribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,19 @@ In order to run the test suite, you need to have running a local instance of Mar
If you have docker, you can do this by running the following command with the compose file provided:

```bash
docker-compose up -d mariadb postgres
docker-compose up -d mariadb mysql postgres
```

After that, provide the `SHIORI_TEST_PG_URL` and `SHIORI_TEST_MYSQL_URL` environment variables with the connection string to the databases:
After that, provide the environment variables for the unitest to connect to the database engines:

- `SHIORI_TEST_MYSQL_URL` for MySQL
- `SHIORI_TEST_MARIADB_URL` for MariaDB
- `SHIORI_TEST_PG_URL` for PostgreSQL

```
SHIORI_TEST_PG_URL=postgres://shiori:shiori@127.0.0.1:5432/shiori?sslmode=disable
SHIORI_TEST_MYSQL_URL=shiori:shiori@tcp(127.0.0.1:3306)/shiori
SHIORI_TEST_MARIADB_URL=shiori:shiori@tcp(127.0.0.1:3307)/shiori
```

Finally, run the tests with the following command:
Expand Down
60 changes: 33 additions & 27 deletions internal/database/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,49 @@ func init() {
}
}

func mysqlTestDatabaseFactory(_ *testing.T, ctx context.Context) (DB, error) {
connString := os.Getenv("SHIORI_TEST_MYSQL_URL")
db, err := OpenMySQLDatabase(ctx, connString)
if err != nil {
return nil, err
}

var dbname string
err = db.withTx(ctx, func(tx *sqlx.Tx) error {
err := tx.QueryRow("SELECT DATABASE()").Scan(&dbname)
func mysqlTestDatabaseFactory(envKey string) testDatabaseFactory {
return func(_ *testing.T, ctx context.Context) (DB, error) {
connString := os.Getenv(envKey)
db, err := OpenMySQLDatabase(ctx, connString)
if err != nil {
return err
return nil, err
}

_, err = tx.ExecContext(ctx, "DROP DATABASE IF EXISTS "+dbname)
if err != nil {
var dbname string
err = db.withTx(ctx, func(tx *sqlx.Tx) error {
err := tx.QueryRow("SELECT DATABASE()").Scan(&dbname)
if err != nil {
return err
}

_, err = tx.ExecContext(ctx, "DROP DATABASE IF EXISTS "+dbname)
if err != nil {
return err
}

_, err = tx.ExecContext(ctx, "CREATE DATABASE "+dbname)
return err
})
if err != nil {
return nil, err
}

_, err = tx.ExecContext(ctx, "CREATE DATABASE "+dbname)
return err
})
if err != nil {
return nil, err
}
if _, err := db.Exec("USE " + dbname); err != nil {
return nil, err
}

if _, err := db.Exec("USE " + dbname); err != nil {
return nil, err
}
if err = db.Migrate(context.TODO()); err != nil {
return nil, err
}

if err = db.Migrate(context.TODO()); err != nil {
return nil, err
return db, err
}

return db, err
}

func TestMysqlsDatabase(t *testing.T) {
testDatabase(t, mysqlTestDatabaseFactory)
testDatabase(t, mysqlTestDatabaseFactory("SHIORI_TEST_MYSQL_URL"))
}

func TestMariaDBDatabase(t *testing.T) {
testDatabase(t, mysqlTestDatabaseFactory("SHIORI_TEST_MARIADB_URL"))
}
Loading