Просмотр исходного кода

BUILDS: Replace some shell scripts with Makefile

Tom Denham 8 лет назад
Родитель
Сommit
4476ec5fd1
8 измененных файлов с 65 добавлено и 127 удалено
  1. 1 0
      .gitignore
  2. 1 1
      .travis.yml
  3. 0 1
      Dockerfile
  4. 54 0
      Makefile
  5. 0 39
      build
  6. 0 27
      cover
  7. 9 0
      dist/license-check.sh
  8. 0 59
      test

+ 1 - 0
.gitignore

@@ -1,3 +1,4 @@
 flannel
 coverage
 bin
+cover.out

+ 1 - 1
.travis.yml

@@ -18,7 +18,7 @@ install:
  - go get ${TOOLS_CMD}/cover
 
 script:
- - ./test
+ - make test
 
 notifications:
   email: false

+ 0 - 1
Dockerfile

@@ -1 +0,0 @@
-FROM golang:1.4.1-onbuild

+ 54 - 0
Makefile

@@ -0,0 +1,54 @@
+.PHONY: all test cover gofmt gofmt-fix license-check
+
+# Grab the absolute directory that contains this file.
+ROOT_DIR:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
+
+# These variables can be overridden by setting an environment variable.
+TEST_PACKAGES?=pkg/ip subnet remote
+TEST_PACKAGES_EXPANDED=$(TEST_PACKAGES:%=github.com/coreos/flannel/%)
+PACKAGES?=$(TEST_PACKAGES) network
+PACKAGES_EXPANDED=$(PACKAGES:%=github.com/coreos/flannel/%)
+
+default: help
+all: test				    ## Run all the tests
+binary: artifacts/flanneld  ## Create the flanneld binary
+
+artifacts/flanneld: $(shell find . -type f  -name '*.go')
+	mkdir -p artifacts
+	go build -o artifacts/flanneld \
+	  -ldflags "-extldflags -static -X github.com/coreos/flannel/version.Version=$(shell git describe --dirty)"
+
+test:
+	go test -cover $(TEST_PACKAGES_EXPANDED)
+
+cover:
+	#A single package must be given - e.g. 'PACKAGES=pkg/ip make cover'
+	go test -coverprofile cover.out $(PACKAGES_EXPANDED)
+	go tool cover -html=cover.out
+
+
+# Throw an error if gofmt finds problems.
+# "read" will return a failure return code if there is no output. This is inverted wth the "!"
+gofmt:
+	! gofmt -d $(PACKAGES) 2>&1 | read
+
+gofmt-fix:
+	gofmt -w $(PACKAGES)
+
+license-check:
+	dist/license-check.sh
+
+## Display this help text
+help: # Some kind of magic from https://gist.github.com/rcmachado/af3db315e31383502660
+	$(info Available targets)
+	@awk '/^[a-zA-Z\-\_0-9]+:/ {								   \
+		nb = sub( /^## /, "", helpMsg );							 \
+		if(nb == 0) {												\
+			helpMsg = $$0;											 \
+			nb = sub( /^[^:]*:.* ## /, "", helpMsg );				  \
+		}															\
+		if (nb)													  \
+			printf "\033[1;31m%-" width "s\033[0m %s\n", $$1, helpMsg; \
+	}															  \
+	{ helpMsg = $$0 }'											 \
+	$(MAKEFILE_LIST)

+ 0 - 39
build

