2020-07-02 17:50:05 +00:00
|
|
|
package client
|
2019-01-23 02:52:39 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-02-13 23:49:06 +00:00
|
|
|
"os"
|
2019-01-23 02:52:39 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-03-03 17:31:29 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared"
|
2019-01-30 12:28:53 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
2020-07-21 13:02:21 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2019-01-30 12:28:53 +00:00
|
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
2019-01-23 02:52:39 +00:00
|
|
|
)
|
|
|
|
|
2020-10-10 00:36:48 +00:00
|
|
|
var _ shared.Service = (*ValidatorService)(nil)
|
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"
|
2020-10-04 15:03:10 +00:00
|
|
|
cleanup := func() {
|
2020-04-14 16:41:09 +00:00
|
|
|
if err := os.RemoveAll(dir); err != nil {
|
|
|
|
log.WithError(err).Debug("Cannot remove keystore folder")
|
|
|
|
}
|
2020-10-04 15:03:10 +00:00
|
|
|
}
|
|
|
|
defer cleanup()
|
|
|
|
code := m.Run()
|
|
|
|
// os.Exit will prevent defer from being called
|
|
|
|
cleanup()
|
|
|
|
os.Exit(code)
|
2019-02-13 23:49:06 +00:00
|
|
|
}
|
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,
|
|
|
|
}
|
|
|
|
|
2020-07-21 13:02:21 +00:00
|
|
|
assert.NoError(t, vs.Stop())
|
2019-01-23 02:52:39 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(1 * time.Second):
|
2020-02-14 16:46:55 +00:00
|
|
|
t.Error("Context not canceled within 1s")
|
2019-01-23 02:52:39 +00:00
|
|
|
case <-vs.ctx.Done():
|
|
|
|
}
|
|
|
|
}
|
2019-01-30 12:28:53 +00:00
|
|
|
|
|
|
|
func TestLifecycle(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
2019-04-26 06:24:01 +00:00
|
|
|
// Use canceled context so that the run function exits immediately..
|
2019-01-30 12:28:53 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
cancel()
|
2019-02-14 20:51:40 +00:00
|
|
|
validatorService := &ValidatorService{
|
2020-10-14 22:20:20 +00:00
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
endpoint: "merkle tries",
|
|
|
|
withCert: "alice.crt",
|
2019-02-13 23:49:06 +00:00
|
|
|
}
|
2019-01-30 12:28:53 +00:00
|
|
|
validatorService.Start()
|
2020-07-21 13:02:21 +00:00
|
|
|
require.NoError(t, validatorService.Stop(), "Could not stop service")
|
2020-08-13 16:22:25 +00:00
|
|
|
require.LogsContain(t, hook, "Stopping service")
|
2019-01-30 12:28:53 +00:00
|
|
|
}
|
|
|
|
|
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()
|
2019-04-26 06:24:01 +00:00
|
|
|
// Use canceled context so that the run function exits immediately.
|
2019-01-30 12:28:53 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
cancel()
|
2019-02-14 20:51:40 +00:00
|
|
|
validatorService := &ValidatorService{
|
2020-10-14 22:20:20 +00:00
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
endpoint: "merkle tries",
|
2019-02-13 23:49:06 +00:00
|
|
|
}
|
2019-01-30 12:28:53 +00:00
|
|
|
validatorService.Start()
|
2020-08-13 16:22:25 +00:00
|
|
|
require.LogsContain(t, hook, "You are using an insecure gRPC connection")
|
2020-07-21 13:02:21 +00:00
|
|
|
require.NoError(t, validatorService.Stop(), "Could not stop service")
|
2020-08-13 16:22:25 +00:00
|
|
|
require.LogsContain(t, hook, "Stopping service")
|
2019-01-30 12:28:53 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 15:11:26 +00:00
|
|
|
func TestStatus_NoConnectionError(t *testing.T) {
|
2019-02-13 23:49:06 +00:00
|
|
|
validatorService := &ValidatorService{}
|
2020-07-21 13:02:21 +00:00
|
|
|
assert.ErrorContains(t, "no connection", validatorService.Status())
|
2019-01-30 12:28:53 +00:00
|
|
|
}
|