|
| 1 | +package maps |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "reflect" |
| 9 | + "regexp" |
| 10 | + "sort" |
| 11 | + "strings" |
| 12 | + "testing" |
| 13 | + |
| 14 | + "github.com/netobserv/netobserv-ebpf-agent/pkg/ebpf" |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | +) |
| 18 | + |
| 19 | +func TestMapNamesMatchEBPFDefinitions(t *testing.T) { |
| 20 | + // Extract map names from netobserv-ebpf-agent/pkg/ebpf |
| 21 | + ebpfMaps := extractEBPFMapNames() |
| 22 | + |
| 23 | + // Get map names from our package |
| 24 | + packageMaps := Maps |
| 25 | + |
| 26 | + // Sort both slices for comparison |
| 27 | + sort.Strings(ebpfMaps) |
| 28 | + sort.Strings(packageMaps) |
| 29 | + fmt.Printf("eBPF maps: %+v\n", ebpfMaps) |
| 30 | + fmt.Printf("Package maps: %+v\n", packageMaps) |
| 31 | + |
| 32 | + // Verify they match |
| 33 | + assert.Equal(t, ebpfMaps, packageMaps, |
| 34 | + "Map names in pkg/maps/maps.go do not match eBPF definitions in bpf/maps_definition.h") |
| 35 | +} |
| 36 | + |
| 37 | +func TestMapNamesMatchBcMk(t *testing.T) { |
| 38 | + // Extract map names from .mk/bc.mk |
| 39 | + bcMaps, err := extractBcMkMapNames("../../.mk/bc.mk") |
| 40 | + require.NoError(t, err, "Failed to extract map names from .mk/bc.mk") |
| 41 | + |
| 42 | + // Get map names from our package |
| 43 | + packageMaps := Maps |
| 44 | + |
| 45 | + // Sort both slices for comparison |
| 46 | + sort.Strings(bcMaps) |
| 47 | + sort.Strings(packageMaps) |
| 48 | + fmt.Printf("bc.mk maps: %+v\n", bcMaps) |
| 49 | + fmt.Printf("Package maps: %+v\n", packageMaps) |
| 50 | + |
| 51 | + // Verify they match |
| 52 | + assert.Equal(t, bcMaps, packageMaps, |
| 53 | + "Map names in pkg/maps/maps.go do not match definitions in .mk/bc.mk.\n"+ |
| 54 | + "If you've added/removed maps, please update both files.") |
| 55 | +} |
| 56 | + |
| 57 | +// extractBcMkMapNames parses the .mk/bc.mk file and extracts map names from the MAPS JSON |
| 58 | +func extractBcMkMapNames(filePath string) ([]string, error) { |
| 59 | + content, err := os.ReadFile(filePath) |
| 60 | + if err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + |
| 64 | + // Find the MAPS definition block |
| 65 | + mapsRegex := regexp.MustCompile(`define MAPS\s*\n((.|\s)*?)\nendef`) |
| 66 | + matches := mapsRegex.FindStringSubmatch(string(content)) |
| 67 | + |
| 68 | + if len(matches) < 2 { |
| 69 | + return nil, errors.New("Could not find MAPS definition in .mk/bc.mk") |
| 70 | + } |
| 71 | + |
| 72 | + // Extract the JSON content |
| 73 | + jsonContent := strings.TrimSpace(matches[1]) |
| 74 | + |
| 75 | + // Parse JSON to extract map names |
| 76 | + var mapsData map[string]string |
| 77 | + if err := json.Unmarshal([]byte(jsonContent), &mapsData); err != nil { |
| 78 | + return nil, err |
| 79 | + } |
| 80 | + |
| 81 | + // Extract just the map names (keys) |
| 82 | + var mapNames []string |
| 83 | + for mapName := range mapsData { |
| 84 | + mapNames = append(mapNames, mapName) |
| 85 | + } |
| 86 | + |
| 87 | + return mapNames, nil |
| 88 | +} |
| 89 | + |
| 90 | +// extractEBPFMapNames extracts map names from the compiled eBPF agent |
| 91 | +func extractEBPFMapNames() []string { |
| 92 | + var maps []string |
| 93 | + mapType := reflect.ValueOf(ebpf.BpfMapSpecs{}) |
| 94 | + for i := 0; i < mapType.NumField(); i++ { |
| 95 | + val := reflect.Indirect(mapType) |
| 96 | + mapName := val.Type().Field(i).Tag.Get("ebpf") |
| 97 | + |
| 98 | + if mapName != "" { |
| 99 | + maps = append(maps, mapName) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return maps |
| 104 | +} |
0 commit comments