Consolidate test doubles

- Migrate various mocks of core namespaces to shared version in `fakes` pkg
- Err on the side of making test doubles less sophisticated
- Don't pull over mocks of namespaces that are only used in example code
This commit is contained in:
Rob Mulholand
2018-07-20 11:37:46 -05:00
parent 63434f6bc9
commit a683e45855
46 changed files with 640 additions and 893 deletions
+41 -48
View File
@@ -7,69 +7,38 @@ import (
"regexp"
"strings"
"log"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/p2p"
"github.com/vulcanize/vulcanizedb/pkg/core"
"strings"
)
type PropertiesReader interface {
type IPropertiesReader interface {
NodeInfo() (id string, name string)
NetworkId() float64
GenesisBlock() string
}
type ClientWrapper struct {
ContextCaller
IPCPath string
}
type ContextCaller interface {
CallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error
SupportedModules() (map[string]string, error)
type PropertiesReader struct {
client core.RpcClient
}
type ParityClient struct {
ClientWrapper
PropertiesReader
}
type GethClient struct {
ClientWrapper
PropertiesReader
}
type InfuraClient struct {
ClientWrapper
PropertiesReader
}
func (clientWrapper ClientWrapper) NodeType() core.NodeType {
if strings.Contains(clientWrapper.IPCPath, "infura") {
return core.INFURA
}
modules, _ := clientWrapper.SupportedModules()
if _, ok := modules["admin"]; ok {
return core.GETH
}
return core.PARITY
}
func makePropertiesReader(wrapper ClientWrapper) PropertiesReader {
switch wrapper.NodeType() {
case core.GETH:
return GethClient{ClientWrapper: wrapper}
case core.PARITY:
return ParityClient{ClientWrapper: wrapper}
case core.INFURA:
return InfuraClient{ClientWrapper: wrapper}
default:
return wrapper
}
}
func MakeNode(wrapper ClientWrapper) core.Node {
pr := makePropertiesReader(wrapper)
func MakeNode(rpcClient core.RpcClient) core.Node {
pr := makePropertiesReader(rpcClient)
id, name := pr.NodeInfo()
return core.Node{
GenesisBlock: pr.GenesisBlock(),
@@ -79,9 +48,33 @@ func MakeNode(wrapper ClientWrapper) core.Node {
}
}
func (client ClientWrapper) NetworkId() float64 {
func makePropertiesReader(client core.RpcClient) IPropertiesReader {
switch getNodeType(client) {
case core.GETH:
return GethClient{PropertiesReader: PropertiesReader{client: client}}
case core.PARITY:
return ParityClient{PropertiesReader: PropertiesReader{client: client}}
case core.INFURA:
return InfuraClient{PropertiesReader: PropertiesReader{client: client}}
default:
return PropertiesReader{client: client}
}
}
func getNodeType(client core.RpcClient) core.NodeType {
if strings.Contains(client.IpcPath(), "infura") {
return core.INFURA
}
modules, _ := client.SupportedModules()
if _, ok := modules["admin"]; ok {
return core.GETH
}
return core.PARITY
}
func (reader PropertiesReader) NetworkId() float64 {
var version string
err := client.CallContext(context.Background(), &version, "net_version")
err := reader.client.CallContext(context.Background(), &version, "net_version")
if err != nil {
log.Println(err)
}
@@ -89,17 +82,17 @@ func (client ClientWrapper) NetworkId() float64 {
return networkId
}
func (client ClientWrapper) GenesisBlock() string {
func (reader PropertiesReader) GenesisBlock() string {
var header *types.Header
blockZero := "0x0"
includeTransactions := false
client.CallContext(context.Background(), &header, "eth_getBlockByNumber", blockZero, includeTransactions)
reader.client.CallContext(context.Background(), &header, "eth_getBlockByNumber", blockZero, includeTransactions)
return header.Hash().Hex()
}
func (client ClientWrapper) NodeInfo() (string, string) {
func (reader PropertiesReader) NodeInfo() (string, string) {
var info p2p.NodeInfo
client.CallContext(context.Background(), &info, "admin_nodeInfo")
reader.client.CallContext(context.Background(), &info, "admin_nodeInfo")
return info.ID, info.Name
}
@@ -115,14 +108,14 @@ func (client InfuraClient) NodeInfo() (string, string) {
func (client ParityClient) parityNodeInfo() string {
var nodeInfo core.ParityNodeInfo
client.CallContext(context.Background(), &nodeInfo, "parity_versionInfo")
client.client.CallContext(context.Background(), &nodeInfo, "parity_versionInfo")
return nodeInfo.String()
}
func (client ParityClient) parityID() string {
var enodeId = regexp.MustCompile(`^enode://(.+)@.+$`)
var enodeURL string
client.CallContext(context.Background(), &enodeURL, "parity_enode")
client.client.CallContext(context.Background(), &enodeURL, "parity_enode")
enode := enodeId.FindStringSubmatch(enodeURL)
if len(enode) < 2 {
return ""
+15 -67
View File
@@ -3,66 +3,15 @@ package node_test
import (
"encoding/json"
"context"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/p2p"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/fakes"
"github.com/vulcanize/vulcanizedb/pkg/geth/node"
)
type MockContextCaller struct {
nodeType core.NodeType
}
var EmpytHeaderHash = "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"
func (MockContextCaller) CallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error {
switch method {
case "admin_nodeInfo":
if p, ok := result.(*p2p.NodeInfo); ok {
p.ID = "enode://GethNode@172.17.0.1:30303"
p.Name = "Geth/v1.7"
}
case "eth_getBlockByNumber":
if p, ok := result.(*types.Header); ok {
*p = types.Header{}
}
case "parity_versionInfo":
if p, ok := result.(*core.ParityNodeInfo); ok {
*p = core.ParityNodeInfo{
Track: "",
ParityVersion: core.ParityVersion{
Major: 1,
Minor: 2,
Patch: 3,
},
Hash: "",
}
}
case "parity_enode":
if p, ok := result.(*string); ok {
*p = "enode://ParityNode@172.17.0.1:30303"
}
case "net_version":
if p, ok := result.(*string); ok {
*p = "1234"
}
}
return nil
}
func (mcc MockContextCaller) SupportedModules() (map[string]string, error) {
result := make(map[string]string)
if mcc.nodeType == core.GETH {
result["admin"] = "ok"
}
return result, nil
}
var _ = Describe("Parity Node Info", func() {
It("verifies parity_versionInfo can be unmarshalled into ParityNodeInfo", func() {
@@ -99,39 +48,38 @@ var _ = Describe("Parity Node Info", func() {
})
It("returns the genesis block for any client", func() {
mcc := MockContextCaller{}
cw := node.ClientWrapper{ContextCaller: mcc}
n := node.MakeNode(cw)
client := fakes.NewMockRpcClient()
n := node.MakeNode(client)
Expect(n.GenesisBlock).To(Equal(EmpytHeaderHash))
})
It("returns the network id for any client", func() {
mcc := MockContextCaller{}
cw := node.ClientWrapper{ContextCaller: mcc}
n := node.MakeNode(cw)
client := fakes.NewMockRpcClient()
n := node.MakeNode(client)
Expect(n.NetworkID).To(Equal(float64(1234)))
})
It("returns parity ID and client name for parity node", func() {
mcc := MockContextCaller{core.PARITY}
cw := node.ClientWrapper{ContextCaller: mcc}
n := node.MakeNode(cw)
client := fakes.NewMockRpcClient()
client.SetNodeType(core.PARITY)
n := node.MakeNode(client)
Expect(n.ID).To(Equal("ParityNode"))
Expect(n.ClientName).To(Equal("Parity/v1.2.3/"))
})
It("returns geth ID and client name for geth node", func() {
mcc := MockContextCaller{core.GETH}
cw := node.ClientWrapper{ContextCaller: mcc}
n := node.MakeNode(cw)
client := fakes.NewMockRpcClient()
client.SetNodeType(core.GETH)
n := node.MakeNode(client)
Expect(n.ID).To(Equal("enode://GethNode@172.17.0.1:30303"))
Expect(n.ClientName).To(Equal("Geth/v1.7"))
})
It("returns infura ID and client name for infura node", func() {
mcc := MockContextCaller{core.INFURA}
cw := node.ClientWrapper{ContextCaller: mcc, IPCPath: "https://mainnet.infura.io/123"}
n := node.MakeNode(cw)
client := fakes.NewMockRpcClient()
client.SetNodeType(core.INFURA)
client.SetIpcPath("infura/path")
n := node.MakeNode(client)
Expect(n.ID).To(Equal("infura"))
Expect(n.ClientName).To(Equal("infura"))
})