From 2b3100bae14586c365b2bf2a259d238937624f7b Mon Sep 17 00:00:00 2001 From: lyyyuna Date: Fri, 23 Jul 2021 11:17:53 +0800 Subject: [PATCH] update e2e test of goc profile --- tests/README.md | 3 ++ tests/e2e/cmd.go | 23 ++++++++++++++ tests/e2e/e2e_test.go | 33 +++++++++++++++++--- tests/e2e/samples/basic4ever-project/go.mod | 1 + tests/e2e/samples/basic4ever-project/main.go | 13 ++++++++ tests/e2e/samples/meta.yaml | 5 ++- 6 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 tests/e2e/samples/basic4ever-project/go.mod create mode 100644 tests/e2e/samples/basic4ever-project/main.go diff --git a/tests/README.md b/tests/README.md index 93196cb..f464517 100644 --- a/tests/README.md +++ b/tests/README.md @@ -2,7 +2,10 @@ ## 如何运行集成测试 +首先编译 goc,并加入 `PATH`。 + ``` +make install make e2e ``` diff --git a/tests/e2e/cmd.go b/tests/e2e/cmd.go index 49590b3..c8a7d11 100644 --- a/tests/e2e/cmd.go +++ b/tests/e2e/cmd.go @@ -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 +} diff --git a/tests/e2e/e2e_test.go b/tests/e2e/e2e_test.go index 522d493..15bd31a 100644 --- a/tests/e2e/e2e_test.go +++ b/tests/e2e/e2e_test.go @@ -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 获取的覆盖率有误") }) }) }) diff --git a/tests/e2e/samples/basic4ever-project/go.mod b/tests/e2e/samples/basic4ever-project/go.mod new file mode 100644 index 0000000..bea9895 --- /dev/null +++ b/tests/e2e/samples/basic4ever-project/go.mod @@ -0,0 +1 @@ +module basic2 \ No newline at end of file diff --git a/tests/e2e/samples/basic4ever-project/main.go b/tests/e2e/samples/basic4ever-project/main.go new file mode 100644 index 0000000..9ace33f --- /dev/null +++ b/tests/e2e/samples/basic4ever-project/main.go @@ -0,0 +1,13 @@ +package main + +import ( + "fmt" + "time" +) + +func main() { + for { + time.Sleep(time.Second) + fmt.Println("hello, world") + } +} diff --git a/tests/e2e/samples/meta.yaml b/tests/e2e/samples/meta.yaml index f2e6ecb..d0433f2 100644 --- a/tests/e2e/samples/meta.yaml +++ b/tests/e2e/samples/meta.yaml @@ -1,4 +1,7 @@ samples: basic: dir: basic-project - description: a basic project only print hello world \ No newline at end of file + description: a basic project only print hello world + basic2: + dir: basic4ever-project + description: a basic project which will never exist \ No newline at end of file