diff --git a/Makefile b/Makefile index 5eb55a9eb..317e4ef22 100644 --- a/Makefile +++ b/Makefile @@ -111,10 +111,11 @@ build_for_integration_test: ) || (make failpoint-disable && exit 1) @make failpoint-disable +test: export ARGS=$$($(PACKAGES)) test: $(PREPARE_MOD) @make failpoint-enable - $(GOTEST) $(RACEFLAG) -tags leak $$($(PACKAGES)) || ( make failpoint-disable && exit 1 ) + $(GOTEST) $(RACEFLAG) -tags br_test,leak $(ARGS) || ( make failpoint-disable && exit 1 ) @make failpoint-disable testcover: tools @@ -188,7 +189,7 @@ static: prepare tools @# exhaustivestruct - Protobuf structs have hidden fields, like "XXX_NoUnkeyedLiteral" @# exhaustive - no need to check exhaustiveness of enum switch statements @# gosec - too many false positive - CGO_ENABLED=0 tools/bin/golangci-lint run --enable-all --deadline 120s \ + CGO_ENABLED=0 tools/bin/golangci-lint run --enable-all --build-tags br_test --deadline 120s \ --disable gochecknoglobals \ --disable goimports \ --disable gofmt \ diff --git a/pkg/mock/backend.go b/pkg/mock/backend.go index 8f69935c3..4b30c66f9 100644 --- a/pkg/mock/backend.go +++ b/pkg/mock/backend.go @@ -1,6 +1,8 @@ // Code generated by MockGen. DO NOT EDIT. // Source: github.com/pingcap/br/pkg/lightning/backend (interfaces: AbstractBackend,Encoder,Rows,Row,EngineWriter) +//+build br_test + // $ mockgen -package mock -mock_names 'AbstractBackend=MockBackend' github.com/pingcap/br/pkg/lightning/backend AbstractBackend,Encoder,Rows,Row,EngineWriter // Package mock is a generated GoMock package. diff --git a/pkg/mock/dummy.go b/pkg/mock/dummy.go new file mode 100644 index 000000000..21b43d21e --- /dev/null +++ b/pkg/mock/dummy.go @@ -0,0 +1,5 @@ +// Copyright 2021 PingCAP, Inc. Licensed under Apache-2.0. + +// This file avoids mock being an empty package when `-tags br_test` is not provided. + +package mock diff --git a/pkg/mock/glue.go b/pkg/mock/glue.go index 63ece855e..2c5f74bf5 100644 --- a/pkg/mock/glue.go +++ b/pkg/mock/glue.go @@ -1,6 +1,8 @@ // Code generated by MockGen. DO NOT EDIT. // Source: github.com/pingcap/br/pkg/lightning/glue/glue.go +//+build br_test + // Package mock is a generated GoMock package. package mock @@ -8,12 +10,12 @@ import ( context "context" sql "database/sql" gomock "github.com/golang/mock/gomock" - parser "github.com/pingcap/parser" - model "github.com/pingcap/parser/model" checkpoints "github.com/pingcap/br/pkg/lightning/checkpoints" config "github.com/pingcap/br/pkg/lightning/config" glue "github.com/pingcap/br/pkg/lightning/glue" log "github.com/pingcap/br/pkg/lightning/log" + parser "github.com/pingcap/parser" + model "github.com/pingcap/parser/model" reflect "reflect" ) diff --git a/pkg/mock/glue_checkpoint.go b/pkg/mock/glue_checkpoint.go index 50bf3fc36..73df5c4e8 100644 --- a/pkg/mock/glue_checkpoint.go +++ b/pkg/mock/glue_checkpoint.go @@ -1,6 +1,8 @@ // Code generated by MockGen. DO NOT EDIT. // Source: github.com/pingcap/br/pkg/lightning/checkpoints/glue_checkpoint.go +//+build br_test + // Package mock is a generated GoMock package. package mock diff --git a/pkg/mock/importer.go b/pkg/mock/importer.go index a58c0d05b..50c9611f8 100644 --- a/pkg/mock/importer.go +++ b/pkg/mock/importer.go @@ -1,6 +1,8 @@ // Code generated by MockGen. DO NOT EDIT. // Source: github.com/pingcap/kvproto/pkg/import_kvpb (interfaces: ImportKVClient,ImportKV_WriteEngineClient) +//+build br_test + // $ mockgen -package mock github.com/pingcap/kvproto/pkg/import_kvpb ImportKVClient,ImportKV_WriteEngineClient // Package mock is a generated GoMock package. diff --git a/pkg/mock/mock_cluster.go b/pkg/mock/mock_cluster.go index 46e4048fd..c5c4c0992 100644 --- a/pkg/mock/mock_cluster.go +++ b/pkg/mock/mock_cluster.go @@ -1,5 +1,7 @@ // Copyright 2020 PingCAP, Inc. Licensed under Apache-2.0. +//+build br_test + package mock import ( diff --git a/pkg/mock/mock_cluster_test.go b/pkg/mock/mock_cluster_test.go index 2ca00923c..9e5455383 100644 --- a/pkg/mock/mock_cluster_test.go +++ b/pkg/mock/mock_cluster_test.go @@ -1,5 +1,7 @@ // Copyright 2020 PingCAP, Inc. Licensed under Apache-2.0. +//+build br_test + package mock_test import ( diff --git a/pkg/mock/s3iface.go b/pkg/mock/s3iface.go index e8151f124..3b65420a0 100644 --- a/pkg/mock/s3iface.go +++ b/pkg/mock/s3iface.go @@ -1,6 +1,8 @@ // Code generated by MockGen. DO NOT EDIT. // Source: github.com/aws/aws-sdk-go/service/s3/s3iface (interfaces: S3API) +//+build br_test + // $ mockgen -package mock github.com/aws/aws-sdk-go/service/s3/s3iface S3API > pkg/mock/s3iface.go // Package mock is a generated GoMock package. diff --git a/pkg/mock/storage.go b/pkg/mock/storage.go index a41f6ac37..2aede68a8 100644 --- a/pkg/mock/storage.go +++ b/pkg/mock/storage.go @@ -1,7 +1,11 @@ // Code generated by MockGen. DO NOT EDIT. // Source: github.com/pingcap/br/pkg/storage (interfaces: ExternalStorage) +<<<<<<< HEAD:pkg/lightning/mock/storage.go // $ mockgen -package mock github.com/pingcap/br/pkg/storage ExternalStorage +======= +//+build br_test +>>>>>>> 969b420... mock: enable the mock package only in unit tests (#838):pkg/mock/storage.go // Package mock is a generated GoMock package. package mock diff --git a/tests/README.md b/tests/README.md index b6d238aa1..5030a8f7b 100644 --- a/tests/README.md +++ b/tests/README.md @@ -1,8 +1,38 @@ -This folder contains all tests which relies on external processes such as TiDB. +# Unit tests Unit tests (the `*_test.go` files inside the source directory) should *never* rely on external programs. +Run `make test` to execute all unit tests. + +To run a specific test, pass `ARGS` into `make test` like + +```sh +make test ARGS='github.com/pingcap/br/pkg/cdclog --test.v --check.v --check.f TestColumn' +# ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# which package to test more extra test flags +``` + +You can also run unit tests directly via `go test` like: + +```sh +make failpoint-enable + +go test -tags br_test github.com/pingcap/br/pkg/cdclog --test.v --check.v --check.f TestColumn +# ^~~~~~~~~~~~~ + +make failpoint-disable +``` + +but note that: + +* the build-tag `br_test` must be enabled (this workarounds the lack of test-dependencies in go.mod) +* failpoints must be toggled manually + +# Integration tests + +This folder contains all tests which relies on external processes such as TiDB. + ## Preparations 1. The following 9 executables must be copied or linked into these locations: @@ -39,7 +69,6 @@ If you have docker installed, you can skip step 1 and step 2 by running ## Running -Run `make test` to execute the unit tests. Run `make integration_test` to execute the integration tests. This command will 1. Build `br`