update e2e test of goc profile
This commit is contained in:
parent
ffd1d2e2e1
commit
2b3100bae1
@ -2,7 +2,10 @@
|
||||
|
||||
## 如何运行集成测试
|
||||
|
||||
首先编译 goc,并加入 `PATH`。
|
||||
|
||||
```
|
||||
make install
|
||||
make e2e
|
||||
```
|
||||
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -23,6 +24,12 @@ 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:]...)
|
||||
@ -77,6 +84,12 @@ 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()
|
||||
@ -88,3 +101,13 @@ func RunShortRunCmd(args []string, dir string, envs []string) (string, error) {
|
||||
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) {
|
||||
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
|
||||
}
|
||||
|
@ -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 获取的覆盖率有误")
|
||||
})
|
||||
})
|
||||
})
|
||||
|
1
tests/e2e/samples/basic4ever-project/go.mod
Normal file
1
tests/e2e/samples/basic4ever-project/go.mod
Normal file
@ -0,0 +1 @@
|
||||
module basic2
|
13
tests/e2e/samples/basic4ever-project/main.go
Normal file
13
tests/e2e/samples/basic4ever-project/main.go
Normal file
@ -0,0 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
for {
|
||||
time.Sleep(time.Second)
|
||||
fmt.Println("hello, world")
|
||||
}
|
||||
}
|
@ -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
|
Loading…
Reference in New Issue
Block a user