diff --git a/internal/strace/strace.go b/internal/strace/strace.go index 162204ab..2083e783 100644 --- a/internal/strace/strace.go +++ b/internal/strace/strace.go @@ -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) diff --git a/internal/strace/strace_test.go b/internal/strace/strace_test.go index 25059f5a..d9fbd04c 100644 --- a/internal/strace/strace_test.go +++ b/internal/strace/strace_test.go @@ -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 @@ -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) {