github.com/cilium/ebpf@v0.16.0/cmd/bpf2go/gen/target_test.go (about) 1 package gen 2 3 import ( 4 "errors" 5 "os/exec" 6 "slices" 7 "testing" 8 9 "github.com/go-quicktest/qt" 10 ) 11 12 func TestCollectTargets(t *testing.T) { 13 clangArches := make(map[string][]GoArch) 14 linuxArchesLE := make(map[string][]GoArch) 15 linuxArchesBE := make(map[string][]GoArch) 16 for arch, archTarget := range targetsByGoArch { 17 clangArches[archTarget.clang] = append(clangArches[archTarget.clang], arch) 18 if archTarget.clang == "bpfel" { 19 linuxArchesLE[archTarget.linux] = append(linuxArchesLE[archTarget.linux], arch) 20 continue 21 } 22 linuxArchesBE[archTarget.linux] = append(linuxArchesBE[archTarget.linux], arch) 23 } 24 for i := range clangArches { 25 slices.Sort(clangArches[i]) 26 } 27 for i := range linuxArchesLE { 28 slices.Sort(linuxArchesLE[i]) 29 } 30 for i := range linuxArchesBE { 31 slices.Sort(linuxArchesBE[i]) 32 } 33 34 nativeTarget, nativeArches, err := FindTarget("native") 35 qt.Assert(t, qt.IsNil(err)) 36 37 tests := []struct { 38 short string 39 target Target 40 arches GoArches 41 }{ 42 { 43 "bpf", 44 Target{"bpf", ""}, 45 nil, 46 }, 47 { 48 "bpfel", 49 Target{"bpfel", ""}, 50 clangArches["bpfel"], 51 }, 52 { 53 "bpfeb", 54 Target{"bpfeb", ""}, 55 clangArches["bpfeb"], 56 }, 57 { 58 "amd64", 59 Target{"bpfel", "x86"}, 60 linuxArchesLE["x86"], 61 }, 62 { 63 "386", 64 Target{"bpfel", "x86"}, 65 linuxArchesLE["x86"], 66 }, 67 { 68 "ppc64", 69 Target{"bpfeb", "powerpc"}, 70 linuxArchesBE["powerpc"], 71 }, 72 { 73 "native", 74 nativeTarget, 75 nativeArches, 76 }, 77 } 78 79 for _, test := range tests { 80 t.Run(test.short, func(t *testing.T) { 81 target, arches, err := FindTarget(test.short) 82 qt.Assert(t, qt.IsNil(err)) 83 qt.Assert(t, qt.Equals(target, test.target)) 84 qt.Assert(t, qt.DeepEquals(arches, test.arches)) 85 }) 86 } 87 } 88 89 func TestCollectTargetsErrors(t *testing.T) { 90 tests := []struct { 91 name string 92 target string 93 }{ 94 {"unknown", "frood"}, 95 {"no linux target", "mipsle"}, 96 } 97 98 for _, test := range tests { 99 t.Run(test.name, func(t *testing.T) { 100 _, _, err := FindTarget(test.target) 101 if err == nil { 102 t.Fatal("Function did not return an error") 103 } 104 t.Log("Error message:", err) 105 }) 106 } 107 } 108 109 func TestGoarches(t *testing.T) { 110 exe := goBin(t) 111 112 for GoArch := range targetsByGoArch { 113 t.Run(string(GoArch), func(t *testing.T) { 114 goEnv := exec.Command(exe, "env") 115 goEnv.Env = []string{"GOROOT=/", "GOOS=linux", "GOARCH=" + string(GoArch)} 116 output, err := goEnv.CombinedOutput() 117 qt.Assert(t, qt.IsNil(err), qt.Commentf("go output is:\n%s", string(output))) 118 }) 119 } 120 } 121 122 func TestClangTargets(t *testing.T) { 123 exe := goBin(t) 124 125 clangTargets := map[string]struct{}{} 126 for _, tgt := range targetsByGoArch { 127 clangTargets[tgt.clang] = struct{}{} 128 } 129 130 for target := range clangTargets { 131 for _, env := range []string{"GOOS", "GOARCH"} { 132 env += "=" + target 133 t.Run(env, func(t *testing.T) { 134 goEnv := exec.Command(exe, "env") 135 goEnv.Env = []string{"GOROOT=/", env} 136 output, err := goEnv.CombinedOutput() 137 t.Log("go output is:", string(output)) 138 qt.Assert(t, qt.IsNotNil(err), qt.Commentf("No clang target should be a valid build constraint")) 139 }) 140 } 141 142 } 143 } 144 145 func goBin(t *testing.T) string { 146 t.Helper() 147 148 exe, err := exec.LookPath("go") 149 if errors.Is(err, exec.ErrNotFound) { 150 t.Skip("go binary is not in PATH") 151 } 152 qt.Assert(t, qt.IsNil(err)) 153 154 return exe 155 }