|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | +) |
| 12 | + |
| 13 | +type libvaccelPaths struct { |
| 14 | + libDir string |
| 15 | + imagesDir string |
| 16 | + modelsDir string |
| 17 | + inputDir string |
| 18 | + labelsDir string |
| 19 | +} |
| 20 | + |
| 21 | +func pkgConfigVar(t *testing.T, pkg, variable string) string { |
| 22 | + t.Helper() |
| 23 | + cmd := exec.Command("pkg-config", "--variable="+variable, pkg) //nolint:gosec |
| 24 | + cmd.Env = os.Environ() |
| 25 | + out, err := cmd.Output() |
| 26 | + if err != nil { |
| 27 | + t.Skipf("pkg-config %s not found: %v", pkg, err) |
| 28 | + } |
| 29 | + return strings.TrimSpace(string(out)) |
| 30 | +} |
| 31 | + |
| 32 | +func resolveLibvaccelPaths(t *testing.T) libvaccelPaths { |
| 33 | + t.Helper() |
| 34 | + prefix := pkgConfigVar(t, "vaccel", "prefix") |
| 35 | + return libvaccelPaths{ |
| 36 | + libDir: pkgConfigVar(t, "vaccel", "libdir"), |
| 37 | + imagesDir: filepath.Join(prefix, "share", "vaccel", "images"), |
| 38 | + modelsDir: filepath.Join(prefix, "share", "vaccel", "models"), |
| 39 | + inputDir: filepath.Join(prefix, "share", "vaccel", "input"), |
| 40 | + labelsDir: filepath.Join(prefix, "share", "vaccel", "labels"), |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func TestExamples(t *testing.T) { |
| 45 | + paths := resolveLibvaccelPaths(t) |
| 46 | + env := append( |
| 47 | + os.Environ(), |
| 48 | + "LD_LIBRARY_PATH="+paths.libDir, |
| 49 | + "VACCEL_PLUGINS=libvaccel-noop.so", |
| 50 | + ) |
| 51 | + |
| 52 | + tests := []struct { |
| 53 | + name string |
| 54 | + dir string |
| 55 | + args []string |
| 56 | + env []string |
| 57 | + wantOut string |
| 58 | + }{ |
| 59 | + { |
| 60 | + name: "noop", |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "classify", |
| 64 | + args: []string{filepath.Join(paths.imagesDir, "example.jpg")}, |
| 65 | + wantOut: `Output(1): This is a dummy classification tag! |
| 66 | +Output(2): This is a dummy classification tag!`, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "detect", |
| 70 | + args: []string{filepath.Join(paths.imagesDir, "example.jpg")}, |
| 71 | + wantOut: `Output(1): This is a dummy imgname! |
| 72 | +Output(2): This is a dummy imgname!`, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "exec", |
| 76 | + args: []string{filepath.Join(paths.libDir, "libmytestlib.so"), "10"}, |
| 77 | + wantOut: `Output(1): 10 |
| 78 | +Output(2): 10 |
| 79 | +Output(3): 10`, |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "nonser", |
| 83 | + args: []string{filepath.Join(paths.libDir, "libmytestlib.so")}, |
| 84 | + wantOut: `Input: 10 20 30 40 50 |
| 85 | +Output: 10 20 30 40 50 `, |
| 86 | + }, |
| 87 | + { |
| 88 | + name: "tf", |
| 89 | + args: []string{filepath.Join(paths.modelsDir, "tf")}, |
| 90 | + wantOut: `Success! |
| 91 | +Output tensor => type:1 nr_dims:2 |
| 92 | +dim[0]: 1 |
| 93 | +dim[1]: 30 |
| 94 | +Result Tensor: |
| 95 | +Tensor shape: [1 30] |
| 96 | +Values: |
| 97 | + [1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000]`, |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "tflite", |
| 101 | + args: []string{filepath.Join(paths.modelsDir, "tf/lstm2.tflite")}, |
| 102 | + wantOut: `Success, TFLite status: 0 |
| 103 | +Output tensor => type:1 nr_dims:2 |
| 104 | +dim[0]: 1 |
| 105 | +dim[1]: 30 |
| 106 | +Result Tensor: |
| 107 | +Tensor shape: [1 30] |
| 108 | +Values: |
| 109 | + [1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000]`, |
| 110 | + }, |
| 111 | + { |
| 112 | + name: "torch", |
| 113 | + args: []string{ |
| 114 | + filepath.Join(paths.imagesDir, "example.jpg"), |
| 115 | + filepath.Join(paths.modelsDir, "torch", "cnn_trace.pt"), |
| 116 | + filepath.Join(paths.labelsDir, "imagenet.txt"), |
| 117 | + }, |
| 118 | + wantOut: `Success! |
| 119 | +Prediction: tench, Tinca tinca`, |
| 120 | + }, |
| 121 | + } |
| 122 | + |
| 123 | + for _, tt := range tests { |
| 124 | + t.Run(tt.name, func(t *testing.T) { |
| 125 | + main := filepath.Join(tt.name, "main.go") |
| 126 | + |
| 127 | + cmd := exec.Command( //nolint:gosec |
| 128 | + "stdbuf", |
| 129 | + append([]string{"-oL", "-eL", "go", "run", main}, tt.args...)..., |
| 130 | + ) |
| 131 | + cmdEnv := make([]string, len(env)+len(tt.env)) |
| 132 | + copy(cmdEnv, env) |
| 133 | + copy(cmdEnv[len(env):], tt.env) |
| 134 | + cmd.Env = cmdEnv |
| 135 | + |
| 136 | + out, err := cmd.CombinedOutput() |
| 137 | + if err != nil { |
| 138 | + t.Fatalf("example failed: %v\n%s", err, out) |
| 139 | + } |
| 140 | + if tt.wantOut != "" && !strings.Contains(string(out), tt.wantOut) { |
| 141 | + t.Errorf("got %q, want %q", out, tt.wantOut) |
| 142 | + } |
| 143 | + }) |
| 144 | + } |
| 145 | +} |
0 commit comments