Merge pull request #259 from kadel/lint-and-vet

Add `go vet`,  and `gofmt` tests.
This commit is contained in:
Tomas Kral 2016-11-25 09:49:15 +01:00 committed by GitHub
commit b0514bd2dd
13 changed files with 63 additions and 124 deletions

View File

@ -12,10 +12,14 @@ install:
- true - true
script: script:
- make validate
- make test-unit
# make test-cmd requires kompose binary
- make binary - make binary
# $GOPATH/bin is in $PATH # $GOPATH/bin is in $PATH
- mkdir -p $GOPATH/bin - mkdir -p $GOPATH/bin
- cp kompose $GOPATH/bin/ - cp kompose $GOPATH/bin/
- make test-unit
- make test-cmd - make test-cmd

View File

@ -26,6 +26,14 @@ clean:
test-unit: test-unit:
./script/make.sh test-unit ./script/make.sh test-unit
test-cmd: test-cmd:
./script/make.sh test-cmd ./script/make.sh test-cmd
validate: gofmt vet
vet:
./script/make.sh validate-vet
lint:
./script/make.sh validate-lint
gofmt:
./script/make.sh validate-gofmt

View File

@ -241,7 +241,7 @@ func getTransformer(opt kobject.ConvertOptions) transformer.Transformer {
} else { } else {
// Create/Init new OpenShift object that is initialized with a newly // Create/Init new OpenShift object that is initialized with a newly
// created Kubernetes object. Openshift inherits from Kubernetes // created Kubernetes object. Openshift inherits from Kubernetes
t = &openshift.OpenShift{kubernetes.Kubernetes{Opt: opt}} t = &openshift.OpenShift{Kubernetes: kubernetes.Kubernetes{Opt: opt}}
} }
return t return t
} }

View File

@ -106,16 +106,16 @@ func TestParseVolume(t *testing.T) {
t.Errorf("In test case %q, returned unexpected error %v", test.test, err) t.Errorf("In test case %q, returned unexpected error %v", test.test, err)
} }
if name != test.name { if name != test.name {
t.Errorf("In test case %q, returned volume name %s, expected %s", name, test.name) t.Errorf("In test case %q, returned volume name %s, expected %s", test.test, name, test.name)
} }
if host != test.host { if host != test.host {
t.Errorf("In test case %q, returned host path %s, expected %s", host, test.host) t.Errorf("In test case %q, returned host path %s, expected %s", test.test, host, test.host)
} }
if container != test.container { if container != test.container {
t.Errorf("In test case %q, returned container path %s, expected %s", container, test.container) t.Errorf("In test case %q, returned container path %s, expected %s", test.test, container, test.container)
} }
if mode != test.mode { if mode != test.mode {
t.Errorf("In test case %q, returned access mode %s, expected %s", mode, test.mode) t.Errorf("In test case %q, returned access mode %s, expected %s", test.test, mode, test.mode)
} }
} }
} }

View File

