summaryrefslogtreecommitdiff
path: root/internal/ior_test.go
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-02-22 22:31:35 +0200
committerPaul Buetow <paul@buetow.org>2026-02-22 22:31:35 +0200
commit4cd2c4e818a1438bf63d1ca05a6cf134f39bc06b (patch)
tree40b4ad1ab60a6f50973a66c4e273c91e0d8d265b /internal/ior_test.go
parent8e52ba5a8661c717f45e00608ad64f0adc6de3e1 (diff)
Add copy_file_range support and tracepoint attach tests
Diffstat (limited to 'internal/ior_test.go')
-rw-r--r--internal/ior_test.go101
1 files changed, 101 insertions, 0 deletions
diff --git a/internal/ior_test.go b/internal/ior_test.go
new file mode 100644
index 0000000..6495e76
--- /dev/null
+++ b/internal/ior_test.go
@@ -0,0 +1,101 @@
+package internal
+
+import (
+ "errors"
+ "strings"
+ "testing"
+)
+
+type fakeTracepointProgram struct {
+ attachCalls int
+ attachErr error
+}
+
+func (p *fakeTracepointProgram) attachTracepoint(_, _ string) error {
+ p.attachCalls++
+ return p.attachErr
+}
+
+type fakeTracepointModule struct {
+ getProgramCalls []string
+ getProgramErrs map[string]error
+ programs map[string]*fakeTracepointProgram
+}
+
+func (m *fakeTracepointModule) getProgram(progName string) (tracepointProgram, error) {
+ m.getProgramCalls = append(m.getProgramCalls, progName)
+ if err, ok := m.getProgramErrs[progName]; ok {
+ return nil, err
+ }
+ if prog, ok := m.programs[progName]; ok {
+ return prog, nil
+ }
+ return nil, errors.New("missing program")
+}
+
+func TestAttachTracepointsWithSkipsFilteredTracepoints(t *testing.T) {
+ module := &fakeTracepointModule{
+ programs: map[string]*fakeTracepointProgram{
+ "handle_sys_enter_read": {},
+ "handle_sys_enter_write": {},
+ },
+ getProgramErrs: map[string]error{},
+ }
+
+ err := attachTracepointsWith(module, func(tracepoint string) bool {
+ return tracepoint == "sys_enter_read"
+ }, []string{"sys_enter_read", "sys_enter_write"})
+ if err != nil {
+ t.Fatalf("attachTracepointsWith returned error: %v", err)
+ }
+
+ if len(module.getProgramCalls) != 1 || module.getProgramCalls[0] != "handle_sys_enter_read" {
+ t.Fatalf("getProgram calls = %v, want only handle_sys_enter_read", module.getProgramCalls)
+ }
+
+ if module.programs["handle_sys_enter_read"].attachCalls != 1 {
+ t.Fatalf("read attach calls = %d, want 1", module.programs["handle_sys_enter_read"].attachCalls)
+ }
+ if module.programs["handle_sys_enter_write"].attachCalls != 0 {
+ t.Fatalf("write attach calls = %d, want 0", module.programs["handle_sys_enter_write"].attachCalls)
+ }
+}
+
+func TestAttachTracepointsWithReturnsErrorWhenProgramMissing(t *testing.T) {
+ module := &fakeTracepointModule{
+ programs: map[string]*fakeTracepointProgram{},
+ getProgramErrs: map[string]error{
+ "handle_sys_enter_read": errors.New("not found"),
+ },
+ }
+
+ err := attachTracepointsWith(module, func(string) bool { return true }, []string{"sys_enter_read"})
+ if err == nil {
+ t.Fatal("attachTracepointsWith returned nil error, want non-nil")
+ }
+ if !strings.Contains(err.Error(), "handle_sys_enter_read") {
+ t.Fatalf("error %q does not mention handle_sys_enter_read", err)
+ }
+}
+
+func TestAttachTracepointsWithAttachFailureContinues(t *testing.T) {
+ module := &fakeTracepointModule{
+ programs: map[string]*fakeTracepointProgram{
+ "handle_sys_enter_read": {attachErr: errors.New("no tracepoint")},
+ "handle_sys_enter_write": {},
+ },
+ getProgramErrs: map[string]error{},
+ }
+
+ err := attachTracepointsWith(module, func(string) bool { return true }, []string{"sys_enter_read", "sys_enter_write"})
+ if err != nil {
+ t.Fatalf("attachTracepointsWith returned error: %v", err)
+ }
+
+ if module.programs["handle_sys_enter_read"].attachCalls != 1 {
+ t.Fatalf("read attach calls = %d, want 1", module.programs["handle_sys_enter_read"].attachCalls)
+ }
+ if module.programs["handle_sys_enter_write"].attachCalls != 1 {
+ t.Fatalf("write attach calls = %d, want 1", module.programs["handle_sys_enter_write"].attachCalls)
+ }
+}