Skip to content

Commit

Permalink
Merge pull request #138 from ruflin/refactor-makefile
Browse files Browse the repository at this point in the history
Clean up Makefile
  • Loading branch information
Steffen Siering committed Oct 9, 2015
2 parents 4bfa5da + 323ce3b commit d33ae45
Show file tree
Hide file tree
Showing 15 changed files with 266 additions and 111 deletions.
6 changes: 1 addition & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ _test
_output
/docs/html_docs
.vagrant
coverage/*
build

# Architecture specific extensions/prefixes
*.[568vq]
Expand All @@ -28,7 +28,6 @@ _cgo_defun.c
_cgo_gotypes.go
_cgo_export.*

/crosscompile.bash
_testmain.go

*.exe
Expand All @@ -40,6 +39,3 @@ _testmain.go
.idea
.jenkins

bin

docker/test.env
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ addonsbefore_install:
- ln -s $TRAVIS_BUILD_DIR $HOME/gopath/src/libbeat

install:
- make deps
- make

script:
Expand All @@ -41,4 +40,4 @@ notifications:

after_success:
# Copy full.cov to coverage.txt because codecov.io requires this file
- test -f coverage/full.cov && bash <(curl -s https://codecov.io/bash) -f coverage/full.cov
- test -f build/coverage/full.cov && bash <(curl -s https://codecov.io/bash) -f build/coverage/full.cov
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Change Log
All notable changes to this project will be documented in this file based on the
[Keep a Changelog](http://keepachangelog.com/) Standard.


## [Unreleased](https://github.com/elastic/libbeat/compare/1.0.0-beta3...HEAD)

### Backward Compatibility Breaks

### Bugfixes

### Added

### Improvements

### Deprecated
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ RUN set -x \
golang.org/x/tools/cmd/cover \
golang.org/x/tools/cmd/vet

COPY docker-entrypoint.sh /entrypoint.sh
COPY scripts/docker-entrypoint.sh /entrypoint.sh

# Setup work environment
ENV LIBBEAT_PATH /go/src/github.com/elastic/libbeat
Expand Down
204 changes: 125 additions & 79 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,68 +1,154 @@
#/bin/bash

### VARIABLE SETUP ###

GODEP=$(GOPATH)/bin/godep
# Hidden directory to install dependencies for jenkins
export PATH := ./bin:$(PATH)
GOFILES = $(shell find . -type f -name '*.go')
SHELL=/bin/bash
ES_HOST?="elasticsearch-200"
BUILD_DIR=build
COVERAGE_DIR=${BUILD_DIR}/coverage
PROCESSES?= 4
TIMEOUT?= 90


### BUILDING ###

# Builds libbeat. No binary created as it is a library
.PHONY: build
build:
go get github.com/tools/godep
build: deps
$(GODEP) go build ./...

# Create test coverage binary
.PHONY: libbeat.test
libbeat.test: $(GOFILES)
$(GODEP) go test -c -covermode=count -coverpkg ./...

# Cross-compile libbeat for the OS and architectures listed in
# crosscompile.bash. The binaries are placed in the ./bin dir.
.PHONY: crosscompile
crosscompile: $(GOFILES)
go get github.com/tools/godep
mkdir -p ${BUILD_DIR}/bin
source scripts/crosscompile.bash; OUT='${BUILD_DIR}/bin' go-build-all

# Fetch dependencies
.PHONY: deps
deps:
go get github.com/tools/godep
# TODO: Is this still needed?
go get -t ./...
# goautotest is used from the Makefile to run tests in a loop
go get github.com/tsg/goautotest
# cover
go get golang.org/x/tools/cmd/cover

.PHONY: gofmt
gofmt:
go fmt ./...

.PHONY: test
test:
$(GODEP) go test -short ./...

# Checks project and source code if everything is according to standard
.PHONY: check
check:
# This should be modified so it throws an error on the build system in case the output is not empty
gofmt -d .
godep go vet ./...

.PHONY: autotest
autotest:
goautotest -short ./...
# Cleans up directory and source code with gofmt
.PHONY: clean
clean:
go fmt ./...
-rm -r build
-rm libbeat.test

# Shortcut for continuous integration
# This should always run before merging.
.PHONY: ci
ci:
make
make check
make testsuite

### Testing ###
# All tests are always run with coverage reporting enabled


# Prepration for tests
.PHONY: prepare-tests
prepare-tests:
mkdir -p ${COVERAGE_DIR}
# coverage tools
go get golang.org/x/tools/cmd/cover
# gotestcover is needed to fetch coverage for multiple packages
go get github.com/pierrre/gotestcover

.PHONY: testlong
testlong:
go vet ./...
make coverage
# Runs the unit tests
.PHONY: unit-tests
unit-tests: prepare-tests
#go test -short ./...
GOPATH=$(shell $(GODEP) path):$(GOPATH) $(GOPATH)/bin/gotestcover -coverprofile=${COVERAGE_DIR}/unit.cov -short -covermode=count github.com/elastic/libbeat/...

# Run integration tests. Unit tests are run as part of the integration tests
.PHONY: integration-tests
integration-tests: prepare-tests
GOPATH=$(shell $(GODEP) path):$(GOPATH) $(GOPATH)/bin/gotestcover -coverprofile=${COVERAGE_DIR}/integration.cov -covermode=count github.com/elastic/libbeat/...

# Runs the integration inside a virtual environment. This can be run on any docker-machine (local, remote)
.PHONY: integration-tests-environment
integration-tests-environment:
make prepare-tests
make build-image
NAME=$$(docker-compose run -d libbeat make integration-tests | awk 'END{print}') || exit 1; \
echo "docker libbeat test container: '$$NAME'"; \
docker attach $$NAME; CODE=$$?;\
mkdir -p ${COVERAGE_DIR}; \
docker cp $$NAME:/go/src/github.com/elastic/libbeat/${COVERAGE_DIR}/integration.cov $(shell pwd)/${COVERAGE_DIR}/; \
docker rm $$NAME > /dev/null; \
exit $$CODE

.PHONY: benchmark
benchmark:
go test -short -bench=. ./...
# Runs the system tests
.PHONY: system-tests
system-tests: libbeat.test prepare-tests system-tests-setup
. build/system-tests/env/bin/activate; nosetests -w tests/system --processes=${PROCESSES} --process-timeout=$(TIMEOUT)
# Writes count mode on top of file
echo 'mode: count' > ${COVERAGE_DIR}/system.cov
# Collects all system coverage files and skips top line with mode
tail -q -n +2 ./build/system-tests/run/**/*.cov >> ${COVERAGE_DIR}/system.cov

