forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin_test.go
109 lines (90 loc) · 3.18 KB
/
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
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package rpcchainvm
import (
"fmt"
"os"
"os/exec"
"testing"
gomock "github.com/golang/mock/gomock"
plugin "github.com/hashicorp/go-plugin"
"github.com/ava-labs/avalanchego/vms/rpcchainvm/grpcutils"
)
// plugin_test collects objects and helpers generally helpful for various rpc tests
const (
chainVMTestKey = "chainVMTest"
stateSyncEnabledTestKey = "stateSyncEnabledTest"
getOngoingSyncStateSummaryTestKey = "getOngoingSyncStateSummaryTest"
getLastStateSummaryTestKey = "getLastStateSummaryTest"
parseStateSummaryTestKey = "parseStateSummaryTest"
getStateSummaryTestKey = "getStateSummaryTest"
acceptStateSummaryTestKey = "acceptStateSummaryTest"
lastAcceptedBlockPostStateSummaryAcceptTestKey = "lastAcceptedBlockPostStateSummaryAcceptTest"
)
var (
TestHandshake = plugin.HandshakeConfig{
ProtocolVersion: protocolVersion,
MagicCookieKey: "VM_PLUGIN",
MagicCookieValue: "dynamic",
}
TestClientPluginMap = map[string]plugin.Plugin{
chainVMTestKey: &testVMPlugin{},
}
TestServerPluginMap = map[string]func(*testing.T, bool) (plugin.Plugin, *gomock.Controller){
chainVMTestKey: chainVMTestPlugin,
stateSyncEnabledTestKey: stateSyncEnabledTestPlugin,
getOngoingSyncStateSummaryTestKey: getOngoingSyncStateSummaryTestPlugin,
getLastStateSummaryTestKey: getLastStateSummaryTestPlugin,
parseStateSummaryTestKey: parseStateSummaryTestPlugin,
getStateSummaryTestKey: getStateSummaryTestPlugin,
acceptStateSummaryTestKey: acceptStateSummaryTestPlugin,
lastAcceptedBlockPostStateSummaryAcceptTestKey: lastAcceptedBlockPostStateSummaryAcceptTestPlugin,
}
)
// helperProcess helps with creating the plugin binary for testing.
func helperProcess(s ...string) *exec.Cmd {
cs := []string{"-test.run=TestHelperProcess", "--"}
cs = append(cs, s...)
env := []string{
"TEST_PROCESS=1",
}
run := os.Args[0]
cmd := exec.Command(run, cs...)
env = append(env, os.Environ()...)
cmd.Env = env
return cmd
}
func TestHelperProcess(t *testing.T) {
if os.Getenv("TEST_PROCESS") != "1" {
return
}
args := os.Args
for len(args) > 0 {
if args[0] == "--" {
args = args[1:]
break
}
args = args[1:]
}
if len(args) == 0 {
fmt.Fprintf(os.Stderr, "failed to receive command\n")
os.Exit(2)
}
plugins := make(map[string]plugin.Plugin)
controllersList := make([]*gomock.Controller, 0, len(args))
for _, testKey := range args {
mockedPlugin, ctrl := TestServerPluginMap[testKey](t, true /*loadExpectations*/)
controllersList = append(controllersList, ctrl)
plugins[testKey] = mockedPlugin
}
plugin.Serve(&plugin.ServeConfig{
HandshakeConfig: TestHandshake,
Plugins: plugins,
// A non-nil value here enables gRPC serving for this plugin.
GRPCServer: grpcutils.NewDefaultServer,
})
for _, ctrl := range controllersList {
ctrl.Finish()
}
os.Exit(0)
}