build: add check for functional environment

Updates: #1000
Reported-by: Madhu Rajanna <madhupr007@gmail.com>
Signed-off-by: Niels de Vos <ndevos@redhat.com>
This commit is contained in:
Niels de Vos 2020-05-04 10:28:40 +02:00 committed by mergify[bot]
parent 78a6de2bd0
commit b89f7fa1d6
2 changed files with 54 additions and 5 deletions

View File

@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
.PHONY: all cephcsi .PHONY: all cephcsi check-env
CONTAINER_CMD?=$(shell docker version >/dev/null 2>&1 && echo docker) CONTAINER_CMD?=$(shell docker version >/dev/null 2>&1 && echo docker)
ifeq ($(CONTAINER_CMD),) ifeq ($(CONTAINER_CMD),)
@ -62,12 +62,12 @@ all: cephcsi
.PHONY: go-test static-check mod-check go-lint go-lint-text gosec .PHONY: go-test static-check mod-check go-lint go-lint-text gosec
test: go-test static-check mod-check test: go-test static-check mod-check
static-check: go-lint go-lint-text gosec static-check: check-env go-lint go-lint-text gosec
go-test: go-test: check-env
./scripts/test-go.sh ./scripts/test-go.sh
mod-check: mod-check: check-env
@echo 'running: go mod verify' @echo 'running: go mod verify'
@go mod verify && [ "$(shell sha512sum go.mod)" = "`sha512sum go.mod`" ] || ( echo "ERROR: go.mod was modified by 'go mod verify'" && false ) @go mod verify && [ "$(shell sha512sum go.mod)" = "`sha512sum go.mod`" ] || ( echo "ERROR: go.mod was modified by 'go mod verify'" && false )
@ -83,8 +83,11 @@ gosec:
func-test: func-test:
go test -mod=vendor github.com/ceph/ceph-csi/e2e $(TESTOPTIONS) go test -mod=vendor github.com/ceph/ceph-csi/e2e $(TESTOPTIONS)
check-env:
@./scripts/check-env.sh
.PHONY: cephcsi .PHONY: cephcsi
cephcsi: cephcsi: check-env
if [ ! -d ./vendor ]; then (go mod tidy && go mod vendor); fi if [ ! -d ./vendor ]; then (go mod tidy && go mod vendor); fi
GOOS=linux go build -mod vendor -a -ldflags '$(LDFLAGS)' -o _output/cephcsi ./cmd/ GOOS=linux go build -mod vendor -a -ldflags '$(LDFLAGS)' -o _output/cephcsi ./cmd/

46
scripts/check-env.sh Executable file
View File

@ -0,0 +1,46 @@
#!/bin/sh
#
# Check the environment for dependencies and configuration
#
# count errors, run script to the end before exiting
ERRORS=0
fail() {
echo "${*}" > /dev/stderr
ERRORS=$((ERRORS+1))
}
# check if 'go' is available
[ -n "$(command -v go)" ] \
|| fail "could not find 'go' executable"
# parse the Golang version, return the digit passed as argument
# 1.13.9 -> go_version 1 -> 1
# 1.13.9 -> go_version 2 -> 13
# 1.13.9 -> go_version 3 -> 9
go_version() {
go version | cut -d' ' -f3 | sed 's/^go//' | cut -d'.' -f"${1}"
}
# Golang needs to be > 1.13
GO_MAJOR=$(go_version 1)
GO_MINOR=$(go_version 2)
if ! [ "${GO_MAJOR}" -gt 1 ]
then
if ! { [ "${GO_MAJOR}" -eq 1 ] && [ "${GO_MINOR}" -ge 13 ]; }
then
fail "go version needs to be >= 1.13"
fi
fi
# we're building with modules, so GO111MODULE needs to be 'on' or 'auto'
# some versions of Go return nothing when GO111MODULE is not explicitly
# configured
[ "$(go env GO111MODULE 2>&1)" = '' ] || [ "$(go env GO111MODULE)" = 'on' ] || [ "$(go env GO111MODULE)" = 'auto' ] \
|| fail "GO111MODULE should be set to 'on' or 'auto'"
# CGO needs to be enabled, we build with go-ceph language bindings
[ "$(go env CGO_ENABLED)" = '1' ] \
|| fail "CGO_ENABLED should be set to '1'"
exit ${ERRORS}