|
6 | 6 | "errors" |
7 | 7 | "flag" |
8 | 8 | "fmt" |
| 9 | + "maps" |
9 | 10 | "os" |
10 | 11 | "path/filepath" |
11 | 12 | "strings" |
@@ -59,76 +60,11 @@ func TestLoadCollectionSpec(t *testing.T) { |
59 | 60 | ValueSize: 8, |
60 | 61 | MaxEntries: 2, |
61 | 62 | }, |
62 | | - "array_of_hash_map": { |
63 | | - Name: "array_of_hash_map", |
64 | | - Type: ArrayOfMaps, |
65 | | - KeySize: 4, |
66 | | - MaxEntries: 2, |
67 | | - }, |
68 | 63 | "perf_event_array": { |
69 | 64 | Name: "perf_event_array", |
70 | 65 | Type: PerfEventArray, |
71 | 66 | MaxEntries: 4096, |
72 | 67 | }, |
73 | | - "btf_pin": { |
74 | | - Name: "btf_pin", |
75 | | - Type: Hash, |
76 | | - KeySize: 4, |
77 | | - ValueSize: 8, |
78 | | - MaxEntries: 1, |
79 | | - Pinning: PinByName, |
80 | | - }, |
81 | | - "bpf_decl_map": { |
82 | | - Name: "bpf_decl_map", |
83 | | - Type: Array, |
84 | | - KeySize: 4, |
85 | | - ValueSize: 8, |
86 | | - MaxEntries: 1, |
87 | | - Tags: []string{"a", "b"}, |
88 | | - }, |
89 | | - "btf_decl_map": { |
90 | | - Name: "btf_decl_map", |
91 | | - Type: Array, |
92 | | - KeySize: 4, |
93 | | - ValueSize: 8, |
94 | | - MaxEntries: 1, |
95 | | - Tags: []string{"a", "b"}, |
96 | | - }, |
97 | | - "btf_outer_map": { |
98 | | - Name: "btf_outer_map", |
99 | | - Type: ArrayOfMaps, |
100 | | - KeySize: 4, |
101 | | - ValueSize: 4, |
102 | | - MaxEntries: 1, |
103 | | - InnerMap: &MapSpec{ |
104 | | - Name: "btf_outer_map_inner", |
105 | | - Type: Hash, |
106 | | - KeySize: 4, |
107 | | - ValueSize: 4, |
108 | | - MaxEntries: 1, |
109 | | - }, |
110 | | - }, |
111 | | - "btf_outer_map_anon": { |
112 | | - Name: "btf_outer_map_anon", |
113 | | - Type: ArrayOfMaps, |
114 | | - KeySize: 4, |
115 | | - ValueSize: 4, |
116 | | - MaxEntries: 1, |
117 | | - InnerMap: &MapSpec{ |
118 | | - Name: "btf_outer_map_anon_inner", |
119 | | - Type: Hash, |
120 | | - KeySize: 4, |
121 | | - ValueSize: 4, |
122 | | - MaxEntries: 1, |
123 | | - }, |
124 | | - }, |
125 | | - "btf_typedef_map": { |
126 | | - Name: "btf_typedef_map", |
127 | | - Type: Array, |
128 | | - KeySize: 4, |
129 | | - ValueSize: 8, |
130 | | - MaxEntries: 1, |
131 | | - }, |
132 | 68 | ".bss": { |
133 | 69 | Name: ".bss", |
134 | 70 | Type: Array, |
@@ -232,38 +168,107 @@ func TestLoadCollectionSpec(t *testing.T) { |
232 | 168 | }, |
233 | 169 | } |
234 | 170 |
|
| 171 | + // BTF-only maps. |
| 172 | + btfOnly := map[string]*MapSpec{ |
| 173 | + "btf_pin": { |
| 174 | + Name: "btf_pin", |
| 175 | + Type: Hash, |
| 176 | + KeySize: 4, |
| 177 | + ValueSize: 8, |
| 178 | + MaxEntries: 1, |
| 179 | + Pinning: PinByName, |
| 180 | + }, |
| 181 | + "bpf_decl_map": { |
| 182 | + Name: "bpf_decl_map", |
| 183 | + Type: Array, |
| 184 | + KeySize: 4, |
| 185 | + ValueSize: 8, |
| 186 | + MaxEntries: 1, |
| 187 | + Tags: []string{"a", "b"}, |
| 188 | + }, |
| 189 | + "btf_decl_map": { |
| 190 | + Name: "btf_decl_map", |
| 191 | + Type: Array, |
| 192 | + KeySize: 4, |
| 193 | + ValueSize: 8, |
| 194 | + MaxEntries: 1, |
| 195 | + Tags: []string{"a", "b"}, |
| 196 | + }, |
| 197 | + "btf_outer_map": { |
| 198 | + Name: "btf_outer_map", |
| 199 | + Type: ArrayOfMaps, |
| 200 | + KeySize: 4, |
| 201 | + ValueSize: 4, |
| 202 | + MaxEntries: 1, |
| 203 | + InnerMap: &MapSpec{ |
| 204 | + Name: "btf_outer_map_inner", |
| 205 | + Type: Hash, |
| 206 | + KeySize: 4, |
| 207 | + ValueSize: 4, |
| 208 | + MaxEntries: 1, |
| 209 | + }, |
| 210 | + }, |
| 211 | + "btf_outer_map_anon": { |
| 212 | + Name: "btf_outer_map_anon", |
| 213 | + Type: ArrayOfMaps, |
| 214 | + KeySize: 4, |
| 215 | + ValueSize: 4, |
| 216 | + MaxEntries: 1, |
| 217 | + InnerMap: &MapSpec{ |
| 218 | + Name: "btf_outer_map_anon_inner", |
| 219 | + Type: Hash, |
| 220 | + KeySize: 4, |
| 221 | + ValueSize: 4, |
| 222 | + MaxEntries: 1, |
| 223 | + }, |
| 224 | + }, |
| 225 | + "btf_typedef_map": { |
| 226 | + Name: "btf_typedef_map", |
| 227 | + Type: Array, |
| 228 | + KeySize: 4, |
| 229 | + ValueSize: 8, |
| 230 | + MaxEntries: 1, |
| 231 | + }, |
| 232 | + } |
| 233 | + |
235 | 234 | testutils.Files(t, testutils.Glob(t, "testdata/loader-*.elf"), func(t *testing.T, file string) { |
236 | | - have, err := LoadCollectionSpec(file) |
237 | | - if err != nil { |
238 | | - t.Fatal("Can't parse ELF:", err) |
239 | | - } |
| 235 | + got, err := LoadCollectionSpec(file) |
| 236 | + qt.Assert(t, qt.IsNil(err)) |
240 | 237 |
|
241 | | - qt.Assert(t, qt.Equals(have.Maps["perf_event_array"].ValueSize, 0)) |
242 | | - qt.Assert(t, qt.IsNotNil(have.Maps["perf_event_array"].Value)) |
| 238 | + // BTF map definition contains a value type, but the size should remain 0. |
| 239 | + // The value type needs to be reflected in the MapSpec. |
| 240 | + qt.Assert(t, qt.Equals(got.Maps["perf_event_array"].ValueSize, 0)) |
| 241 | + qt.Assert(t, qt.IsNotNil(got.Maps["perf_event_array"].Value)) |
243 | 242 |
|
244 | | - qt.Assert(t, qt.CmpEquals(have, coll, csCmpOpts)) |
| 243 | + // Copy and extend the CollectionSpec with BTF-only objects. |
| 244 | + want := coll.Copy() |
| 245 | + maps.Copy(want.Maps, btfOnly) |
245 | 246 |
|
246 | | - qt.Assert(t, qt.IsNil(have.Variables["arg"].Set(uint32(1)))) |
247 | | - qt.Assert(t, qt.IsNil(have.Variables["arg2"].Set(uint32(2)))) |
| 247 | + testLoadCollectionSpec(t, got, want) |
| 248 | + }) |
248 | 249 |
|
249 | | - have.Maps["array_of_hash_map"].InnerMap = have.Maps["hash_map"] |
250 | | - coll, err := newCollection(t, have, &CollectionOptions{ |
251 | | - Maps: MapOptions{ |
252 | | - PinPath: testutils.TempBPFFS(t), |
253 | | - }, |
254 | | - Programs: ProgramOptions{ |
255 | | - LogLevel: LogLevelBranch, |
256 | | - }, |
257 | | - }) |
| 250 | + testutils.Files(t, testutils.Glob(t, "testdata/loader_nobtf-*.elf"), func(t *testing.T, file string) { |
| 251 | + got, err := LoadCollectionSpec(file) |
| 252 | + qt.Assert(t, qt.IsNil(err)) |
258 | 253 |
|
259 | | - testutils.SkipIfNotSupported(t, err) |
260 | | - if err != nil { |
261 | | - t.Fatal(err) |
262 | | - } |
| 254 | + testLoadCollectionSpec(t, got, coll.Copy()) |
| 255 | + }) |
| 256 | +} |
| 257 | + |
| 258 | +func testLoadCollectionSpec(t *testing.T, got, want *CollectionSpec) { |
| 259 | + t.Helper() |
263 | 260 |
|
264 | | - ret := mustRun(t, coll.Programs["xdp_prog"], nil) |
265 | | - qt.Assert(t, qt.Equals(ret, 7)) |
| 261 | + qt.Assert(t, qt.CmpEquals(got, want, csCmpOpts)) |
| 262 | + |
| 263 | + coll, err := newCollection(t, got, &CollectionOptions{ |
| 264 | + Maps: MapOptions{PinPath: testutils.TempBPFFS(t)}, |
| 265 | + Programs: ProgramOptions{LogLevel: LogLevelBranch}, |
266 | 266 | }) |
| 267 | + testutils.SkipIfNotSupported(t, err) |
| 268 | + qt.Assert(t, qt.IsNil(err)) |
| 269 | + |
| 270 | + ret := mustRun(t, coll.Programs["xdp_prog"], nil) |
| 271 | + qt.Assert(t, qt.Equals(ret, 7)) |
267 | 272 | } |
268 | 273 |
|
269 | 274 | func BenchmarkELFLoader(b *testing.B) { |
|
0 commit comments