@ -133,12 +133,12 @@ func (b *Bundle) LoadFile(file string) kobject.KomposeObject {
buf, err := ioutil.ReadFile(file) buf, err := ioutil.ReadFile(file)
if err != nil { if err != nil {
logrus.Fatalf("Failed to read bundles file: ", err) logrus.Fatalf("Failed to read bundles file: %s ", err)
} }
reader := strings.NewReader(string(buf)) reader := strings.NewReader(string(buf))
bundle, err := loadFile(reader) bundle, err := loadFile(reader)
if err != nil { if err != nil {
logrus.Fatalf("Failed to parse bundles file: ", err) logrus.Fatalf("Failed to parse bundles file: %s", err)
} }
for name, service := range bundle.Services { for name, service := range bundle.Services {

View File

@ -245,6 +245,7 @@ func (k *Kubernetes) CreateService(name string, service kobject.ServiceConfig, o
svc.Spec.Ports = servicePorts svc.Spec.Ports = servicePorts
svc.Spec.Type = api.ServiceType(service.ServiceType) svc.Spec.Type = api.ServiceType(service.ServiceType)
// Configure annotations // Configure annotations
annotations := transformer.ConfigAnnotations(service) annotations := transformer.ConfigAnnotations(service)
svc.ObjectMeta.Annotations = annotations svc.ObjectMeta.Annotations = annotations

View File

@ -382,7 +382,7 @@ func (k *Kubernetes) Deploy(komposeObject kobject.KomposeObject, opt kobject.Con
if !opt.EmptyVols { if !opt.EmptyVols {
pvcStr = " and PersistentVolumeClaims " pvcStr = " and PersistentVolumeClaims "
} }
fmt.Println("We are going to create Kubernetes Deployments, Services" +pvcStr+ "for your Dockerized application. \n" + fmt.Println("We are going to create Kubernetes Deployments, Services" + pvcStr + "for your Dockerized application. \n" +
"If you need different kind of resources, use the 'kompose convert' and 'kubectl create -f' commands instead. \n") "If you need different kind of resources, use the 'kompose convert' and 'kubectl create -f' commands instead. \n")
factory := cmdutil.NewFactory(nil) factory := cmdutil.NewFactory(nil)
@ -424,7 +424,7 @@ func (k *Kubernetes) Deploy(komposeObject kobject.KomposeObject, opt kobject.Con
} else { } else {
pvcStr = "" pvcStr = ""
} }
fmt.Println("\nYour application has been deployed to Kubernetes. You can run 'kubectl get deployment,svc,pods" +pvcStr+ "' for details.") fmt.Println("\nYour application has been deployed to Kubernetes. You can run 'kubectl get deployment,svc,pods" + pvcStr + "' for details.")
return nil return nil
} }

View File

@ -178,7 +178,7 @@ func (o *OpenShift) Deploy(komposeObject kobject.KomposeObject, opt kobject.Conv
if !opt.EmptyVols { if !opt.EmptyVols {
pvcStr = " and PersistentVolumeClaims " pvcStr = " and PersistentVolumeClaims "
} }
fmt.Println("We are going to create OpenShift DeploymentConfigs, Services" +pvcStr+ "for your Dockerized application. \n" + fmt.Println("We are going to create OpenShift DeploymentConfigs, Services" + pvcStr + "for your Dockerized application. \n" +
"If you need different kind of resources, use the 'kompose convert' and 'oc create -f' commands instead. \n") "If you need different kind of resources, use the 'kompose convert' and 'oc create -f' commands instead. \n")
// initialize OpenShift Client // initialize OpenShift Client
@ -238,7 +238,7 @@ func (o *OpenShift) Deploy(komposeObject kobject.KomposeObject, opt kobject.Conv
} else { } else {
pvcStr = "" pvcStr = ""
} }
fmt.Println("\nYour application has been deployed to OpenShift. You can run 'oc get dc,svc,is" +pvcStr+ "' for details.") fmt.Println("\nYour application has been deployed to OpenShift. You can run 'oc get dc,svc,is" + pvcStr + "' for details.")
return nil return nil
} }

View File

@ -14,34 +14,5 @@
# 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.
if [ -z "$VALIDATE_UPSTREAM" ]; then # all Kompose packages (skip vendor dir)
# this is kind of an expensive check, so let's not do this twice if we PKGS=$( go list -e ./... | grep -v '/vendor/' )
# are running more than one validate bundlescript
VALIDATE_REPO='https://github.com/kubernetes-incubator/kompose.git'
VALIDATE_BRANCH='master'
if [ "$TRAVIS" = 'true' -a "$TRAVIS_PULL_REQUEST" != 'false' ]; then
VALIDATE_REPO="https://github.com/${TRAVIS_REPO_SLUG}.git"
VALIDATE_BRANCH="${TRAVIS_BRANCH}"
fi
VALIDATE_HEAD="$(git rev-parse --verify HEAD)"
git fetch -q "$VALIDATE_REPO" "refs/heads/$VALIDATE_BRANCH"
VALIDATE_UPSTREAM="$(git rev-parse --verify FETCH_HEAD)"
VALIDATE_COMMIT_LOG="$VALIDATE_UPSTREAM..$VALIDATE_HEAD"
VALIDATE_COMMIT_DIFF="$VALIDATE_UPSTREAM...$VALIDATE_HEAD"
validate_diff() {
if [ "$VALIDATE_UPSTREAM" != "$VALIDATE_HEAD" ]; then
git diff "$VALIDATE_COMMIT_DIFF" "$@"
fi
}
validate_log() {
if [ "$VALIDATE_UPSTREAM" != "$VALIDATE_HEAD" ]; then
git log "$VALIDATE_COMMIT_LOG" "$@"
fi
}
fi

View File

