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" |
khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 20 | "testing" |
| 21 | |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 22 | "github.com/stretchr/testify/assert" |
| 23 | "google.golang.org/grpc" |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | // A Mock Probe that returns the Ready member using the IsReady() func |
| 27 | type MockReadyProbe struct { |
| 28 | Ready bool |
| 29 | } |
| 30 | |
| 31 | func (m *MockReadyProbe) IsReady() bool { |
| 32 | return m.Ready |
| 33 | } |
| 34 | |
| 35 | // A Mock handler that returns the request as its result |
| 36 | func MockUnaryHandler(ctx context.Context, req interface{}) (interface{}, error) { |
| 37 | _ = ctx |
| 38 | return req, nil |
| 39 | } |
| 40 | |
| 41 | func TestNewGrpcServer(t *testing.T) { |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 42 | server := NewGrpcServer("127.0.0.1:1234", nil, false, nil) |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 43 | assert.NotNil(t, server) |
| 44 | } |
| 45 | |
| 46 | func TestMkServerInterceptorNoProbe(t *testing.T) { |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 47 | server := NewGrpcServer("127.0.0.1:1234", nil, false, nil) |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 48 | assert.NotNil(t, server) |
| 49 | |
| 50 | f := mkServerInterceptor(server) |
| 51 | assert.NotNil(t, f) |
| 52 | |
| 53 | req := "SomeRequest" |
| 54 | serverInfo := grpc.UnaryServerInfo{Server: nil, FullMethod: "somemethod"} |
| 55 | |
| 56 | result, err := f(context.Background(), |
| 57 | req, |
| 58 | &serverInfo, |
| 59 | MockUnaryHandler) |
| 60 | |
| 61 | assert.Nil(t, err) |
| 62 | assert.Equal(t, "SomeRequest", result) |
| 63 | } |
| 64 | |
| 65 | func TestMkServerInterceptorReady(t *testing.T) { |
Scott Baker | defa2bf | 2019-11-08 12:03:56 -0800 | [diff] [blame] | 66 | probe := &MockReadyProbe{Ready: true} |
| 67 | |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 68 | server := NewGrpcServer("127.0.0.1:1234", nil, false, probe) |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 69 | assert.NotNil(t, server) |
| 70 | |
| 71 | f := mkServerInterceptor(server) |
| 72 | assert.NotNil(t, f) |
| 73 | |
| 74 | req := "SomeRequest" |
| 75 | serverInfo := grpc.UnaryServerInfo{Server: nil, FullMethod: "somemethod"} |
| 76 | |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 77 | result, err := f(context.Background(), |
| 78 | req, |
| 79 | &serverInfo, |
| 80 | MockUnaryHandler) |
| 81 | |
| 82 | assert.Nil(t, err) |
| 83 | assert.NotNil(t, result) |
| 84 | } |
| 85 | |
| 86 | func TestMkServerInterceptorNotReady(t *testing.T) { |
Scott Baker | defa2bf | 2019-11-08 12:03:56 -0800 | [diff] [blame] | 87 | probe := &MockReadyProbe{Ready: false} |
| 88 | |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 89 | server := NewGrpcServer("127.0.0.1:1234", nil, false, probe) |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 90 | assert.NotNil(t, server) |
| 91 | |
| 92 | f := mkServerInterceptor(server) |
| 93 | assert.NotNil(t, f) |
| 94 | |
| 95 | req := "SomeRequest" |
| 96 | serverInfo := grpc.UnaryServerInfo{Server: nil, FullMethod: "somemethod"} |
| 97 | |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 98 | result, err := f(context.Background(), |
| 99 | req, |
| 100 | &serverInfo, |
| 101 | MockUnaryHandler) |
| 102 | |
| 103 | assert.NotNil(t, err) |
| 104 | assert.Nil(t, result) |
| 105 | } |