diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index bc31b11fe..080e2a230 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -1,7 +1,8 @@ name: Tests on: push: - branches: [ main ] + branches: + - main pull_request: jobs: @@ -22,7 +23,7 @@ jobs: ports: - 3307:3306 # needed because the mysql container does not provide a healthcheck - options: --health-cmd "mysqladmin ping -h localhost" --health-interval 10s --health-timeout 5s --health-retries 5 + options: --health-cmd "mysqladmin ping -h localhost" --health-interval 5s --health-timeout 5s --health-retries 10 steps: - uses: actions/checkout@v3 @@ -59,15 +60,15 @@ jobs: services: postgres: - image: postgres:14 + image: postgres:10 env: POSTGRES_USER: postgres POSTGRES_PASSWORD: postgres POSTGRES_DB: postgres ports: - - 5432:5432 + - 5433:5432 # needed because the postgres container does not provide a healthcheck - options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5 + options: --health-cmd pg_isready --health-interval 5s --health-timeout 5s --health-retries 10 steps: - uses: actions/checkout@v3 @@ -113,8 +114,10 @@ jobs: run: | mkdir -p crdb/certs pushd crdb - wget -qO- https://binaries.cockroachdb.com/cockroach-v20.2.4.linux-amd64.tgz | tar zxv - mv cockroach-v20.2.4.linux-amd64/cockroach . + wget -qO- https://binaries.cockroachdb.com/cockroach-v21.1.19.linux-amd64.tgz | tar -xz + mv cockroach-v21.1.19.linux-amd64/* . + ln -s `pwd`/cockroach /usr/local/bin/ + cockroach version ./cockroach cert create-ca --certs-dir certs --ca-key key ./cockroach cert create-client root --certs-dir certs --ca-key key ./cockroach cert create-node localhost 127.0.0.1 `hostname -s` `hostname -f` --certs-dir certs --ca-key key @@ -156,14 +159,16 @@ jobs: run: | mkdir -p crdb pushd crdb - wget -qO- https://binaries.cockroachdb.com/cockroach-v20.2.4.linux-amd64.tgz | tar zxv - mv cockroach-v20.2.4.linux-amd64/cockroach . + wget -qO- https://binaries.cockroachdb.com/cockroach-v21.1.19.linux-amd64.tgz | tar -xz + mv cockroach-v21.1.19.linux-amd64/* . + ln -s `pwd`/cockroach /usr/local/bin/ ./cockroach start-single-node --insecure --background popd - name: Build and run soda env: SODA_DIALECT: "cockroach" + COCKROACH_PORT: 26257 run: | go build -v -tags sqlite -o tsoda ./soda ./tsoda drop -e $SODA_DIALECT -p ./testdata/migrations @@ -173,6 +178,7 @@ jobs: - name: Test env: SODA_DIALECT: "cockroach" + COCKROACH_PORT: 26257 run: | go test -tags sqlite -race -cover ./... diff --git a/Makefile b/Makefile index 24d4eab5f..87d9afb8e 100644 --- a/Makefile +++ b/Makefile @@ -7,36 +7,15 @@ GO_BIN ?= go install: deps $(GO_BIN) install -tags ${TAGS} -v ./soda -deps: - $(GO_BIN) get github.com/gobuffalo/release - $(GO_BIN) get -tags ${TAGS} -t ./... -ifeq ($(GO111MODULE),on) - $(GO_BIN) mod tidy -endif - build: $(GO_BIN) build -v . test: $(GO_BIN) test -tags ${TAGS} ./... -ci-test: - $(GO_BIN) test -tags ${TAGS} -race ./... - lint: golangci-lint run -update: - $(GO_BIN) get -u -tags ${TAGS} -ifeq ($(GO111MODULE),on) - $(GO_BIN) mod tidy -endif - make test - make install -ifeq ($(GO111MODULE),on) - $(GO_BIN) mod tidy -endif - release-test: ./test.sh diff --git a/database.yml b/database.yml index e7d66b08a..196359bfd 100644 --- a/database.yml +++ b/database.yml @@ -9,7 +9,7 @@ mysql: readTimeout: 5s postgres: - url: {{ envOr "POSTGRESQL_URL" "postgres://postgres:postgres@localhost:5432/pop_test?sslmode=disable" }} + url: {{ envOr "POSTGRESQL_URL" "postgres://postgres:postgres@localhost:5433/pop_test?sslmode=disable" }} pool: 25 cockroach: @@ -29,5 +29,5 @@ cockroach_ssl: sqlite: dialect: "sqlite3" - database: "./sql_scripts/sqlite/test.sqlite" + database: "tmp/test.sqlite" diff --git a/docker-compose.yml b/docker-compose.yml index 1ea16701b..0a9725fdc 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,26 +8,42 @@ services: - MYSQL_DATABASE=pop_test - MYSQL_USER=pop - MYSQL_PASSWORD=pop - volumes: - - ./sql:/docker-entrypoint-initdb.d ports: - - "3306:3306" + - "3307:3306" + healthcheck: + test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"] + interval: 5s + timeout: 5s + retries: 10 + start_period: 3s + postgres: - image: postgres:10.21 + image: postgres:10 environment: - POSTGRES_DB=pop_test - POSTGRES_USER=postgres - POSTGRES_PASSWORD=postgres - POSTGRES_DB=postgres ports: - - "5432:5432" - volumes: - - ./sqldumps:/docker-entrypoint-initdb.d + - "5433:5432" + healthcheck: + test: ["CMD", "pg_isready"] + interval: 5s + timeout: 5s + retries: 10 + start_period: 3s + cockroach: - image: cockroachdb/cockroach:v20.2.4 - user: ${CURRENT_UID:?"Please run as follows 'CURRENT_UID=$$(id -u):$$(id -g) docker-compose up'"} + image: cockroachdb/cockroach:latest-v21.1 ports: - - "26257:26257" - volumes: - - "./cockroach-data/roach1:/cockroach/cockroach-data" + - "26258:26257" + - "8081:8080" + #volumes: + #- ./_vol/cockroach:/cockroach/cockroach-data command: start-single-node --insecure + healthcheck: + test: ["CMD", "curl", "http://localhost:8080/health"] + interval: 5s + timeout: 5s + retries: 10 + start_period: 3s diff --git a/test.sh b/test.sh index d99c6744c..1b908bc25 100755 --- a/test.sh +++ b/test.sh @@ -1,84 +1,61 @@ #!/bin/bash -######################################################## -# test.sh is a wrapper to execute integration tests for -# pop. -######################################################## - set -e -clear -VERBOSE="" -DEBUG='NO' +# NOTE: See also docker-compose.yml and database.yml to configure database +# properties. +export MYSQL_PORT=3307 +export COCKROACH_PORT=26258 -for i in "$@" -do -case $i in - -v) - VERBOSE="-v" - shift - ;; - -d) - DEBUG='YES' - shift - ;; - *) - # unknown option - ;; -esac -done +COMPOSE=docker-compose +which docker-compose || COMPOSE="docker compose" + +args=$@ function cleanup { echo "Cleanup resources..." - docker-compose down + $COMPOSE down + docker volume prune -f rm tsoda - find ./sql_scripts/sqlite -name *.sqlite* -delete + find ./tmp -name *.sqlite* -delete || true } # defer cleanup, so it will be executed even after premature exit trap cleanup EXIT -# The cockroach volume is created by the root user if no user is set. -# Therefore we set the current user according to https://github.com/docker/compose/issues/1532#issuecomment-619548112 -CURRENT_UID="$(id -u):$(id -g)" -export CURRENT_UID - -docker-compose up -d -sleep 5 # Ensure mysql is online - -go build -v -tags sqlite -o tsoda ./soda - -export GO111MODULE=on - function test { - echo "!!! Testing $1" export SODA_DIALECT=$1 - echo ./tsoda -v - echo "Setup..." + + echo "" + echo "######################################################################" + echo "### Running unit tests for $SODA_DIALECT" ./tsoda drop -e $SODA_DIALECT -c ./database.yml -p ./testdata/migrations ./tsoda create -e $SODA_DIALECT -c ./database.yml -p ./testdata/migrations ./tsoda migrate -e $SODA_DIALECT -c ./database.yml -p ./testdata/migrations - echo "Test..." - go test -race -tags sqlite $VERBOSE -count=1 ./... + go test -cover -race -tags sqlite -count=1 $args ./... } function debug_test { - echo "!!! Debug Testing $1" - export SODA_DIALECT=$1 - echo ./tsoda -v - echo "Setup..." - ./tsoda drop -e $SODA_DIALECT -c ./database.yml -p ./testdata/migrations - ./tsoda create -e $SODA_DIALECT -c ./database.yml -p ./testdata/migrations - ./tsoda migrate -e $SODA_DIALECT -c ./database.yml -p ./testdata/migrations - echo "Test and debug..." - dlv test github.com/gobuffalo/pop + export SODA_DIALECT=$1 + + echo "" + echo "######################################################################" + echo "### Running unit tests for $SODA_DIALECT" + ./tsoda drop -e $SODA_DIALECT -c ./database.yml -p ./testdata/migrations + ./tsoda create -e $SODA_DIALECT -c ./database.yml -p ./testdata/migrations + ./tsoda migrate -e $SODA_DIALECT -c ./database.yml -p ./testdata/migrations + dlv test github.com/gobuffalo/pop } -dialects=("postgres" "cockroach" "mysql" "sqlite") +dialects="postgres cockroach mysql sqlite" + +$COMPOSE up --wait + +go build -v -tags sqlite -o tsoda ./soda -for dialect in "${dialects[@]}" ; do - if [ $DEBUG = 'NO' ]; then - test ${dialect} - else - debug_test ${dialect} - fi +for dialect in $dialects; do + if [ "$DEBUG" = "YES" ]; then + debug_test ${dialect} + else + test ${dialect} + fi done