Skip to content

Commit

Permalink
Modify server tests to also testing orders application
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Gilmer committed Feb 14, 2020
1 parent 11b4f2c commit 4e941fa
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 65 deletions.
89 changes: 51 additions & 38 deletions .circleci/config.yml
Expand Up @@ -201,6 +201,53 @@ commands:
- run:
name: 'Describe image scan findings'
command: scripts/ecr-describe-image-scan-findings << parameters.repo >> ${CIRCLE_SHA1}

server_tests:
parameters:
application:
type: string
steps:
- run:
name: make server_test_build <<parameters.application>>
command: make server_test_build
environment:
APPLICATION: "<< parameters.application >>"
- run:
name: make db_test_migrate <<parameters.application>>
command: make db_test_migrate
environment:
APPLICATION: "<< parameters.application >>"
DB_PASSWORD: mysecretpassword
DB_USER: postgres
DB_HOST: localhost
DB_PORT_TEST: 5433
DB_PORT: 5432
DB_NAME: test_db
DB_NAME_TEST: test_db
MIGRATION_MANIFEST: '/home/circleci/transcom/mymove/migrations/<< parameters.application >>/migrations_manifest.txt'
MIGRATION_PATH: 'file:///home/circleci/transcom/mymove/migrations/<< parameters.application >>/schema;file:///home/circleci/transcom/mymove/migrations/<< parameters.application >>/secure'
- run:
name: make server_test_standalone <<parameters.application>>
command: |
echo 'export LOGIN_GOV_SECRET_KEY=$(echo $E2E_LOGIN_GOV_SECRET_KEY | base64 --decode)' >> $BASH_ENV
source $BASH_ENV
make server_test_standalone
environment:
APPLICATION: "<< parameters.application >>"
DB_PASSWORD: mysecretpassword
DB_USER: postgres
DB_HOST: localhost
DB_PORT_TEST: 5433
DB_PORT: 5432
DB_NAME: test_db
DB_NAME_TEST: test_db
EIA_KEY: db2522a43820268a41a802a16ae9fd26 # dummy key generated with openssl rand -hex 16
ENV: test
ENVIRONMENT: test
JUNIT: 1
MIGRATION_MANIFEST: '/home/circleci/transcom/mymove/migrations/<< parameters.application >>/migrations_manifest.txt'
MIGRATION_PATH: 'file:///home/circleci/transcom/mymove/migrations/<< parameters.application >>/schema;file:///home/circleci/transcom/mymove/migrations/<< parameters.application >>/secure'
SERVE_API_INTERNAL: true
e2e_tests:
steps:
- run:
Expand Down Expand Up @@ -409,44 +456,10 @@ jobs:
- run: echo 'export PATH=${PATH}:~/go/bin:~/transcom/mymove/bin' >> $BASH_ENV
- run: make bin/go-junit-report
- run: make bin/milmove
- run:
name: make db_test_migrate
command: make db_test_migrate
environment:
APPLICATION: app
DB_PASSWORD: mysecretpassword
DB_USER: postgres
DB_HOST: localhost
DB_PORT_TEST: 5433
DB_PORT: 5432
DB_NAME: test_db
DB_NAME_TEST: test_db
MIGRATION_MANIFEST: '/home/circleci/transcom/mymove/migrations/app/migrations_manifest.txt'
MIGRATION_PATH: 'file:///home/circleci/transcom/mymove/migrations/app/schema;file:///home/circleci/transcom/mymove/migrations/app/secure'
- run:
name: make server_test_standalone
command: |
echo 'export LOGIN_GOV_SECRET_KEY=$(echo $E2E_LOGIN_GOV_SECRET_KEY | base64 --decode)' >> $BASH_ENV
source $BASH_ENV
mkdir -p tmp/test-results/gotest
# setup a trap incase the tests fail, we still want to generate the report
trap "bin/go-junit-report < tmp/test-results/gotest/go-test.out > tmp/test-results/gotest/go-test-report.xml" EXIT
make server_test_standalone | tee tmp/test-results/gotest/go-test.out
environment:
APPLICATION: app
DB_PASSWORD: mysecretpassword
DB_USER: postgres
DB_HOST: localhost
DB_PORT_TEST: 5433
DB_PORT: 5432
DB_NAME: test_db
DB_NAME_TEST: test_db
EIA_KEY: db2522a43820268a41a802a16ae9fd26 # dummy key generated with openssl rand -hex 16
ENV: test
ENVIRONMENT: test
MIGRATION_MANIFEST: '/home/circleci/transcom/mymove/migrations/app/migrations_manifest.txt'
MIGRATION_PATH: 'file:///home/circleci/transcom/mymove/migrations/app/schema;file:///home/circleci/transcom/mymove/migrations/app/secure'
SERVE_API_INTERNAL: true
- server_tests:
application: app
- server_tests:
application: orders
- store_artifacts:
path: ~/transcom/mymove/tmp/test-results
destination: test-results
Expand Down
30 changes: 9 additions & 21 deletions Makefile
Expand Up @@ -161,7 +161,7 @@ client_deps: .check_hosts.stamp .check_node_version.stamp .client_deps.stamp ##
.PHONY: client_build
client_build: .client_deps.stamp .client_build.stamp ## Build the client

