From 8cbc9045e1d37152859b43584bd0486eaad03c91 Mon Sep 17 00:00:00 2001 From: letty Date: Fri, 18 May 2018 16:04:33 +0800 Subject: [PATCH] test: add integration test coverage Signed-off-by: letty --- .travis.yml | 3 ++ Makefile | 4 ++ hack/build | 34 +++++++++++++- hack/make.sh | 60 ++++++++++++++++++++++--- main_test.go | 41 +++++++++++++++++ test/api_container_exec_inspect_test.go | 1 + 6 files changed, 137 insertions(+), 6 deletions(-) create mode 100644 main_test.go diff --git a/.travis.yml b/.travis.yml index e1509f058..52be888f9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,3 +24,6 @@ env: script: - sudo make -e ${TEST_SUITE} + +after_success: +- bash <(curl -s https://codecov.io/bash) || echo Codecov_Did_Not_Collect_Coverage_Reports diff --git a/Makefile b/Makefile index 12a486338..1d6da8cb7 100644 --- a/Makefile +++ b/Makefile @@ -26,6 +26,10 @@ server: pre modules client: pre @./hack/build client +.PHONY: testserver +testserver: pre modules + @./hack/build testserver + .PHONY: clean clean: $(GOCLEAN) diff --git a/hack/build b/hack/build index 80bb7253d..688664693 100755 --- a/hack/build +++ b/hack/build @@ -47,6 +47,32 @@ function client() $GOBUILD -o $CLI_BINARY_NAME github.com/alibaba/pouch/cli } +# +# Do not calculate the coverage for the following pkgs +# +SKIP_PKGS=(\ + "github.com/alibaba/pouch/vendor" \ + "github.com/alibaba/pouch/extra" \ + "github.com/alibaba/pouch/test" \ + "github.com/alibaba/pouch/apis/types" \ + "github.com/alibaba/pouch/cri/config" \ + "github.com/alibaba/pouch/storage/volume/examples/demo" \ + "github.com/alibaba/pouch/storage/volume/types" \ +) + +function testserver() +{ + cd $BUILDPATH/src/github.com/alibaba/pouch + cmd="go list ./... " + for j in $(seq 0 $(( ${#SKIP_PKGS[@]} -1)) ); do + cmd+="| grep -v \"${SKIP_PKGS[$j]}\" " + done + + pkgs=$(eval $cmd |grep -v 'github.com/alibaba/pouch/cli' | tr '\n' ','|sed 's/,$//g') + + go test -c -race -cover -covermode=atomic -o pouchd-test -coverpkg $pkgs +} + function vet() { cd $BUILDPATH/src/github.com/alibaba/pouch @@ -58,7 +84,13 @@ function unit-test() { cd $BUILDPATH/src/github.com/alibaba/pouch go test -i - for d in `go list ./... | grep -v 'github.com/alibaba/pouch/test' | grep -v 'github.com/alibaba/pouch/extra' | grep -v 'github.com/alibaba/pouch/vendor' ` + + cmd="go list ./... " + for j in $(seq 0 $(( ${#SKIP_PKGS[@]} -1)) ); do + cmd+="| grep -v \"${SKIP_PKGS[$j]}\" " + done + + for d in $(eval $cmd |grep -vw '^github.com/alibaba/pouch$' ) do go test -race -coverprofile=profile.out -covermode=atomic $d if [ -f profile.out ] ; then diff --git a/hack/make.sh b/hack/make.sh index f6843ec94..d86854f0f 100755 --- a/hack/make.sh +++ b/hack/make.sh @@ -23,6 +23,17 @@ if [[ $SOURCEDIR != $DIR ]];then ln -sf $DIR/ $SOURCEDIR fi +# +# CAL_INTEGRATION_TEST_COVERAGE indicates whehter or not calculate integration test coverage. +# By default it is yes. +# +CAL_INTEGRATION_TEST_COVERAGE=${CAL_INTEGRATION_TEST_COVERAGE:-"yes"} +if [[ $CAL_INTEGRATION_TEST_COVERAGE == "yes" ]]; then + POUCHD="pouchd-test -test.coverprofile=$DIR/integrationcover.out DEVEL" +else + POUCHD="pouchd" +fi + function get_containerd_version { if which containerd &>/dev/null; then @@ -157,6 +168,9 @@ function install_pouch install_runc # copy pouch daemon and pouch cli to PATH echo "Install pouch." + if [[ $CAL_INTEGRATION_TEST_COVERAGE == "yes" ]]; then + cp -f $DIR/pouchd-test /usr/local/bin/ + fi cp -f $DIR/pouch $DIR/pouchd /usr/local/bin/ install_lxcfs install_nsenter @@ -169,9 +183,19 @@ function target docker run --rm -v $(pwd):$SOURCEDIR $IMAGE bash -c "make check" ;; build) + # + # Also build pouchd-test binary if CAL_INTEGRATION_TEST_COVERAGE doesn't + # equal to 'no'. + # + if [[ $CAL_INTEGRATION_TEST_COVERAGE == "yes" ]]; then + docker run --rm -v $(pwd):$SOURCEDIR $IMAGE \ + bash -c "make testserver" >$TMP/build.log || + { echo "make build log:"; cat $TMP/build.log; return 1; } + fi docker run --rm -v $(pwd):$SOURCEDIR $IMAGE \ bash -c "make build" >$TMP/build.log || { echo "make build log:"; cat $TMP/build.log; return 1; } + install_pouch >$TMP/install.log || { echo "install pouch log:"; cat $TMP/install.log; return 1; } ;; @@ -184,10 +208,11 @@ function target env PATH=$GOROOT/bin:$PATH $SOURCEDIR/hack/cri-test/test-cri.sh ;; integration-test) - + install_dumb_init || echo "Warning: dumb-init install failed!\ rich container related tests will be skipped" + docker run --rm -v $(pwd):$SOURCEDIR \ -e GOPATH=/go:$SOURCEDIR/extra/libnetwork/Godeps/_workspace \ $IMAGE \ @@ -196,10 +221,10 @@ function target #start pouch daemon echo "start pouch daemon" if stat /usr/bin/lxcfs ; then - pouchd --debug --enable-lxcfs=true \ + $POUCHD --debug --enable-lxcfs=true \ --lxcfs=/usr/bin/lxcfs > $TMP/log 2>&1 & else - pouchd --debug > $TMP/log 2>&1 & + $POUCHD --debug > $TMP/log 2>&1 & fi # wait until pouch daemon is ready @@ -226,8 +251,14 @@ function target cp -rf $DIR/test/tls /tmp/ # If test is failed, print pouch daemon log. - $DIR/test/integration-test -test.v -check.v || - { echo "pouch daemon log:"; cat $TMP/log; return 1; } + set +e + $DIR/test/integration-test -test.v -check.v + if (( $? != 0 )); then + echo "pouch daemon log:" + cat $TMP/log + return 1 + fi + set -e ;; *) echo "no such target: $target" @@ -253,7 +284,26 @@ function main for target in ${targets[@]}; do target $target + ret=$? + if (( $ret != 0 )); then + return $ret + fi done + + if [[ $CAL_INTEGRATION_TEST_COVERAGE == "yes" ]]; then + if ! echo ${targets[@]} | grep -q "integration" ; then + return $ret + fi + # + # kill pouchd-test and get the coverage + # + pkill --signal 3 pouchd-test || echo "no pouchd-test to be killed" + sleep 5 + + tail -1 $TMP/log + cat $DIR/integrationcover.out >> $DIR/coverage.txt + return $ret + fi } main "$@" diff --git a/main_test.go b/main_test.go new file mode 100644 index 000000000..028d1c7e9 --- /dev/null +++ b/main_test.go @@ -0,0 +1,41 @@ +package main + +import ( + "os" + "os/signal" + "strings" + "syscall" + "testing" +) + +func TestMain(t *testing.T) { + var ( + args []string + ) + + for _, arg := range os.Args { + switch { + case strings.HasPrefix(arg, "DEVEL"): + case strings.HasPrefix(arg, "-test"): + default: + args = append(args, arg) + } + } + + waitCh := make(chan int, 1) + + os.Args = args + go func() { + main() + close(waitCh) + }() + + signalCh := make(chan os.Signal, 1) + signal.Notify(signalCh, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGHUP) + select { + case <-signalCh: + return + case <-waitCh: + return + } +} diff --git a/test/api_container_exec_inspect_test.go b/test/api_container_exec_inspect_test.go index 0be5a0d9f..b34778948 100644 --- a/test/api_container_exec_inspect_test.go +++ b/test/api_container_exec_inspect_test.go @@ -25,6 +25,7 @@ func (suite *APIContainerExecInspectSuite) SetUpTest(c *check.C) { // TestContainerCreateExecOk tests execing containers is OK. func (suite *APIContainerExecInspectSuite) TestContainerExecInspectOk(c *check.C) { + c.Skip("skip flaky test due to issue#1372") cname := "TestContainerExecInspectOk" CreateBusyboxContainerOk(c, cname)