github.com/prysmaticlabs/prysm@v1.4.4/validator/client/service_test.go (about) 1 package client 2 3 import ( 4 "context" 5 "strings" 6 "testing" 7 "time" 8 9 "github.com/prysmaticlabs/prysm/shared" 10 "github.com/prysmaticlabs/prysm/shared/testutil/assert" 11 "github.com/prysmaticlabs/prysm/shared/testutil/require" 12 logTest "github.com/sirupsen/logrus/hooks/test" 13 "google.golang.org/grpc/metadata" 14 ) 15 16 var _ shared.Service = (*ValidatorService)(nil) 17 var _ GenesisFetcher = (*ValidatorService)(nil) 18 var _ SyncChecker = (*ValidatorService)(nil) 19 20 func TestStop_CancelsContext(t *testing.T) { 21 ctx, cancel := context.WithCancel(context.Background()) 22 vs := &ValidatorService{ 23 ctx: ctx, 24 cancel: cancel, 25 } 26 27 assert.NoError(t, vs.Stop()) 28 29 select { 30 case <-time.After(1 * time.Second): 31 t.Error("Context not canceled within 1s") 32 case <-vs.ctx.Done(): 33 } 34 } 35 36 func TestLifecycle(t *testing.T) { 37 hook := logTest.NewGlobal() 38 // Use canceled context so that the run function exits immediately.. 39 ctx, cancel := context.WithCancel(context.Background()) 40 cancel() 41 validatorService := &ValidatorService{ 42 ctx: ctx, 43 cancel: cancel, 44 endpoint: "merkle tries", 45 withCert: "alice.crt", 46 } 47 validatorService.Start() 48 require.NoError(t, validatorService.Stop(), "Could not stop service") 49 require.LogsContain(t, hook, "Stopping service") 50 } 51 52 func TestLifecycle_Insecure(t *testing.T) { 53 hook := logTest.NewGlobal() 54 // Use canceled context so that the run function exits immediately. 55 ctx, cancel := context.WithCancel(context.Background()) 56 cancel() 57 validatorService := &ValidatorService{ 58 ctx: ctx, 59 cancel: cancel, 60 endpoint: "merkle tries", 61 } 62 validatorService.Start() 63 require.LogsContain(t, hook, "You are using an insecure gRPC connection") 64 require.NoError(t, validatorService.Stop(), "Could not stop service") 65 require.LogsContain(t, hook, "Stopping service") 66 } 67 68 func TestStatus_NoConnectionError(t *testing.T) { 69 validatorService := &ValidatorService{} 70 assert.ErrorContains(t, "no connection", validatorService.Status()) 71 } 72 73 func TestStart_GrpcHeaders(t *testing.T) { 74 hook := logTest.NewGlobal() 75 // Use canceled context so that the run function exits immediately. 76 ctx, cancel := context.WithCancel(context.Background()) 77 cancel() 78 for input, output := range map[string][]string{ 79 "should-break": {}, 80 "key=value": {"key", "value"}, 81 "": {}, 82 ",": {}, 83 "key=value,Authorization=Q=": { 84 "key", "value", "Authorization", "Q=", 85 }, 86 "Authorization=this is a valid value": { 87 "Authorization", "this is a valid value", 88 }, 89 } { 90 validatorService := &ValidatorService{ 91 ctx: ctx, 92 cancel: cancel, 93 endpoint: "merkle tries", 94 grpcHeaders: strings.Split(input, ","), 95 } 96 validatorService.Start() 97 md, _ := metadata.FromOutgoingContext(validatorService.ctx) 98 if input == "should-break" { 99 require.LogsContain(t, hook, "Incorrect gRPC header flag format. Skipping should-break") 100 } else if len(output) == 0 { 101 require.DeepEqual(t, md, metadata.MD(nil)) 102 } else { 103 require.DeepEqual(t, md, metadata.Pairs(output...)) 104 } 105 } 106 }