@ -25,10 +25,6 @@ DEFAULT_BUNDLES=(
validate-lint validate-lint
validate-vet validate-vet
binary binary
# test-unit
# test-integration
# test-acceptance
) )
bundle() { bundle() {
local bundle="$1"; shift local bundle="$1"; shift

View File

@ -16,29 +16,24 @@
source "$(dirname "$BASH_SOURCE")/.validate" source "$(dirname "$BASH_SOURCE")/.validate"
IFS=$'\n' GO_FILES=$(find . -path ./vendor -prune -o -name '*.go' -print )
files=( $(validate_diff --diff-filter=ACMR --name-only -- '*.go' | grep -v '^vendor/' || true) )
unset IFS
badFiles=()
for f in "${files[@]}"; do for file in $GO_FILES; do
# we use "git show" here to validate that what's committed is formatted gofmtOutput=$(gofmt -l "$file")
if [ "$(git show "$VALIDATE_HEAD:$f" | gofmt -s -l)" ]; then if [ "$gofmtOutput" ]; then
badFiles+=( "$f" ) errors+=("$gofmtOutput")
fi fi
done done
if [ ${#badFiles[@]} -eq 0 ]; then
echo 'Congratulations! All Go source files are properly formatted.'
if [ ${#errors[@]} -eq 0 ]; then
echo "gofmt OK"
else else
{ echo "gofmt ERROR - These files are not formated by gofmt:"
echo "These files are not properly gofmt'd:" for err in "${errors[@]}"; do
for f in "${badFiles[@]}"; do echo "$err"
echo " - $f" done
done exit 1
echo
echo 'Please reformat the above files using "gofmt -s -w" and commit the result.'
echo
} >&2
false
fi fi

View File

@ -1,4 +1,5 @@
#!/bin/bash #!/bin/bash
set -e
# Copyright 2016 The Kubernetes Authors All rights reserved. # Copyright 2016 The Kubernetes Authors All rights reserved.
# #
@ -16,33 +17,21 @@
source "$(dirname "$BASH_SOURCE")/.validate" source "$(dirname "$BASH_SOURCE")/.validate"
# We will eventually get to the point where packages should be the complete list for pkg in $PKGS; do
# of subpackages, vendoring excluded, as given by: lintOutput=$(golint "$pkg")
# # if lineOutput is not empty, save it to errros array
IFS=$'\n' if [ "$lintOutput" ]; then
files=( $(validate_diff --diff-filter=ACMR --name-only -- '*.go' | grep -v '^vendor/\|^integration' || true) ) errors+=("$lintOutput")
unset IFS
errors=()
for f in "${files[@]}"; do
# we use "git show" here to validate that what's committed passes go lint
failedLint=$(golint "$f")
if [ "$failedLint" ]; then
errors+=( "$failedLint" )
fi fi
done done
if [ ${#errors[@]} -eq 0 ]; then if [ ${#errors[@]} -eq 0 ]; then
echo 'Congratulations! All Go source files have been linted.' echo "golint OK"
else else
{ echo "golint ERRORS:"
echo "Errors from golint:" for err in "${errors[@]}"; do
for err in "${errors[@]}"; do echo "$err"
echo "$err" done
done exit 1
echo
echo 'Please fix the above errors. You can test via "golint" and commit the result.'
echo
} >&2
false
fi fi

View File

@ -1,4 +1,5 @@
#!/bin/bash #!/bin/bash
set -e
# Copyright 2016 The Kubernetes Authors All rights reserved. # Copyright 2016 The Kubernetes Authors All rights reserved.
# #
@ -16,31 +17,5 @@
source "$(dirname "$BASH_SOURCE")/.validate" source "$(dirname "$BASH_SOURCE")/.validate"
IFS=$'\n' go vet $PKGS;
files=( $(validate_diff --diff-filter=ACMR --name-only -- '*.go' | grep -v '^vendor/' || true) )
unset IFS
errors=()
for f in "${files[@]}"; do
# we use "git show" here to validate that what's committed passes go vet
failedVet=$(go vet "$f")
if [ "$failedVet" ]; then
errors+=( "$failedVet" )
fi
done
if [ ${#errors[@]} -eq 0 ]; then
echo 'Congratulations! All Go source files have been vetted.'
else
{
echo "Errors from go vet:"
for err in "${errors[@]}"; do
echo " - $err"
done
echo
echo 'Please fix the above errors. You can test via "go vet" and commit the result.'
echo
} >&2
false
fi