github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/rpc/service_test.go (about)

     1  package rpc
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"io/ioutil"
     7  	"testing"
     8  	"time"
     9  
    10  	mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
    11  	mockPOW "github.com/prysmaticlabs/prysm/beacon-chain/powchain/testing"
    12  	mockSync "github.com/prysmaticlabs/prysm/beacon-chain/sync/initial-sync/testing"
    13  	"github.com/prysmaticlabs/prysm/shared/testutil/assert"
    14  	"github.com/prysmaticlabs/prysm/shared/testutil/require"
    15  	"github.com/sirupsen/logrus"
    16  	logTest "github.com/sirupsen/logrus/hooks/test"
    17  )
    18  
    19  func init() {
    20  	logrus.SetLevel(logrus.DebugLevel)
    21  	logrus.SetOutput(ioutil.Discard)
    22  }
    23  
    24  func TestLifecycle_OK(t *testing.T) {
    25  	hook := logTest.NewGlobal()
    26  	chainService := &mock.ChainService{
    27  		Genesis: time.Now(),
    28  	}
    29  	rpcService := NewService(context.Background(), &Config{
    30  		Port:                "7348",
    31  		SyncService:         &mockSync.Sync{IsSyncing: false},
    32  		BlockReceiver:       chainService,
    33  		AttestationReceiver: chainService,
    34  		HeadFetcher:         chainService,
    35  		GenesisTimeFetcher:  chainService,
    36  		POWChainService:     &mockPOW.POWChain{},
    37  		StateNotifier:       chainService.StateNotifier(),
    38  	})
    39  
    40  	rpcService.Start()
    41  
    42  	require.LogsContain(t, hook, "listening on port")
    43  	assert.NoError(t, rpcService.Stop())
    44  }
    45  
    46  func TestStatus_CredentialError(t *testing.T) {
    47  	credentialErr := errors.New("credentialError")
    48  	s := &Service{
    49  		cfg:             &Config{SyncService: &mockSync.Sync{IsSyncing: false}},
    50  		credentialError: credentialErr,
    51  	}
    52  
    53  	assert.ErrorContains(t, s.credentialError.Error(), s.Status())
    54  }
    55  
    56  func TestRPC_InsecureEndpoint(t *testing.T) {
    57  	hook := logTest.NewGlobal()
    58  	chainService := &mock.ChainService{Genesis: time.Now()}
    59  	rpcService := NewService(context.Background(), &Config{
    60  		Port:                "7777",
    61  		SyncService:         &mockSync.Sync{IsSyncing: false},
    62  		BlockReceiver:       chainService,
    63  		GenesisTimeFetcher:  chainService,
    64  		AttestationReceiver: chainService,
    65  		HeadFetcher:         chainService,
    66  		POWChainService:     &mockPOW.POWChain{},
    67  		StateNotifier:       chainService.StateNotifier(),
    68  	})
    69  
    70  	rpcService.Start()
    71  
    72  	require.LogsContain(t, hook, "listening on port")
    73  	require.LogsContain(t, hook, "You are using an insecure gRPC server")
    74  	assert.NoError(t, rpcService.Stop())
    75  }