Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable test-long in CI #222

Merged
merged 3 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 11 additions & 21 deletions .github/workflows/ci_release.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
name: Release
on:
push:
# Nightly schedule pending full e2e testing with other repos
#schedule:
# Run daily at 1:15am
#- cron: "15 1 * * *"
branches:
- main
pull_request:
workflow_dispatch:
# Inputs the workflow accepts.
inputs:
Expand Down Expand Up @@ -36,32 +35,23 @@ jobs:
run: go install gitlab.com/NebulousLabs/analyze@latest
- name: Install golangci-lint
run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.45.0
- name: Update Dependecies and Clean Up
run: make deps
- name: Lint
run: make lint
- name: Run unit tests
run: make test

# Check if there were any changes since the last tag if this is not a push
# event
changes:
needs: [hadolint, test]
runs-on: ubuntu-latest
outputs:
updates: ${{steps.changes.outputs.any == 'true'}}
if: ${{ github.event_name != 'push' }}
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0 # Required due to the way Git works, without it this action won't be able to find any or the correct tags
- uses: SkynetLabs/.github/.github/actions/changes-since-last-tag@master
- name: Start Mongo Container
run: make start-mongo
- name: Run Long tests
run: make test-long-ci

# Make a release if
# - there were changes and this is a scheduled job
# - This is a manually trigger job, i.e. workflow_dispatch
release:
needs: changes
needs: [hadolint, test]
runs-on: ubuntu-latest
if: ${{ (needs.changes.outputs.updates == 'true' && github.event_name == 'schedule') || github.event_name == 'workflow_dispatch' }}
if: ${{ github.event_name == 'workflow_dispatch' }}
outputs:
new_version: ${{ steps.version.outputs.new-version }}
steps:
Expand Down
82 changes: 34 additions & 48 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ racevars= history_size=3 halt_on_error=1 atexit_sleep_ms=2000
# all will build and install release binaries
all: release

deps:
go mod download
go mod tidy

# clean removes all directories that get automatically created during
# development.
# Also ensures that any docker containers are gone in the event of an error on a
Expand All @@ -25,14 +29,26 @@ else
- DEL /F /Q cover output
endif

run = .

# count says how many times to run the tests.
count = 1
# pkgs changes which packages the makefile calls operate on. run changes which
# tests are run during testing.
pkgs = ./ ./api ./database ./email ./hash ./jwt ./lib ./metafetcher ./skynet

# integration-pkgs defines the packages which contain integration tests
integration-pkgs = ./test ./test/api ./test/database ./test/email
pkgs = \
./ \
./api \
./database \
./email \
./hash \
./jwt \
./lib \
./metafetcher \
./skynet \
./test \
./test/api \
./test/database \
./test/email

# fmt calls go fmt on all packages.
fmt:
Expand Down Expand Up @@ -68,16 +84,8 @@ ifneq ("$(OS)","Windows_NT")
go mod tidy
endif

# Credentials and port we are going to use for our test MongoDB instance.
MONGO_USER=admin
MONGO_PASSWORD=aO4tV5tC1oU3oQ7u
MONGO_PORT=17017

# call_mongo is a helper function that executes a query in an `eval` call to the
# test mongo instance.
define call_mongo
docker exec skynet-accounts-mongo-test-db mongo -u $(MONGO_USER) -p $(MONGO_PASSWORD) --port $(MONGO_PORT) --eval $(1)
endef
# Define docker container name our test MongoDB instance.
MONGO_TEST_CONTAINER_NAME=blocker-mongo-test-db

