diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 9b9d81b4..78fe0e6d 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -43,13 +43,22 @@ jobs: with: go-version-file: ${{ github.workspace }}/go.mod - - name: Test + - name: Run tests with code coverage run: | TEST_MONGODB_IMAGE=${{ matrix.image }} make test-cluster sleep 10 - make test-race + make test-cover make test-cluster-clean + + - name: Upload coverage results + uses: codecov/codecov-action@v3 + with: + file: cover.out + flags: agent + env_vars: GO_VERSION,TEST_MONGODB_IMAGE + fail_ci_if_error: false + - name: Run debug commands on failure if: ${{ failure() }} run: | diff --git a/.gitignore b/.gitignore index 2189c264..11a5485e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,13 @@ .dist .env + .vscode/ .idea + bin build dist + +cover.out mongodb_exporter .DS_Store diff --git a/Makefile b/Makefile index 0fb59475..3fbaa884 100644 --- a/Makefile +++ b/Makefile @@ -50,12 +50,12 @@ define TEST_ENV TEST_MONGODB_S1_PRIMARY_PORT=$(TEST_MONGODB_S1_PRIMARY_PORT) \ TEST_MONGODB_S1_SECONDARY1_PORT=$(TEST_MONGODB_S1_SECONDARY1_PORT) \ TEST_MONGODB_S1_SECONDARY2_PORT=$(TEST_MONGODB_S1_SECONDARY2_PORT) \ - TEST_MONGODB_S1_ARTBITER_PORT=$(TEST_MONGODB_S1_ARBITER_PORT) \ + TEST_MONGODB_S1_ARBITER_PORT=$(TEST_MONGODB_S1_ARBITER_PORT) \ TEST_MONGODB_S2_RS=$(TEST_MONGODB_S2_RS) \ TEST_MONGODB_S2_PRIMARY_PORT=$(TEST_MONGODB_S2_PRIMARY_PORT) \ TEST_MONGODB_S2_SECONDARY1_PORT=$(TEST_MONGODB_S2_SECONDARY1_PORT) \ TEST_MONGODB_S2_SECONDARY2_PORT=$(TEST_MONGODB_S2_SECONDARY2_PORT) \ - TEST_MONGODB_S2_ARTBITER_PORT=$(TEST_MONGODB_S2_ARBITER_PORT) \ + TEST_MONGODB_S2_ARBITER_PORT=$(TEST_MONGODB_S2_ARBITER_PORT) \ TEST_MONGODB_CONFIGSVR_RS=$(TEST_MONGODB_CONFIGSVR_RS) \ TEST_MONGODB_CONFIGSVR1_PORT=$(TEST_MONGODB_CONFIGSVR1_PORT) \ TEST_MONGODB_CONFIGSVR2_PORT=$(TEST_MONGODB_CONFIGSVR2_PORT) \ @@ -67,7 +67,7 @@ endef env: @echo $(TEST_ENV) | tr ' ' '\n' >.env -init: ## Install linters. +init: ## Install linters cd tools && go generate -x -tags=tools build: ## Compile using plain go build @@ -82,7 +82,7 @@ release: ## Build the binaries using goreleaser FILES = $(shell find . -type f -name '*.go' -not -path "./vendor/*") -format: ## Format source code. +format: ## Format source code go mod tidy bin/gofumpt -l -w $(FILES) bin/gci write --section Standard --section Default --section "Prefix(github.com/percona/mongodb_exporter)" . @@ -90,24 +90,26 @@ format: ## Format source code. check: ## Run checks/linters bin/golangci-lint run -check-license: ## Check license in headers. +check-license: ## Check license in headers @go run .github/check-license.go -help: ## Display this help message. +help: ## Display this help message @echo "Please use \`make \` where is one of:" @grep '^[a-zA-Z]' $(MAKEFILE_LIST) | \ awk -F ':.*?## ' 'NF==2 {printf " %-26s%s\n", $$1, $$2}' -test: env ## Run all tests. +test: env ## Run all tests go test -v -count 1 -timeout 30s ./... -test-race: env ## Run all tests with race flag. +test-race: env ## Run all tests with race flag go test -race -v -timeout 30s ./... +test-cover: env ## Run tests and collect cross-package coverage information + go test -race -timeout 30s -coverprofile=cover.out -covermode=atomic -coverpkg=./... ./... + test-cluster: env ## Starts MongoDB test cluster. Use env var TEST_MONGODB_IMAGE to set flavor and version. Example: TEST_MONGODB_IMAGE=mongo:3.6 make test-cluster docker compose up --build -d ./docker/scripts/setup-pbm.sh test-cluster-clean: env ## Stops MongoDB test cluster. - docker compose down --remove-orphans - + docker compose down --remove-orphans --volumes diff --git a/docker-compose.yml b/docker-compose.yml index d4974c11..9e7cacc7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -176,7 +176,7 @@ services: container_name: "mongo-cnf-2" image: ${TEST_MONGODB_IMAGE:-mongo:4.4} ports: - - "${TEST_MONGODB_CONFIGSVR1_PORT:-17007}:27017" + - "${TEST_MONGODB_CONFIGSVR2_PORT:-17008}:27017" command: mongod --dbpath /data/db --replSet cnf-serv --configsvr --port 27017 --oplogSize 16 networks: - cnf-serv @@ -185,7 +185,7 @@ services: container_name: "mongo-cnf-3" image: ${TEST_MONGODB_IMAGE:-mongo:4.4} ports: - - "${TEST_MONGODB_CONFIGSVR2_PORT:-17008}:27017" + - "${TEST_MONGODB_CONFIGSVR3_PORT:-17009}:27017" command: mongod --dbpath /data/db --replSet cnf-serv --configsvr --port 27017 --oplogSize 16 networks: - cnf-serv @@ -194,7 +194,7 @@ services: container_name: "mongo-cnf-1" image: ${TEST_MONGODB_IMAGE:-mongo:4.4} ports: - - "${TEST_MONGODB_CONFIGSVR3_PORT:-17009}:27017" + - "${TEST_MONGODB_CONFIGSVR1_PORT:-17007}:27017" command: mongod --dbpath /data/db --replSet cnf-serv --configsvr --port 27017 --oplogSize 16 networks: - cnf-serv diff --git a/docker/scripts/setup.sh b/docker/scripts/setup.sh index 6ccfac0d..1d5917ae 100755 --- a/docker/scripts/setup.sh +++ b/docker/scripts/setup.sh @@ -100,6 +100,7 @@ function general_servers() { EOF } + case $1 in cnf_servers) cnf_servers