diff --git a/.gitignore b/.gitignore index a5e69a40..8f3b0cc8 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,6 @@ postgres_exporter_integration_test *-stamp .idea *.iml +cover.out +cover.*.out + diff --git a/.travis.yml b/.travis.yml index 019369b6..f007104a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,7 @@ go: - '1.7' # Make sure we have p2 and the postgres client. 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 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 && @@ -16,6 +17,8 @@ script: - make all - make docker - 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: - docker login -e $DOCKER_EMAIL -u $DOCKER_USER -p $DOCKER_PASS # Push a tagged build if a tag is found. diff --git a/Makefile b/Makefile index 8be2ae52..df504c78 100644 --- a/Makefile +++ b/Makefile @@ -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 +VERSION ?= $(shell git describe --dirty) all: vet test postgres_exporter @@ -10,24 +11,32 @@ cross: docker-build docker # Simple go build 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) 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 docker: postgres_exporter docker build -t $(CONTAINER_NAME) . 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: - go test -v -cover . + go test -v -covermode count -coverprofile=cover.test.out 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 # and do a self-contained build. diff --git a/README.md b/README.md index e7da9916..8ca28973 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ [![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 @@ -108,3 +110,4 @@ AS GRANT SELECT ON postgres_exporter.pg_stat_replication TO postgres_exporter; GRANT SELECT ON postgres_exporter.pg_stat_activity TO postgres_exporter; ``` + diff --git a/concatenate_coverage b/concatenate_coverage new file mode 100755 index 00000000..4490a18b --- /dev/null +++ b/concatenate_coverage @@ -0,0 +1,12 @@ +#!/bin/bash +# Concatenate a list of coverage reports +# Usage: concatenate_coverage [[test] ...] + +output_file=$1 +shift +cat $1 > $output_file +shift + +for f in $@ ; do + tail -n +2 $f >> $output_file +done diff --git a/postgres_exporter_integration_test_script b/postgres_exporter_integration_test_script new file mode 100755 index 00000000..03f2945a --- /dev/null +++ b/postgres_exporter_integration_test_script @@ -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 diff --git a/tests/test-smoke b/tests/test-smoke index eab74e85..a27875f2 100755 --- a/tests/test-smoke +++ b/tests/test-smoke @@ -81,6 +81,7 @@ smoketest_postgres() { wait_for_postgres localhost 55432 DATA_SOURCE_NAME="postgresql://postgres:$POSTGRES_PASSWORD@localhost:55432/?sslmode=disable" $test_binary --log.level=debug || exit $? + # exporter_pid=$! # trap "docker logs $CONTAINER_NAME ; docker kill $CONTAINER_NAME ; docker rm $CONTAINER_NAME ; kill $exporter_pid; exit 1" EXIT INT TERM # wait_for_exporter