prysm-pulse/beacon-chain/rpc/service_test.go
Nishant Das 6b6273fec1
Modify RPC Methods To Handle ETH1Failure (#3915)
* refactor powchain config

* send mock eth1 votes when proposing blocks

* fix all tests

* lint

* fix all tests

* fix blockchain test

* Apply suggestions from code review

Co-Authored-By: Raul Jordan <raul@prysmaticlabs.com>

* raul's review

* add warning

* new changes

* add connection method

* some more cleaning up

* change commit to latest

* fix mocks

* Update beacon-chain/powchain/service.go

Co-Authored-By: Preston Van Loon <preston@prysmaticlabs.com>

* preston's review

* remove debug log

* fix test

* fix more tests

* fix more tests

* one more test

* more tests fixed
2019-11-05 11:00:51 +08:00

97 lines
2.8 KiB
Go

package rpc
import (
"context"
"errors"
"fmt"
"io/ioutil"
"testing"
mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
mockPOW "github.com/prysmaticlabs/prysm/beacon-chain/powchain/testing"
mockSync "github.com/prysmaticlabs/prysm/beacon-chain/sync/initial-sync/testing"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/sirupsen/logrus"
logTest "github.com/sirupsen/logrus/hooks/test"
)
func init() {
logrus.SetLevel(logrus.DebugLevel)
logrus.SetOutput(ioutil.Discard)
}
func TestLifecycle_OK(t *testing.T) {
hook := logTest.NewGlobal()
rpcService := NewService(context.Background(), &Config{
Port: "7348",
CertFlag: "alice.crt",
KeyFlag: "alice.key",
SyncService: &mockSync.Sync{IsSyncing: false},
BlockReceiver: &mock.ChainService{},
AttestationReceiver: &mock.ChainService{},
HeadFetcher: &mock.ChainService{},
StateFeedListener: &mock.ChainService{},
POWChainService: &mockPOW.POWChain{},
})
rpcService.Start()
testutil.AssertLogsContain(t, hook, "listening on port")
rpcService.Stop()
}
func TestRPC_BadEndpoint(t *testing.T) {
hook := logTest.NewGlobal()
rpcService := NewService(context.Background(), &Config{
Port: "ralph merkle!!!",
SyncService: &mockSync.Sync{IsSyncing: false},
BlockReceiver: &mock.ChainService{},
AttestationReceiver: &mock.ChainService{},
HeadFetcher: &mock.ChainService{},
POWChainService: &mockPOW.POWChain{},
StateFeedListener: &mock.ChainService{},
})
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")
rpcService.Start()
testutil.AssertLogsContain(t, hook, "Could not listen to port in Start()")
rpcService.Stop()
}
func TestStatus_CredentialError(t *testing.T) {
credentialErr := errors.New("credentialError")
s := &Service{credentialError: credentialErr}
if err := s.Status(); err != s.credentialError {
t.Errorf("Wanted: %v, got: %v", s.credentialError, s.Status())
}
}
func TestRPC_InsecureEndpoint(t *testing.T) {
hook := logTest.NewGlobal()
rpcService := NewService(context.Background(), &Config{
Port: "7777",
SyncService: &mockSync.Sync{IsSyncing: false},
BlockReceiver: &mock.ChainService{},
AttestationReceiver: &mock.ChainService{},
HeadFetcher: &mock.ChainService{},
StateFeedListener: &mock.ChainService{},
POWChainService: &mockPOW.POWChain{},
})
rpcService.Start()
testutil.AssertLogsContain(t, hook, fmt.Sprint("listening on port"))
testutil.AssertLogsContain(t, hook, "You are using an insecure gRPC connection")
rpcService.Stop()
}