# start-mongo starts a local mongoDB container with no persistence.
# We first prepare for the start of the container by making sure the test
Expand All @@ -86,29 +94,10 @@ endef
# single node replica set. All the output is discarded because it's noisy and
# if it causes a failure we'll immediately know where it is even without it.
start-mongo:
-docker stop skynet-accounts-mongo-test-db 1>/dev/null 2>&1
-docker rm skynet-accounts-mongo-test-db 1>/dev/null 2>&1
chmod 400 $(shell pwd)/test/fixtures/mongo_keyfile
docker run \
--rm \
--detach \
--name skynet-accounts-mongo-test-db \
-p $(MONGO_PORT):$(MONGO_PORT) \
-e MONGO_INITDB_ROOT_USERNAME=$(MONGO_USER) \
-e MONGO_INITDB_ROOT_PASSWORD=$(MONGO_PASSWORD) \
-v $(shell pwd)/test/fixtures/mongo_keyfile:/data/mgkey \
mongo:4.4.1 mongod --port=$(MONGO_PORT) --replSet=skynet --keyFile=/data/mgkey 1>/dev/null 2>&1
# wait for mongo to start before we try to configure it
status=1 ; while [[ $$status -gt 0 ]]; do \
sleep 1 ; \
$(call call_mongo,"") 1>/dev/null 2>&1 ; \
status=$$? ; \
done
# Initialise a single node replica set.
$(call call_mongo,"rs.initiate({_id: \"skynet\", members: [{ _id: 0, host: \"localhost:$(MONGO_PORT)\" }]})") 1>/dev/null 2>&1
./test/setup.sh $(MONGO_TEST_CONTAINER_NAME)

stop-mongo:
-docker stop skynet-accounts-mongo-test-db
-docker stop $(MONGO_TEST_CONTAINER_NAME)

# debug builds and installs debug binaries. This will also install the utils.
debug:
Expand Down Expand Up @@ -141,19 +130,16 @@ bench: fmt
test:
go test -short -tags='debug testing netgo' -timeout=5s $(pkgs) -run=. -count=$(count)

test-long: lint lint-ci start-mongo
test-long: lint lint-ci start-mongo test-long-ci stop-mongo

test-long-ci:
@mkdir -p cover
GORACE='$(racevars)' go test -race --coverprofile='./cover/cover.out' -v -failfast -tags='testing debug netgo' -timeout=60s $(pkgs) -run=$(run) -count=$(count)
GORACE='$(racevars)' go test -race -v -tags='testing debug netgo' -timeout=600s $(integration-pkgs) -run=$(run) -count=$(count)
-make stop-mongo

# test-single allows us to run a single integration test.
# Make sure to start MongoDB yourself!
# Example: make test-single RUN=TestHandlers
test-single: export COOKIE_HASH_KEY="7eb32cfab5014d14394648dae1cf4e606727eee2267f6a50213cd842e61c5bce"
test-single: export COOKIE_ENC_KEY="65d31d12b80fc57df16d84c02a9bb62e2bc3b633388b05e49ef8abfdf0d35cf3"
test-single:
GORACE='$(racevars)' go test -race -v -tags='testing debug netgo' -timeout=300s $(integration-pkgs) -run=$(run) -count=$(count)
GORACE='$(racevars)' go test -race --coverprofile='./cover/cover.out' -v -failfast -tags='testing debug netgo' -timeout=600s $(pkgs) -run=$(run) -count=$(count)

# Cookie vars
# TODO: Are these used?
MSevey marked this conversation as resolved.
Show resolved Hide resolved
COOKIE_HASH_KEY="7eb32cfab5014d14394648dae1cf4e606727eee2267f6a50213cd842e61c5bce"
COOKIE_ENC_KEY="65d31d12b80fc57df16d84c02a9bb62e2bc3b633388b05e49ef8abfdf0d35cf3"

# docker-generate is a docker command for env var generation
#
Expand All @@ -167,4 +153,4 @@ docker-generate: clean
sleep 3
@docker stop genenv || true && docker rm --force genenv

