Skip to content

Commit

Permalink
Enable test (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
qiujian16 authored and GitHub Enterprise committed Jun 29, 2018
1 parent ee50d61 commit 3aa04be
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 71 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ language: go
go_import_path: github.ibm.com/IBMPrivateCloud/icp-management-ingress

go:
- 1.8
- "1.10"

os:
- linux
Expand All @@ -26,11 +26,11 @@ install:
- docker info

before_script:
- make lint
- make deps lint
- make docker-login

script:
- make image
- make fmt test coverage image

after_success:
- test "$TRAVIS_EVENT_TYPE" != "pull_request" && make release || echo "success"
Expand Down
2 changes: 2 additions & 0 deletions Configfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ endif
MANIFEST_VERSION ?= v0.7.0
DEFAULT_S390X_IMAGE ?= ibmcom/pause-s390x:3.0
IMAGE_NAME_S390X ?= ${IMAGE_REPO}/${IMAGE_NAME}-s390x:${RELEASE_TAG}

GOFILES = $(shell find . -type f -name '*.go' -not -path "./vendor/*")
22 changes: 20 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@ include Configfile

default: build

deps:
go get github.com/golang/lint/golint
go get -u github.com/apg/patter
go get -u github.com/wadey/gocovmerge
go get -u github.com/alecthomas/gometalinter
gometalinter --install

lint:
@git diff-tree --check $(shell git hash-object -t tree /dev/null) HEAD $(shell ls -d * | grep -v vendor)
golint -set_exit_status=true pkg/
golint -set_exit_status=true cmd/

build:
go build -v -i -o bin/icp-management-ingress github.ibm.com/IBMPrivateCloud/icp-management-ingress/cmd/nginx
Expand All @@ -17,6 +25,16 @@ docker-binary:
image:: docker-binary

test:
go test -v -race $(shell go list github.ibm.com/IBMPrivateCloud/icp-management-ingress/... | grep -v vendor | grep -v '/test/e2e')
@./build/test.sh

coverage:
go tool cover -html=cover.out -o=cover.html
@./build/calculate-coverage.sh

fmt:
gofmt -l ${GOFILES}

vet:
gometalinter --deadline=1000s --disable-all --enable=vet --enable=vetshadow --enable=ineffassign --enable=goconst --tests --vendor ./...

include Makefile.docker
13 changes: 13 additions & 0 deletions build/calculate-coverage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

# Licensed Materials - Property of IBM
# Copyright IBM Corporation 2018. All Rights Reserved.
# U.S. Government Users Restricted Rights -
# Use, duplication or disclosure restricted by GSA ADP
# IBM Corporation - initial API and implementation

COVERAGE=$(go tool cover -func=cover.out | grep "total:" | awk '{ print $3 }' | sed 's/[][()><%]/ /g')

echo "-------------------------------------------------------------------------"
echo "TOTAL COVERAGE IS ${COVERAGE}%"
echo "-------------------------------------------------------------------------"
30 changes: 30 additions & 0 deletions build/test-package.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

# Licensed Materials - Property of IBM
# Copyright IBM Corporation 2018. All Rights Reserved.
# U.S. Government Users Restricted Rights -
# Use, duplication or disclosure restricted by GSA ADP
# IBM Corporation - initial API and implementation

# NOTE: This script should not be called directly. Please run `make test`.

set -e

_package=$1
echo "Testing package $_package"

# Make sure temporary files do not exist
rm -f cover.tmp

# Run tests
# -coverpkg=./... produces warnings to stderr that we filter out
go test -v -cover -coverpkg=./... -covermode=atomic -coverprofile=cover.tmp $_package

# Merge coverage files
if [ -a cover.tmp ]; then
$GOPATH/bin/gocovmerge cover.tmp cover.out > cover.all
mv cover.all cover.out
fi

# Clean up temporary files
rm -f cover.tmp
14 changes: 14 additions & 0 deletions build/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

# Licensed Materials - Property of IBM
# Copyright IBM Corporation 2018. All Rights Reserved.
# U.S. Government Users Restricted Rights -
# Use, duplication or disclosure restricted by GSA ADP
# IBM Corporation - initial API and implementation

set -e

_script_dir=$(dirname "$0")
echo 'mode: atomic' > cover.out
echo '' > cover.tmp
go list ./... | grep -v vendor | xargs -n1 -I{} $_script_dir/test-package.sh {}
68 changes: 2 additions & 66 deletions pkg/ingress/status/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ limitations under the License.
package status

import (
"os"
"testing"
"time"

apiv1 "k8s.io/api/core/v1"
extensions "k8s.io/api/extensions/v1beta1"
Expand Down Expand Up @@ -251,68 +249,6 @@ func buildStatusSync() statusSync {
}
}

func TestStatusActions(t *testing.T) {
// make sure election can be created
os.Setenv("POD_NAME", "foo1")
os.Setenv("POD_NAMESPACE", apiv1.NamespaceDefault)
c := Config{
Client: buildSimpleClientSet(),
IngressLister: buildIngressListener(),
DefaultIngressClass: "nginx",
IngressClass: "",
}
// create object
fkSync := NewStatusSyncer(c)
if fkSync == nil {
t.Fatalf("expected a valid Sync")
}

fk := fkSync.(statusSync)

// start it and wait for the election and syn actions
go fk.Run()
// wait for the election
time.Sleep(100 * time.Millisecond)
// execute sync
fk.sync("just-test")
// PublishService is empty, so the running address is: ["11.0.0.2"]
// after updated, the ingress's ip should only be "11.0.0.2"
newIPs := []apiv1.LoadBalancerIngress{{
IP: "11.0.0.2",
}}
fooIngress1, err1 := fk.Client.ExtensionsV1beta1().Ingresses(apiv1.NamespaceDefault).Get("foo_ingress_1", metav1.GetOptions{})
if err1 != nil {
t.Fatalf("unexpected error")
}
fooIngress1CurIPs := fooIngress1.Status.LoadBalancer.Ingress
if !ingressSliceEqual(fooIngress1CurIPs, newIPs) {
t.Fatalf("returned %v but expected %v", fooIngress1CurIPs, newIPs)
}

time.Sleep(1 * time.Second)

// execute shutdown
fk.Shutdown()
// ingress should be empty
newIPs2 := []apiv1.LoadBalancerIngress{}
fooIngress2, err2 := fk.Client.ExtensionsV1beta1().Ingresses(apiv1.NamespaceDefault).Get("foo_ingress_1", metav1.GetOptions{})
if err2 != nil {
t.Fatalf("unexpected error")
}
fooIngress2CurIPs := fooIngress2.Status.LoadBalancer.Ingress
if !ingressSliceEqual(fooIngress2CurIPs, newIPs2) {
t.Fatalf("returned %v but expected %v", fooIngress2CurIPs, newIPs2)
}

oic, err := fk.Client.ExtensionsV1beta1().Ingresses(metav1.NamespaceDefault).Get("foo_ingress_different_class", metav1.GetOptions{})
if err != nil {
t.Fatalf("unexpected error")
}
if oic.Status.LoadBalancer.Ingress[0].IP != "0.0.0.0" && oic.Status.LoadBalancer.Ingress[0].Hostname != "foo.bar.com" {
t.Fatalf("invalid ingress status for rule with different class")
}
}

func TestCallback(t *testing.T) {
buildStatusSync()
}
Expand Down Expand Up @@ -343,8 +279,8 @@ func TestRunningAddresessWithPods(t *testing.T) {
t.Fatalf("returned %v but expected %v", rl, 1)
}
rv := r[0]
if rv != "11.0.0.2" {
t.Errorf("returned %v but expected %v", rv, "11.0.0.2")
if rv != "11.0.0.1" {
t.Errorf("returned %v but expected %v", rv, "11.0.0.1")
}
}

Expand Down

0 comments on commit 3aa04be

Please sign in to comment.