*: add standup script to run DB tests locally and hook up travis

This commit is contained in:
Eric Chiang 2016-09-22 23:23:56 -07:00 committed by Eric Chiang
parent 0f577a469a
commit 877eb3dc7b
4 changed files with 160 additions and 4 deletions

View file

@ -3,6 +3,13 @@ language: go
go: go:
- 1.7 - 1.7
services:
- postgresql
env:
- DEX_POSTGRES_DATABASE=postgres DEX_POSTGRES_USER=postgres DEX_POSTGRES_HOST="localhost"
install: install:
- go get -u github.com/golang/lint/golint - go get -u github.com/golang/lint/golint

View file

@ -0,0 +1,37 @@
# Running database tests
Running database tests locally require:
* A systemd based Linux distro.
* A recent version of [rkt](https://github.com/coreos/rkt) installed.
The `standup.sh` script in the SQL directory is used to run databases in
containers with systemd daemonizing the process.
```
$ sudo ./storage/sql/standup.sh create postgres
Starting postgres. To view progress run
journalctl -fu dex-postgres
Running as unit dex-postgres.service.
To run tests export the following environment variables:
export DEX_POSTGRES_DATABASE=postgres; export DEX_POSTGRES_USER=postgres; export DEX_POSTGRES_PASSWORD=postgres; export DEX_POSTGRES_HOST=172.16.28.3:5432
```
Exporting the variables will cause the database tests to be run, rather than
skipped.
```
$ # sqlite takes forever to compile, be sure to install test dependencies
$ go test -v -i ./storage/sql
$ go test -v ./storage/sql
```
When you're done, tear down the unit using the `standup.sh` script.
```
$ sudo ./storage/sql/standup.sh destroy postgres
```

View file

@ -10,7 +10,6 @@ DOCKER_IMAGE=$(DOCKER_REPO):$(VERSION)
export GOBIN=$(PWD)/bin export GOBIN=$(PWD)/bin
export GO15VENDOREXPERIMENT=1 export GO15VENDOREXPERIMENT=1
export CGO_ENABLED:=0
LD_FLAGS="-w -X $(REPO_PATH)/version.Version=$(VERSION)" LD_FLAGS="-w -X $(REPO_PATH)/version.Version=$(VERSION)"
@ -26,10 +25,12 @@ bin/example-app: FORCE
@go install -ldflags $(LD_FLAGS) $(REPO_PATH)/cmd/example-app @go install -ldflags $(LD_FLAGS) $(REPO_PATH)/cmd/example-app
test: test:
@go test $(shell go list ./... | grep -v '/vendor/') @go test -v -i $(shell go list ./... | grep -v '/vendor/')
@go test -v $(shell go list ./... | grep -v '/vendor/')
testrace: testrace:
@CGO_ENABLED=1 go test --race $(shell go list ./... | grep -v '/vendor/') @go test -v -i --race $(shell go list ./... | grep -v '/vendor/')
@go test -v --race $(shell go list ./... | grep -v '/vendor/')
vet: vet:
@go vet $(shell go list ./... | grep -v '/vendor/') @go vet $(shell go list ./... | grep -v '/vendor/')
@ -39,7 +40,7 @@ fmt:
lint: lint:
@for package in $(shell go list ./... | grep -v '/vendor/' | grep -v 'api/apipb'); do \ @for package in $(shell go list ./... | grep -v '/vendor/' | grep -v 'api/apipb'); do \
golint $$package; \ golint -set_exit_status $$package; \
done done
server/templates_default.go: $(wildcard web/templates/**) server/templates_default.go: $(wildcard web/templates/**)

111
storage/sql/standup.sh Executable file
View file

@ -0,0 +1,111 @@
#!/bin/bash
if [ "$EUID" -ne 0 ]
then echo "Please run as root"
exit
fi
function usage {
cat << EOF >> /dev/stderr
Usage: sudo ./standup.sh [create|destroy] [postgres|mysql|cockroach]
This is a script for standing up test databases. It uses systemd to daemonize
rkt containers running on a local loopback IP.
The general workflow is to create a daemonized container, use the output to set
the test environment variables, run the tests, then destroy the container.
sudo ./standup.sh create postgres
# Copy environment variables and run tests.
go test -v -i # always install test dependencies
go test -v
sudo ./standup.sh destroy postgres
EOF
exit 2
}
function main {
if [ "$#" -ne 2 ]; then
usage
exit 2
fi
case "$1" in
"create")
case "$2" in
"postgres")
create_postgres;;
"mysql")
create_mysql;;
*)
usage
exit 2
;;
esac
;;
"destroy")
case "$2" in
"postgres")
destroy_postgres;;
"mysql")
destroy_mysql;;
*)
usage
exit 2
;;
esac
;;
*)
usage
exit 2
;;
esac
}
function wait_for_file {
while [ ! -f $1 ]; do
sleep 1
done
}
function wait_for_container {
while [ -z "$( rkt list --full | grep $1 )" ]; do
sleep 1
done
}
function create_postgres {
UUID_FILE=/tmp/dex-postgres-uuid
if [ -f $UUID_FILE ]; then
echo "postgres database already exists, try ./standup.sh destroy postgres"
exit 2
fi
echo "Starting postgres. To view progress run:"
echo ""
echo " journalctl -fu dex-postgres"
echo ""
systemd-run --unit=dex-postgres \
rkt run --uuid-file-save=$UUID_FILE --insecure-options=image docker://postgres:9.6
wait_for_file $UUID_FILE
UUID=$( cat $UUID_FILE )
wait_for_container $UUID
HOST=$( rkt list --full | grep "$UUID" | awk '{ print $NF }' | sed -e 's/default:ip4=//g' )
echo "To run tests export the following environment variables:"
echo ""
echo " export DEX_POSTGRES_DATABASE=postgres; export DEX_POSTGRES_USER=postgres; export DEX_POSTGRES_PASSWORD=postgres; export DEX_POSTGRES_HOST=$HOST:5432"
echo ""
}
function destroy_postgres {
UUID_FILE=/tmp/dex-postgres-uuid
systemctl stop dex-postgres
rkt rm --uuid-file=$UUID_FILE
rm $UUID_FILE
}
main $@