From e92a9f64e927f97254d7a3ffcbd87adcae0edc54 Mon Sep 17 00:00:00 2001 From: Kemal Akkoyun Date: Fri, 18 Mar 2022 10:14:34 +0100 Subject: [PATCH] Add tests Signed-off-by: Kemal Akkoyun --- cmd/debug-info/main.go | 6 ++-- pkg/debuginfo/find.go | 14 ++++++-- pkg/debuginfo/find_test.go | 74 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 pkg/debuginfo/find_test.go diff --git a/cmd/debug-info/main.go b/cmd/debug-info/main.go index 6372aaef0e..3863dc3163 100644 --- a/cmd/debug-info/main.go +++ b/cmd/debug-info/main.go @@ -1,4 +1,4 @@ -// Copyright 2021 The Parca Authors +// Copyright (c) 2022 The Parca Authors // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at @@ -10,6 +10,7 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. +// package main @@ -160,12 +161,11 @@ func main() { }) case "buildid ": g.Add(func() error { - buildID, err := buildid.BuildID(flags.Buildid.Path) + _, err := buildid.BuildID(flags.Buildid.Path) if err != nil { level.Error(logger).Log("msg", "failed to extract elf build ID", "err", err) return err } - fmt.Println(buildID) return nil }, func(error) { cancel() diff --git a/pkg/debuginfo/find.go b/pkg/debuginfo/find.go index 723d5cd2a1..46c856ed37 100644 --- a/pkg/debuginfo/find.go +++ b/pkg/debuginfo/find.go @@ -18,6 +18,7 @@ import ( "context" "errors" "fmt" + "io/fs" "os" "path/filepath" @@ -25,6 +26,12 @@ import ( "github.com/goburrow/cache" ) +type realfs struct{} + +func (f *realfs) Open(name string) (fs.File, error) { return os.Open(name) } + +var fileSystem fs.FS = &realfs{} + // Finder finds the additional debug information on the system. type Finder struct { logger log.Logger @@ -73,13 +80,16 @@ func (f *Finder) Find(ctx context.Context, buildID, root string) (string, error) } func find(buildID, root string) (string, error) { + if len(buildID) < 2 { + return "", errors.New("invalid build ID") + } // Debian: /usr/lib/debug/.build-id/f9/02f8a561c3abdb9c8d8c859d4243bd8c3f928f.debug // -- apt install -dbg // Fedora: /usr/lib/debug/.build-id/da/40581445b62eff074d67fae906792cb26e8d54.debug // -- dnf --enablerepo=fedora-debuginfo --enablerepo=updates-debuginfo install -debuginfo // Arch: https://wiki.archlinux.org/title/Debugging/Getting_traces - file := filepath.Join(root, "/usr/lib/debug", ".build-id", buildID[:2], buildID[2:]) - _, err := os.Stat(file) + file := filepath.Join(root, "/usr/lib/debug", ".build-id", buildID[:2], buildID[2:]) + ".debug" + _, err := fs.Stat(fileSystem, file) if err == nil { return file, nil } diff --git a/pkg/debuginfo/find_test.go b/pkg/debuginfo/find_test.go new file mode 100644 index 0000000000..4243debcfd --- /dev/null +++ b/pkg/debuginfo/find_test.go @@ -0,0 +1,74 @@ +// Copyright (c) 2022 The Parca Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +package debuginfo + +import ( + "testing" + + "github.com/parca-dev/parca-agent/pkg/testutil" +) + +func Test_find(t *testing.T) { + oldFs := fileSystem + mfs := testutil.NewFakeFS(map[string][]byte{ + "/proc/124/root/usr/lib/debug/.build-id/d1/b25b63b3edc63832fd885e4b997f8a463ea573.debug": []byte("whatever"), + }) + fileSystem = mfs + t.Cleanup(func() { + fileSystem = oldFs + }) + + type args struct { + buildID string + root string + } + tests := []struct { + name string + args args + want string + wantErr bool + }{ + { + name: "empty", + args: args{ + buildID: "", + root: "", + }, + want: "", + wantErr: true, + }, + { + name: "valid", + args: args{ + buildID: "d1b25b63b3edc63832fd885e4b997f8a463ea573", + root: "/proc/124/root", + }, + want: "/proc/124/root/usr/lib/debug/.build-id/d1/b25b63b3edc63832fd885e4b997f8a463ea573.debug", + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := find(tt.args.buildID, tt.args.root) + if (err != nil) != tt.wantErr { + t.Errorf("find() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("find() got = %v, want %v", got, tt.want) + } + }) + } +}