diff --git a/.travis.yml b/.travis.yml index 091098a5..19c568f7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,18 +20,7 @@ install: - true script: - - make check-vendor - - make validate - - make test-unit-cover + - make test # gover collects all .coverprofile files and saves it to one file gover.coverprofile - gover - goveralls -coverprofile=gover.coverprofile -service=travis-ci - - # make test-cmd requires kompose binary - - make bin - - # $GOPATH/bin is in $PATH - - mkdir -p $GOPATH/bin - - cp kompose $GOPATH/bin/ - - - make test-cmd diff --git a/Makefile b/Makefile index 76c54bce..c166adcc 100644 --- a/Makefile +++ b/Makefile @@ -13,47 +13,86 @@ # See the License for the specific language governing permissions and # limitations under the License. -.PHONY: all -KOMPOSE_ENVS := \ - -e OS_PLATFORM_ARG \ - -e OS_ARCH_ARG \ - -e TESTDIRS \ - -e TESTFLAGS \ - -e TESTVERBOSE - -BIND_DIR := bundles +GITCOMMIT := $(shell git rev-parse --short HEAD) +BUILD_FLAGS := -ldflags="-w -X github.com/kubernetes-incubator/kompose/version.GITCOMMIT=$(GITCOMMIT)" +PKGS := $(shell glide novendor) +TEST_IMAGE := kompose/tests:latest default: bin -all: validate - CGO_ENABLED=1 ./script/make.sh +.PHONY: all +all: bin +.PHONY: bin bin: - CGO_ENABLED=1 ./script/make.sh binary + go build ${BUILD_FLAGS} -o kompose main.go +.PHONY: install +install: + go install ${BUILD_FLAGS} + +# kompile kompose for multiple platforms +.PHONY: cross cross: - # CGO_ENABLED=1 ./script/make.sh binary-cross - ./script/make.sh binary-cross + gox -os="darwin linux windows" -arch="386 amd64" -output="bundles/kompose_{{.OS}}-{{.Arch}}/kompose" $(BUILD_FLAGS) +.PHONY: clean clean: - ./script/make.sh clean + rm -f kompose + rm -r -f bundles +.PHONY: test-unit test-unit: - ./script/make.sh test-unit -test-cmd: - ./script/make.sh test-cmd -test-unit-cover: - ./script/make.sh test-unit-cover + go test $(BUILD_FLAGS) -race -cover -v $(PKGS) +# Run unit tests and collect coverage +.PHONY: test-unit-cover +test-unit-cover: + # First install packages that are dependencies of the test. + go test -i -race -cover $(PKGS) + # go test doesn't support colleting coverage across multiple packages, + # generate go test commands using go list and run go test for every package separately + go list -f '"go test -race -cover -v -coverprofile={{.Dir}}/.coverprofile {{.ImportPath}}"' github.com/kubernetes-incubator/kompose/... | grep -v "vendor" | xargs -L 1 -P4 sh -c + +# run commandline tests +.PHONY: test-cmd +test-cmd: + ./script/test/cmd/tests.sh + +# run all validation tests +.PHONY: validate validate: gofmt vet lint +.PHONY: vet vet: - ./script/make.sh validate-vet -lint: - ./script/make.sh validate-lint -gofmt: - ./script/make.sh validate-gofmt + go vet $(PKGS) +.PHONY: lint +lint: + ./script/check-lint.sh + +.PHONY: gofmt +gofmt: + ./script/check-gofmt.sh + +# Checks if there are nested vendor dirs inside Kompose vendor and if vendor was cleaned by glide-vc +.PHONY: check-vendor check-vendor: - ./script/make.sh check-vendor + ./script/check-vendor.sh + +# Run all tests +.PHONY: test +test: check-vendor validate test-unit-cover install test-cmd + +# build docker image that is used for running all test localy +.PHONY: test-image +test-image: + docker build -t $(TEST_IMAGE) -f script/test_in_container/Dockerfile script/test_in_container/ + +# run all test localy in docker image (image can be build by by build-test-image target) +.PHONY: test-container +test-container: + docker run -v `pwd`:/opt/tmp/kompose:ro -it $(TEST_IMAGE) + + diff --git a/pkg/testutils/git.go b/pkg/testutils/git.go index e446ebff..b89c0a29 100644 --- a/pkg/testutils/git.go +++ b/pkg/testutils/git.go @@ -26,9 +26,12 @@ func CreateLocalDirectory(t *testing.T) string { func CreateLocalGitDirectory(t *testing.T) string { dir := CreateLocalDirectory(t) cmd := NewCommand( - `git init && touch README && - git add README && - git commit -m 'testcommit'`) + `git init && + git config user.email "you@example.com" && + git config user.name "Your Name" && + touch README && + git add README && + git commit --no-gpg-sign -m 'testcommit'`) cmd.Dir = dir _, err := cmd.Output() if err != nil { diff --git a/script/.build b/script/.build deleted file mode 100644 index 0c0de213..00000000 --- a/script/.build +++ /dev/null @@ -1,19 +0,0 @@ -#!/bin/bash - -# Copyright 2016 The Kubernetes Authors All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -GITCOMMIT=$(git rev-parse --short HEAD) - -BUILD_FLAGS=(-ldflags="-w -X github.com/kubernetes-incubator/kompose/version.GITCOMMIT=${GITCOMMIT}") diff --git a/script/.validate b/script/.validate deleted file mode 100644 index 6730c9ab..00000000 --- a/script/.validate +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/bash - -# Copyright 2016 The Kubernetes Authors All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -# all Kompose packages (skip vendor dir) -PKGS=$( go list -e ./... | grep -v '/vendor/' ) diff --git a/script/bash_autocomplete b/script/bash_autocomplete deleted file mode 100644 index 3af2531d..00000000 --- a/script/bash_autocomplete +++ /dev/null @@ -1,28 +0,0 @@ -#! /bin/bash - -# Copyright 2016 The Kubernetes Authors All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -: ${PROG:=$(basename ${BASH_SOURCE})} - -_cli_bash_autocomplete() { - local cur opts base - COMPREPLY=() - cur="${COMP_WORDS[COMP_CWORD]}" - opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} --generate-bash-completion ) - COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) - return 0 - } - - complete -F _cli_bash_autocomplete $PROG diff --git a/script/binary b/script/binary deleted file mode 100755 index 08fbab54..00000000 --- a/script/binary +++ /dev/null @@ -1,36 +0,0 @@ -#!/bin/bash - -# Copyright 2016 The Kubernetes Authors All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -set -e - -source "$(dirname "$BASH_SOURCE")/.build" - -OUT_FILE="./kompose" - -# Get rid of existing binary -rm -f $OUT_FILE - -# Build binary -go build \ - "${BUILD_FLAGS[@]}" \ - -o $OUT_FILE \ - main.go - -if [ $? -eq 0 ]; then - echo "Build successful. Program saved as ${OUT_FILE}" -else - echo "Build failed." -fi diff --git a/script/binary-cross b/script/binary-cross deleted file mode 100644 index 761d912b..00000000 --- a/script/binary-cross +++ /dev/null @@ -1,42 +0,0 @@ -#!/bin/bash - -# Copyright 2016 The Kubernetes Authors All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -set -e - -source "$(dirname "$BASH_SOURCE")/.build" - -if [ -z "$1" ]; then - OS_PLATFORM_ARG=(-os="darwin linux windows") -else - OS_PLATFORM_ARG=($1) -fi - -if [ -z "$2" ]; then - OS_ARCH_ARG=(-arch="386 amd64") -else - OS_ARCH_ARG=($2) -fi - -GITCOMMIT=$(git rev-parse --short HEAD) - -# Get rid of existing binaries -rm -f kompose* - -# Build binaries -gox "${OS_PLATFORM_ARG[@]}" "${OS_ARCH_ARG[@]}" \ - -output="bundles/kompose_{{.OS}}-{{.Arch}}/kompose" \ - "${BUILD_FLAGS[@]}" \ - . diff --git a/script/validate-gofmt b/script/check-gofmt.sh similarity index 92% rename from script/validate-gofmt rename to script/check-gofmt.sh index 4455fc36..fc23949f 100755 --- a/script/validate-gofmt +++ b/script/check-gofmt.sh @@ -14,11 +14,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -source "$(dirname "$BASH_SOURCE")/.validate" + +# This checks if all go source files in current directory are format using gofmt GO_FILES=$(find . -path ./vendor -prune -o -name '*.go' -print ) - for file in $GO_FILES; do gofmtOutput=$(gofmt -l "$file") if [ "$gofmtOutput" ]; then @@ -27,7 +27,6 @@ for file in $GO_FILES; do done - if [ ${#errors[@]} -eq 0 ]; then echo "gofmt OK" else diff --git a/script/validate-lint b/script/check-lint.sh similarity index 93% rename from script/validate-lint rename to script/check-lint.sh index f22ddbc3..d6123933 100755 --- a/script/validate-lint +++ b/script/check-lint.sh @@ -15,9 +15,8 @@ set -e # See the License for the specific language governing permissions and # limitations under the License. -source "$(dirname "$BASH_SOURCE")/.validate" -for pkg in $PKGS; do +for pkg in $(glide novendor); do lintOutput=$(golint "$pkg") # if lineOutput is not empty, save it to errros array if [ "$lintOutput" ]; then diff --git a/script/check-vendor b/script/check-vendor.sh similarity index 93% rename from script/check-vendor rename to script/check-vendor.sh index f4e4c6a0..1339a4ee 100755 --- a/script/check-vendor +++ b/script/check-vendor.sh @@ -14,12 +14,11 @@ # See the License for the specific language governing permissions and # limitations under the License. - - # Check if there are nested vendor dirs inside Kompose vendor. # All dependencies should be flattened and there shouldn't be vendor in inside vendor. + function check_nested_vendor() { - echo "Checking if there are nested vendor dirs" + echo "Checking for nested vendor dirs" # count all vendor directories inside Kompose vendor NO_NESTED_VENDORS=$(find vendor/ -type d | sed 's/^[^/]*.//g' | grep -E "vendor$" | grep -v _vendor | wc -l) @@ -41,7 +40,7 @@ function check_glide-vc() { echo "Checking if vendor was cleaned using glide-vc." # dry run glide-vc and count how many could be deleted. - NO_DELETED_FILES=$($GOPATH/bin/glide-vc --only-code --no-tests --dryrun | wc -l) + NO_DELETED_FILES=$(glide-vc --only-code --no-tests --dryrun | wc -l) if [ $NO_DELETED_FILES -ne 0 ]; then echo "ERROR" diff --git a/script/make.sh b/script/make.sh deleted file mode 100755 index efd3c6ca..00000000 --- a/script/make.sh +++ /dev/null @@ -1,46 +0,0 @@ -#!/usr/bin/env bash - -# Copyright 2016 The Kubernetes Authors All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -set -e - -export KOMPOSE_PKG='github.com/kubernetes-incubator/kompose' - -# List of bundles to create when no argument is passed -DEFAULT_BUNDLES=( - validate-gofmt - validate-git-marks - validate-lint - validate-vet - binary -) -bundle() { - local bundle="$1"; shift - echo "---> Making bundle: $(basename "$bundle") (in $DEST)" - source "script/$bundle" "$@" -} - -if [ $# -lt 1 ]; then - bundles=(${DEFAULT_BUNDLES[@]}) -else - bundles=($@) -fi -for bundle in ${bundles[@]}; do - export DEST=. - ABS_DEST="$(cd "$DEST" && pwd -P)" - bundle "$bundle" - echo -done - diff --git a/script/test-cmd b/script/test-cmd deleted file mode 100755 index fd2b1d00..00000000 --- a/script/test-cmd +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/bash - -# Copyright 2016 The Kubernetes Authors All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -./script/test/cmd/tests.sh diff --git a/script/test-unit-cover b/script/test-unit-cover deleted file mode 100755 index c0365c7c..00000000 --- a/script/test-unit-cover +++ /dev/null @@ -1,27 +0,0 @@ -#!/bin/bash - -# Copyright 2016 The Kubernetes Authors All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - - -source "$(dirname "$BASH_SOURCE")/.build" - - -# Install packages that are dependencies of the test. (this will speed up go test runs) -go test -i -race -cover $( go list github.com/kubernetes-incubator/kompose/... | grep -v '/vendor/' ) - -# go test doesn't support colleting coverage across multiple packages, -# so this is why this runs go test for each package separately -# generate go test commands using go list and run tests -go list -f '"go test -race -cover -v -coverprofile={{.Dir}}/.coverprofile {{.ImportPath}}"' github.com/kubernetes-incubator/kompose/... | grep -v "vendor" | xargs -L 1 -P4 sh -c diff --git a/script/test_in_container/Dockerfile b/script/test_in_container/Dockerfile new file mode 100644 index 00000000..6d8c09d5 --- /dev/null +++ b/script/test_in_container/Dockerfile @@ -0,0 +1,46 @@ +# Copyright 2016 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +# This Dockefile creates image where all Kompose tests can be run + +FROM registry.centos.org/centos/centos:7 + +RUN yum install -y epel-release +RUN yum install -y golang make git jq &&\ + yum -y clean all + +ENV GOPATH="/opt/go" \ + # KOMPOSE_TMP_SRC is where kompose source will be mounted from host + KOMPOSE_TMP_SRC="/opt/tmp/kompose" + +ENV PATH="$PATH:$GOPATH/bin" \ + # KOMPOSE_SRC is where kompose source will be copied when container starts (by run.sh script) + # this is to ensure that we won't write anything to host volume mount + KOMPOSE_SRC="$GOPATH/src/github.com/kubernetes-incubator/kompose" + +RUN go get github.com/Masterminds/glide &&\ + go get github.com/sgotti/glide-vc &&\ + go get github.com/golang/lint/golint + +WORKDIR $KOMPOSE_SRC +# This image can be run as any user +RUN chmod -R ugo+rw $GOPATH + + +COPY run.sh /opt/tools/ +ENTRYPOINT ["/opt/tools/run.sh"] + + + diff --git a/script/test-unit b/script/test_in_container/run.sh similarity index 62% rename from script/test-unit rename to script/test_in_container/run.sh index 8349dc7e..df2a3e97 100755 --- a/script/test-unit +++ b/script/test_in_container/run.sh @@ -1,6 +1,8 @@ #!/bin/bash -# Copyright 2016 The Kubernetes Authors All rights reserved. +set -e + +# Copyright 2017 The Kubernetes Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -14,8 +16,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -set -e -source "$(dirname "$BASH_SOURCE")/.build" +# Container should be started with Kompose source mounted as read-only volume $KOMPOSE_TMP_SRC +# Copy Kompose sources to another directory after container starts. +# We can be sure that this container won't modify any files on hosts disk. +cp -r $KOMPOSE_TMP_SRC/ $(dirname $KOMPOSE_SRC) -go test "${BUILD_FLAGS[@]}" -race -cover -v $( go list github.com/kubernetes-incubator/kompose/... | grep -v '/vendor/' ) +make test diff --git a/script/validate-git-marks b/script/validate-git-marks deleted file mode 100755 index 9397e24e..00000000 --- a/script/validate-git-marks +++ /dev/null @@ -1,58 +0,0 @@ -#!/usr/bin/env bash - -# Copyright 2016 The Kubernetes Authors All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -source "$(dirname "$BASH_SOURCE")/.validate" - -# folders=$(find * -type d | egrep -v '^Godeps|bundles|.git') - -IFS=$'\n' -files=( $(validate_diff --diff-filter=ACMR --name-only -- '*' | grep -v '^vendor/' || true) ) -unset IFS - -badFiles=() -for f in "${files[@]}"; do - if [ $(grep -r "^<<<<<<<" $f) ]; then - badFiles+=( "$f" ) - continue - fi - - if [ $(grep -r "^>>>>>>>" $f) ]; then - badFiles+=( "$f" ) - continue - fi - - if [ $(grep -r "^=======$" $f) ]; then - badFiles+=( "$f" ) - continue - fi - set -e -done - - -if [ ${#badFiles[@]} -eq 0 ]; then - echo 'Congratulations! There is no conflict.' -else - { - echo "There is trace of conflict(s) in the following files :" - for f in "${badFiles[@]}"; do - echo " - $f" - done - echo - echo 'Please fix the conflict(s) commit the result.' - echo - } >&2 - false -fi diff --git a/script/validate-vet b/script/validate-vet deleted file mode 100755 index 0625823a..00000000 --- a/script/validate-vet +++ /dev/null @@ -1,21 +0,0 @@ -#!/bin/bash -set -e - -# Copyright 2016 The Kubernetes Authors All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -source "$(dirname "$BASH_SOURCE")/.validate" - -go vet $PKGS; - \ No newline at end of file