Merge pull request #75 from lyyyuna/goc-70

Add some e2e tests
This commit is contained in:
qiniu-bot 2020-07-17 14:49:42 +08:00 committed by GitHub
commit a2a0880ff7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 651 additions and 26 deletions

View File

@ -11,3 +11,11 @@ fmt:
govet-check:
go vet ./...
clean:
find tests/ -type f -name '*.bak' -delete
find tests/ -type f -name '*.cov' -delete
find tests/ -type f -name 'simple-project' -delete
find tests/ -type f -name '*_profile_listen_addr' -delete
find tests/ -type f -name 'simple_gopath_project' -delete

View File

@ -25,11 +25,12 @@ import (
)
var (
target string
center string
agentPort AgentPort
debugGoc bool
buildFlags string
target string
center string
agentPort AgentPort
debugGoc bool
debugInCISyncFile string
buildFlags string
goRunExecFlag string
goRunArguments string

View File

@ -18,9 +18,10 @@ package cmd
import (
"fmt"
"log"
"os"
log "github.com/sirupsen/logrus"
"github.com/qiniu/goc/pkg/cover"
"github.com/spf13/cobra"
)
@ -37,6 +38,7 @@ goc list [flags]
if err != nil {
log.Fatalf("list failed, err: %v", err)
}
log.Infoln(string(res))
fmt.Fprint(os.Stdout, string(res))
},
}

View File

@ -17,9 +17,11 @@
package cmd
import (
"os"
"path/filepath"
"runtime"
"strconv"
"time"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
@ -58,10 +60,23 @@ Find more information at:
})
}
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
if debugInCISyncFile != "" {
f, err := os.Create(debugInCISyncFile)
if err != nil {
log.Fatalln(err)
}
defer f.Close()
time.Sleep(5 * time.Second)
}
},
}
func init() {
rootCmd.PersistentFlags().BoolVar(&debugGoc, "debug", false, "run goc in debug mode")
rootCmd.PersistentFlags().StringVar(&debugInCISyncFile, "debugcisyncfile", "", "internal use only, no explain")
rootCmd.PersistentFlags().MarkHidden("debugcisyncfile")
viper.BindPFlags(rootCmd.PersistentFlags())
}

View File

