2019-01-23 02:52:39 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-02-13 23:49:06 +00:00
|
|
|
"crypto/rand"
|
2019-04-18 17:23:38 +00:00
|
|
|
"encoding/hex"
|
2019-02-13 23:49:06 +00:00
|
|
|
"os"
|
2019-01-30 12:28:53 +00:00
|
|
|
"strings"
|
2019-01-23 02:52:39 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-03-03 17:31:29 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared"
|
2019-04-24 05:39:02 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/featureconfig"
|
2019-02-13 23:49:06 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/keystore"
|
2019-01-30 12:28:53 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
2019-03-03 17:31:29 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/accounts"
|
2019-01-30 12:28:53 +00:00
|
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
2019-01-23 02:52:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ = shared.Service(&ValidatorService{})
|
2019-02-13 23:49:06 +00:00
|
|
|
var validatorKey *keystore.Key
|
2019-04-18 17:23:38 +00:00
|
|
|
var keyMap map[string]*keystore.Key
|
|
|
|
var keyMapThreeValidators map[string]*keystore.Key
|
2019-02-13 23:49:06 +00:00
|
|
|
|
2019-04-20 11:24:40 +00:00
|
|
|
func keySetup() {
|
2019-04-18 17:23:38 +00:00
|
|
|
keyMap = make(map[string]*keystore.Key)
|
|
|
|
keyMapThreeValidators = make(map[string]*keystore.Key)
|
2019-04-20 11:24:40 +00:00
|
|
|
|
|
|
|
validatorKey, _ = keystore.NewKey(rand.Reader)
|
|
|
|
keyMap[hex.EncodeToString(validatorKey.PublicKey.Marshal())] = validatorKey
|
|
|
|
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
vKey, _ := keystore.NewKey(rand.Reader)
|
|
|
|
keyMapThreeValidators[hex.EncodeToString(vKey.PublicKey.Marshal())] = vKey
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-24 05:39:02 +00:00
|
|
|
func init() {
|
|
|
|
featureconfig.InitFeatureConfig(&featureconfig.FeatureFlagConfig{
|
|
|
|
CacheTreeHash: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-20 11:24:40 +00:00
|
|
|
func TestMain(m *testing.M) {
|
2019-02-13 23:49:06 +00:00
|
|
|
dir := testutil.TempDir() + "/keystore1"
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
accounts.NewValidatorAccount(dir, "1234")
|
2019-04-20 11:24:40 +00:00
|
|
|
keySetup()
|
2019-02-13 23:49:06 +00:00
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
2019-01-23 02:52:39 +00:00
|
|
|
|
2019-02-22 15:11:26 +00:00
|
|
|
func TestStop_CancelsContext(t *testing.T) {
|
2019-01-23 02:52:39 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
vs := &ValidatorService{
|
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := vs.Stop(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
t.Error("ctx not cancelled within 1s")
|
|
|
|
case <-vs.ctx.Done():
|
|
|
|
}
|
|
|
|
}
|
2019-01-30 12:28:53 +00:00
|
|
|
|
|
|
|
func TestLifecycle(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
// Use cancelled context so that the run function exits immediately..
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
cancel()
|
2019-02-14 20:51:40 +00:00
|
|
|
validatorService := &ValidatorService{
|
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
endpoint: "merkle tries",
|
|
|
|
withCert: "alice.crt",
|
2019-04-18 17:23:38 +00:00
|
|
|
keys: keyMap,
|
2019-02-13 23:49:06 +00:00
|
|
|
}
|
2019-01-30 12:28:53 +00:00
|
|
|
validatorService.Start()
|
|
|
|
if err := validatorService.Stop(); err != nil {
|
|
|
|
t.Fatalf("Could not stop service: %v", err)
|
|
|
|
}
|
|
|
|
testutil.AssertLogsContain(t, hook, "Stopping service")
|
|
|
|
}
|
|
|
|
|
2019-02-22 15:11:26 +00:00
|
|
|
func TestLifecycle_Insecure(t *testing.T) {
|
2019-01-30 12:28:53 +00:00
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
// Use cancelled context so that the run function exits immediately.
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
cancel()
|
2019-02-14 20:51:40 +00:00
|
|
|
validatorService := &ValidatorService{
|
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
endpoint: "merkle tries",
|
2019-04-18 17:23:38 +00:00
|
|
|
keys: keyMap,
|
2019-02-13 23:49:06 +00:00
|
|
|
}
|
2019-01-30 12:28:53 +00:00
|
|
|
validatorService.Start()
|
|
|
|
testutil.AssertLogsContain(t, hook, "You are using an insecure gRPC connection")
|
|
|
|
if err := validatorService.Stop(); err != nil {
|
|
|
|
t.Fatalf("Could not stop service: %v", err)
|
|
|
|
}
|
|
|
|
testutil.AssertLogsContain(t, hook, "Stopping service")
|
|
|
|
}
|
|
|
|
|
2019-02-22 15:11:26 +00:00
|
|
|
func TestStatus_NoConnectionError(t *testing.T) {
|
2019-02-13 23:49:06 +00:00
|
|
|
validatorService := &ValidatorService{}
|
2019-01-30 12:28:53 +00:00
|
|
|
if err := validatorService.Status(); !strings.Contains(err.Error(), "no connection") {
|
|
|
|
t.Errorf("Expected status check to fail if no connection is found, received: %v", err)
|
|
|
|
}
|
|
|
|
}
|