Merge pull request #209 from lyyyuna/v2

update e2e test of goc profile
This commit is contained in:
Li Yiyang 2021-07-23 11:44:44 +08:00 committed by GitHub
commit 8aca5c7146
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 83 additions and 5 deletions

View File

@ -2,7 +2,10 @@
## 如何运行集成测试
首先编译 goc并加入 `PATH`
```
make install
make e2e
```

View File

@ -6,6 +6,8 @@ import (
"fmt"
"os"
"os/exec"
"runtime"
"strings"
"time"
)
@ -23,9 +25,18 @@ type LongRunCmd struct {
//
// You can specify the whole command arg list, the directory where the command
// will be run in, and the env list.
//
// args 命令列表
//
// dir 命令运行所在的目录
//
// envs 额外的环境变量
func NewLongRunCmd(args []string, dir string, envs []string) *LongRunCmd {
ctx, cancel := context.WithCancel(context.Background())
cmd := exec.CommandContext(ctx, args[0], args[1:]...)
if runtime.GOOS == "windows" {
cmd = exec.CommandContext(ctx, args[0]+".exe", args[1:]...)
}
cmd.Dir = dir
cmd.Env = append(os.Environ(), envs...)
@ -77,14 +88,36 @@ func (l *LongRunCmd) GetStdoutStdErr() (string, string) {
}
// RunShortRunCmd defines a cmd which run and exits immediately
//
// args 命令列表
//
// dir 命令运行所在的目录
//
// envs 额外的环境变量
func RunShortRunCmd(args []string, dir string, envs []string) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*20)
defer cancel()
cmd := exec.CommandContext(ctx, args[0], args[1:]...)
if runtime.GOOS == "windows" {
cmd = exec.CommandContext(ctx, args[0]+".exe", args[1:]...)
}
cmd.Dir = dir
cmd.Env = append(os.Environ(), envs...)
output, err := cmd.CombinedOutput()
return string(output), err
}
// SearchSymbolInBinary searches if the specified symbol is compiled into the bianry
func SearchSymbolInBinary(dir string, binary string, symbol string) (bool, error) {
if runtime.GOOS == "windows" {
binary = binary + ".exe"
}
output, err := RunShortRunCmd([]string{"go", "tool", "objdump", "-s", symbol, binary}, dir, nil)
if err != nil {
return false, fmt.Errorf("cannot lookup into the binary: %w", err)
}
return strings.Contains(output, symbol), nil
}

View File

@ -1,6 +1,8 @@
package e2e
import (
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
@ -22,12 +24,17 @@ var _ = Describe("1 [基础测试]", func() {
By("使用 goc build 命令编译")
_, err = RunShortRunCmd([]string{"goc", "build", "."}, dir, nil)
Expect(err).To(BeNil(), "goc build 运行错误")
By("检查代码是否被插入二进制")
contain, err := SearchSymbolInBinary(dir, "basic", "basic/goc-cover-agent-apis-auto-generated-11111-22222-package.loadFileCover")
Expect(err).To(BeNil(), "二进制检查失败")
Expect(contain).To(BeTrue(), "二进制中未找到插桩的符号")
})
})
Describe("2 测试 server 命令", func() {
It("1.2.1 测试 API 接口", func() {
dir, err := mgr.GetSampleByKey("basic")
It("1.2.1 测试编译/list/profile基础场景", func() {
dir, err := mgr.GetSampleByKey("basic2")
Expect(err).To(BeNil(), "找不到 sample")
By("启动 goc server")
@ -35,10 +42,28 @@ var _ = Describe("1 [基础测试]", func() {
lc.Run()
defer lc.Stop()
By("编译一个长时间运行的程序")
output, err := RunShortRunCmd([]string{"goc", "build", "."}, dir, nil)
Expect(err).To(BeNil(), "编译失败:"+output)
By("长时间运行 basic2")
basicC := NewLongRunCmd([]string{"./basic2"}, dir, nil)
basicC.Run()
defer basicC.Stop()
By("使用 goc list 获取服务列表")
output, err := RunShortRunCmd([]string{"goc", "list"}, dir, nil)
output, err = RunShortRunCmd([]string{"goc", "list"}, dir, nil)
Expect(err).To(BeNil(), "goc list 运行错误")
Expect(output).To(ContainSubstring("REMOTEIP"), "goc list 输出应该包含 REMOTEIP")
Expect(output).To(ContainSubstring("127.0.0.1 ./basic2"), "goc list 输出应该包含 basic 服务")
By("使用 goc profile 获取覆盖率")
profileStr := `mode: count
basic2/main.go:8.13,9.6 1 1
basic2/main.go:9.6,12.3 2 2`
time.Sleep(time.Second)
output, err = RunShortRunCmd([]string{"goc", "profile"}, dir, nil)
Expect(err).To(BeNil(), "goc profile 运行错误")
Expect(output).To(ContainSubstring(profileStr), "goc profile 获取的覆盖率有误")
})
})
})

View File

@ -0,0 +1 @@
module basic2

View File

@ -0,0 +1,13 @@
package main
import (
"fmt"
"time"
)
func main() {
for {
time.Sleep(time.Second)
fmt.Println("hello, world")
}
}

View File

@ -1,4 +1,7 @@
samples:
basic:
dir: basic-project
description: a basic project only print hello world
description: a basic project only print hello world
basic2:
dir: basic4ever-project
description: a basic project which will never exist