Skip to content

Commit

Permalink
strace parsing: fix regex issue when unlink syscall does not have path (
Browse files Browse the repository at this point in the history
#970)

Signed-off-by: Max Fisher <[email protected]>
  • Loading branch information
maxfisher-g authored Nov 16, 2023
1 parent 493ed15 commit 86acaa5
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 25 deletions.
2 changes: 1 addition & 1 deletion internal/strace/strace.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var (
socketPattern = regexp.MustCompile(`{Family: ([^,]+), (Addr: ([^,]*), Port: ([0-9]+)|[^}]+)}`)

// 0x7fe003272980 /tmp/jpu6po61
unlinkPatten = regexp.MustCompile(`0x[a-f\d]+ ([^)]+)`)
unlinkPatten = regexp.MustCompile(`0x[a-f\d]+ ([^)]+)?`)

// unlinkat(0x4 /tmp/pip-pip-egg-info-ng4_5gp_/temps.egg-info, 0x7fe0031c9a10 top_level.txt, 0x0)
// unlinkat(AT_FDCWD /app, 0x5569a7e83380 /app/vendor/composer/e06632ca, 0x200)
Expand Down
81 changes: 57 additions & 24 deletions internal/strace/strace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,63 @@ func TestParseFileWriteWithZeroBytesWritten(t *testing.T) {
utils.RemoveTempFilesDirectory()
}

func TestUnlinkOneEntry(t *testing.T) {
tests := []struct {
name string
input string
want strace.FileInfo
}{
{
// unlink with path
name: "unlink",
input: "I0902 01:19:17.729518 303 strace.go:625] [ 4: 4] python3 X unlink(0x7ff5f78e4980 /tmp/lbosrzlp) = 0 (0x0) (58.552µs)",
want: strace.FileInfo{
Path: "/tmp/lbosrzlp",
Delete: true,
},
},
{
// unlink with no path and error
name: "unlink2",
input: "I1116 06:22:52.164421 1158 strace.go:625] [ 23: 23] cmake X unlink(0x7f234e5bd500 ) = 0 (0x0) errno=2 (no such file or directory) (667ns)",
want: strace.FileInfo{
Path: "",
Delete: true,
},
},
{
name: "unlinkat",
input: "I0902 01:19:18.991729 303 strace.go:631] [ 4: 4] python3 X unlinkat(0x3 /tmp/pip-unpack-7xfj8327, 0x7ff5f790c410 temps-0.3.0.tar.gz, 0x0) = 0 (0x0) (39.914µs)",
want: strace.FileInfo{
Path: "/tmp/pip-unpack-7xfj8327/temps-0.3.0.tar.gz",
Delete: true,
},
},
{
name: "unlinkat_2",
input: "I0907 23:56:32.113900 302 strace.go:631] [ 48: 48] rm X unlinkat(AT_FDCWD /app, 0x5569a7e83380 /app/vendor/composer/e06632ca, 0x200) = 0 (0x0) (69.951µs)",
want: strace.FileInfo{
Path: "/app/vendor/composer/e06632ca",
Delete: true,
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
r := strings.NewReader(test.input)
res, err := strace.Parse(context.Background(), r, nopLogger)
if err != nil || res == nil {
t.Errorf(`Parse(r) = %v, %v, want _, nil`, res, err)
}
files := res.Files()
if len(files) != 1 || !reflect.DeepEqual(files[0], test.want) {
t.Errorf(`Files() = %v, want [%v]`, files, test.want)
}
})
}

}

func TestParseFilesOneEntry(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -287,30 +344,6 @@ func TestParseFilesOneEntry(t *testing.T) {
Write: false,
},
},
{
name: "unlink",
input: "I0902 01:19:17.729518 303 strace.go:625] [ 4: 4] python3 X unlink(0x7ff5f78e4980 /tmp/lbosrzlp) = 0 (0x0) (58.552µs)",
want: strace.FileInfo{
Path: "/tmp/lbosrzlp",
Delete: true,
},
},
{
name: "unlinkat",
input: "I0902 01:19:18.991729 303 strace.go:631] [ 4: 4] python3 X unlinkat(0x3 /tmp/pip-unpack-7xfj8327, 0x7ff5f790c410 temps-0.3.0.tar.gz, 0x0) = 0 (0x0) (39.914µs)",
want: strace.FileInfo{
Path: "/tmp/pip-unpack-7xfj8327/temps-0.3.0.tar.gz",
Delete: true,
},
},
{
name: "unlinkat_2",
input: "I0907 23:56:32.113900 302 strace.go:631] [ 48: 48] rm X unlinkat(AT_FDCWD /app, 0x5569a7e83380 /app/vendor/composer/e06632ca, 0x200) = 0 (0x0) (69.951µs)",
want: strace.FileInfo{
Path: "/app/vendor/composer/e06632ca",
Delete: true,
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
Expand Down

0 comments on commit 86acaa5

Please sign in to comment.