blob: 4f376e2e0d14a9e3c28b5951802dfc3df6cbe3e3 [file] [log] [blame]
Scott Baker104b67d2019-10-29 15:56:27 -07001/*
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 */
16package grpc
17
18import (
19 "context"
khenaidoo0927c722021-12-15 16:49:32 -050020 "testing"
21
Scott Baker104b67d2019-10-29 15:56:27 -070022 "github.com/stretchr/testify/assert"
23 "google.golang.org/grpc"
Scott Baker104b67d2019-10-29 15:56:27 -070024)
25
26// A Mock Probe that returns the Ready member using the IsReady() func
27type MockReadyProbe struct {
28 Ready bool
29}
30
31func (m *MockReadyProbe) IsReady() bool {
32 return m.Ready
33}
34
35// A Mock handler that returns the request as its result
36func MockUnaryHandler(ctx context.Context, req interface{}) (interface{}, error) {
37 _ = ctx
38 return req, nil
39}
40
41func TestNewGrpcServer(t *testing.T) {
Neha Sharmadd9af392020-04-28 09:03:57 +000042 server := NewGrpcServer("127.0.0.1:1234", nil, false, nil)
Scott Baker104b67d2019-10-29 15:56:27 -070043 assert.NotNil(t, server)
44}
45
46func TestMkServerInterceptorNoProbe(t *testing.T) {
Neha Sharmadd9af392020-04-28 09:03:57 +000047 server := NewGrpcServer("127.0.0.1:1234", nil, false, nil)
Scott Baker104b67d2019-10-29 15:56:27 -070048 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
65func TestMkServerInterceptorReady(t *testing.T) {
Scott Bakerdefa2bf2019-11-08 12:03:56 -080066 probe := &MockReadyProbe{Ready: true}
67
Neha Sharmadd9af392020-04-28 09:03:57 +000068 server := NewGrpcServer("127.0.0.1:1234", nil, false, probe)
Scott Baker104b67d2019-10-29 15:56:27 -070069 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 Baker104b67d2019-10-29 15:56:27 -070077 result, err := f(context.Background(),
78 req,
79 &serverInfo,
80 MockUnaryHandler)
81
82 assert.Nil(t, err)
83 assert.NotNil(t, result)
84}
85
86func TestMkServerInterceptorNotReady(t *testing.T) {
Scott Bakerdefa2bf2019-11-08 12:03:56 -080087 probe := &MockReadyProbe{Ready: false}
88
Neha Sharmadd9af392020-04-28 09:03:57 +000089 server := NewGrpcServer("127.0.0.1:1234", nil, false, probe)
Scott Baker104b67d2019-10-29 15:56:27 -070090 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 Baker104b67d2019-10-29 15:56:27 -070098 result, err := f(context.Background(),
99 req,
100 &serverInfo,
101 MockUnaryHandler)
102
103 assert.NotNil(t, err)
104 assert.Nil(t, result)
105}