-
Notifications
You must be signed in to change notification settings - Fork 232
/
load_test.go
84 lines (70 loc) · 1.79 KB
/
load_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package plugins_test
import (
"os"
"os/exec"
"path/filepath"
"plugin"
"runtime"
"testing"
"github.com/stretchr/testify/assert"
"github.com/alpacahq/marketstore/v4/plugins"
)
func setup(t *testing.T) (tearDown func(), testPluginLib, oldGoPath, absTestPluginLib string) {
t.Helper()
dirName := t.TempDir()
if osType := runtime.GOOS; osType != "linux" {
t.Skip("Only linux runs plugins")
}
binDirName := filepath.Join(dirName, "bin")
assert.Nil(t, os.MkdirAll(binDirName, 0o777))
testFileName := "plugin.go"
testFilePath := filepath.Join(dirName, testFileName)
testPluginLib = "plugin.so"
soFilePath := filepath.Join(binDirName, testPluginLib)
file, err := os.Create(testFilePath)
if err != nil {
t.Fatal("Could not create test plugin source file")
}
code := `package main
func main() {}
`
_, err = file.WriteString(code)
assert.Nil(t, err)
assert.Nil(t, file.Close())
cmd := exec.Command("go",
"build",
"-buildmode=plugin",
"-o",
soFilePath,
testFilePath)
if err := cmd.Run(); err != nil {
t.Log(err)
t.Skip("Unable to build test plugin ** is go version > 1.9 in your path?")
}
goPath := os.Getenv("GOPATH")
newGoPath := dirName + ":" + goPath
oldGoPath = goPath
absTestPluginLib = soFilePath
os.Setenv("GOPATH", newGoPath)
return func() {
if oldGoPath != "" {
os.Setenv("GOPATH", oldGoPath)
}
}, testPluginLib, oldGoPath, absTestPluginLib
}
func TestLoadFromGOPATH(t *testing.T) {
tearDown, testPluginLib, _, absTestPluginLib := setup(t)
defer tearDown()
var pi *plugin.Plugin
var err error
pi, err = plugins.Load(testPluginLib)
assert.NotNil(t, pi)
assert.Nil(t, err)
pi, err = plugins.Load("nonexistent")
assert.Nil(t, pi)
assert.NotNil(t, err)
// abs path case
pi, err = plugins.Load(absTestPluginLib)
assert.NotNil(t, pi)
assert.Nil(t, err)
}