From 10a44439958490388479d88f2adf4526239228a7 Mon Sep 17 00:00:00 2001 From: "jishiwen.jsw" Date: Sat, 15 Nov 2025 22:08:30 +0800 Subject: [PATCH 1/2] feat: Link the instrumented binary and run it and further check output content Signed-off-by: yuluo-yx --- tool/internal/instrument/instrument_test.go | 89 ++++----------------- 1 file changed, 17 insertions(+), 72 deletions(-) diff --git a/tool/internal/instrument/instrument_test.go b/tool/internal/instrument/instrument_test.go index 574fa700..0ca5ff27 100644 --- a/tool/internal/instrument/instrument_test.go +++ b/tool/internal/instrument/instrument_test.go @@ -62,8 +62,23 @@ func TestInstrument(t *testing.T) { } else { require.NoError(t, err) } - // TODO: Link the instrumented binary and run it and further check - // output content + // Build the instrumented binary and run it, then verify output + binPath := filepath.Join(tempDir, "app") + buildCmd := exec.Command("go", "build", "-o", binPath, ".") + buildCmd.Dir = tempDir + buildCmd.Env = append(os.Environ(), "GO111MODULE=off") + out, err := buildCmd.CombinedOutput() + require.NoErrorf(t, err, "go build failed: %s", string(out)) + + runCmd := exec.Command(binPath) + runtimeOut, err := runCmd.CombinedOutput() + require.NoErrorf(t, err, "running app failed: %s", string(runtimeOut)) + + output := string(runtimeOut) + // Base output from source.go + require.Contains(t, output, "Hello, World!") + // Output from added raw rule newfile.go(func2) + require.Contains(t, output, "func2") }) } } @@ -112,59 +127,6 @@ func createTestRuleJSON(mainGoFile string) ([]byte, error) { { PackageName: "main", ModulePath: "main", - FuncRules: map[string][]*rule.InstFuncRule{ - mainGoFile: { - { - InstBaseRule: rule.InstBaseRule{ - Name: "hook_func", - Target: "main", - }, - Path: filepath.Join(".", "testdata"), - Func: "Func1", - Before: "H1Before", - After: "H1After", - }, - { - InstBaseRule: rule.InstBaseRule{ - Name: "hook_same_func", - Target: "main", - }, - Path: filepath.Join(".", "testdata"), - Func: "Func1", - Before: "H2Before", - After: "H2After", - }, - { - InstBaseRule: rule.InstBaseRule{ - Name: "hook_func_with_recv", - Target: "main", - }, - Path: filepath.Join(".", "testdata"), - Func: "Func1", - Recv: "*T", - Before: "H3Before", - }, - { - InstBaseRule: rule.InstBaseRule{ - Name: "hook_func_no_before", - Target: "main", - }, - Path: filepath.Join(".", "testdata"), - Func: "Func1", - Recv: "*T", - After: "H3After", - }, - { - InstBaseRule: rule.InstBaseRule{ - Name: "underscore_param", - Target: "main", - }, - Path: filepath.Join(".", "testdata"), - Func: "Func2", - Before: "H4Before", - }, - }, - }, RawRules: map[string][]*rule.InstRawRule{ mainGoFile: { { @@ -177,23 +139,6 @@ func createTestRuleJSON(mainGoFile string) ([]byte, error) { }, }, }, - StructRules: map[string][]*rule.InstStructRule{ - mainGoFile: { - { - InstBaseRule: rule.InstBaseRule{ - Name: "add_new_field", - Target: "main", - }, - Struct: "T", - NewField: []*rule.InstStructField{ - { - Name: "NewField", - Type: "string", - }, - }, - }, - }, - }, FileRules: []*rule.InstFileRule{ { InstBaseRule: rule.InstBaseRule{ From f683611cb5b2fdbe8cdb1352415f416b5951bb23 Mon Sep 17 00:00:00 2001 From: yuluo-yx Date: Mon, 17 Nov 2025 20:56:53 +0800 Subject: [PATCH 2/2] feat: add TestInstrumentWithHooks test Signed-off-by: yuluo-yx --- tool/internal/instrument/instrument_test.go | 111 ++++++++++++++++++++ 1 file changed, 111 insertions(+) diff --git a/tool/internal/instrument/instrument_test.go b/tool/internal/instrument/instrument_test.go index 0ca5ff27..349a218f 100644 --- a/tool/internal/instrument/instrument_test.go +++ b/tool/internal/instrument/instrument_test.go @@ -153,3 +153,114 @@ func createTestRuleJSON(mainGoFile string) ([]byte, error) { } return json.Marshal(ruleSet) } + +// TestInstrumentWithHooks tests FuncRules and StructRules instrumentation +func TestInstrumentWithHooks(t *testing.T) { + logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ + Level: slog.LevelDebug, + })) + ctx := util.ContextWithLogger(t.Context(), logger) + + tempDir := t.TempDir() + t.Setenv(util.EnvOtelWorkDir, tempDir) + + // Create source code file + mainGoFile := filepath.Join(tempDir, "main.go") + err := os.MkdirAll(filepath.Dir(mainGoFile), 0o755) + require.NoError(t, err) + err = util.CopyFile(filepath.Join("testdata", "source.go"), mainGoFile) + require.NoError(t, err) + + // Create matched.json with FuncRules and StructRules + matchedJSON, err := createTestRuleJSONWithHooks(mainGoFile) + require.NoError(t, err) + matchedFile := filepath.Join(tempDir, util.BuildTempDir, matchedJSONFile) + err = os.MkdirAll(filepath.Dir(matchedFile), 0o755) + require.NoError(t, err) + err = util.WriteFile(matchedFile, string(matchedJSON)) + require.NoError(t, err) + + args := createCompileArgs(tempDir) + err = Toolexec(ctx, args) + require.NoError(t, err, "Instrumentation with FuncRules and StructRules should succeed") +} + +func createTestRuleJSONWithHooks(mainGoFile string) ([]byte, error) { + ruleSet := []*rule.InstRuleSet{ + { + PackageName: "main", + ModulePath: "main", + FuncRules: map[string][]*rule.InstFuncRule{ + mainGoFile: { + { + InstBaseRule: rule.InstBaseRule{ + Name: "hook_func", + Target: "main", + }, + Path: filepath.Join(".", "testdata"), + Func: "Func1", + Before: "H1Before", + After: "H1After", + }, + { + InstBaseRule: rule.InstBaseRule{ + Name: "hook_same_func", + Target: "main", + }, + Path: filepath.Join(".", "testdata"), + Func: "Func1", + Before: "H2Before", + After: "H2After", + }, + { + InstBaseRule: rule.InstBaseRule{ + Name: "hook_func_with_recv", + Target: "main", + }, + Path: filepath.Join(".", "testdata"), + Func: "Func1", + Recv: "*T", + Before: "H3Before", + }, + { + InstBaseRule: rule.InstBaseRule{ + Name: "hook_func_no_before", + Target: "main", + }, + Path: filepath.Join(".", "testdata"), + Func: "Func1", + Recv: "*T", + After: "H3After", + }, + { + InstBaseRule: rule.InstBaseRule{ + Name: "underscore_param", + Target: "main", + }, + Path: filepath.Join(".", "testdata"), + Func: "Func2", + Before: "H4Before", + }, + }, + }, + StructRules: map[string][]*rule.InstStructRule{ + mainGoFile: { + { + InstBaseRule: rule.InstBaseRule{ + Name: "add_new_field", + Target: "main", + }, + Struct: "T", + NewField: []*rule.InstStructField{ + { + Name: "NewField", + Type: "string", + }, + }, + }, + }, + }, + }, + } + return json.Marshal(ruleSet) +}