Skip to content

Commit

Permalink
ci: add test for dynamic CallContext APIs generation (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
y1yang0 committed Aug 1, 2024
1 parent 5287beb commit e8abd17
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 4 deletions.
10 changes: 10 additions & 0 deletions pkg/rules/test/errors_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,13 @@ func onExitTestGetSetRecv(call errors.CallContext, arg1 int, arg2 float64) {
call.SetReturnVal(0, arg1)
call.SetReturnVal(1, arg2)
}

func onExitOnlyRet(call errors.CallContext, _ int, _ string) {
call.SetReturnVal(0, 2033)
call.SetReturnVal(1, "hangzhou")
}

func onEnterOnlyArgs(call errors.CallContext, _ int, _ string) {
call.SetParam(0, 2024)
call.SetParam(1, "shanghai")
}
8 changes: 8 additions & 0 deletions pkg/rules/test/long/sub/p4.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,11 @@ type Recv struct{ X int }
func (t *Recv) TestGetSetRecv(arg1 int, arg2 float64) (int, float64) {
return arg1, arg2
}

func OnlyRet() (int, string) {
return 1024, "gansu"
}

func OnlyArgs(arg1 int, arg2 string) {
println(arg1, arg2)
}
8 changes: 8 additions & 0 deletions pkg/rules/test/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,12 @@ func init() {
api.NewRule("errors", "TestGetSetRecv", "*Recv", "onEnterTestGetSetRecv", "onExitTestGetSetRecv").
WithRuleName("testrule").
Register()

api.NewRule("errors", "OnlyRet", "", "", "onExitOnlyRet").
WithRuleName("testrule").
Register()

api.NewRule("errors", "OnlyArgs", "", "onEnterOnlyArgs", "").
WithRuleName("testrule").
Register()
}
4 changes: 4 additions & 0 deletions test/errors-test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ func main() {
recv := &errors.Recv{}
a, b := recv.TestGetSetRecv(1, 3.14)
fmt.Printf("recv%v %v %v\n", recv, a, b)

errors.OnlyArgs(1, "jiangsu")
c, d := errors.OnlyRet()
fmt.Printf("onlyret%v %v\n", c, d)
}
4 changes: 3 additions & 1 deletion test/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestRunErrors(t *testing.T) {
UseApp(ErrorsAppName)

RunInstrument(t, "-debuglog", "-disablerules=fmt")
stdout, _ := RunApp(t, ErrorsAppName)
stdout, stderr := RunApp(t, ErrorsAppName)
ExpectContains(t, stdout, "wow")
ExpectContains(t, stdout, "old:wow")
ExpectContains(t, stdout, "ptr<nil>")
Expand All @@ -21,6 +21,8 @@ func TestRunErrors(t *testing.T) {
ExpectContains(t, stdout, "4008208820")
ExpectContains(t, stdout, "118888")
ExpectContains(t, stdout, "0.001")
ExpectContains(t, stderr, "2024 shanghai")
ExpectContains(t, stdout, "2033 hangzhou")

text := ReadInstrumentLog(t, "debug_fn_otel_inst_file_p4.go")
re := regexp.MustCompile(".*OtelOnEnterTrampoline_TestSkip.*")
Expand Down
2 changes: 1 addition & 1 deletion tool/instrument/inst_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ func (rp *RuleProcessor) applyFuncRules(bundle *resource.RuleBundle) (err error)
return fmt.Errorf("failed to rewrite: %w for %v",
err, rule)
}
log.Printf("Apply func rule %s for %v\n", rule, file)
log.Printf("Apply func rule %s\n", rule)
}
break
}
Expand Down
9 changes: 7 additions & 2 deletions tool/instrument/instrument.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func Instrument() error {
// Is compile command?
if shared.IsCompileCommand(strings.Join(args, " ")) {
if shared.Verbose {
log.Printf("Compiling: %v\n", args)
log.Printf("CompileCmd: %v\n", args)
}
bundles, err := resource.LoadRuleBundles()
if err != nil {
Expand All @@ -151,7 +151,12 @@ func Instrument() error {
if err != nil {
return fmt.Errorf("failed to apply rules: %w", err)
}
log.Printf("Compiled: %v", rp.compileArgs)
if !shared.Verbose {
log.Printf("CompileCmd: %v (%v)\n",
bundle.ImportPath, bundle.PackageName)
} else {
log.Printf("CompileCmd: %v\n", rp.compileArgs)
}
// Good, run final compilation after instrumentation
return util.RunCmd(rp.compileArgs...)
}
Expand Down

0 comments on commit e8abd17

Please sign in to comment.