2019-10-31 03:26:55 +00:00
|
|
|
package service
|
2019-09-26 16:29:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2019-10-31 03:26:55 +00:00
|
|
|
"flag"
|
2019-09-26 16:29:10 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
2019-12-13 07:31:37 +00:00
|
|
|
"time"
|
2019-09-26 16:29:10 +00:00
|
|
|
|
2020-01-29 01:44:51 +00:00
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/mock"
|
2019-09-26 16:29:10 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
2020-01-29 01:44:51 +00:00
|
|
|
"github.com/urfave/cli"
|
2019-09-26 16:29:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
logrus.SetLevel(logrus.DebugLevel)
|
|
|
|
logrus.SetOutput(ioutil.Discard)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLifecycle_OK(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
2019-10-31 03:26:55 +00:00
|
|
|
app := cli.NewApp()
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
2020-01-29 01:44:51 +00:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
2019-10-31 03:26:55 +00:00
|
|
|
context := cli.NewContext(app, set, nil)
|
|
|
|
rpcService, err := NewRPCService(&Config{
|
2019-12-13 07:31:37 +00:00
|
|
|
Port: 7348,
|
2019-10-31 03:26:55 +00:00
|
|
|
}, context)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("gRPC Service fail to initialize:", err)
|
|
|
|
}
|
2020-01-29 01:44:51 +00:00
|
|
|
client := mock.NewMockBeaconChainClient(ctrl)
|
|
|
|
rpcService.beaconClient = client
|
|
|
|
client.EXPECT().GetChainHead(
|
|
|
|
gomock.Any(),
|
|
|
|
gomock.Any(),
|
|
|
|
).Return(ðpb.ChainHead{HeadSlot: 1}, nil)
|
|
|
|
client.EXPECT().StreamAttestations(
|
|
|
|
gomock.Any(),
|
|
|
|
gomock.Any(),
|
|
|
|
).Return(nil, nil)
|
2019-12-13 07:31:37 +00:00
|
|
|
waitForStarted(rpcService, t)
|
|
|
|
rpcService.Close()
|
2019-09-26 16:29:10 +00:00
|
|
|
testutil.AssertLogsContain(t, hook, "Starting service")
|
|
|
|
testutil.AssertLogsContain(t, hook, "Listening on port")
|
|
|
|
testutil.AssertLogsContain(t, hook, "Stopping service")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRPC_BadEndpoint(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
2019-10-31 03:26:55 +00:00
|
|
|
app := cli.NewApp()
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
2020-01-29 01:44:51 +00:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
2019-10-31 03:26:55 +00:00
|
|
|
context := cli.NewContext(app, set, nil)
|
|
|
|
rpcService, err := NewRPCService(&Config{
|
2019-12-13 07:31:37 +00:00
|
|
|
Port: 99999999,
|
2019-10-31 03:26:55 +00:00
|
|
|
}, context)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("gRPC Service fail to initialize:", err)
|
|
|
|
}
|
2020-01-29 01:44:51 +00:00
|
|
|
client := mock.NewMockBeaconChainClient(ctrl)
|
|
|
|
rpcService.beaconClient = client
|
|
|
|
client.EXPECT().GetChainHead(
|
|
|
|
gomock.Any(),
|
|
|
|
gomock.Any(),
|
|
|
|
).Return(ðpb.ChainHead{HeadSlot: 1}, nil)
|
|
|
|
client.EXPECT().StreamAttestations(
|
|
|
|
gomock.Any(),
|
|
|
|
gomock.Any(),
|
|
|
|
).Return(nil, nil)
|
2019-09-26 16:29:10 +00:00
|
|
|
testutil.AssertLogsDoNotContain(t, hook, "Could not listen to port in Start()")
|
|
|
|
testutil.AssertLogsDoNotContain(t, hook, "Could not load TLS keys")
|
|
|
|
testutil.AssertLogsDoNotContain(t, hook, "Could not serve gRPC")
|
|
|
|
|
2019-12-13 07:31:37 +00:00
|
|
|
waitForStarted(rpcService, t)
|
2019-09-26 16:29:10 +00:00
|
|
|
|
|
|
|
testutil.AssertLogsContain(t, hook, "Starting service")
|
|
|
|
testutil.AssertLogsContain(t, hook, "Could not listen to port in Start()")
|
|
|
|
|
2019-10-31 03:26:55 +00:00
|
|
|
rpcService.Close()
|
2019-09-26 16:29:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStatus_CredentialError(t *testing.T) {
|
|
|
|
credentialErr := errors.New("credentialError")
|
|
|
|
s := &Service{credentialError: credentialErr}
|
|
|
|
|
2019-12-13 07:31:37 +00:00
|
|
|
if _, err := s.Status(); err != s.credentialError {
|
|
|
|
t.Errorf("Wanted: %v, got: %v", s.credentialError, err)
|
2019-09-26 16:29:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRPC_InsecureEndpoint(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
2019-10-31 03:26:55 +00:00
|
|
|
app := cli.NewApp()
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
context := cli.NewContext(app, set, nil)
|
2020-01-29 01:44:51 +00:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
2019-10-31 03:26:55 +00:00
|
|
|
rpcService, err := NewRPCService(&Config{
|
2019-12-13 07:31:37 +00:00
|
|
|
Port: 5555,
|
2019-10-31 03:26:55 +00:00
|
|
|
}, context)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("gRPC Service fail to initialize:", err)
|
|
|
|
}
|
2020-01-29 01:44:51 +00:00
|
|
|
client := mock.NewMockBeaconChainClient(ctrl)
|
|
|
|
rpcService.beaconClient = client
|
|
|
|
client.EXPECT().GetChainHead(
|
|
|
|
gomock.Any(),
|
|
|
|
gomock.Any(),
|
|
|
|
).Return(ðpb.ChainHead{HeadSlot: 1}, nil)
|
|
|
|
client.EXPECT().StreamAttestations(
|
|
|
|
gomock.Any(),
|
|
|
|
gomock.Any(),
|
|
|
|
).Return(nil, nil)
|
2019-12-13 07:31:37 +00:00
|
|
|
waitForStarted(rpcService, t)
|
2019-09-26 16:29:10 +00:00
|
|
|
|
|
|
|
testutil.AssertLogsContain(t, hook, "Starting service")
|
|
|
|
testutil.AssertLogsContain(t, hook, fmt.Sprint("Listening on port"))
|
|
|
|
testutil.AssertLogsContain(t, hook, "You are using an insecure gRPC connection")
|
|
|
|
|
2019-10-31 03:26:55 +00:00
|
|
|
rpcService.Close()
|
2019-09-26 16:29:10 +00:00
|
|
|
testutil.AssertLogsContain(t, hook, "Stopping service")
|
|
|
|
}
|
2019-12-13 07:31:37 +00:00
|
|
|
|
|
|
|
func waitForStarted(rpcService *Service, t *testing.T) {
|
|
|
|
go rpcService.Start()
|
|
|
|
tick := time.Tick(100 * time.Millisecond)
|
|
|
|
for {
|
|
|
|
<-tick
|
|
|
|
s, err := rpcService.Status()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if s {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|