Skip to content

Commit

Permalink
Add CI test
Browse files Browse the repository at this point in the history
  • Loading branch information
thomiceli committed Sep 16, 2024
1 parent b207ae1 commit 7648331
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 10 deletions.
56 changes: 51 additions & 5 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
name: "Go CI"
on:
push:
branches:
- master
- 'dev-*'
workflow_dispatch:

pull_request:
paths-ignore:
- '**.yml'

jobs:
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand All @@ -31,6 +31,7 @@ jobs:
run: make fmt check_changes

check:
name: Check
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand All @@ -47,12 +48,58 @@ jobs:
- name: Check translations
run: make check-tr

test-db:
name: Test
strategy:
fail-fast: false
matrix:
os: ["ubuntu-latest"]
go: ["1.22"]
database: [postgres, mysql]
include:
- database: postgres
image: postgres:16
port: 5432
- database: mysql
image: mysql:8
port: 3306

runs-on: ${{ matrix.os }}
services:
database:
image: ${{ matrix.image }}
ports:
- ${{ matrix.port }}
env:
POSTGRES_PASSWORD: opengist
POSTGRES_DB: opengist_test
MYSQL_ROOT_PASSWORD: opengist
MYSQL_DATABASE: opengist_test
options: >-
--health-cmd ${{ matrix.database == 'postgres' && 'pg_isready' || '"mysqladmin ping"' }}
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Set up Go ${{ matrix.go }}
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go }}

- name: Run tests
run: make test TEST_DB_TYPE=${{ matrix.database }}

test:
name: Test
strategy:
fail-fast: false
matrix:
os: ["ubuntu-latest", "macOS-latest", "windows-latest"]
go: ["1.22"]
database: ["sqlite"]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
Expand All @@ -64,5 +111,4 @@ jobs:
go-version: ${{ matrix.go }}

- name: Run tests
run: make test

run: make test TEST_DB_TYPE=${{ matrix.database }}
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
BINARY_NAME := opengist
GIT_TAG := $(shell git describe --tags)
VERSION_PKG := github.com/thomiceli/opengist/internal/config.OpengistVersion
TEST_DB_TYPE ?= sqlite

all: clean install build

Expand Down Expand Up @@ -72,7 +73,7 @@ fmt:
@go fmt ./...

test:
@go test ./... -p 1
@OPENGIST_TEST_DB=$(TEST_DB_TYPE) go test ./... -p 1

check-tr:
@bash ./scripts/check-translations.sh
4 changes: 4 additions & 0 deletions internal/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,7 @@ func DeprecationDBFilename() {
config.C.DBUri = config.C.DBFilename
}
}

func TruncateDatabase() error {
return db.Migrator().DropTable("likes", &User{}, "gists", &SSHKey{}, &AdminSetting{}, &Invitation{})
}
28 changes: 24 additions & 4 deletions internal/web/test/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"github.com/thomiceli/opengist/internal/web"
)

var databaseType string

type testServer struct {
server *web.Server
sessionCookie string
Expand Down Expand Up @@ -132,6 +134,17 @@ func structToURLValues(s interface{}) url.Values {
}

func setup(t *testing.T) {
var databaseDsn string
databaseType = os.Getenv("OPENGIST_TEST_DB")
switch databaseType {
case "sqlite":
databaseDsn = "file::memory:"
case "postgres":
databaseDsn = "postgres://root:opengist@postgres:5432/opengist_test"
case "mysql":
databaseDsn = "mysql://root:opengist@mysql:3306/opengist_test"
}

_ = os.Setenv("OPENGIST_SKIP_GIT_HOOKS", "1")

err := config.InitConfig("", io.Discard)
Expand All @@ -155,9 +168,13 @@ func setup(t *testing.T) {
err = os.MkdirAll(filepath.Join(homePath, "tmp", "repos"), 0755)
require.NoError(t, err, "Could not create tmp repos directory")

err = db.Setup("file::memory:", true)
err = db.Setup(databaseDsn, true)
require.NoError(t, err, "Could not initialize database")

if err != nil {
log.Fatal().Err(err).Msg("Could not initialize database")
}

err = memdb.Setup()
require.NoError(t, err, "Could not initialize in memory database")

Expand All @@ -168,10 +185,10 @@ func setup(t *testing.T) {
func teardown(t *testing.T, s *testServer) {
s.stop()

err := db.Close()
require.NoError(t, err, "Could not close database")
//err := db.Close()
//require.NoError(t, err, "Could not close database")

err = os.RemoveAll(path.Join(config.GetHomeDir(), "tests"))
err := os.RemoveAll(path.Join(config.GetHomeDir(), "tests"))
require.NoError(t, err, "Could not remove repos directory")

err = os.RemoveAll(path.Join(config.GetHomeDir(), "tmp", "repos"))
Expand All @@ -180,6 +197,9 @@ func teardown(t *testing.T, s *testServer) {
err = os.RemoveAll(path.Join(config.GetHomeDir(), "tmp", "sessions"))
require.NoError(t, err, "Could not remove repos directory")

err = db.TruncateDatabase()
require.NoError(t, err, "Could not truncate database")

// err = os.RemoveAll(path.Join(config.C.OpengistHome, "testsindex"))
// require.NoError(t, err, "Could not remove repos directory")

Expand Down

0 comments on commit 7648331

Please sign in to comment.