-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhashicorp_plugin_test.go
155 lines (134 loc) · 3.19 KB
/
hashicorp_plugin_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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package funplugin
import (
"os"
"path/filepath"
"runtime"
"testing"
"github.com/httprunner/funplugin/fungo"
"github.com/httprunner/funplugin/myexec"
"github.com/stretchr/testify/assert"
)
var pluginBinPath = "fungo/examples/debugtalk.bin"
func buildHashicorpGoPlugin() {
logger.Info("[setup test] build hashicorp go plugin", "path", pluginBinPath)
err := myexec.RunCommand("go", "build",
"-o", pluginBinPath,
"fungo/examples/hashicorp.go", "fungo/examples/debugtalk.go")
if err != nil {
panic(err)
}
}
func removeHashicorpGoPlugin() {
logger.Info("[teardown test] remove hashicorp plugin", "path", pluginBinPath)
os.Remove(pluginBinPath)
}
func TestHashicorpGRPCGoPlugin(t *testing.T) {
buildHashicorpGoPlugin()
defer removeHashicorpGoPlugin()
logFile := filepath.Join("docs", "logs", "hashicorp_grpc_go.log")
plugin, err := Init("fungo/examples/debugtalk.bin",
WithDebugLogger(true),
WithLogFile(logFile),
WithDisableTime(true))
if err != nil {
t.Fatal(err)
}
defer plugin.Quit()
assertPlugin(t, plugin)
}
func TestHashicorpRPCGoPlugin(t *testing.T) {
buildHashicorpGoPlugin()
defer removeHashicorpGoPlugin()
logFile := filepath.Join("docs", "logs", "hashicorp_rpc_go.log")
os.Setenv(fungo.PluginTypeEnvName, "rpc")
plugin, err := Init("fungo/examples/debugtalk.bin",
WithDebugLogger(true),
WithLogFile(logFile),
WithDisableTime(true))
if err != nil {
t.Fatal(err)
}
defer plugin.Quit()
assertPlugin(t, plugin)
}
func TestHashicorpPythonPluginWithVenv(t *testing.T) {
dir, err := os.MkdirTemp(os.TempDir(), "prefix")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
venvDir := filepath.Join(dir, ".hrp", "venv")
err = myexec.RunCommand("python3", "-m", "venv", venvDir)
if err != nil {
t.Fatal(err)
}
var python3 string
if runtime.GOOS == "windows" {
python3 = filepath.Join(venvDir, "Scripts", "python3.exe")
} else {
python3 = filepath.Join(venvDir, "bin", "python3")
}
err = myexec.RunCommand(python3, "-m", "pip", "install", "funppy")
if err != nil {
t.Fatal(err)
}
plugin, err := Init("funppy/examples/debugtalk.py", WithPython3(python3))
if err != nil {
t.Fatal(err)
}
defer plugin.Quit()
assertPlugin(t, plugin)
}
func assertPlugin(t *testing.T, plugin IPlugin) {
var err error
if !assert.True(t, plugin.Has("sum_ints")) {
t.Fail()
}
if !assert.True(t, plugin.Has("concatenate")) {
t.Fail()
}
var v2 interface{}
v2, err = plugin.Call("sum_ints", 1, 2, 3, 4)
if err != nil {
t.Fatal(err)
}
if !assert.EqualValues(t, 10, v2) {
t.Fail()
}
v2, err = plugin.Call("sum_two_int", 1, 2)
if err != nil {
t.Fatal(err)
}
if !assert.EqualValues(t, 3, v2) {
t.Fail()
}
v2, err = plugin.Call("sum", 1, 2, 3.4, 5)
if err != nil {
t.Fatal(err)
}
if !assert.Equal(t, 11.4, v2) {
t.Fail()
}
var v3 interface{}
v3, err = plugin.Call("sum_two_string", "a", "b")
if err != nil {
t.Fatal(err)
}
if !assert.Equal(t, "ab", v3) {
t.Fail()
}
v3, err = plugin.Call("sum_strings", "a", "b", "c")
if err != nil {
t.Fatal(err)
}
if !assert.Equal(t, "abc", v3) {
t.Fail()
}
v3, err = plugin.Call("concatenate", "a", 2, "c", 3.4)
if err != nil {
t.Fatal(err)
}
if !assert.Equal(t, "a2c3.4", v3) {
t.Fail()
}
}