@@ -1,39 +0,0 @@
-#!/usr/bin/env bash
-set -e
-
-function linker_dashX {
-	local version=$(go version)
-	local regex="go([0-9]+).([0-9]+)."
-	if [[ $version =~ $regex ]]; then
-		if [ ${BASH_REMATCH[1]} -eq "1" -a ${BASH_REMATCH[2]} -le "4" ]; then
-			echo "$1 \"$2\""
-		else
-			echo "$1=$2"
-		fi
-	else
-		echo "could not determine Go version"
-		exit 1
-	fi
-}
-
-ORG_PATH="github.com/coreos"
-REPO_PATH="${ORG_PATH}/flannel"
-VERSION=$(git describe --dirty)
-GLDFLAGS="-X $(linker_dashX github.com/coreos/flannel/version.Version ${VERSION})"
-
-if [ ! -h gopath/src/${REPO_PATH} ]; then
-	mkdir -p gopath/src/${ORG_PATH}
-	ln -s ../../../.. gopath/src/${REPO_PATH} || exit 255
-fi
-
-export GOBIN=${PWD}/bin
-export GOPATH=${PWD}/gopath
-
-eval $(go env)
-
-if [ ${GOOS} = "linux" ]; then
-	echo "Building flanneld..."
-	go build -o ${GOBIN}/flanneld -ldflags "${GLDFLAGS}" ${REPO_PATH}
-else
-	echo "Not on Linux - skipping flanneld build"
-fi

+ 0 - 27
cover

@@ -1,27 +0,0 @@
-#!/bin/bash -e
-#
-# Generate coverage HTML for a package
-# e.g. PKG=./unit ./cover
-#
-
-if [ -z "$PKG" ]; then
-	echo "cover only works with a single package, sorry"
-	exit 255
-fi
-
-COVEROUT="coverage"
-
-if ! [ -d "$COVEROUT" ]; then
-	mkdir "$COVEROUT"
-fi
-
-# strip out slashes and dots
-COVERPKG=${PKG//\//}
-COVERPKG=${COVERPKG//./}
-
-# generate arg for "go test"
-export COVER="-coverprofile ${COVEROUT}/${COVERPKG}.out"
-
-source ./test
-
-go tool cover -html=${COVEROUT}/${COVERPKG}.out

+ 9 - 0
dist/license-check.sh

@@ -0,0 +1,9 @@
+#!/usr/bin/env bash
+
+licRes=$(for file in $(find . -type f -iname '*.go' ! -path './vendor/*'); do
+		head -n3 "${file}" | grep -Eq "(Copyright|generated|GENERATED)" || echo -e "  ${file}"
+	done;)
+if [ -n "${licRes}" ]; then
+	echo -e "license header checking failed:\n${licRes}"
+	exit 255
+fi

+ 0 - 59
test

@@ -1,59 +0,0 @@
-#!/bin/bash -e
-#
-# Run all flannel tests (not including functional)
-#   ./test
-#   ./test -v
-#
-# Run tests for one package
-#   PKG=./unit ./test
-#   PKG=ssh ./test
-#
-
-# Invoke ./cover for HTML output
-COVER=${COVER:-"-cover"}
-
-source ./build
-
-TESTABLE="pkg/ip subnet network remote"
-FORMATTABLE="$TESTABLE"
-
-# user has not provided PKG override
-if [ -z "$PKG" ]; then
-	TEST=$TESTABLE
-	FMT=$FORMATTABLE
-
-# user has provided PKG override
-else
-	# strip out slashes and dots from PKG=./foo/
-	TEST=${PKG//\//}
-	TEST=${TEST//./}
-
-	# only run gofmt on packages provided by user
-	FMT="$TEST"
-fi
-
-# split TEST into an array and prepend REPO_PATH to each local package
-split=(${TEST// / })
-TEST=${split[@]/#/${REPO_PATH}/}
-
-echo "Running tests..."
-go test -i ${TEST}
-go test ${COVER} $@ ${TEST}
-
-echo "Checking gofmt..."
-fmtRes=$(gofmt -l $FMT)
-if [ -n "${fmtRes}" ]; then
-	echo -e "gofmt checking failed:\n${fmtRes}"
-	exit 255
-fi
-
-echo "Checking for license header..."
-licRes=$(for file in $(find . -type f -iname '*.go' ! -path './vendor/*'); do
-		head -n3 "${file}" | grep -Eq "(Copyright|generated|GENERATED)" || echo -e "  ${file}"
-	done;)
-if [ -n "${licRes}" ]; then
-	echo -e "license header checking failed:\n${licRes}"
-	exit 255
-fi
-
-echo "Success"