GH action support

This commit is contained in:
Kegan Dougal 2021-06-03 15:23:07 +01:00
parent 0dcd3fac09
commit 8d640a7c16
2 changed files with 85 additions and 13 deletions

48
.github/workflows/tests.yml vendored Normal file
View File

@ -0,0 +1,48 @@
name: Tests
on:
push:
branches: ["main"]
pull_request:
branches: ["main"]
jobs:
build:
runs-on: ubuntu-latest
# Service containers to run with `container-job`
services:
# Label used to access the service container
postgres:
# Docker Hub image
image: postgres:13-alpine
# Provide the password for postgres
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: syncv3
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v2
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.15
- name: Build
run: go build ./cmd/syncv3
- name: Test
run: go test ./...
env:
POSTGRES_HOST: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: syncv3

View File

@ -10,31 +10,55 @@ import (
var postgresConnectionString = "user=xxxxx dbname=syncv3_test sslmode=disable"
func TestMain(m *testing.M) {
func createLocalDB() string {
fmt.Println("Note: tests require a postgres install accessible to the current user")
dbName := "syncv3_test"
// cleanup if we died mid-way last time, hence don't check err output in case it was already deleted
exec.Command("dropdb", dbName).Run()
if err := exec.Command("createdb", dbName).Run(); err != nil {
fmt.Println("createdb failed: ", err)
os.Exit(2)
}
return dbName
}
func currentUser() string {
user, err := user.Current()
if err != nil {
fmt.Println("cannot get current user: ", err)
os.Exit(2)
}
postgresConnectionString = fmt.Sprintf(
"user=%s dbname=%s sslmode=disable",
user.Username, dbName,
)
return user.Username
}
if err := exec.Command("createdb", dbName).Run(); err != nil {
fmt.Println("createdb failed: ", err)
os.Exit(2)
func prepareDBConnectionString() (connStr string) {
// Required vars: user and db
// We'll try to infer from the local env if they are missing
user := os.Getenv("POSTGRES_USER")
if user == "" {
user = currentUser()
}
dbName := os.Getenv("POSTGRES_DB")
if dbName == "" {
dbName = createLocalDB()
}
connStr = fmt.Sprintf(
"user=%s dbname=%s sslmode=disable",
user, dbName,
)
// optional vars, used in CI
password := os.Getenv("POSTGRES_PASSWORD")
if password != "" {
connStr += fmt.Sprintf(" password=%s", password)
}
host := os.Getenv("POSTGRES_HOST")
if host != "" {
connStr += fmt.Sprintf(" host=%s", host)
}
return
}
func TestMain(m *testing.M) {
postgresConnectionString = prepareDBConnectionString()
exitCode := m.Run()
// cleanup
fmt.Println("cleaning up database")
exec.Command("dropdb", dbName).Run()
os.Exit(exitCode)
}