From 4b61cc9e9cb0d3c840f15e937a9243b723f0b397 Mon Sep 17 00:00:00 2001 From: Mya Date: Thu, 19 May 2022 12:57:04 -0500 Subject: [PATCH] Makefile: update test target to support a local execution workflow This change attempts to run tests as close to CI as possible. It introduces a docker-compose file that deploys some supporting services. While this change makes it possible to run tests locally, it does not address the core time it takes to run tests end to end. make test/postgres 1417.87s user 263.38s system 291% cpu 9:37.73 total Resolves https://github.com/storj/dev-enablement/issues/10 Change-Id: I4d9adc125992c1b1c133e8fe108384fa3c66ff7a --- DEVELOPING.md | 23 ++++++++++++++++ Makefile | 56 +++++++++++++++++++++++++++++++++++++-- docker-compose.tests.yaml | 44 ++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 docker-compose.tests.yaml diff --git a/DEVELOPING.md b/DEVELOPING.md index 54251cb8f..dc7b78a97 100644 --- a/DEVELOPING.md +++ b/DEVELOPING.md @@ -260,6 +260,29 @@ git rebase -i HEAD~3 ... ``` +## Executing tests locally + +The Storj project has an extensive suite of integration tests. Many of these tests require several infrastructure +dependencies. These dependencies are defined and managed by the `docker-compose.tests.yaml` file. Tests can be executed +against Postgres (`test/postgres`), or CockroachDB (`test/cockroach`), or against both (`test`). By default, the full +suite of tests is run, but can be limited using the `TEST_TARGET` make variable. + +```sh +# run only against Postgres +make test/postgres TEST_TARGET="./satellite/oidc/..." +``` + +You can also provide multiple targets for the test harness to run in case your change spans across several packages. + +```sh +# run against both Postgres and Cockroach +make test TEST_TARGET="./satellite/oidc/... ./satellite/satellitedb/..." +``` + +_**Note:** While you can run the full suite of tests locally, you will likely be waiting around for them to complete. +By starting with the tests from packages you have modified, you can build a great deal of confidence in your changes +before pushing them up for review._ + ## Developing locally with `storj-up` Following the instructions in the `storj-up` project `README`, the following will deploy a copy of the stack. diff --git a/Makefile b/Makefile index 11baff408..4a69cc094 100644 --- a/Makefile +++ b/Makefile @@ -104,9 +104,61 @@ install-sim: ## install storj-sim ##@ Test +TEST_TARGET ?= "./..." + +.PHONY: test/setup +test/setup: + @docker compose -f docker-compose.tests.yaml up -d + @sleep 3 + @docker exec -it storj-crdb1-1 bash -c 'cockroach sql --insecure -e "create database testcockroach;"' + @docker exec -it storj-crdb2-1 bash -c 'cockroach sql --insecure -e "create database testcockroach;"' + @docker exec -it storj-crdb3-1 bash -c 'cockroach sql --insecure -e "create database testcockroach;"' + @docker exec -it storj-crdb4-1 bash -c 'cockroach sql --insecure -e "create database testcockroach;"' + @docker exec -it storj-crdb5-1 bash -c 'cockroach sql --insecure -e "create database testcockroach;"' + @docker exec -it storj-crdb4-1 bash -c 'cockroach sql --insecure -e "create database testmetabase;"' + @docker exec -it storj-postgres-1 bash -c 'echo "postgres" | psql -U postgres -c "create database teststorj;"' + @docker exec -it storj-postgres-1 bash -c 'echo "postgres" | psql -U postgres -c "create database testmetabase;"' + +.PHONY: test/postgres +test/postgres: test/setup ## Run tests against Postgres (developer) + @env \ + STORJ_TEST_POSTGRES='postgres://postgres:postgres@localhost/teststorj?sslmode=disable' \ + STORJ_TEST_LOG_LEVEL='info' \ + go test -tags noembed -parallel 4 -p 6 -vet=off -race -v -cover -coverprofile=.coverprofile $(TEST_TARGET) || { \ + docker compose -f docker-compose.tests.yaml rm -fs; \ + } + @docker compose -f docker-compose.tests.yaml rm -fs + @echo done + +.PHONY: test/cockroach +test/cockroach: test/setup ## Run tests against CockroachDB (developer) + @env \ + STORJ_TEST_COCKROACH="cockroach://root@localhost:26256/testcockroach?sslmode=disable" \ + STORJ_TEST_COCKROACH="$$STORJ_TEST_COCKROACH;cockroach://root@localhost:26257/testcockroach?sslmode=disable" \ + STORJ_TEST_COCKROACH="$$STORJ_TEST_COCKROACH;cockroach://root@localhost:26258/testcockroach?sslmode=disable" \ + STORJ_TEST_COCKROACH="$$STORJ_TEST_COCKROACH;cockroach://root@localhost:26259/testcockroach?sslmode=disable" \ + STORJ_TEST_COCKROACH_ALT='cockroach://root@localhost:26260/testcockroach?sslmode=disable' \ + STORJ_TEST_LOG_LEVEL='info' \ + go test -tags noembed -parallel 4 -p 6 -vet=off -race -v -cover -coverprofile=.coverprofile $(TEST_TARGET) || { \ + docker compose -f docker-compose.tests.yaml rm -fs; \ + } + @docker compose -f docker-compose.tests.yaml rm -fs + @echo done + .PHONY: test -test: ## Run tests on source code (jenkins) - go test -race -v -cover -coverprofile=.coverprofile ./... +test: test/setup ## Run tests against CockroachDB and Postgres (developer) + @env \ + STORJ_TEST_POSTGRES='postgres://postgres:postgres@localhost/teststorj?sslmode=disable' \ + STORJ_TEST_COCKROACH="cockroach://root@localhost:26256/testcockroach?sslmode=disable" \ + STORJ_TEST_COCKROACH="$$STORJ_TEST_COCKROACH;cockroach://root@localhost:26257/testcockroach?sslmode=disable" \ + STORJ_TEST_COCKROACH="$$STORJ_TEST_COCKROACH;cockroach://root@localhost:26258/testcockroach?sslmode=disable" \ + STORJ_TEST_COCKROACH="$$STORJ_TEST_COCKROACH;cockroach://root@localhost:26259/testcockroach?sslmode=disable" \ + STORJ_TEST_COCKROACH_ALT='cockroach://root@localhost:26260/testcockroach?sslmode=disable' \ + STORJ_TEST_LOG_LEVEL='info' \ + go test -tags noembed -parallel 4 -p 6 -vet=off -race -v -cover -coverprofile=.coverprofile $(TEST_TARGET) || { \ + docker compose -f docker-compose.tests.yaml rm -fs; \ + } + @docker compose -f docker-compose.tests.yaml rm -fs @echo done .PHONY: test-sim diff --git a/docker-compose.tests.yaml b/docker-compose.tests.yaml new file mode 100644 index 000000000..f4ca6104f --- /dev/null +++ b/docker-compose.tests.yaml @@ -0,0 +1,44 @@ +# Support services used by tests + +version: "3.4" + +services: + postgres: + hostname: postgres + image: postgres + environment: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + ports: + - "5432:5432" + + crdb1: + hostname: crdb1 + command: [ "start-single-node", "--insecure" ] + image: cockroachdb/cockroach + ports: + - "26256:26257" + crdb2: + hostname: crdb2 + command: [ "start-single-node", "--insecure" ] + image: cockroachdb/cockroach + ports: + - "26257:26257" + crdb3: + hostname: crdb3 + command: [ "start-single-node", "--insecure" ] + image: cockroachdb/cockroach + ports: + - "26258:26257" + crdb4: + hostname: crdb4 + command: [ "start-single-node", "--insecure" ] + image: cockroachdb/cockroach + ports: + - "26259:26257" + crdb5: + hostname: crdb5 + command: [ "start-single-node", "--insecure" ] + image: cockroachdb/cockroach + ports: + - "26260:26257"