|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package oomwatcher |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/parca-dev/oomprof/oomprof" |
| 10 | +) |
| 11 | + |
| 12 | +func TestPfelfReaderImplementsInterface(t *testing.T) { |
| 13 | + // Verify that pfelfReader implements oomprof.ELFReader |
| 14 | + var _ oomprof.ELFReader = &pfelfReader{} |
| 15 | +} |
| 16 | + |
| 17 | +func TestPfelfFileImplementsInterface(t *testing.T) { |
| 18 | + // Verify that pfelfFile implements oomprof.ELFFile |
| 19 | + var _ oomprof.ELFFile = &pfelfFile{} |
| 20 | +} |
| 21 | + |
| 22 | +func TestInit(t *testing.T) { |
| 23 | + // Verify that the global ELF reader is set |
| 24 | + reader := oomprof.GetELFReader() |
| 25 | + if reader == nil { |
| 26 | + t.Fatal("Expected global ELF reader to be set") |
| 27 | + } |
| 28 | + |
| 29 | + // Verify it's our implementation by checking the type |
| 30 | + if _, ok := reader.(*pfelfReader); !ok { |
| 31 | + t.Errorf("Expected global ELF reader to be *pfelfReader, got %T", reader) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func TestPfelfReaderOpenAndRead(t *testing.T) { |
| 36 | + reader := &pfelfReader{} |
| 37 | + |
| 38 | + // Try to open the Go binary itself (should be a Go binary) |
| 39 | + file, err := reader.Open("/home/tpr/.gvm/gos/go1.25.1/bin/go") |
| 40 | + if err != nil { |
| 41 | + t.Skipf("Could not open Go binary for testing: %v", err) |
| 42 | + } |
| 43 | + defer file.Close() |
| 44 | + |
| 45 | + // Test GetBuildID |
| 46 | + buildID, err := file.GetBuildID() |
| 47 | + if err != nil { |
| 48 | + t.Logf("GetBuildID returned error (may be expected): %v", err) |
| 49 | + } else { |
| 50 | + t.Logf("Build ID: %s", buildID) |
| 51 | + if buildID == "" { |
| 52 | + t.Error("Expected non-empty build ID") |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // Test GoVersion |
| 57 | + goVersion, err := file.GoVersion() |
| 58 | + if err != nil { |
| 59 | + t.Fatalf("GoVersion failed: %v", err) |
| 60 | + } |
| 61 | + if goVersion == "" { |
| 62 | + t.Error("Expected non-empty Go version") |
| 63 | + } |
| 64 | + t.Logf("Go version: %s", goVersion) |
| 65 | + |
| 66 | + // Test LookupSymbol - try to find a common Go symbol |
| 67 | + symInfo, err := file.LookupSymbol("runtime.main") |
| 68 | + if err != nil { |
| 69 | + t.Logf("Could not find runtime.main symbol: %v", err) |
| 70 | + } else { |
| 71 | + t.Logf("Found symbol runtime.main at address 0x%x", symInfo.Address) |
| 72 | + if symInfo.Name != "runtime.main" { |
| 73 | + t.Errorf("Expected symbol name 'runtime.main', got '%s'", symInfo.Name) |
| 74 | + } |
| 75 | + if symInfo.Address == 0 { |
| 76 | + t.Error("Expected non-zero address for runtime.main") |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments