Add coverage and code quality badges.

This commit is contained in:
Will Rouesnel 2017-02-25 00:19:07 +11:00
parent 8ded56a7da
commit ff8e0c5382
7 changed files with 52 additions and 6 deletions

3
.gitignore vendored
View File

@ -6,3 +6,6 @@ postgres_exporter_integration_test
*-stamp *-stamp
.idea .idea
*.iml *.iml
cover.out
cover.*.out

View File

@ -6,6 +6,7 @@ go:
- '1.7' - '1.7'
# Make sure we have p2 and the postgres client. # Make sure we have p2 and the postgres client.
before_install: before_install:
- go get -v github.com/mattn/goveralls
- sudo wget -O /usr/local/bin/p2 https://github.com/wrouesnel/p2cli/releases/download/r4/p2 && - sudo wget -O /usr/local/bin/p2 https://github.com/wrouesnel/p2cli/releases/download/r4/p2 &&
sudo chmod +x /usr/local/bin/p2 sudo chmod +x /usr/local/bin/p2
- sudo wget -O /usr/local/bin/docker-compose https://github.com/docker/compose/releases/download/1.9.0-rc4/docker-compose-Linux-x86_64 && - sudo wget -O /usr/local/bin/docker-compose https://github.com/docker/compose/releases/download/1.9.0-rc4/docker-compose-Linux-x86_64 &&
@ -16,6 +17,8 @@ script:
- make all - make all
- make docker - make docker
- make test-integration - make test-integration
- ./concatenate_coverage cover.out cover.test.out cover.integration.out
- $HOME/gopath/bin/goveralls -coverprofile=cover.out -service=travis-ci
after_success: after_success:
- docker login -e $DOCKER_EMAIL -u $DOCKER_USER -p $DOCKER_PASS - docker login -e $DOCKER_EMAIL -u $DOCKER_USER -p $DOCKER_PASS
# Push a tagged build if a tag is found. # Push a tagged build if a tag is found.

View File

@ -1,7 +1,8 @@
GO_SRC := $(shell find . -type f -name "*.go") GO_SRC := $(shell find -type f -name '*.go' ! -path '*/vendor/*')
CONTAINER_NAME ?= wrouesnel/postgres_exporter:latest CONTAINER_NAME ?= wrouesnel/postgres_exporter:latest
VERSION ?= $(shell git describe --dirty)
all: vet test postgres_exporter all: vet test postgres_exporter
@ -10,24 +11,32 @@ cross: docker-build docker
# Simple go build # Simple go build
postgres_exporter: $(GO_SRC) postgres_exporter: $(GO_SRC)
CGO_ENABLED=0 go build -a -ldflags "-extldflags '-static' -X main.Version=$(shell git describe --dirty)" -o postgres_exporter . CGO_ENABLED=0 go build -a -ldflags "-extldflags '-static' -X main.Version=$(VERSION)" -o postgres_exporter .
postgres_exporter_integration_test: $(GO_SRC) postgres_exporter_integration_test: $(GO_SRC)
CGO_ENABLED=0 go test -c -tags integration \ CGO_ENABLED=0 go test -c -tags integration \
-a -ldflags "-extldflags '-static' -X main.Version=git:$(shell git describe --dirty)" -o postgres_exporter_integration_test . -a -ldflags "-extldflags '-static' -X main.Version=$(VERSION)" -o postgres_exporter_integration_test -cover -covermode count .
# Take a go build and turn it into a minimal container # Take a go build and turn it into a minimal container
docker: postgres_exporter docker: postgres_exporter
docker build -t $(CONTAINER_NAME) . docker build -t $(CONTAINER_NAME) .
vet: vet:
go vet . go vet
# Check code conforms to go fmt
style:
! gofmt -s -l $(GO_SRC) 2>&1 | read 2>/dev/null
# Format the code
fmt:
gofmt -s -w $(GO_SRC)
test: test:
go test -v -cover . go test -v -covermode count -coverprofile=cover.test.out
test-integration: postgres_exporter postgres_exporter_integration_test test-integration: postgres_exporter postgres_exporter_integration_test
tests/test-smoke ./postgres_exporter ./postgres_exporter_integration_test tests/test-smoke "$(shell pwd)/postgres_exporter" "$(shell pwd)/postgres_exporter_integration_test_script $(shell pwd)/postgres_exporter_integration_test $(shell pwd)/cover.integration.out"
# Do a self-contained docker build - we pull the official upstream container # Do a self-contained docker build - we pull the official upstream container
# and do a self-contained build. # and do a self-contained build.

View File

@ -1,4 +1,6 @@
[![Build Status](https://travis-ci.org/wrouesnel/postgres_exporter.svg?branch=master)](https://travis-ci.org/wrouesnel/postgres_exporter) [![Build Status](https://travis-ci.org/wrouesnel/postgres_exporter.svg?branch=master)](https://travis-ci.org/wrouesnel/postgres_exporter)
[![Coverage Status](https://coveralls.io/repos/github/wrouesnel/postgres_exporter/badge.svg?branch=master)](https://coveralls.io/github/wrouesnel/postgres_exporter?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/wrouesnel/postgres_exporter)](https://goreportcard.com/report/github.com/wrouesnel/postgres_exporter)
# PostgresSQL Server Exporter # PostgresSQL Server Exporter
@ -108,3 +110,4 @@ AS
GRANT SELECT ON postgres_exporter.pg_stat_replication TO postgres_exporter; GRANT SELECT ON postgres_exporter.pg_stat_replication TO postgres_exporter;
GRANT SELECT ON postgres_exporter.pg_stat_activity TO postgres_exporter; GRANT SELECT ON postgres_exporter.pg_stat_activity TO postgres_exporter;
``` ```

12
concatenate_coverage Executable file
View File

@ -0,0 +1,12 @@
#!/bin/bash
# Concatenate a list of coverage reports
# Usage: concatenate_coverage <out> [[test] ...]
output_file=$1
shift
cat $1 > $output_file
shift
for f in $@ ; do
tail -n +2 $f >> $output_file
done

View File

@ -0,0 +1,15 @@
#!/bin/bash
# This script wraps the integration test binary so it produces concatenated
# test output.
test_binary=$1
shift
output_cov=$1
shift
echo "mode: count" > $output_cov
test_cov=$(mktemp)
$test_binary -test.coverprofile=$test_cov $@
tail -n +2 $test_cov >> $output_cov
rm -f $test_cov

View File

@ -81,6 +81,7 @@ smoketest_postgres() {
wait_for_postgres localhost 55432 wait_for_postgres localhost 55432
DATA_SOURCE_NAME="postgresql://postgres:$POSTGRES_PASSWORD@localhost:55432/?sslmode=disable" $test_binary --log.level=debug || exit $? DATA_SOURCE_NAME="postgresql://postgres:$POSTGRES_PASSWORD@localhost:55432/?sslmode=disable" $test_binary --log.level=debug || exit $?
# exporter_pid=$! # exporter_pid=$!
# trap "docker logs $CONTAINER_NAME ; docker kill $CONTAINER_NAME ; docker rm $CONTAINER_NAME ; kill $exporter_pid; exit 1" EXIT INT TERM # trap "docker logs $CONTAINER_NAME ; docker kill $CONTAINER_NAME ; docker rm $CONTAINER_NAME ; kill $exporter_pid; exit 1" EXIT INT TERM
# wait_for_exporter # wait_for_exporter