From 0d2a69297e6fd5422001a4f73975d5317abcaa88 Mon Sep 17 00:00:00 2001 From: Shashank Sinha Date: Tue, 27 Sep 2022 20:15:53 +0530 Subject: [PATCH 1/3] Add code coverage action in pipeline --- .github/workflows/go.yml | 14 ++++++++++++++ .gitignore | 4 ++++ Makefile | 19 +++++++++++-------- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 675f5d0f2..fccbb868e 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -83,3 +83,17 @@ jobs: bin/golangci-lint run -c=.golangci-required.yml --out-format=line-number | env REVIEWDOG_GITHUB_API_TOKEN=${{ secrets.GITHUB_TOKEN }} bin/reviewdog -f=golangci-lint -level=error -reporter=github-pr-check bin/golangci-lint run -c=.golangci.yml --out-format=line-number | env REVIEWDOG_GITHUB_API_TOKEN=${{ secrets.GITHUB_TOKEN }} bin/reviewdog -f=golangci-lint -level=error -reporter=github-pr-review make check-license + + - name: Run code coverage + run: | + TEST_MONGODB_IMAGE=${{ matrix.image }} make test-cluster + 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 diff --git a/.gitignore b/.gitignore index 70bde491b..547d800bb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,12 @@ .dist .env + .vscode/ .idea + bin build dist + +cover.out mongodb_exporter diff --git a/Makefile b/Makefile index 47b608827..28fb6f6a5 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -81,7 +81,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)" . @@ -89,22 +89,25 @@ 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-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 +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:4.4 make test-cluster docker-compose up -d -test-cluster-clean: env ## Stops MongoDB test cluster. +test-cluster-clean: env ## Stops MongoDB test cluster docker-compose down --remove-orphans From 30c79a4d94e6d5d53b931cdcddae9e5dbe168314 Mon Sep 17 00:00:00 2001 From: Shashank Sinha Date: Tue, 27 Sep 2022 21:14:22 +0530 Subject: [PATCH 2/3] Trigger pipeline From bfff0a67b006f8968b2d7d9caa8bcc2a01a46f52 Mon Sep 17 00:00:00 2001 From: Shashank Sinha Date: Tue, 27 Sep 2022 22:46:30 +0530 Subject: [PATCH 3/3] Fix configuration of test sandbox --- Makefile | 6 +++--- docker-compose.yml | 15 ++++++++------- docker/scripts/setup.sh | 32 +++++++++++--------------------- exporter/topology_info_test.go | 4 ++-- 4 files changed, 24 insertions(+), 33 deletions(-) diff --git a/Makefile b/Makefile index 28fb6f6a5..ff55b2d6a 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) \ @@ -110,4 +110,4 @@ test-cluster: env ## Starts MongoDB test cluster. Use env var TEST_MON docker-compose up -d test-cluster-clean: env ## Stops MongoDB test cluster - docker-compose down --remove-orphans + docker-compose down --remove-orphans --volumes --timeout 1 diff --git a/docker-compose.yml b/docker-compose.yml index 11193b8ee..53bb62efd 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -58,21 +58,21 @@ services: container_name: "mongo-2-2" image: ${TEST_MONGODB_IMAGE:-mongo:4.2} ports: - - "${TEST_MONGODB_S2_PRIMARY_PORT:-17004}:27017" + - "${TEST_MONGODB_S2_SECONDARY1_PORT:-17005}:27017" command: mongod --replSet rs2 --shardsvr --port 27017 --oplogSize 16 mongo-2-3: container_name: "mongo-2-3" image: ${TEST_MONGODB_IMAGE:-mongo:4.2} ports: - - "${TEST_MONGODB_S2_SECONDARY1_PORT:-17005}:27017" + - "${TEST_MONGODB_S2_SECONDARY2_PORT:-17006}:27017" command: mongod --replSet rs2 --shardsvr --port 27017 --oplogSize 16 mongo-2-1: container_name: "mongo-2-1" image: ${TEST_MONGODB_IMAGE:-mongo:4.2} ports: - - "${TEST_MONGODB_S2_SECONDARY2_PORT:-17006}:27017" + - "${TEST_MONGODB_S2_PRIMARY_PORT:-17004}:27017" command: mongod --replSet rs2 --shardsvr --port 27017 --oplogSize 16 links: - mongo-2-2:mongo-2-2 @@ -83,7 +83,7 @@ services: image: ${TEST_MONGODB_IMAGE:-mongo:4.2} ports: - "${TEST_MONGODB_S2_ARBITER:-17012}:27017" - command: mongod --replSet rs1 --shardsvr --port 27017 --oplogSize 16 + command: mongod --replSet rs2 --shardsvr --port 27017 --oplogSize 16 mongo-rs2-setup: container_name: "mongo-rs2-setup" @@ -97,6 +97,7 @@ services: - mongo-2-1:mongo-2-1 - mongo-2-2:mongo-2-2 - mongo-2-3:mongo-2-3 + - mongo-2-arbiter:mongo-2-arbiter volumes: - ./docker/scripts:/scripts environment: @@ -112,21 +113,21 @@ services: container_name: "mongo-cnf-2" image: ${TEST_MONGODB_IMAGE:-mongo:4.2} 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 mongo-cnf-3: container_name: "mongo-cnf-3" image: ${TEST_MONGODB_IMAGE:-mongo:4.2} 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 mongo-cnf-1: container_name: "mongo-cnf-1" image: ${TEST_MONGODB_IMAGE:-mongo:4.2} 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 links: - mongo-cnf-2:mongo-cnf-2 diff --git a/docker/scripts/setup.sh b/docker/scripts/setup.sh index e663230f4..dccc2a4ab 100755 --- a/docker/scripts/setup.sh +++ b/docker/scripts/setup.sh @@ -1,14 +1,9 @@ #!/bin/bash -mongodb1=`getent hosts ${MONGO1} | awk '{ print $1 }'` -mongodb2=`getent hosts ${MONGO2} | awk '{ print $1 }'` -mongodb3=`getent hosts ${MONGO3} | awk '{ print $1 }'` -arbiter=`getent hosts ${ARBITER} | awk '{ print $1 }'` - port=${PORT:-27017} echo "Waiting for startup.." -until mongo --host ${mongodb1}:${port} --eval 'quit(db.runCommand({ ping: 1 }).ok ? 0 : 2)' &>/dev/null; do +until mongo --host ${MONGO1}:${port} --eval 'quit(db.runCommand({ ping: 1 }).ok ? 0 : 2)' &>/dev/null; do printf '.' sleep 1 done @@ -20,7 +15,7 @@ echo setup.sh time now: `date +"%T" ` function cnf_servers() { echo "setup cnf servers" - mongo --host ${mongodb1}:${port} <