build/index.html: ## milmove serve requires this file to boot, but it isn't used during local development
build/index.html: ## milmove serve_app requires this file to boot, but it isn't used during local development
mkdir -p build
touch build/index.html

Expand Down Expand Up @@ -306,7 +306,7 @@ server_build: bin/milmove ## Build the server
# This command is for running the server by itself, it will serve the compiled frontend on its own
# Note: Don't double wrap with aws-vault because the pkg/cli/vault.go will handle it
server_run_standalone: check_log_dir server_build client_build db_dev_run
DEBUG_LOGGING=true ./bin/milmove serve 2>&1 | tee -a log/dev.log
DEBUG_LOGGING=true ./bin/milmove serve_${APPLICATION} 2>&1 | tee -a log/dev.log

# This command will rebuild the swagger go code and rerun server on any changes
server_run:
Expand All @@ -323,7 +323,7 @@ server_run_default: .check_hosts.stamp .check_go_version.stamp .check_gopath.sta
--excludeDir node_modules \
--immediate \
--buildArgs "-i -ldflags=\"$(WEBSERVER_LDFLAGS)\"" \
serve \
serve_${APPLICATION} \
2>&1 | tee -a log/dev.log

.PHONY: server_run_debug
Expand Down Expand Up @@ -402,36 +402,24 @@ server_test: db_test_reset db_test_migrate server_test_standalone ## Run server

.PHONY: server_test_standalone
server_test_standalone: ## Run server unit tests with no deps
# Don't run tests in /cmd or /pkg/gen/ or mocks
# Pass `-short` to exclude long running tests
# Disable test caching with `-count 1` - caching was masking local test failures
ifndef CIRCLECI
DB_NAME=$(DB_NAME_TEST) DB_PORT=$(DB_PORT_TEST) go test -parallel 8 -count 1 -short $$(go list ./... | grep -v \\/pkg\\/gen\\/ | grep -v \\/cmd\\/ | grep -v mocks)
else
# Limit the maximum number of tests to run in parallel for CircleCI due to memory constraints.
# Add verbose (-v) so go-junit-report can parse it for CircleCI results
DB_NAME=$(DB_NAME_TEST) DB_PORT=$(DB_PORT_TEST) go test -v -parallel 4 -count 1 -short $$(go list ./... | grep -v \\/pkg\\/gen\\/ | grep -v \\/cmd\\/ | grep -v mocks)
endif
NO_DB=1 scripts/run-server-test