.PHONY: coverage
coverage:
# gotestcover is needed to fetch coverage for multiple packages
go get github.com/pierrre/gotestcover
mkdir -p coverage
GOPATH=$(shell $(GODEP) path):$(GOPATH) $(GOPATH)/bin/gotestcover -coverprofile=coverage/unit.cov -covermode=count github.com/elastic/libbeat/...
$(GODEP) go tool cover -html=coverage/unit.cov -o coverage/unit.html
# Runs the system tests
.PHONY: system-tests
system-tests-setup: tests/system/requirements.txt
test -d env || virtualenv build/system-tests/env > /dev/null
. build/system-tests/env/bin/activate && pip install -Ur tests/system/requirements.txt > /dev/null
touch build/system-tests/env/bin/activate

.PHONY: clean
clean:
make gofmt
-rm -r coverage

# Run benchmark tests
.PHONY: benchmark-tests
benchmark-tests:
# No benchmark tests exist so far
#go test -short -bench=. ./...

# Runs all tests and generates the coverage reports
.PHONY: testsuite
testsuite:
make integration-tests-environment
make system-tests
make benchmark-tests
make coverage-report


# Generates a coverage report from the existing coverage files
# It assumes that some covrage reports already exists, otherwise it will fail
.PHONY: coverage-report
coverage-report:
# Writes count mode on top of file
echo 'mode: count' > ./${COVERAGE_DIR}/full.cov
# Collects all coverage files and skips top line with mode
tail -q -n +2 ./${COVERAGE_DIR}/*.cov >> ./${COVERAGE_DIR}/full.cov
$(GODEP) go tool cover -html=./${COVERAGE_DIR}/full.cov -o ${COVERAGE_DIR}/full.html



### CONTAINER ENVIRONMENT ####

# Builds the environment to test libbeat
.PHONY: build-image
build-image: write-environment
make clean
docker-compose build

# Runs the environment so the redis and elasticsearch can also be used for local development
Expand All @@ -79,53 +165,13 @@ stop-environment:

.PHONY: write-environment
write-environment:
echo "ES_HOST=${ES_HOST}" > docker/test.env
echo "ES_PORT=9200" >> docker/test.env

# Runs the full test suite and puts out the result. This can be run on any docker-machine (local, remote)
.PHONY: testsuite
testsuite: build-image write-environment
NAME=$$(docker-compose run -d libbeat make testlong | awk 'END{print}') || exit 1; \
echo "docker libbeat test container: '$$NAME'"; \
docker attach $$NAME; CODE=$$?;\
mkdir -p coverage; \
docker cp $$NAME:/go/src/github.com/elastic/libbeat/coverage/unit.cov $(shell pwd)/coverage/; \
docker cp $$NAME:/go/src/github.com/elastic/libbeat/coverage/unit.html $(shell pwd)/coverage/; \
docker rm $$NAME > /dev/null; \
exit $$CODE
mkdir -p build
echo "ES_HOST=${ES_HOST}" > build/test.env
echo "ES_PORT=9200" >> build/test.env

# Sets up docker-compose locally for jenkins so no global installation is needed
.PHONY: docker-compose-setup
docker-compose-setup:
mkdir -p bin
curl -L https://github.com/docker/compose/releases/download/1.4.0/docker-compose-`uname -s`-`uname -m` > bin/docker-compose
chmod +x bin/docker-compose

.PHONY: libbeat.test
libbeat.test: $(GOFILES)
$(GODEP) go test -c -covermode=count -coverpkg ./...


.PHONY: system-tests
system-tests: libbeat.test
mkdir -p coverage
./libbeat.test -c tests/files/config.yml -d "*" -test.coverprofile coverage/system.cov

# Cross-compile libbeat for the OS and architectures listed in
# crosscompile.bash. The binaries are placed in the ./bin dir.
.PHONY: crosscompile
crosscompile: $(GOFILES)
go get github.com/tools/godep
curl https://raw.githubusercontent.com/elastic/filebeat/fe0f6a82d46b56d852f3f9ef81196aef4624d1a7/crosscompile.bash > crosscompile.bash
mkdir -p bin
source crosscompile.bash; OUT='bin' go-build-all

.PHONY: full-coverage
full-coverage:
make testlong
make -C ./tests/system coverage
# Writes count mode on top of file
echo 'mode: count' > ./coverage/full.cov
# Collects all coverage files and skips top line with mode
tail -q -n +2 ./coverage/*.cov >> ./coverage/full.cov
$(GODEP) go tool cover -html=./coverage/full.cov -o coverage/full.html
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ libbeat:
- LS_HOST=logstash
- LS_LUMBERJACK_TCP_PORT=12345
env_file:
- docker/test.env
- build/test.env
elasticsearch-172:
image: elasticsearch:1.7.2
elasticsearch-200:
Expand All @@ -20,9 +20,9 @@ elasticsearch-200:
redis:
image: redis
logstash:
build: docker/logstash
build: scripts/docker/logstash
env_file:
- docker/test.env
- build/test.env
links:
- elasticsearch-172
- elasticsearch-200
Loading

0 comments on commit d33ae45

Please sign in to comment.