.PHONY: all fmt install release clean check test test-long test-single start-mongo stop-mongo docker-generate
.PHONY: all deps fmt install release clean check test test-long test-long-ci start-mongo stop-mongo docker-generate
2 changes: 1 addition & 1 deletion api/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func TestUserLimitsGetFromTier(t *testing.T) {
}
}()
// The call that we expect to log a critical.
_ = userLimitsGetFromTier("", math.MaxInt, false, true)
_ = userLimitsGetFromTier("", math.MaxInt64, false, true)
return
}()
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions test/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import (
// TestWithDBSession ensures that database transactions are started, committed,
// and aborted properly.
func TestWithDBSession(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
ctx := context.Background()
dbName := test.DBNameForTest(t.Name())
db, err := test.NewDatabase(ctx, dbName)
Expand Down Expand Up @@ -137,6 +140,9 @@ func TestWithDBSession(t *testing.T) {

// TestUserTierCache ensures out tier cache works as expected.
func TestUserTierCache(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
dbName := test.DBNameForTest(t.Name())
at, err := test.NewAccountsTester(dbName)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions test/api/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ type subtest struct {
// TestHandlers is a meta test that sets up a test instance of accounts and runs
// a suite of tests that ensure all handlers behave as expected.
func TestHandlers(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
dbName := test.DBNameForTest(t.Name())
at, err := test.NewAccountsTester(dbName)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion test/api/stripe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ func TestStripe(t *testing.T) {
// We only run tests against Stripe's test infrastructure. For that we need
// a test API key.
key, ok := os.LookupEnv("STRIPE_API_KEY")
if !ok || !strings.HasPrefix(key, "sk_test_") {
if !ok {
t.Skipf("Skipping %s. If you want to run this test, update STRIPE_API_KEY to hold a test API key.\n", t.Name())
}
if !strings.HasPrefix(key, "sk_test_") {
t.Skipf("Skipping %s. If you want to run this test, update STRIPE_API_KEY to hold a test API key.\n"+
"Expected STRIPE_API_KEY that starts with '%s', got '%s'", t.Name(), "sk_test_", key[:8])
}
Expand Down
3 changes: 3 additions & 0 deletions test/database/apikeys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (

// TestAPIKeys ensures the DB operations with API keys work as expected.
func TestAPIKeys(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
ctx := context.Background()
dbName := test.DBNameForTest(t.Name())
db, err := test.NewDatabase(ctx, dbName)
Expand Down
6 changes: 6 additions & 0 deletions test/database/challenge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import (

// TestValidateChallengeResponse is a unit test using a database.
func TestValidateChallengeResponse(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
ctx := context.Background()
dbName := test.DBNameForTest(t.Name())
db, err := test.NewDatabase(ctx, dbName)
Expand Down Expand Up @@ -126,6 +129,9 @@ func TestValidateChallengeResponse(t *testing.T) {
// TestUnconfirmedUserUpdate ensures the entire flow for unconfirmed user
// updates works as expected.
func TestUnconfirmedUserUpdate(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
ctx := context.Background()
dbName := test.DBNameForTest(t.Name())
db, err := test.NewDatabase(ctx, dbName)
Expand Down
3 changes: 3 additions & 0 deletions test/database/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (
// TestConfiguration ensures we can correctly read and write from/to the
// configuration DB table.
func TestConfiguration(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
ctx := context.Background()
dbName := test.DBNameForTest(t.Name())
db, err := test.NewDatabase(ctx, dbName)
Expand Down
9 changes: 9 additions & 0 deletions test/database/upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import (
// TestUploadsByUser ensures UploadsByUser returns the correct uploads,
// in the correct order, with the correct sized and so on.
func TestUploadsByUser(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
ctx := context.Background()
dbName := test.DBNameForTest(t.Name())
db, err := test.NewDatabase(ctx, dbName)
Expand Down Expand Up @@ -145,6 +148,9 @@ func TestUploadsByUser(t *testing.T) {
// TestUnpinUploads ensures UnpinUploads unpins all uploads of this
// skylink by this user without affecting uploads by other users.
func TestUnpinUploads(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
ctx := context.Background()
dbName := test.DBNameForTest(t.Name())
db, err := test.NewDatabase(ctx, dbName)
Expand Down Expand Up @@ -255,6 +261,9 @@ func TestUnpinUploads(t *testing.T) {

// TestUploadCreateAnon ensures that UploadCreate can create anonymous uploads.
func TestUploadCreateAnon(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
ctx := context.Background()
dbName := test.DBNameForTest(t.Name())
db, err := database.NewCustomDB(ctx, dbName, test.DBTestCredentials(), nil)
Expand Down
Loading