.PHONY: server_test_build
server_test_build:
# Try to compile tests, but don't run them.
go test -run=nope -count 1 $$(go list ./... | grep -v \\/pkg\\/gen\\/ | grep -v \\/cmd\\/ | grep -v mocks)
DRY_RUN=1 scripts/run-server-test

.PHONY: server_test_all
server_test_all: db_dev_reset db_dev_migrate ## Run all server unit tests
# Like server_test but runs extended tests that may hit external services.
DB_PORT=$(DB_PORT_TEST) go test -parallel 4 -count 1 $$(go list ./... | grep -v \\/pkg\\/gen\\/ | grep -v \\/cmd\\/ | grep -v mocks)
LONG_TEST=1 scripts/run-server-test

.PHONY: server_test_coverage_generate
server_test_coverage_generate: db_test_reset db_test_migrate server_test_coverage_generate_standalone ## Run server unit test coverage

.PHONY: server_test_coverage_generate_standalone
server_test_coverage_generate_standalone: ## Run server unit tests with coverage and no deps
# Don't run tests in /cmd or /pkg/gen
# Use -test.parallel 1 to test packages serially and avoid database collisions
# Disable test caching with `-count 1` - caching was masking local test failures
# Add coverage tracker via go cover
DB_PORT=$(DB_PORT_TEST) go test -coverprofile=coverage.out -covermode=count -parallel 1 -count 1 -short $$(go list ./... | grep -v \\/pkg\\/gen\\/ | grep -v \\/cmd\\/ | grep -v mocks)
NO_DB=1 COVERAGE=1 scripts/run-server-test

.PHONY: server_test_coverage
server_test_coverage: db_test_reset db_test_migrate server_test_coverage_generate ## Run server unit test coverage with html output
Expand All @@ -443,7 +431,7 @@ server_test_docker:

.PHONY: server_test_docker_down
server_test_docker_down:
docker-compose -f docker-compose.circle.yml down
docker-compose -f docker-compose.circle.yml --compatibility down

#
# ----- END SERVER TARGETS -----
Expand Down
1 change: 1 addition & 0 deletions docker-compose.circle.yml
Expand Up @@ -29,6 +29,7 @@ services:
- ./:/home/circleci/transcom/mymove
working_dir: /home/circleci/transcom/mymove
environment:
- APPLICATION
- CLIENT_AUTH_SECRET_KEY
- CSRF_AUTH_KEY
- CIRCLECI=1
Expand Down
1 change: 1 addition & 0 deletions scripts/README.md
Expand Up @@ -119,6 +119,7 @@ This subset of development scripts is used for testing
| --- | --- |
| `run-e2e-test` | Runs cypress tests with interactive GUI |
| `run-e2e-test-docker` | Runs cypress tests entirely inside docker containers like in CircleCI |
| `run-server-test` | Run golang server tests |
| `run-server-test-in-circle-container` | Executed in docker-compose.circle.yml to run the `make server_test` task in a CircleCI container |

### Secure Migrations
Expand Down
84 changes: 84 additions & 0 deletions scripts/run-server-test
@@ -0,0 +1,84 @@
#! /usr/bin/env bash

#
# Run golang server tests
#
# Set these environment variables to change behavior:
# - APPLICATION: 'app' or 'orders' will change which tests are run and which migrations are run for them
# - CIRCLECI: Will reduce parallelism and make the test output verbose
# - COVERAGE: '1' will enable test coverage flags
# - DRY_RUN: '1' will build the tests but not run them
# - JUNIT: '1' will run go-junit-report on test output
# - LONG_TEST: '1' will remove the '-short' flag and run extended tests
# - NO_DB: Will disable the db reset and migration steps
#
# Don't run tests in /cmd (those are done in acceptance testing) or for generated code (in /pkg/gen/ or mocks)
# Disable test caching with `-count 1` - caching was masking local test failures

set -eu -o pipefail

