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