File: server_interceptors_test.go

package info (click to toggle)
golang-gitlab-gitlab-org-labkit 1.17.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,092 kB
  • sloc: sh: 210; javascript: 49; makefile: 4
file content (217 lines) | stat: -rw-r--r-- 5,228 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package grpccorrelation

import (
	"context"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"gitlab.com/gitlab-org/labkit/correlation"
	"google.golang.org/grpc"
	"google.golang.org/grpc/metadata"
)

var (
	_ grpc.ServerTransportStream = (*mockServerTransportStream)(nil)
	_ grpc.ServerStream          = (*mockServerStream)(nil)
)

type tcType struct {
	name               string
	md                 metadata.MD
	withoutPropagation bool

	expectRandom       bool
	expectedClientName string
}

func TestServerCorrelationInterceptors(t *testing.T) {
	tests := []tcType{
		{
			name: "default",
			md: metadata.Pairs(
				metadataCorrelatorKey,
				correlationID,
				metadataClientNameKey,
				clientName,
			),
			expectedClientName: clientName,
		},
		{
			name: "id present but not trusted",
			md: metadata.Pairs(
				metadataCorrelatorKey,
				correlationID,
			),
			withoutPropagation: true,
			expectRandom:       true,
		},
		{
			name: "id present, trusted but empty",
			md: metadata.Pairs(
				metadataCorrelatorKey,
				"",
			),
			withoutPropagation: true,
			expectRandom:       true,
		},
		{
			name:               "id absent and not trusted",
			md:                 metadata.Pairs(),
			withoutPropagation: true,
			expectRandom:       true,
		},
		{
			name:         "id absent and trusted",
			md:           metadata.Pairs(),
			expectRandom: true,
		},
		{
			name:         "no metadata",
			md:           nil,
			expectRandom: true,
		},
	}

	t.Run("unary", func(t *testing.T) {
		for _, tc := range tests {
			t.Run(tc.name, testUnaryServerCorrelationInterceptor(tc, false))
			t.Run(tc.name+" (reverse)", testUnaryServerCorrelationInterceptor(tc, true))
		}
	})
	t.Run("streaming", func(t *testing.T) {
		for _, tc := range tests {
			t.Run(tc.name, testStreamingServerCorrelationInterceptor(tc, false))
			t.Run(tc.name+" (reverse)", testStreamingServerCorrelationInterceptor(tc, true))
		}
	})
}

func testUnaryServerCorrelationInterceptor(tc tcType, reverseCorrelationID bool) func(*testing.T) {
	return func(t *testing.T) {
		t.Helper()

		sts := &mockServerTransportStream{}
		ctx := grpc.NewContextWithServerTransportStream(context.Background(), sts)
		if tc.md != nil {
			ctx = metadata.NewIncomingContext(ctx, tc.md)
		}
		interceptor := UnaryServerCorrelationInterceptor(constructServerOpts(tc, reverseCorrelationID)...)
		_, err := interceptor(
			ctx,
			nil,
			nil,
			func(ctx context.Context, req interface{}) (interface{}, error) {
				testServerCtx(ctx, t, tc, reverseCorrelationID, sts.header)
				return nil, nil
			},
		)
		require.NoError(t, err)
	}
}

func testStreamingServerCorrelationInterceptor(tc tcType, reverseCorrelationID bool) func(*testing.T) {
	return func(t *testing.T) {
		t.Helper()

		ctx := context.Background()
		if tc.md != nil {
			ctx = metadata.NewIncomingContext(ctx, tc.md)
		}
		ss := &mockServerStream{
			ctx: ctx,
		}
		interceptor := StreamServerCorrelationInterceptor(constructServerOpts(tc, reverseCorrelationID)...)
		err := interceptor(
			nil,
			ss,
			nil,
			func(srv interface{}, stream grpc.ServerStream) error {
				testServerCtx(stream.Context(), t, tc, reverseCorrelationID, ss.header)
				return nil
			},
		)
		require.NoError(t, err)
	}
}

func constructServerOpts(tc tcType, reverseCorrelationID bool) []ServerCorrelationInterceptorOption {
	var opts []ServerCorrelationInterceptorOption
	if tc.withoutPropagation {
		opts = append(opts, WithoutPropagation())
	}
	if reverseCorrelationID {
		opts = append(opts, WithReversePropagation())
	}
	return opts
}

func testServerCtx(ctx context.Context, t *testing.T, tc tcType, reverseCorrelationID bool, header metadata.MD) {
	t.Helper()

	actualID := correlation.ExtractFromContext(ctx)
	if tc.expectRandom {
		assert.NotEqual(t, correlationID, actualID)
		assert.NotEmpty(t, actualID)
	} else {
		assert.Equal(t, correlationID, actualID)
	}
	vals := header.Get(metadataCorrelatorKey)
	if reverseCorrelationID {
		assert.Equal(t, []string{actualID}, vals)
	} else {
		assert.Empty(t, vals)
	}
	assert.Equal(t, tc.expectedClientName, correlation.ExtractClientNameFromContext(ctx))
}

type mockServerTransportStream struct {
	header metadata.MD
}

func (s *mockServerTransportStream) Method() string {
	panic("implement me")
}

func (s *mockServerTransportStream) SetHeader(md metadata.MD) error {
	s.header = metadata.Join(s.header, md)
	return nil
}

func (s *mockServerTransportStream) SendHeader(md metadata.MD) error {
	panic("implement me")
}

func (s *mockServerTransportStream) SetTrailer(md metadata.MD) error {
	panic("implement me")
}

type mockServerStream struct {
	ctx    context.Context
	header metadata.MD
}

func (s *mockServerStream) SetHeader(md metadata.MD) error {
	s.header = metadata.Join(s.header, md)
	return nil
}

func (s *mockServerStream) SendHeader(md metadata.MD) error {
	panic("implement me")
}

func (s *mockServerStream) SetTrailer(md metadata.MD) {
	panic("implement me")
}

func (s *mockServerStream) Context() context.Context {
	return s.ctx
}

func (s *mockServerStream) SendMsg(m interface{}) error {
	panic("implement me")
}

func (s *mockServerStream) RecvMsg(m interface{}) error {
	panic("implement me")
}