if [ "${APPLICATION}" == "app" ]; then
package_list=$(go list ./... | grep -v ordersapi | grep -v \\/pkg\\/gen\\/ | grep -v \\/cmd\\/ | grep -v mocks)
elif [ "${APPLICATION}" == "orders" ]; then
package_list=$(go list ./pkg/handlers/ordersapi)
else
echo "Must provider the environment variable APPLICATION and set to 'app' or 'orders'"
exit 1
fi

# Try to compile tests, but don't run them.
if [[ "${DRY_RUN:-}" == "1" ]]; then
echo "Compiling tests only, not running"
# shellcheck disable=SC2086
go test -run=nope -count 1 ${package_list}
exit 0
fi

# Like server_test but runs extended tests that may hit external services.
short_flag="-short"
if [[ "${LONG_TEST:-}" == "1" ]]; then
short_flag=""
fi

# Add coverage tracker via go cover
coverage_flag=""
if [[ "${COVERAGE:-}" == "1" ]]; then
coverage_flag="-coverprofile=coverage.out -covermode=count"
fi

# Set up the test DB instance and wipe it
# do not run db commands if NO_DB is set to 1
if [[ "${NO_DB:-}" -ne 1 ]]; then
make db_test_reset db_test_migrate
fi

# Setup test output for reporting
test_dir="tmp/test-results/gotest/${APPLICATION}"
test_output_file="${test_dir}/go-test.out"
test_report_file="${test_dir}/go-test-report.xml"
mkdir -p "${test_dir}"

function junit_cleanup()
{
# generate the junit report
bin/go-junit-report < "${test_output_file}" > "${test_report_file}"
}

# Set up junit report and ensure its run on exit
if [[ "${JUNIT:-}" == "1" ]]; then
if [ ! -f bin/go-junit-report ]; then
make bin/go-junit-report
fi
# setup a trap incase the tests fail, we still want to generate the report
trap junit_cleanup EXIT
fi

if [ -n "${CIRCLECI+x}" ]; then
# Limit the maximum number of tests to run in parallel for CircleCI due to memory constraints.
# Add verbose (-v) so go-junit-report can parse it for CircleCI results
# shellcheck disable=SC2086
DB_NAME="${DB_NAME_TEST}" DB_PORT="${DB_PORT_TEST}" go test -v -parallel 4 -count 1 ${coverage_flag} ${short_flag} ${package_list} | tee "${test_output_file}"
else
# shellcheck disable=SC2086
DB_NAME="${DB_NAME_TEST}" DB_PORT="${DB_PORT_TEST}" go test -parallel 8 -count 1 ${coverage_flag} ${short_flag} ${package_list} | tee "${test_output_file}"
fi
24 changes: 18 additions & 6 deletions scripts/run-server-test-in-circle-container
Expand Up @@ -6,18 +6,30 @@

set -eu -o pipefail

if [ "${APPLICATION}" != "app" ] && [ "${APPLICATION}" != "ordres" ]; then
echo "Must provider the environment variable APPLICATION and set to 'app' or 'orders'"
exit 1
fi

make clean

go get ./...

# These targets are built with GOARCH=amd64 GOOS=linux and overwrite the local binaries
make bin/go-junit-report
make bin/milmove
# Test that go tests will build first
make server_test_build

# Migrate the DB
make bin/milmove
make db_test_migrate

mkdir -p tmp/test-results/gotest
function cleanup()
{
# These targets are built with GOARCH=amd64 GOOS=linux and overwrite the local binaries
# because docker-compose mounts the local directory and the bin directory is used
rm -f bin/milmove
rm -f bin/go-junit-report
}

## setup a trap incase the tests fail, we still want to generate the report
trap "bin/go-junit-report < tmp/test-results/gotest/go-test.out > tmp/test-results/gotest/go-test-report.xml" EXIT
make server_test_standalone | tee tmp/test-results/gotest/go-test.out
trap cleanup EXIT
JUNIT=1 make server_test_standalone

0 comments on commit 4e941fa

Please sign in to comment.