Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package grpc |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "github.com/stretchr/testify/assert" |
| 21 | "google.golang.org/grpc" |
| 22 | "testing" |
| 23 | ) |
| 24 | |
| 25 | // A Mock Probe that returns the Ready member using the IsReady() func |
| 26 | type MockReadyProbe struct { |
| 27 | Ready bool |
| 28 | } |
| 29 | |
| 30 | func (m *MockReadyProbe) IsReady() bool { |
| 31 | return m.Ready |
| 32 | } |
| 33 | |
| 34 | // A Mock handler that returns the request as its result |
| 35 | func MockUnaryHandler(ctx context.Context, req interface{}) (interface{}, error) { |
| 36 | _ = ctx |
| 37 | return req, nil |
| 38 | } |
| 39 | |
| 40 | func TestNewGrpcServer(t *testing.T) { |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 41 | server := NewGrpcServer("127.0.0.1:1234", nil, false, nil) |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 42 | assert.NotNil(t, server) |
| 43 | } |
| 44 | |
| 45 | func TestMkServerInterceptorNoProbe(t *testing.T) { |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 46 | server := NewGrpcServer("127.0.0.1:1234", nil, false, nil) |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 47 | assert.NotNil(t, server) |
| 48 | |
| 49 | f := mkServerInterceptor(server) |
| 50 | assert.NotNil(t, f) |
| 51 | |
| 52 | req := "SomeRequest" |
| 53 | serverInfo := grpc.UnaryServerInfo{Server: nil, FullMethod: "somemethod"} |
| 54 | |
| 55 | result, err := f(context.Background(), |
| 56 | req, |
| 57 | &serverInfo, |
| 58 | MockUnaryHandler) |
| 59 | |
| 60 | assert.Nil(t, err) |
| 61 | assert.Equal(t, "SomeRequest", result) |
| 62 | } |
| 63 | |
| 64 | func TestMkServerInterceptorReady(t *testing.T) { |
Scott Baker | defa2bf | 2019-11-08 12:03:56 -0800 | [diff] [blame] | 65 | probe := &MockReadyProbe{Ready: true} |
| 66 | |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 67 | server := NewGrpcServer("127.0.0.1:1234", nil, false, probe) |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 68 | assert.NotNil(t, server) |
| 69 | |
| 70 | f := mkServerInterceptor(server) |
| 71 | assert.NotNil(t, f) |
| 72 | |
| 73 | req := "SomeRequest" |
| 74 | serverInfo := grpc.UnaryServerInfo{Server: nil, FullMethod: "somemethod"} |
| 75 | |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 76 | result, err := f(context.Background(), |
| 77 | req, |
| 78 | &serverInfo, |
| 79 | MockUnaryHandler) |
| 80 | |
| 81 | assert.Nil(t, err) |
| 82 | assert.NotNil(t, result) |
| 83 | } |
| 84 | |
| 85 | func TestMkServerInterceptorNotReady(t *testing.T) { |
Scott Baker | defa2bf | 2019-11-08 12:03:56 -0800 | [diff] [blame] | 86 | probe := &MockReadyProbe{Ready: false} |
| 87 | |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 88 | server := NewGrpcServer("127.0.0.1:1234", nil, false, probe) |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 89 | assert.NotNil(t, server) |
| 90 | |
| 91 | f := mkServerInterceptor(server) |
| 92 | assert.NotNil(t, f) |
| 93 | |
| 94 | req := "SomeRequest" |
| 95 | serverInfo := grpc.UnaryServerInfo{Server: nil, FullMethod: "somemethod"} |
| 96 | |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 97 | result, err := f(context.Background(), |
| 98 | req, |
| 99 | &serverInfo, |
| 100 | MockUnaryHandler) |
| 101 | |
| 102 | assert.NotNil(t, err) |
| 103 | assert.Nil(t, result) |
| 104 | } |