@ -39,12 +39,10 @@ func (b *Build) Run() error {
cmd.Stderr = os.Stderr
err := cmd.Start()
if err != nil {
log.Errorf("Fail to start command: %v. The error is: %v", cmd.Args, err)
return fmt.Errorf("fail to execute: %v, err: %w", cmd.Args, err)
}
if err = cmd.Wait(); err != nil {
log.Errorf("Fail to go run: %v. The error is: %v", cmd.Args, err)
return fmt.Errorf("fail to execute: %v, err: %w", cmd.Args, err)
}

46
tests/build.bats Executable file
View File

@ -0,0 +1,46 @@
#!/usr/bin/env bats
# Copyright 2020 Qiniu Cloud (七牛云)
#
# 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.
load util.sh
setup_file() {
# run centered server
goc server 3>&- &
GOC_PID=$!
sleep 2
goc init
info "goc server started"
}
teardown_file() {
kill -9 $GOC_PID
}
@test "test basic goc build command" {
cd samples/run_for_several_seconds
wait_profile_backend "build1"
run gocc build --debug --debugcisyncfile ci-sync.bak;
info build output: $output
[ "$status" -eq 0 ]
}
@test "test goc build command without debug" {
cd samples/run_for_several_seconds
wait_profile_backend "build2"
run gocc build --debugcisyncfile ci-sync.bak;
info build output: $output
[ "$status" -eq 0 ]
}

62
tests/clear.bats Executable file
View File

@ -0,0 +1,62 @@
#!/usr/bin/env bats
# Copyright 2020 Qiniu Cloud (七牛云)
#
# 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.
load util.sh
setup_file() {
# run centered server
goc server 3>&- &
GOC_PID=$!
sleep 2
goc init
# run covered goc
gocc server --port=:60001 --debug 3>&- &
GOCC_PID=$!
sleep 1
WORKDIR=$PWD
cd samples/run_for_several_seconds
gocc build --center=http://127.0.0.1:60001
./simple-project 3>&- &
SAMPLE_PID=$!
sleep 2
info "goc server started"
}
teardown_file() {
kill -9 $GOC_PID
kill -9 $GOCC_PID
kill -9 $SAMPLE_PID
}
@test "test basic goc clear command" {
wait_profile_backend "clear1"
run gocc clear --debug --debugcisyncfile ci-sync.bak;
info clear1 output: $output
[ "$status" -eq 0 ]
[[ "$output" == *"coverage counter clear call successfully"* ]]
}
@test "test clear another center" {
wait_profile_backend "clear2"
run gocc clear --center=http://127.0.0.1:60001 --debug --debugcisyncfile ci-sync.bak;
info clear2 output: $output
[ "$status" -eq 0 ]
[[ "$output" == *"coverage counter clear call successfully"* ]]
}

49
tests/cover.bats Executable file
View File

@ -0,0 +1,49 @@
#!/usr/bin/env bats
# Copyright 2020 Qiniu Cloud (七牛云)
#
# 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.
load util.sh
setup_file() {
mkdir -p test-temp
cp samples/simple_project/main.go test-temp
cp samples/simple_project/go.mod test-temp
# run centered server
goc server 3>&- &
GOC_PID=$!
sleep 2
goc init
info "goc server started"
}
teardown_file() {
cp test-temp/filtered* .
rm -rf test-temp
kill -9 $GOC_PID
}
@test "test basic goc cover command" {
cd test-temp
wait_profile_backend "cover1"
run gocc cover --debug --debugcisyncfile ci-sync.bak;
info cover1 output: $output
[ "$status" -eq 0 ]
run ls http_cover_apis_auto_generated.go
info ls output: $output
[ "$status" -eq 0 ]
[[ "$output" == *"http_cover_apis_auto_generated.go"* ]]
}

69
tests/diff.bats Executable file
View File

@ -0,0 +1,69 @@
#!/usr/bin/env bats
# Copyright 2020 Qiniu Cloud (七牛云)
#
# 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.
load util.sh
setup_file() {
# run centered server
goc server 3>&- &
GOC_PID=$!
sleep 2
goc init
info "goc server started"
}
teardown_file() {
kill -9 $GOC_PID
}
@test "test basic goc diff command" {
cd samples/diff_samples
wait_profile_backend "diff1"
run gocc diff --new-profile=./new.voc --base-profile=./base.voc --debug --debugcisyncfile ci-sync.bak;
info list output: $output
[ "$status" -eq 0 ]
[[ "$output" == *"qiniu.com/kodo/apiserver/server/main.go | 50.0% | 100.0% | 50.0%"* ]]
[[ "$output" == *"Total | 50.0% | 100.0% | 50.0%"* ]]
}
@test "test diff in prow environment with periodic job" {
cd samples/diff_samples
wait_profile_backend "diff2"
export JOB_TYPE=periodic
run gocc diff --new-profile=./new.voc --prow-postsubmit-job=base --debug --debugcisyncfile ci-sync.bak;
info diff1 output: $output
[ "$status" -eq 0 ]
[[ "$output" == *"do nothing"* ]]
}
@test "test diff in prow environment with postsubmit job" {
cd samples/diff_samples
wait_profile_backend "diff3"
export JOB_TYPE=postsubmit
run gocc diff --new-profile=./new.voc --prow-postsubmit-job=base --debug --debugcisyncfile ci-sync.bak;
info diff2 output: $output
[ "$status" -eq 0 ]
[[ "$output" == *"do nothing"* ]]
}

52
tests/init.bats Executable file
View File

@ -0,0 +1,52 @@
#!/usr/bin/env bats
# Copyright 2020 Qiniu Cloud (七牛云)
#
# 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.
load util.sh
setup_file() {
# run centered server
goc server 3>&- &
GOC_PID=$!
sleep 2
goc init
# run covered goc
gocc server --port=:60001 --debug 3>&- &
GOCC_PID=$!
sleep 1
WORKDIR=$PWD
cd samples/run_for_several_seconds
gocc build --center=http://127.0.0.1:60001
./simple-project 3>&- &
SAMPLE_PID=$!
sleep 2
info "goc server started"
}
teardown_file() {
kill -9 $GOC_PID
kill -9 $GOCC_PID
kill -9 $SAMPLE_PID
}
@test "test init command" {
wait_profile_backend "init1"
run gocc init --center=http://127.0.0.1:60001 --debug --debugcisyncfile ci-sync.bak;
info init output: $output
[ "$status" -eq 0 ]
}

41
tests/install.bats Executable file
View File

@ -0,0 +1,41 @@
#!/usr/bin/env bats
# Copyright 2020 Qiniu Cloud (七牛云)
#
# 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.
load util.sh
setup_file() {
# run centered server
goc server 3>&- &
GOC_PID=$!
sleep 2
goc init
info "goc server started"
}
teardown_file() {
kill -9 $GOC_PID
}
@test "test basic goc install command" {
info $PWD
export GOPATH=$PWD/samples/simple_gopath_project
export GO111MODULE=off
cd samples/simple_gopath_project/src/qiniu.com/simple_gopath_project
wait_profile_backend "install"
run gocc install --debug --debugcisyncfile ci-sync.bak;
info install output: $output
[ "$status" -eq 0 ]
}

46
tests/list.bats Executable file
View File

@ -0,0 +1,46 @@
#!/usr/bin/env bats
# Copyright 2020 Qiniu Cloud (七牛云)
#
# 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.
load util.sh
setup_file() {
# run centered server
goc server 3>&- &
GOC_PID=$!
sleep 2
goc init
# run covered goc
gocc server --port=:60001 --debug 3>&- &
GOCC_PID=$!
sleep 1
info "goc server started"
}
teardown_file() {
kill -9 $GOC_PID
kill -9 $GOCC_PID
}
@test "test basic goc list command" {
wait_profile_backend "list"
run gocc list --debug --debugcisyncfile ci-sync.bak;
info list output: $output
[ "$status" -eq 0 ]
[[ "$output" == *"gocc"* ]]
[[ "$output" == *"http"* ]]
}

53
tests/profile.bats Executable file
View File

@ -0,0 +1,53 @@
#!/usr/bin/env bats
# Copyright 2020 Qiniu Cloud (七牛云)
#
# 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.
load util.sh
setup_file() {
# run centered server
goc server 3>&- &
GOC_PID=$!
sleep 2
goc init
# run covered goc
gocc server --port=:60001 --debug 3>&- &
GOCC_PID=$!
sleep 1
info "goc server started"
}
teardown_file() {
kill -9 $GOC_PID
kill -9 $GOCC_PID
}
@test "test goc profile to stdout" {
wait_profile_backend "profile1"
run gocc profile --debug --debugcisyncfile ci-sync.bak;
[ "$status" -eq 0 ]
[[ "$output" == *"mode: count"* ]]
}
@test "test goc profile to file" {
wait_profile_backend "profile2"
run gocc profile -o test-profile.bak --debug --debugcisyncfile ci-sync.bak;
[ "$status" -eq 0 ]
run cat test-profile.bak
[[ "$output" == *"mode: count"* ]]
}

54
tests/register.bats Executable file
View File

@ -0,0 +1,54 @@
#!/usr/bin/env bats
# Copyright 2020 Qiniu Cloud (七牛云)
#
# 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.
load util.sh
setup_file() {
# run centered server
goc server 3>&- &
GOC_PID=$!
sleep 2
goc init
# run covered goc
gocc server --port=:60001 --debug 3>&- &
GOCC_PID=$!
sleep 1
info "goc server started"
}
teardown_file() {
kill -9 $GOC_PID
kill -9 $GOCC_PID
}
@test "test basic goc register command" {
wait_profile_backend "register1"
run gocc register --center=http://127.0.0.1:60001 --name=xyz --address=http://137.0.0.1:666 --debug --debugcisyncfile ci-sync.bak;
info register output: $output
[ "$status" -eq 0 ]
[[ "$output" == *"success"* ]]
}
@test "test goc register without port" {
wait_profile_backend "register2"
run gocc register --center=http://127.0.0.1:60001 --name=xyz --address=http://137.0.0.1 --debug --debugcisyncfile ci-sync.bak;
info register output: $output
[ "$status" -eq 0 ]
[[ "$output" == *"missing port"* ]]
}

View File

@ -17,8 +17,28 @@ set -ex
echo "test start"
bats server.bats
bats -t server.bats
bats gocrun.bats
bats -t run.bats
bats -t version.bats
bats -t list.bats
bats -t clear.bats
bats -t build.bats
bats -t profile.bats
bats -t install.bats
bats -t register.bats
bats -t init.bats
bats -t diff.bats
bats -t cover.bats
bash <(curl -s https://codecov.io/bash) -f 'filtered*' -F e2e

20
tests/run-in-local.sh Executable file
View File

@ -0,0 +1,20 @@
#!/usr/bin/env bash
# Copyright 2020 Qiniu Cloud (七牛云)
#
# 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 -ex
echo "test start"
bats -t run.bats

43
tests/run.bats Executable file
View File

@ -0,0 +1,43 @@
#!/usr/bin/env bats
# Copyright 2020 Qiniu Cloud (七牛云)
#
# 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.
load util.sh
setup_file() {
# run centered server
goc server 3>&- &
GOC_PID=$!
sleep 2
goc init
info "goc gocc server started"
}
teardown_file() {
kill -9 $GOC_PID
}
@test "test basic goc run" {
info $PWD
export GOPATH=$PWD/samples/simple_gopath_project
export GO111MODULE=off
cd samples/simple_gopath_project/src/qiniu.com/simple_gopath_project
wait_profile_backend "run1"
run gocc run . --debug --debugcisyncfile ci-sync.bak;
info run output: $output
[ "$status" -eq 0 ]
[[ "$output" == *"hello, world."* ]]
}

View File

@ -0,0 +1,3 @@
mode: atomic
qiniu.com/kodo/apiserver/server/main.go:32.49,33.13 1 30
qiniu.com/kodo/apiserver/server/main.go:42.49,43.13 1 0

View File

@ -0,0 +1,3 @@
mode: atomic
qiniu.com/kodo/apiserver/server/main.go:32.49,33.13 1 30
qiniu.com/kodo/apiserver/server/main.go:42.49,43.13 1 1

View File

@ -13,6 +13,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
load util.sh
setup_file() {
# run centered server
goc server 3>&- &
@ -23,12 +25,12 @@ setup_file() {
gocc server --port=:60001 --debug 3>&- &
GOCC_PID=$!
sleep 2
echo "goc gocc server started"
info "goc gocc server started"
}
teardown_file() {
# collect from center
goc profile --debug -o filtered.cov
goc profile --debug -o filtered-server.cov
kill -9 $GOC_PID
kill -9 $GOCC_PID
}

39
tests/util.sh Normal file
View File

@ -0,0 +1,39 @@
#!/usr/bin/env bash
# Copyright 2020 Qiniu Cloud (七牛云)
#
# 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.
info() {
echo -e "[$(date +'%Y-%m-%dT%H:%M:%S.%N%z')] INFO: $@" >&3
}
wait_profile() {
local n=0
local timeout=10
until [[ ${n} -ge ${timeout} ]]
do
# check whether the target port is listened by specific process
if [[ -f ci-sync.bak ]]; then
break
fi
n=$[${n}+1]
sleep 1
done
# collect from center
goc profile -o filtered-$1.cov
}
wait_profile_backend() {
rm ci-sync.bak || true
coproc { wait_profile $1; }
}

View File

@ -13,6 +13,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
load util.sh
setup_file() {
# run centered server
goc server 3>&- &
@ -20,24 +22,21 @@ setup_file() {
sleep 2
goc init
# run covered goc run
WORKDIR=$PWD
cd samples/run_for_several_seconds
ls -al
gocc run --debug . 3>&- &
GOCC_PID=$!
sleep 2
echo "goc gocc server started"
info "goc server started"
}
setup() {
goc init
rm ci-sync.bak || true
}
teardown_file() {
cd $WORKDIR
# collect from center
goc profile --debug -o filtered2.cov
kill -9 $GOC_PID
kill -9 $GOCC_PID
}
@test "test basic goc run" {
@test "test basic goc version command" {
wait_profile_backend "version"
run gocc version --debug --debugcisyncfile ci-sync.bak;
[ "$output" = "(devel)" ]
}